Exemplo n.º 1
0
 /**
  * Initiate set of modifiers.
  *
  * @return ModifierInterface[]
  */
 protected function getModifiers()
 {
     foreach ($this->modifiers as $index => $modifier) {
         if (!is_object($modifier)) {
             //Initiating using container
             $this->modifiers[$index] = $this->container->make($modifier, ['environment' => $this->environment]);
         }
     }
     return $this->modifiers;
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function __invoke(Request $request, Response $response, callable $next = null)
 {
     $queue = $this->container->make(CookieQueue::class, ['request' => $request, 'httpConfig' => $this->httpConfig]);
     //Opening cookie scope
     $scope = $this->container->replace(CookieQueue::class, $queue);
     try {
         /**
          * Debug: middleware creates scope for [CookieQueue].
          */
         $response = $next($this->unpackCookies($request)->withAttribute('cookieQueue', $queue), $response);
         //New cookies
         return $this->packCookies($response, $queue);
     } finally {
         $this->container->restore($scope);
     }
 }
Exemplo n.º 3
0
 /**
  * Get next chain to be called. Exceptions will be converted to responses.
  *
  * @param int      $position
  * @param Request  $request
  * @param Response $response
  * @return null|Response
  * @throws \Exception
  */
 protected function next($position, Request $request, Response $response)
 {
     if (!isset($this->middlewares[$position])) {
         //Middleware target endpoint to be called and converted into response
         return $this->mountResponse($request, $response);
     }
     /**
      * @var callable $next
      */
     $next = $this->middlewares[$position];
     if (is_string($next)) {
         //Resolve using container
         $next = $this->container->make($next);
     }
     //Executing next middleware
     return $next($request, $response, $this->getNext($position, $request, $response));
 }
Exemplo n.º 4
0
 /**
  * @param string $controller
  * @param string $action
  * @param array  $parameters
  * @return mixed
  * @throws ControllerException
  */
 protected function execute($controller, $action, array $parameters)
 {
     $benchmark = $this->benchmark('callAction', $controller . '::' . ($action ?: '~default~'));
     $scope = $this->container->replace(Vault::class, $this);
     $this->controller = $controller;
     try {
         //Initiating controller with all required dependencies
         $object = $this->container->make($this->config->controllers()[$controller]);
         if (!$object instanceof ControllerInterface) {
             throw new ControllerException("Invalid '{$controller}', ControllerInterface not implemented.", ControllerException::NOT_FOUND);
         }
         return $object->callAction($action, $parameters);
     } finally {
         $this->benchmark($benchmark);
         $this->container->restore($scope);
         $this->controller = '';
     }
 }
Exemplo n.º 5
0
 /**
  * Get engine by it's type.
  *
  * @param string $engine
  * @param bool   $reload If true engine will receive new instance of loader and enviroment.
  * @return EngineInterface
  */
 public function engine($engine, $reload = false)
 {
     if (!isset($this->engines[$engine])) {
         $parameters = $this->config->engineParameters($engine);
         $parameters['loader'] = $this->loader($engine);
         $parameters['environment'] = $this->environment();
         //We have to create an engine
         $benchmark = $this->benchmark('engine', $engine);
         try {
             $this->engines[$engine] = $this->container->make($this->config->engineClass($engine), $parameters);
         } finally {
             $this->benchmark($benchmark);
         }
         $reload = true;
     }
     //Configuring engine
     if ($reload) {
         $this->engines[$engine]->setLoader($this->loader($engine));
         $this->engines[$engine]->setEnvironment($this->environment());
     }
     return $this->engines[$engine];
 }
Exemplo n.º 6
0
 /**
  * Get or create instance of ConsoleApplication.
  *
  * @return Application
  */
 public function application()
 {
     if (!empty($this->application)) {
         return $this->application;
     }
     $this->application = new Application('Spiral Console Toolkit', Core::VERSION);
     //Commands lookup
     if (empty($this->commands)) {
         $this->locateCommands();
     }
     foreach ($this->commands as $command) {
         try {
             //Constructing command class
             $command = $this->container->make($command);
             if (method_exists($command, 'isAvailable') && !$command->isAvailable()) {
                 continue;
             }
         } catch (\Exception $exception) {
             continue;
         }
         $this->application->add($command);
     }
     return $this->application;
 }