22
33namespace Corviz ;
44
5+ use Closure ;
56use Corviz \Behaviour \Runnable ;
67use Corviz \DI \Container ;
78use Corviz \DI \Provider ;
1213use Corviz \Mvc \Controller ;
1314use Corviz \Routing \Map ;
1415use Corviz \String \ParametrizedString ;
16+ use Exception ;
17+ use RecursiveArrayIterator ;
18+ use RecursiveIteratorIterator ;
1519
1620/**
1721 * Main app controller.
@@ -63,15 +67,15 @@ public function config(string $conf)
6367 /**
6468 * Get the current running application.
6569 *
66- * @return \Corviz\ Application
70+ * @return Application
6771 */
6872 public static function current (): self
6973 {
7074 return self ::$ current ;
7175 }
7276
7377 /**
74- * @return \Corviz\DI\ Container
78+ * @return Container
7579 */
7680 public function getContainer (): Container
7781 {
@@ -116,7 +120,7 @@ public function run(...$args)
116120
117121 //Check controller
118122 if (!$ controller instanceof Controller) {
119- throw new \ Exception ("Invalid controller: {$ route ['controller ' ]}" );
123+ throw new Exception ("Invalid controller: {$ route ['controller ' ]}" );
120124 }
121125
122126 /*
@@ -143,6 +147,25 @@ public function run(...$args)
143147 self ::$ current = null ;
144148 }
145149
150+ /**
151+ * @param callable $proccess
152+ * @return void
153+ * @throws Exception
154+ */
155+ public function background (callable $ proccess )
156+ {
157+ self ::$ current = $ this ;
158+ $ this ->container ->set (self ::class, $ this );
159+
160+ //Load application definitions.
161+ $ this ->registerProviders ();
162+
163+ $ proccess = Closure::fromCallable ($ proccess );
164+ $ this ->container ->invoke ($ proccess , '__invoke ' );
165+
166+ self ::$ current = null ;
167+ }
168+
146169 /**
147170 * @param array $groups
148171 *
@@ -153,8 +176,8 @@ private function buildMiddlewareQueue(array $groups)
153176 $ queue = [];
154177 $ middlewareList = $ this ->config ('app ' )['middleware ' ];
155178
156- $ groupsIterator = new \ RecursiveIteratorIterator (
157- new \ RecursiveArrayIterator ($ groups )
179+ $ groupsIterator = new RecursiveIteratorIterator (
180+ new RecursiveArrayIterator ($ groups )
158181 );
159182
160183 foreach ($ groupsIterator as $ middleware ) {
@@ -170,13 +193,13 @@ private function buildMiddlewareQueue(array $groups)
170193
171194 /**
172195 * @param array $queue
173- * @param \ Closure $controllerClosure
196+ * @param Closure $controllerClosure
174197 *
175- * @throws \ Exception
198+ * @throws Exception
176199 *
177200 * @return Response
178201 */
179- private function proccessMiddlewareQueue (array $ queue , \ Closure $ controllerClosure )
202+ private function proccessMiddlewareQueue (array $ queue , Closure $ controllerClosure )
180203 {
181204 $ return = null ;
182205
@@ -194,7 +217,7 @@ private function proccessMiddlewareQueue(array $queue, \Closure $controllerClosu
194217 };
195218 $ previousFn = $ fn ;
196219 } else {
197- throw new \ Exception ("Invalid middleware: ' $ middleware' " );
220+ throw new Exception ("Invalid middleware: ' $ middleware' " );
198221 }
199222 }
200223
@@ -217,7 +240,7 @@ private function registerProviders()
217240 $ obj = new $ provider ($ this );
218241
219242 if (!$ obj instanceof Provider) {
220- throw new \ Exception ("Invalid provider: $ provider " );
243+ throw new Exception ("Invalid provider: $ provider " );
221244 }
222245
223246 $ this ->container ->invoke ($ obj , 'register ' );
0 commit comments