/**
  * @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);
 }
Example #2
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 ApplicationInterface $application
  *
  * @return mixed
  */
 public function run(ApplicationInterface $app)
 {
     $app->setParam('layout.name', 'home');
 }