Exemple #1
0
 /**
  * @param Request  $request
  * @param callable $next
  *
  * @return mixed
  */
 public function handle(Request $request, callable $next)
 {
     /** @var DispatchResult $response */
     $response = $next($request);
     if (!$response instanceof DispatchResult) {
         return $response;
     }
     $templateName = $response->getDispatchedClassName();
     $subPath = $response->getDispatchedMethod();
     $subPath = Word::camelCaseToDash($subPath);
     $response = $response->getResult();
     $data = [];
     if ($response instanceof ViewModel) {
         $data = $response->getContent();
     }
     if ($data instanceof Response) {
         return $data;
     }
     /** @var View $viewRenderer */
     $viewRenderer = $this->serviceManager->load('Arhitect\\Contract\\View');
     $templateName = explode('Controller\\', $templateName);
     array_shift($templateName);
     $templateName = implode('/', $templateName);
     $templateName = preg_replace('/Controller$/', '', $templateName);
     $templateName .= DIRECTORY_SEPARATOR;
     if ($response instanceof HtmlView) {
         if ($response->getTemplateName()) {
             $templateName = $response->getTemplateName();
             $subPath = NULL;
         }
     }
     $content = $viewRenderer->render(strtolower($templateName) . strtolower($subPath) . '.twig', $data);
     return new Response($content);
 }
 /**
  * @param Configuration  $configuration
  * @param ServiceManager $serviceManager
  */
 public function __construct(Configuration $configuration, ServiceManager $serviceManager)
 {
     if (!$configuration->get('twig', null)) {
         return;
     }
     $cachePath = $configuration->getApplicationPath() . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
     $options = [];
     if ($configuration->get('cache.config.enabled', false)) {
         $options['cache'] = $cachePath;
     }
     $paths = $configuration->get('twig.paths', []);
     foreach ($paths as $offset => $path) {
         $paths[$offset] = dirname($configuration->getApplicationPath()) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . $path;
     }
     $loader = new \Twig_Loader_Filesystem($paths);
     $this->twig = new \Twig_Environment($loader, $options + $configuration->get('twig.options', []));
     if (isset($configuration->get('twig.options', [])['debug']) && $configuration->get('twig.options', [])['debug']) {
         $this->twig->addExtension(new \Twig_Extension_Debug());
     }
     foreach ($configuration->get('twig.helpers', []) as $functionName => $helperClass) {
         $func = new \Twig_SimpleFunction($functionName, function () use($helperClass, $serviceManager) {
             $instance = $serviceManager->load($helperClass);
             return call_user_func_array($instance, func_get_args());
         });
         $this->twig->addFunction($func);
     }
     foreach ($configuration->get('twig.filters', []) as $functionName => $helperClass) {
         $func = new \Twig_SimpleFilter($functionName, function () use($helperClass, $serviceManager) {
             $instance = $serviceManager->load($helperClass);
             return call_user_func_array($instance, func_get_args());
         });
         $this->twig->addFilter($func);
     }
 }
 /**
  * @param Request  $request
  * @param callable $next
  *
  * @return mixed
  * @throws \Exception
  */
 public function handle(Request $request, callable $next)
 {
     $resource = $request->param('__NAMESPACE__');
     $controllerName = $request->param('__CONTROLLER__');
     $method = $request->param('__ACTION__');
     if ($controllerName) {
         if (FALSE === strstr($controllerName, '\\')) {
             $controllerName = Word::dashToCamelCase($controllerName);
             $controllerName .= 'Controller';
             $controllerName = rtrim($resource, '\\') . '\\' . $controllerName;
         }
     } else {
         $controllerName = $resource;
     }
     if (FALSE !== strstr($controllerName, '@')) {
         $controllerName = explode('@', $controllerName);
         $method = $controllerName[1];
         $controllerName = $controllerName[0];
     }
     $method = lcfirst(Word::dashToCamelCase($method));
     $request->set('__CONTROLLER__', $controllerName);
     $request->set('__ACTION__', $method);
     if (!class_exists($controllerName)) {
         throw new \Exception(sprintf('Class "%s" does not exist', $controllerName), Response::HTTP_NOT_FOUND);
     }
     if (!method_exists($controllerName, strtoupper($method))) {
         throw new \Exception(sprintf('Method "%s" does not exist in class "%s"', $method, $controllerName), Response::HTTP_NOT_FOUND);
     }
     $controller = $this->serviceManager->load($controllerName);
     $params = $request->query();
     $params = ArrayUtils::merge($params, $request->post(), FALSE, TRUE);
     //        $params = ArrayUtils::merge($params, $request->attributes->all(), false, true);
     try {
         $mvcResponse = $this->serviceManager->call([$controller, $method], $params);
     } catch (ValidationError $error) {
         $method = $request->post('__PREVIOUS__', 'get');
         $mvcResponse = $this->serviceManager->call([$controller, strtolower($method)], $params);
     }
     if ($mvcResponse instanceof Response) {
         return $mvcResponse;
     }
     $mvcResponse = new DispatchResult($mvcResponse, $controllerName, $method);
     $result = $next($request);
     if ($result) {
         return $result;
     }
     return $mvcResponse;
 }
 /**
  * @return null
  */
 public function __invoke()
 {
     $services = $this->configuration->get('services', []);
     $table = [];
     foreach ($services as $providerClass) {
         /** @var ServiceProvider $provider */
         $provider = new $providerClass();
         $provides = $provider->provide();
         if (is_callable($provides)) {
             try {
                 $provides = $this->serviceManager->get(is_array($provider->alias()) ? $provider->alias()[0] : $provider->alias());
                 $provides = 'Factory => ' . get_class($provides);
             } catch (\Exception $e) {
                 $provides = 'Factory => (ERROR. Could not load by "Class")';
             }
         }
         $table[] = ['Class' => implode(',', is_array($provider->alias()) ? $provider->alias() : [$provider->alias()]), 'Provides' => $provides, 'Defined by' => $providerClass];
     }
     $this->climate->out('');
     $this->climate->yellowTable($table);
     $this->climate->out('');
 }
 /**
  * @param Request  $request
  * @param callable $next
  *
  * @return mixed
  */
 public function handle(Request $request, callable $next)
 {
     $this->serviceManager->add(Request::class, $request);
     return $next($request);
 }