public function run() { $app = $this->xtr->getApplication(); /** @var $matcher RedirectableUrlMatcher */ $context = $app['request_context']; $matched = false; $path = $this->xtr->getRequest()->getPathInfo(); $lastException = null; // matching fallback route groups: foreach ($this->xtr['routes'] as $fallback => $routes) { $routes = RouteCollection::FromArray($routes)->flat(); $matcher = new RedirectableUrlMatcher($routes->toSilexCollection(), $context); try { $matcher->match($path); $matched = $fallback; break; } catch (ResourceNotFoundException $e) { // not found: silent continue $lastException = $e; } } if ($matched === false) { throw $lastException; } // configuring distinct service: $allow = [$matched]; $fallbacks = array_flip(array_keys($this->xtr['routes'])); unset($fallbacks[$matched]); $deny = array_keys($fallbacks); $this->xtr['@distinct.settings'] = ['allow' => $allow, 'deny' => $deny]; }
public function run() { $this->migrateSettings(); $app = $this->xtr->getApplication(); $routes = $this->xtr['routes']; /** @var $appRoutes \Symfony\Component\Routing\RouteCollection */ $appRoutes = $app['routes']; // adding allowed routes before routing, silex could match one: foreach ($this->config['settings']['allow'] as $allowed) { if ($allowed == '*') { $silexRoutes = RouteCollection::FromArray($routes)->flat()->toSilexCollection(); } else { if (!isset($routes[$allowed]) or !is_array($routes[$allowed])) { throw new \RuntimeException('Allowed distinct block "' . $allowed . '" not found or illegal.'); } $silexRoutes = RouteCollection::FromArray($routes[$allowed])->flat()->toSilexCollection(); } $appRoutes->addCollection($silexRoutes); } // adding denied routes before the controller executes, because these might be needed for generating URLs: $app->before(function (Request $request, Application $app) use($routes, $appRoutes) { foreach ($this->config['settings']['deny'] as $denied) { if ($denied == '-') { break; } if (!isset($routes[$denied]) or !is_array($routes[$denied])) { throw new \RuntimeException('Denied distinct block "' . $denied . '" not found or illegal.'); } $silexRoutes = RouteCollection::FromArray($routes[$denied])->flat()->toSilexCollection(); $appRoutes->addCollection($silexRoutes); } }); // flagging XTR service that we completed the handling of routes, no more sub-services need to be executed: $this->xtr->flagProcessed(); }