44
55use Symfony \Component \Process \ProcessBuilder ;
66use SystemCtl \Exception \UnitTypeNotSupportedException ;
7+ use SystemCtl \Unit \Service ;
8+ use SystemCtl \Unit \Timer ;
9+ use SystemCtl \Unit \UnitInterface ;
710
811class SystemCtl
912{
1013 /** @var string systemctl binary path */
1114 private static $ binary = '/bin/systemctl ' ;
1215
13- /** @var bool */
14- private static $ sudo = false ;
16+ /** @var int timeout for commands */
17+ private static $ timeout = 3 ;
1518
1619 public const AVAILABLE_UNITS = [
17- ' service ' ,
20+ Service:: UNIT ,
1821 'socket ' ,
1922 'device ' ,
2023 'mount ' ,
2124 'automount ' ,
2225 'swap ' ,
2326 'target ' ,
2427 'path ' ,
25- ' timer ' ,
28+ Timer:: UNIT ,
2629 'slice ' ,
2730 'scope '
2831 ];
2932
3033 public const SUPPORTED_UNITS = [
31- ' service ' ,
32- ' timer '
34+ Service:: UNIT ,
35+ Timer:: UNIT ,
3336 ];
3437
3538 /**
39+ * Change systemctl binary
40+ *
3641 * @param string $binary
3742 */
3843 public static function setBinary (string $ binary ): void
3944 {
4045 self ::$ binary = $ binary ;
4146 }
4247
48+ /**
49+ * Change command execution timeout
50+ *
51+ * @param int $timeout
52+ */
53+ public static function setTimeout (int $ timeout ): void
54+ {
55+ self ::$ timeout = $ timeout ;
56+ }
57+
4358 /**
4459 * @param string $unitSuffix
4560 * @param string $unitName
@@ -49,13 +64,13 @@ public static function setBinary(string $binary): void
4964 */
5065 public static function unitFromSuffix (string $ unitSuffix , string $ unitName ): UnitInterface
5166 {
52- $ unitClass = 'SystemCtl \\' . ucfirst ($ unitSuffix );
67+ $ unitClass = 'SystemCtl \\Unit \\ ' . ucfirst ($ unitSuffix );
5368
5469 if (!class_exists ($ unitClass )) {
5570 throw new UnitTypeNotSupportedException ('Unit type ' . $ unitSuffix . ' not supported ' );
5671 }
5772
58- return new $ unitClass ($ unitName , new ProcessBuilder ([' sudo ' , self ::$ binary ]));
73+ return new $ unitClass ($ unitName , new ProcessBuilder ([self ::$ binary ]));
5974 }
6075
6176 /**
@@ -100,7 +115,7 @@ public function getService(string $name): Service
100115 */
101116 public function getServices (?string $ unitPrefix = null ): array
102117 {
103- $ units = $ this ->listUnits ($ unitPrefix , [' service ' ]);
118+ $ units = $ this ->listUnits ($ unitPrefix , [Service:: UNIT ]);
104119
105120 return array_map (function ($ unitName ) {
106121 return new Service ($ unitName , $ this ->getProcessBuilder ());
@@ -122,7 +137,7 @@ public function getTimer(string $name): Timer
122137 */
123138 public function getTimers (?string $ unitPrefix = null ): array
124139 {
125- $ units = $ this ->listUnits ($ unitPrefix , [' timer ' ]);
140+ $ units = $ this ->listUnits ($ unitPrefix , [Timer:: UNIT ]);
126141
127142 return array_map (function ($ unitName ) {
128143 return new Timer ($ unitName , $ this ->getProcessBuilder ());
@@ -136,7 +151,7 @@ public function getProcessBuilder(): ProcessBuilder
136151 {
137152 $ builder = ProcessBuilder::create ();
138153 $ builder ->setPrefix (self ::$ binary );
139- $ builder ->setTimeout (3 );
154+ $ builder ->setTimeout (self :: $ timeout );
140155
141156 return $ builder ;
142157 }
0 commit comments