1616 */
1717class Config implements ConfigInterface
1818{
19- public const SOCKET = '/usr/local/var/run/unit/control.sock ' ;
20- public const ADDRESS = 'http://localhost ' ;
21-
2219 /**
2320 * Listeners accept requests
2421 *
@@ -53,7 +50,7 @@ class Config implements ConfigInterface
5350 *
5451 * @throws UnitException
5552 */
56- public function __construct (array $ data )
53+ public function __construct (array $ data, private UnitRequest $ _unitRequest )
5754 {
5855 if (array_key_exists ('routes ' , $ data )) {
5956 foreach ($ data ['routes ' ] as $ routeName => $ routeData ) {
@@ -68,8 +65,8 @@ public function __construct(array $data)
6865 'php ' => new Application \PhpApplication ($ appData ),
6966 'external ' => new Application \NodeJsApplication ($ appData ),
7067 };
71-
7268 $ this ->_applications [$ appName ]->setName ($ appName );
69+ $ this ->_applications [$ appName ]->setUnitRequest ($ _unitRequest );
7370 }
7471 }
7572
@@ -126,11 +123,10 @@ public function getListenerByPort(int $port): Listener|null
126123 */
127124 public function removeListener (Listener $ listener ): bool
128125 {
129- $ request = new UnitRequest (self ::SOCKET , self ::ADDRESS );
130- $ request ->setMethod (HttpMethodsEnum::DELETE ->value );
126+ $ this ->_unitRequest ->setMethod (HttpMethodsEnum::DELETE ->value );
131127
132128 $ listenerId = $ listener ->getListener ();
133- $ request ->send ("/config/listeners/ {$ listenerId }" );
129+ $ this -> _unitRequest ->send ("/config/listeners/ {$ listenerId }" );
134130
135131 return true ;
136132 }
@@ -169,11 +165,10 @@ public function getApplication($applicationName): ApplicationAbstract
169165 */
170166 public function removeApplication (ApplicationAbstract $ application ): bool
171167 {
172- $ request = new UnitRequest (self ::SOCKET , self ::ADDRESS );
173- $ request ->setMethod (HttpMethodsEnum::DELETE ->value );
168+ $ this ->_unitRequest ->setMethod (HttpMethodsEnum::DELETE ->value );
174169
175170 $ applicationName = $ application ->getName ();
176- print_r ( $ request -> send ("/config/applications/ {$ applicationName }" ) );
171+ $ this -> _unitRequest -> send ("/config/applications/ {$ applicationName }" );
177172
178173 return true ;
179174 }
@@ -213,9 +208,8 @@ public function removeRoutes(): bool
213208 }
214209
215210 try {
216- $ request = new UnitRequest (self ::SOCKET , self ::ADDRESS );
217- $ request ->setMethod (HttpMethodsEnum::DELETE ->value );
218- $ request ->send ('/config/routes ' );
211+ $ this ->_unitRequest ->setMethod (HttpMethodsEnum::DELETE ->value );
212+ $ this ->_unitRequest ->send ('/config/routes ' );
219213 } catch (UnitException $ exception ) {
220214 return false ;
221215 }
@@ -250,10 +244,9 @@ public function setAccessLog($path, $format = null): bool
250244 }
251245
252246 try {
253- $ request = new UnitRequest (self ::SOCKET , self ::ADDRESS );
254- $ request ->setMethod (HttpMethodsEnum::PUT ->value );
255- $ request ->setData (json_encode ($ data ));
256- $ request ->send ('/config/access_log ' );
247+ $ this ->_unitRequest ->setMethod (HttpMethodsEnum::PUT ->value );
248+ $ this ->_unitRequest ->setData (json_encode ($ data ));
249+ $ this ->_unitRequest ->send ('/config/access_log ' );
257250 } catch (UnitException $ exception ) {
258251 return false ;
259252 }
@@ -263,8 +256,7 @@ public function setAccessLog($path, $format = null): bool
263256
264257 public function getAccessLog (): ?AccessLog
265258 {
266- $ request = new UnitRequest (self ::SOCKET , self ::ADDRESS );
267- $ result = $ request ->send ('/config/access_log ' );
259+ $ result = $ this ->_unitRequest ->send ('/config/access_log ' );
268260
269261 // TODO: need null
270262 return new AccessLog ($ result );
@@ -273,9 +265,8 @@ public function getAccessLog(): ?AccessLog
273265 public function removeAccessLog (): bool
274266 {
275267 try {
276- $ request = new UnitRequest (self ::SOCKET , self ::ADDRESS );
277- $ request ->setMethod (HttpMethodsEnum::DELETE ->value );
278- $ request ->send ('/config/access_log ' );
268+ $ this ->_unitRequest ->setMethod (HttpMethodsEnum::DELETE ->value );
269+ $ this ->_unitRequest ->send ('/config/access_log ' );
279270 } catch (UnitException $ exception ) {
280271 return false ;
281272 }
0 commit comments