예제 #1
0
 /**
  * @param string $name
  * @param array $data
  * @param boolean $qsa
  * @throws \Exception
  * @return string
  */
 public function assemble($name, array $data = [], $reset = \false, $qsa = \true)
 {
     static $baseUrl;
     if ($name === \null && $this->currentRoute instanceof Route) {
         $name = $this->currentRoute->getName();
     }
     if (!isset($this->routes[$name])) {
         throw new CoreException(sprintf('[' . __METHOD__ . '] Route "%s" not found!', $name), 500);
     }
     $route = $this->routes[$name];
     $pattern = $route->getPattern();
     $data += $this->globalParams;
     if (isset($this->routesStatic[$pattern])) {
         $url = $pattern;
     } else {
         $url = $route->assemble($data, $reset);
     }
     if ($qsa === \true) {
         $qs = $this->request->getQuery();
         if (!empty($data)) {
             $qs = $data + $qs;
         }
         $qs = array_filter($qs);
         if (!empty($qs)) {
             $url .= '?' . http_build_query($qs);
         }
     }
     if ($baseUrl === \null) {
         $baseUrl = $this->request->getBaseUrl();
     }
     return $baseUrl . static::URL_DELIMITER . trim($url, static::URL_DELIMITER);
 }
예제 #2
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;
 }
예제 #3
0
 public function registerServices()
 {
     $config = $this->container->get('config');
     $this->container->set('request', function () {
         return Request::createFromEnvironment();
     }, \false);
     $this->container->set('response', function () {
         return new HtmlResponse();
     }, \false);
     $this->container->set('router', function () {
         return new Router();
     }, \false);
     $this->container->set('event', function () {
         return new Event\Manager();
     }, \false);
     $this->container->set('logger', function () use($config) {
         return new FileLog($config->get('log'));
     }, \false);
     if ($config->get('log.enabled')) {
         $logger = $this->container->get('logger');
         ErrorHandler::register($logger);
         CoreException::setLogger($logger);
     }
     $this->container->setBindings($this, ['resolver' => 'registerResolverBinder', 'exception.handler' => 'registerExceptionBinder', 'translator' => 'registerTranslatorBinder', 'caches' => 'registerCacheBinder', 'cache' => 'registerCacheBinder', 'acl' => 'registerAclBinder', 'db' => 'registerDbBinder']);
 }