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