Beispiel #1
0
 public function testChangeContainerDependencyInRuntime()
 {
     $container = new Container();
     $container->set('DB', MySql::class);
     $this->assertTrue($container->get('DB') instanceof MySql, 'Expecting MySql instance');
     $container->set('DB', Postgres::class);
     $this->assertTrue($container->get('DB') instanceof Postgres, 'Expecting Postgres instance');
 }
Beispiel #2
0
 /**
  * @param array    $queue
  * @param \Closure $controllerClosure
  *
  * @throws \Exception
  *
  * @return Response
  */
 private function proccessMiddlewareQueue(array $queue, \Closure $controllerClosure)
 {
     $return = null;
     if (!empty($queue)) {
         //Proccess queue
         $previousFn = $controllerClosure;
         foreach ($queue as $middleware) {
             //Middleware instance
             $obj = $this->container->get($middleware);
             if ($obj instanceof Middleware) {
                 $fn = function () use($obj, $previousFn) {
                     return $obj->handle($previousFn);
                 };
                 $previousFn = $fn;
             } else {
                 throw new \Exception("Invalid middleware: '{$middleware}'");
             }
         }
         $return = $previousFn();
     } else {
         //Run controller immediately
         $return = $controllerClosure();
     }
     return $return;
 }