22
33namespace SystemCtl \Unit ;
44
5+ use Symfony \Component \Process \Process ;
56use Symfony \Component \Process \ProcessBuilder ;
67use SystemCtl \Exception \CommandFailedException ;
78
89abstract class AbstractUnit implements UnitInterface
910{
1011 /** @var string */
11- protected $ type ;
12+ private $ name ;
1213
1314 /** @var string */
14- private $ name ;
15+ private $ type ;
1516
1617 /** @var ProcessBuilder */
17- protected $ processBuilder ;
18+ private $ processBuilder ;
19+
20+ /** @var bool */
21+ private $ yell = false ;
1822
1923 /**
2024 * Create new service with given name
@@ -38,6 +42,16 @@ public function getName(): string
3842 return $ this ->name ;
3943 }
4044
45+ /**
46+ * Set a flag whether to yell with an exception if an command failes or not
47+ *
48+ * @param $state
49+ */
50+ public function yell ($ state )
51+ {
52+ $ this ->yell = $ state ;
53+ }
54+
4155 /**
4256 * @inheritDoc
4357 */
@@ -48,33 +62,35 @@ public function isMultiInstance(): bool
4862
4963 /**
5064 * @inheritDoc
65+ * @todo: Everything in here should happen inside the constructor and stored
66+ * afterwards.
5167 */
5268 public function getInstanceName (): ?string
5369 {
54- $ parts = explode ('@ ' , $ this ->name );
55- return $ parts [1 ] ?? null ;
70+ $ instanceName = explode ('@ ' , $ this ->name , 2 )[1 ] ?? null ;
71+
72+ if (is_string ($ instanceName ) && strpos ($ instanceName , '. ' ) !== false ) {
73+ $ instanceName = explode ('. ' , $ instanceName , 2 )[0 ];
74+ }
75+
76+ return $ instanceName ;
5677 }
5778
5879 /**
5980 * Execute a single command
6081 *
6182 * @param string $command
62- * @param bool $raise
6383 *
6484 * @throws CommandFailedException Raised if process was not successful and user wants to raise exception
6585 * instead of returning the actual process exit code
6686 *
6787 * @return bool
6888 */
69- protected function execute (string $ command, bool $ raise = true ): bool
89+ protected function execute (string $ command ): bool
7090 {
71- $ process = $ this ->processBuilder
72- ->setArguments ([$ command , $ this ->getName ()])
73- ->getProcess ();
91+ $ process = $ this ->runCommandAgainstService ($ command );
7492
75- $ process ->run ();
76-
77- if (!$ process ->isSuccessful () && $ raise ) {
93+ if (!$ process ->isSuccessful () && $ this ->yell ) {
7894 $ exceptionCall = CommandFailedException::class . '::from ' . ucfirst ($ this ->type );
7995 throw call_user_func_array ($ exceptionCall , [$ this ->getName (), $ command ]);
8096 }
@@ -83,88 +99,94 @@ protected function execute(string $command, bool $raise = true): bool
8399 }
84100
85101 /**
86- * @inheritdoc
102+ * @param string $command
103+ *
104+ * @return Process
87105 */
88- public function start ( bool $ raise = false ): bool
106+ protected function runCommandAgainstService ( string $ command ): Process
89107 {
90- return $ this ->execute (__FUNCTION__ , $ raise );
108+ $ process = $ this ->processBuilder
109+ ->setArguments ([$ command , $ this ->getName ()])
110+ ->getProcess ();
111+
112+ $ process ->run ();
113+
114+ return $ process ;
91115 }
92116
93117 /**
94- * @inheritdoc
118+ * @return bool
95119 */
96- public function stop ( bool $ raise = false ): bool
120+ public function start ( ): bool
97121 {
98- return $ this ->execute (__FUNCTION__ , $ raise );
122+ return $ this ->execute (__FUNCTION__ );
99123 }
100124
101125 /**
102- * @inheritdoc
126+ * @return bool
103127 */
104- public function disable ( bool $ raise = false ): bool
128+ public function stop ( ): bool
105129 {
106- return $ this ->execute (__FUNCTION__ , $ raise );
130+ return $ this ->execute (__FUNCTION__ );
107131 }
108132
109133 /**
110- * @inheritdoc
134+ * @return bool
111135 */
112- public function reload ( bool $ raise = false ): bool
136+ public function disable ( ): bool
113137 {
114- return $ this ->execute (__FUNCTION__ , $ raise );
138+ return $ this ->execute (__FUNCTION__ );
115139 }
116140
117141 /**
118- * @inheritdoc
142+ * @return bool
119143 */
120- public function restart ( bool $ raise = false ): bool
144+ public function reload ( ): bool
121145 {
122- return $ this ->execute (__FUNCTION__ , $ raise );
146+ return $ this ->execute (__FUNCTION__ );
123147 }
124148
125149 /**
126- * @inheritdoc
150+ * @return bool
127151 */
128- public function enable ( bool $ raise = false ): bool
152+ public function restart ( ): bool
129153 {
130- return $ this ->execute (__FUNCTION__ , $ raise );
154+ return $ this ->execute (__FUNCTION__ );
131155 }
132156
133157 /**
134- * @inheritdoc
158+ * @return bool
135159 */
136- public function isActive ( bool $ raise = false ): bool
160+ public function enable ( ): bool
137161 {
138- $ process = $ this ->processBuilder
139- ->setArguments (['is-active ' , $ this ->getName ()])
140- ->getProcess ();
141-
142- $ process ->run ();
162+ return $ this ->execute (__FUNCTION__ );
163+ }
143164
144- if (!$ process ->isSuccessful () && $ raise ) {
145- $ exceptionCall = CommandFailedException::class . '::from ' . ucfirst ($ this ->type );
146- throw call_user_func_array ($ exceptionCall , [$ this ->getName (), 'is-active ' ]);
147- }
165+ /**
166+ * @return bool
167+ */
168+ public function isEnabled (): bool
169+ {
170+ $ process = $ this ->runCommandAgainstService ('is-enabled ' );
148171
149- return trim ($ process ->getOutput ()) === 'active ' ;
172+ return $ process -> isSuccessful () && trim ($ process ->getOutput ()) === 'enabled ' ;
150173 }
151174
152175 /**
153- * @inheritDoc
176+ * @return bool
154177 */
155- public function isEnabled ( bool $ raise = false ): bool
178+ public function isActive ( ): bool
156179 {
157- $ process = $ this ->processBuilder
158- ->setArguments (['is-enabled ' , $ this ->getName ()])
159- ->getProcess ();
160-
161- $ process ->run ();
180+ $ process = $ this ->runCommandAgainstService ('is-active ' );
162181
163- if (!$ process ->isSuccessful () && $ raise ) {
164- $ exceptionCall = CommandFailedException::class . '::from ' . ucfirst ($ this ->type );
165- throw call_user_func_array ($ exceptionCall , [$ this ->getName (), 'is-enabled ' ]);
166- }
182+ return $ process ->isSuccessful () && trim ($ process ->getOutput ()) === 'active ' ;
183+ }
167184
168- return trim ($ process ->getOutput ()) === 'enabled ' ;
185+ /**
186+ * @return bool
187+ */
188+ public function isRunning (): bool
189+ {
190+ return $ this ->isActive ();
169191 }
170192}
0 commit comments