コード例 #1
0
 public function register(\CentralApps\Base\Application $application)
 {
     $key = $this->key;
     $container = $application->getContainer();
     $container[$this->key] = $container->share(function ($c) use($key, $application) {
         // routing settings
         $routing_settings = $c->getSettingFromNestedKey($nested_key = array($key));
         $cache = isset($routing_settings['cache']) ? $routing_settings['cache'] : null;
         $locator = new \Symfony\Component\Config\FileLocator(array($application->getApplicationRootFolder()));
         $loader = new \Symfony\Component\Routing\Loader\YamlFileLoader($locator);
         $loader->load('routes.yml');
         $request = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
         $request_method = isset($_POST) && isset($_POST['_method']) ? $_POST['_method'] : (isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '');
         $request_context = new \Symfony\Component\Routing\RequestContext($request, $request_method, isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
         $router = new \Symfony\Component\Routing\Router(new \Symfony\Component\Routing\Loader\YamlFileLoader($locator), 'routes.yml', array('cache_dir' => $cache), $request_context);
         return $router;
     });
     $container[$this->key . '.url_generator'] = $container->share(function ($container) use($key) {
         return new \Symfony\Component\Routing\Generator\UrlGenerator($container[$key]->getRouteCollection(), new \Symfony\Component\Routing\RequestContext('', isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : ''));
     });
     $this->registerRouteFunction($application);
 }
コード例 #2
0
ファイル: Application.php プロジェクト: cangit/beatrix
 public function run()
 {
     if (isset($this->settings['maintenance'])) {
         if ($this->settings['maintenance'] === true) {
             $this['logger']->info('Site is in maintenance mode.');
             $maintenanceWorkers = $this->settings['maintenanceWorkers'];
             if (!in_array($_SERVER['REMOTE_ADDR'], $maintenanceWorkers)) {
                 $controller = '\\' . $this->settings['defaultPages']['maintenance'];
                 if (!class_exists($controller)) {
                     $this['logger']->warning('Could not locate/read maintenance controller defined in /app/config/beatrix/settings.php');
                     $loadFail = new LoadFail('500', $this);
                     $loadFail->run();
                     return;
                 }
                 if (method_exists($controller, '__construct')) {
                     $this->Controller = new $controller($this);
                 } else {
                     $this->Controller = new $controller();
                 }
                 $requestMethod = $this['request']->getMethod();
                 $this->initiateControllerMethod($requestMethod);
                 return;
             }
         }
     }
     if (!is_readable(APP_ROOT . '/app/config/routes.yml')) {
         $this['logger']->warning('Could not locate/read routes file. Looked for app/config/routes.yml');
         $loadFail = new LoadFail('500', $this);
         $loadFail->run();
         return;
     } else {
         $collection = new RouteCollection();
         $Locator = new FileLocator([APP_ROOT . '/app/config']);
         $loader = new YamlFileLoader($Locator);
         $collection->addCollection($loader->beatrixLoad('routes.yml', $this['cache'], $this->setting('cache.routes')));
         if (isset($this->settings['routes']) && is_array($this->settings['routes'])) {
             $routes = $this->settings['routes'];
             foreach ($routes as $route) {
                 if (is_file(APP_ROOT . '/vendor/' . $route . 'routes.yml')) {
                     $Locator = new FileLocator([APP_ROOT . '/vendor/' . $route]);
                     $loader = new \Symfony\Component\Routing\Loader\YamlFileLoader($Locator);
                     $collection->addCollection($loader->load('routes.yml'));
                 } else {
                     $this['logger']->warning('Could not find routing file: ' . APP_ROOT . '/vendor/' . $route . 'routes.yml');
                 }
             }
         }
         try {
             $Context = new RequestContext($this['request']);
             $matcher = new UrlMatcher($collection, $Context);
             $attributes = $matcher->match($this['request']->getPathInfo());
             $this['request']->attributes->add($attributes);
             if (substr($attributes['_controller'], 0, 1) === '@') {
                 $controller = substr($attributes['_controller'], 1);
             } else {
                 $controller = "controller\\" . $attributes['_controller'];
             }
         } catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
             $this['logger']->info(sprintf('Did not find a route matching input "%s", using app/config/routes.yml', $this['request']->getPathInfo()));
             $loadFail = new LoadFail('404', $this);
             $loadFail->run();
             return;
         } catch (\InvalidArgumentException $e) {
             $this['logger']->error('InvalidArgumentException thrown when trying to load resource: ' . $this['request']->getPathInfo());
             $loadFail = new LoadFail('500', $this);
             $loadFail->run();
             return;
         } catch (\Exception $e) {
             $this['logger']->error('Exception thrown when trying to load resource. Probably syntax error in routes.yml file.');
             $loadFail = new LoadFail('500', $this);
             $loadFail->run();
             return;
         }
         if (!class_exists($controller)) {
             $filePath = str_replace('\\', '/', $controller);
             $file = 'src/' . $filePath . '.php';
             $loadFail = new LoadFail('500', $this);
             if (!file_exists($file)) {
                 $this['logger']->error(sprintf('The file "%s" could not be found when trying to run controller "%s()".', $file, $controller));
             } else {
                 $this['logger']->error(sprintf('Could not find controller "%s()" in file "%s", malformed namespace or classname.', $controller, $file));
             }
             $loadFail->run();
             return;
         }
         if (method_exists($controller, '__construct')) {
             $this->Controller = new $controller($this);
         } else {
             $this->Controller = new $controller();
         }
     }
     $requestMethod = $this['request']->getMethod();
     $this->initiateControllerMethod($requestMethod);
 }