/**
  * @param ApplicationInterface $app
  *
  * @throws Exception
  */
 public function run(ApplicationInterface $app)
 {
     $this->application = $app;
     $action = $app->getRequest()->getAction();
     if ($action) {
         $actionMiddleware = new ActionMiddleware(Invokable::cast($action));
         $serviceId = $action instanceof ServiceReference ? $action->getId() : $this->computeServiceName($app->getRequest()->getUri()->getPath());
         $app->getStep('action')->plug($actionMiddleware);
     } else {
         $route = $app->getRequest()->getRoute();
         // compute service id
         $serviceId = $this->computeServiceName($route);
         // if no service matching the route has been registered,
         // try to locate a class that could be used as service
         if (!$app->getServicesFactory()->isServiceRegistered($serviceId)) {
             $actionClass = $this->resolveActionClassName($route);
             $action = $this->resolveActionFullyQualifiedName($actionClass);
             if (!$action) {
                 throw new Exception(sprintf('No callback found to map the requested route "%s"', $route), Exception::ACTION_NOT_FOUND);
             }
             $app->getServicesFactory()->registerService(['id' => $serviceId, 'class' => $action]);
         }
         // replace action by serviceId to ensure it will be fetched using the ServicesFactory
         $actionReference = new ServiceReference($serviceId);
         // wrap action to inject returned value in application
         $app->getStep('action')->plug($actionMiddleware = new ActionMiddleware($actionReference));
     }
     // store action as application parameter for further reference
     $app->setParam('runtime.action.middleware', $actionMiddleware);
     $app->setParam('runtime.action.service-id', $serviceId);
 }
 /**
  * @param $filter
  */
 public function addFilter($filter)
 {
     if (is_null($this->filters)) {
         $this->initFiltersCollection();
     }
     $this->filters->append(Invokable::cast($filter));
 }
Exemple #3
0
 public function run(ApplicationInterface $app)
 {
     $matchedRoute = $app->getRequest()->getMatchedRoute();
     $action = Invokable::cast($matchedRoute->getAction());
     $app->getServicesFactory()->injectDependencies($action->getCallable());
     $app->setParam('runtime.action.middleware', $action);
     $result = $action->getCallable()($app);
     if ($result instanceof Response) {
         $app->setResponse($result);
     } else {
         // set default content type
         $app->setResponse((new HttpResponse())->withHeader('Content-Type', 'text/html'));
         Collection::cast($result)->each(function ($value, $var) {
             Vars::set($var, $value);
         });
     }
 }
 /**
  * @param $injector invokable
  *
  * @return $this
  */
 public function registerInjector($injector)
 {
     if (!is_callable($injector)) {
         // turn injector to Invokable if it is not a native callable
         $injector = Invokable::cast($injector);
     }
     $this->injectors[] = $injector;
     return $this;
 }
 public function testNotInvokableReferencedServiceException()
 {
     $service = new DynamicMethodProvider();
     $servicesFactory = $this->getMock(ServicesFactory::class);
     $servicesFactory->expects($this->once())->method('has')->with('service.id')->willReturn(true);
     $servicesFactory->expects($this->once())->method('get')->with('service.id')->willReturn($service);
     $application = $this->getMock(ApplicationInterface::class);
     $application->method('getServicesFactory')->willReturn($servicesFactory);
     $invokable = new Invokable(new ServiceReference('service.id'));
     $invokable->setApplication($application);
     try {
         $invokable->getCallable();
     } catch (Exception $e) {
     }
     $this->expectsException(function () use($e) {
         throw $e;
     }, Exception::class, null, Exception::FAILED_RUNNING_OPERATION);
     $this->expectsException(function () use($e) {
         throw $e->getPrevious();
     }, Exception::class, null, Exception::REFERENCED_SERVICE_IS_NOT_CALLABLE);
 }
 /**
  * EmbeddedMiddleware constructor.
  *
  * @param $invokable
  */
 public function __construct($invokable)
 {
     $this->invokable = Invokable::cast($invokable);
 }
 /**
  * @param  $exceptionHandler
  *
  * @return $this
  */
 public function setExceptionHandler($exceptionHandler) : ApplicationInterface
 {
     $this->exceptionHandler = Invokable::cast($exceptionHandler);
     return $this;
 }