예제 #1
0
 /**
  * @param string $package
  * @param Http\Request $request
  * @param Http\Response $response
  * @param bool $subRequest
  * @throws CoreException
  * @return \Micro\Http\Response
  */
 public function resolve($package, Http\Request $request, Http\Response $response, $subRequest = \false)
 {
     if (!is_string($package) || strpos($package, '@') === \false) {
         throw new CoreException('[' . __METHOD__ . '] Package must be in [Package\\Handler@action] format', 500);
     }
     list($package, $action) = explode('@', $package);
     if (!class_exists($package, \true)) {
         throw new CoreException('[' . __METHOD__ . '] Package class "' . $package . '" not found', 404);
     }
     $parts = explode('\\', $package);
     $packageParam = Utils::decamelize($parts[0]);
     $controllerParam = Utils::decamelize($parts[count($parts) - 1]);
     $actionParam = Utils::decamelize($action);
     $request->setParam('package', $packageParam);
     $request->setParam('controller', $controllerParam);
     $request->setParam('action', $actionParam);
     $packageInstance = new $package($request, $response);
     if ($packageInstance instanceof Controller) {
         $actionMethod = lcfirst(Utils::camelize($action)) . 'Action';
     } else {
         $actionMethod = lcfirst(Utils::camelize($action));
     }
     if (!method_exists($packageInstance, $actionMethod)) {
         throw new CoreException('[' . __METHOD__ . '] Method "' . $actionMethod . '" not found in "' . $package . '"', 404);
     }
     if ($packageInstance instanceof ContainerAwareInterface) {
         $packageInstance->setContainer($this);
     }
     $scope = '';
     if ($packageInstance instanceof Controller) {
         $packageInstance->init();
         $scope = $packageInstance->getScope();
     }
     if (($packageResponse = $packageInstance->{$actionMethod}()) instanceof Http\Response) {
         return $packageResponse;
     }
     if (is_object($packageResponse) && !$packageResponse instanceof View) {
         throw new CoreException('[' . __METHOD__ . '] Package response is object and must be instance of View', 500);
     }
     if ($packageResponse === \null || is_array($packageResponse)) {
         if ($packageInstance instanceof Controller) {
             $view = $packageInstance->getView();
         } else {
             $view = new View();
         }
         if (is_array($packageResponse)) {
             $view->addData($packageResponse);
         }
         $packageResponse = $view;
     }
     if ($packageResponse instanceof View) {
         if ($packageResponse->getTemplate() === \null) {
             $packageResponse->setTemplate(($scope ? $scope . '/' : '') . $controllerParam . '/' . $actionParam);
         }
         $packageResponse->injectPaths((array) package_path($parts[0], 'Resources/views'));
         if (($eventResponse = $this->get('event')->trigger('render.start', ['view' => $packageResponse])) instanceof Http\Response) {
             return $eventResponse;
         }
         if ($subRequest) {
             $packageResponse->setRenderParent(\false);
         }
         $response->setBody((string) $packageResponse->render());
     } else {
         $response->setBody((string) $packageResponse);
     }
     return $response;
 }
예제 #2
0
 /**
  * @param mixed $module
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param bool $subRequest
  * @throws CoreException
  * @return ResponseInterface
  */
 public function resolve($module, ServerRequestInterface $request, ResponseInterface $response, $subRequest = \false)
 {
     if ($module instanceof ResponseInterface) {
         return $module;
     }
     if (($matches = $this->matchResolve($module)) === \null) {
         // write string to the response
         if (\is_string($module) || \is_object($module) && \method_exists($module, '__toString')) {
             $response->getBody()->write((string) $module);
             return $response;
         }
         throw new CoreException(\sprintf('Handler [%s] must be in [Handler@action] format', \is_object($module) ? \get_class($module) : $module));
     }
     $module = $matches[0];
     $action = $matches[1];
     if ($this->container->has($module)) {
         $moduleInstance = $this->container->get($module);
         if (!\is_object($moduleInstance) || $moduleInstance instanceof \Closure) {
             throw new CoreException('Handler "' . $module . '" is container service but it is not object', 500);
         }
         $module = \get_class($moduleInstance);
     } else {
         if (!\class_exists($module, \true)) {
             throw new CoreException('Handler class "' . $module . '" not found', 404);
         }
         $moduleInstance = new $module($request, $response, $this->container);
         if ($moduleInstance instanceof ContainerAwareInterface) {
             $moduleInstance->setContainer($this->container);
         }
     }
     $parts = explode('\\', $module);
     $moduleParam = Utils::decamelize($parts[0]);
     $controllerParam = Utils::decamelize($parts[count($parts) - 1]);
     $actionParam = Utils::decamelize($action);
     $request->withAttribute('module', $moduleParam);
     $request->withAttribute('controller', $controllerParam);
     $request->withAttribute('action', $actionParam);
     if ($moduleInstance instanceof Controller) {
         $action = $action . 'Action';
     }
     if (!\method_exists($moduleInstance, $action) && !\method_exists($moduleInstance, '__call')) {
         throw new CoreException('Method "' . $action . '" not found in "' . $module . '"', 404);
     }
     if ($moduleInstance instanceof Controller) {
         $moduleInstance->init();
     }
     $scope = '';
     if (\method_exists($moduleInstance, 'getScope')) {
         $scope = $moduleInstance->getScope();
     }
     if (($moduleResponse = $moduleInstance->{$action}()) instanceof ResponseInterface) {
         return $moduleResponse;
     }
     if (\is_object($moduleResponse) && !$moduleResponse instanceof View) {
         throw new CoreException('Handler response is object and must be instance of View', 500);
     }
     // resolve View object
     if ($moduleResponse === \null || is_array($moduleResponse)) {
         if ($moduleInstance instanceof Controller) {
             $view = $moduleInstance->getView();
         } else {
             $view = new View();
         }
         if (is_array($moduleResponse)) {
             $view->assign($moduleResponse);
         }
         $moduleResponse = $view;
     }
     if ($moduleResponse instanceof View) {
         if ($moduleResponse->getTemplate() === \null) {
             $moduleResponse->setTemplate(($scope ? $scope . '/' : '') . $controllerParam . '/' . $actionParam);
         }
         try {
             $moduleResponse->addPath(module_path($parts[0], '/Resources/views'), $moduleParam);
             $moduleResponse->addPath(config('view.paths', []));
         } catch (\Exception $e) {
         }
         if ($this->useEvent === \true && ($eventResponse = $this->container->get('event')->trigger('render.start', ['view' => $moduleResponse])) instanceof ResponseInterface) {
             return $eventResponse;
         }
         if ($subRequest) {
             $moduleResponse->setRenderParent(\false);
         }
         $response->getBody()->write((string) $moduleResponse->render());
     } else {
         $response->getBody()->write((string) $moduleResponse);
     }
     return $response;
 }