1 <?php
2 namespace Alo\Windows;
3
4 use Alo\Exception\OSException;
5 use Alo;
6
7 if (!defined('GEN_START')) {
8 http_response_code(404);
9 } elseif (!defined('PHPUNIT_RUNNING') && !Alo::serverIsWindows()) {
10 throw new OSException('The service manager is only supported on Windows.');
11 } else {
12
13 /**
14 * Windows service handler
15 *
16 * @author Art <a.molcanovas@gmail.com>
17 */
18 abstract class Service {
19
20 /**
21 * Checks if a service exists
22 *
23 * @author Art <a.molcanovas@gmail.com>
24 *
25 * @param string $name Service name
26 *
27 * @return bool
28 */
29 static function exists($name) {
30 return trim(shell_exec(DIR_SYS . 'bin' . DIRECTORY_SEPARATOR . 'serviceexists.bat ' . $name)) == 'OK';
31 }
32
33 /**
34 * Deletes a service
35 *
36 * @author Art <a.molcanovas@gmail.com>
37 *
38 * @param string $name Service name
39 *
40 * @return string shell_exec() output
41 */
42 static function delete($name) {
43 return self::stop($name) . PHP_EOL . shell_exec('sc delete ' . $name);
44 }
45
46 /**
47 * Stops a service
48 *
49 * @author Art <a.molcanovas@gmail.com>
50 *
51 * @param string $name Service name
52 *
53 * @return string shell_exec() output
54 */
55 static function stop($name) {
56 return shell_exec('sc stop ' . $name);
57 }
58
59 /**
60 * Starts a service
61 *
62 * @author Art <a.molcanovas@gmail.com>
63 *
64 * @param string $name Service name
65 *
66 * @return string shell_exec() output
67 */
68 static function start($name) {
69 return shell_exec('sc start ' . $name);
70 }
71
72 /**
73 * Installes a service from an executable
74 *
75 * @author Art <a.molcanovas@gmail.com>
76 *
77 * @param string $serviceName The name of the service
78 * @param string $exePath Path to the executable
79 * @param null|string $displayName Optionally, a custom display name for the service
80 *
81 * @return string shell_exec() output
82 */
83 static function installExe($serviceName, $exePath, $displayName = null) {
84 $cmd = 'sc create ' . $serviceName . ' binPath= "' . $exePath . '"';
85
86 if ($displayName) {
87 $cmd .= ' DisplayName= "' . $displayName . '"';
88 }
89
90 return shell_exec($cmd);
91 }
92 }
93 }
94