33namespace Pavlusha311245 \UnitPhpSdk ;
44
55use Pavlusha311245 \UnitPhpSdk \Abstract \ApplicationAbstract ;
6+ use Pavlusha311245 \UnitPhpSdk \Config \AccessLog ;
67use Pavlusha311245 \UnitPhpSdk \Config \Application ;
78use Pavlusha311245 \UnitPhpSdk \Config \Listener ;
89use Pavlusha311245 \UnitPhpSdk \Config \Route ;
9- use Pavlusha311245 \UnitPhpSdk \Enums \ApplicationTypeEnum ;
10+ use Pavlusha311245 \UnitPhpSdk \Enums \HttpMethodsEnum ;
11+ use Pavlusha311245 \UnitPhpSdk \Exceptions \UnitException ;
1012use Pavlusha311245 \UnitPhpSdk \Interfaces \ConfigInterface ;
11- use PHPUnit \TextUI \Configuration \Php ;
1213
1314/**
1415 * This class contains Nginx Unit config data
1516 */
1617class Config implements ConfigInterface
1718{
19+ const SOCKET = '/usr/local/var/run/unit/control.sock ' ;
20+ const ADDRESS = 'http://localhost ' ;
21+
1822 /**
1923 * Listeners accept requests
2024 *
@@ -51,30 +55,41 @@ class Config implements ConfigInterface
5155 */
5256 public function __construct (array $ data )
5357 {
54- foreach ($ data ['routes ' ] as $ routeName => $ routeData ) {
55- $ this ->_routes [$ routeName ] = new Route ($ routeName , $ routeData );
58+ if (array_key_exists ('routes ' , $ data )) {
59+ foreach ($ data ['routes ' ] as $ routeName => $ routeData ) {
60+ $ this ->_routes [$ routeName ] = new Route ($ routeName , $ routeData );
61+ }
5662 }
57- foreach ($ data ['applications ' ] as $ appName => $ appData ) {
58- // TODO: implement go and nodejs detect
59- $ this ->_applications [$ appName ] = match ($ appData ['type ' ]) {
60- 'php ' => new Application \PhpApplication ($ appData ),
61- 'external ' => new Application \NodeJsApplication ($ appData ),
62- };
63+
64+ if (array_key_exists ('applications ' , $ data )) {
65+ foreach ($ data ['applications ' ] as $ appName => $ appData ) {
66+ // TODO: implement go and nodejs detect
67+ $ this ->_applications [$ appName ] = match ($ appData ['type ' ]) {
68+ 'php ' => new Application \PhpApplication ($ appData ),
69+ 'external ' => new Application \NodeJsApplication ($ appData ),
70+ };
71+
72+ $ this ->_applications [$ appName ]->setName ($ appName );
73+ }
6374 }
6475
65- foreach ($ data ['listeners ' ] as $ listener => $ listenerData ) {
66- $ listener = (new Listener (
67- _listener: $ listener
68- ))->parseFromArray ($ listenerData );
69- $ typePath = $ listener ->getPass ()[0 ];
70- $ typePathName = $ listener ->getPass ()[1 ];
76+ if (array_key_exists ('listeners ' , $ data )) {
77+ foreach ($ data ['listeners ' ] as $ listener => $ listenerData ) {
78+ $ listener = (new Listener (
79+ _listener: $ listener
80+ ))->parseFromArray ($ listenerData );
81+ $ typePath = $ listener ->getPass ()[0 ];
82+ $ typePathName = $ listener ->getPass ()[1 ];
7183
72- ($ this ->{"_ {$ typePath }" }[$ typePathName ])->setListener ($ listener );
84+ ($ this ->{"_ {$ typePath }" }[$ typePathName ])->setListener ($ listener );
7385
74- $ this ->_listeners [] = $ listener ;
86+ $ this ->_listeners [] = $ listener ;
87+ }
7588 }
7689
77- $ this ->_upstreams = $ data ['upstreams ' ] ?? [];
90+ if (array_key_exists ('upstreams ' , $ data )) {
91+ $ this ->_upstreams = $ data ['upstreams ' ] ?? [];
92+ }
7893 }
7994
8095 /**
@@ -95,9 +110,23 @@ public function getListenerByPort(int $port): Listener|null
95110 }
96111
97112 /**
98- * Get listeners from config
113+ * Remove listener
99114 *
100- * @return array
115+ * @throws UnitException
116+ */
117+ public function removeListener (Listener $ listener ): bool
118+ {
119+ $ request = new UnitRequest (self ::SOCKET , self ::ADDRESS );
120+ $ request ->setMethod (HttpMethodsEnum::DELETE ->value );
121+
122+ $ listenerId = $ listener ->getListener ();
123+ $ request ->send ("/config/listeners/ {$ listenerId }" );
124+
125+ return true ;
126+ }
127+
128+ /**
129+ * @inheritDoc
101130 */
102131 public function getListeners (): array
103132 {
@@ -125,6 +154,20 @@ public function getApplication($applicationName): ApplicationAbstract
125154 return $ this ->_applications [$ applicationName ];
126155 }
127156
157+ /**
158+ * @throws UnitException
159+ */
160+ public function removeApplication (ApplicationAbstract $ application ): bool
161+ {
162+ $ request = new UnitRequest (self ::SOCKET , self ::ADDRESS );
163+ $ request ->setMethod (HttpMethodsEnum::DELETE ->value );
164+
165+ $ applicationName = $ application ->getName ();
166+ print_r ($ request ->send ("/config/applications/ {$ applicationName }" ));
167+
168+ return true ;
169+ }
170+
128171 /**
129172 * Get routes from config
130173 *
@@ -138,14 +181,43 @@ public function getRoutes(): array
138181 /**
139182 * Get route from config by name
140183 *
141- * @param $routeName
142- * @return mixed
184+ * @param string $routeName
185+ * @return Route|null
143186 */
144187 public function getRoute (string $ routeName ): Route |null
145188 {
146189 return $ this ->_routes [$ routeName ] ?? null ;
147190 }
148191
192+ /**
193+ * @throws UnitException
194+ */
195+ public function removeRoutes (): bool
196+ {
197+ foreach ($ this ->getRoutes () as $ route ) {
198+ if ($ route ->hasListeners ()) {
199+ foreach ($ route ->getListeners () as $ listener ) {
200+ $ this ->removeListener ($ listener );
201+ }
202+ }
203+ }
204+
205+ try {
206+ $ request = new UnitRequest (self ::SOCKET , self ::ADDRESS );
207+ $ request ->setMethod (HttpMethodsEnum::DELETE ->value );
208+ $ request ->send ('/config/routes ' );
209+ } catch (UnitException $ exception ) {
210+ return false ;
211+ }
212+
213+ return true ;
214+ }
215+
216+ public function removeRoute (Route |string $ route ): bool
217+ {
218+ // TODO: should be implemented
219+ }
220+
149221 /**
150222 * Get upstreams
151223 *
@@ -156,6 +228,51 @@ public function getUpstreams(): mixed
156228 return $ this ->_upstreams ;
157229 }
158230
231+ /**
232+ * @inheritDoc
233+ */
234+ public function setAccessLog ($ path , $ format = null ): bool
235+ {
236+ $ data ['path ' ] = $ path ;
237+
238+ if (!empty ($ format )) {
239+ $ data ['format ' ] = $ format ;
240+ }
241+
242+ try {
243+ $ request = new UnitRequest (self ::SOCKET , self ::ADDRESS );
244+ $ request ->setMethod (HttpMethodsEnum::PUT ->value );
245+ $ request ->setData (json_encode ($ data ));
246+ $ request ->send ('/config/access_log ' );
247+ } catch (UnitException $ exception ) {
248+ return false ;
249+ }
250+
251+ return true ;
252+ }
253+
254+ public function getAccessLog (): ?AccessLog
255+ {
256+ $ request = new UnitRequest (self ::SOCKET , self ::ADDRESS );
257+ $ result = $ request ->send ('/config/access_log ' );
258+
259+ // TODO: need null
260+ return new AccessLog ($ result );
261+ }
262+
263+ public function removeAccessLog (): bool
264+ {
265+ try {
266+ $ request = new UnitRequest (self ::SOCKET , self ::ADDRESS );
267+ $ request ->setMethod (HttpMethodsEnum::DELETE ->value );
268+ $ request ->send ('/config/access_log ' );
269+ } catch (UnitException $ exception ) {
270+ return false ;
271+ }
272+
273+ return true ;
274+ }
275+
159276 /**
160277 * Return config as array
161278 *
0 commit comments