/** * {@inheritdoc} */ protected function alterRoutes(RouteCollection $collection) { foreach ($collection as $name => $route) { if ($route->hasRequirement('_module_dependencies')) { $modules = $route->getRequirement('_module_dependencies'); $explode_and = $this->explodeString($modules, '+'); if (count($explode_and) > 1) { foreach ($explode_and as $module) { // If any moduleExists() call returns FALSE, remove the route and // move on to the next. if (!$this->moduleHandler->moduleExists($module)) { $collection->remove($name); continue 2; } } } else { // OR condition, exploding on ',' character. foreach ($this->explodeString($modules, ',') as $module) { if ($this->moduleHandler->moduleExists($module)) { continue 2; } } // If no modules are found, and we get this far, remove the route. $collection->remove($name); } } } }
/** * {@inheritdoc} */ public function filter(RouteCollection $collection, Request $request) { $method = $request->getMethod(); $all_supported_methods = []; foreach ($collection->all() as $name => $route) { $supported_methods = $route->getMethods(); // A route not restricted to specific methods allows any method. If this // is the case, we'll also have at least one route left in the collection, // hence we don't need to calculate the set of all supported methods. if (empty($supported_methods)) { continue; } // If the GET method is allowed we also need to allow the HEAD method // since HEAD is a GET method that doesn't return the body. if (in_array('GET', $supported_methods, TRUE)) { $supported_methods[] = 'HEAD'; } if (!in_array($method, $supported_methods, TRUE)) { $all_supported_methods = array_merge($supported_methods, $all_supported_methods); $collection->remove($name); } } if (count($collection)) { return $collection; } throw new MethodNotAllowedException(array_unique($all_supported_methods)); }
/** * {@inheritdoc} */ protected function alterRoutes(RouteCollection $collection) { foreach ($this->entityStorage->loadMultiple() as $entity_id => $entity) { /** @var $entity \Drupal\page_manager\PageInterface */ // If the page is disabled skip making a route for it. if (!$entity->status() || $entity->isFallbackPage()) { continue; } // Prepare a route name to use if this is a custom page. $route_name = "page_manager.page_view_{$entity_id}"; // Prepare the values that need to be altered for an existing page. $path = $entity->getPath(); $parameters = ['page_manager_page' => ['type' => 'entity:page']]; // Loop through all existing routes to see if this is overriding a route. foreach ($collection->all() as $name => $collection_route) { // Find all paths which match the path of the current display. $route_path = RouteCompiler::getPathWithoutDefaults($collection_route); $route_path = RouteCompiler::getPatternOutline($route_path); if ($path == $route_path) { // Adjust the path to translate %placeholders to {slugs}. $path = $collection_route->getPath(); // Merge in any route parameter definitions. $parameters += $collection_route->getOption('parameters') ?: []; // Update the route name this will be added to. $route_name = $name; // Remove the existing route. $collection->remove($route_name); break; } } // Construct an add a new route. $route = new Route($path, ['_entity_view' => 'page_manager_page', 'page_manager_page' => $entity_id, '_title' => $entity->label()], ['_entity_access' => 'page_manager_page.view'], ['parameters' => $parameters, '_admin_route' => $entity->usesAdminTheme()]); $collection->add($route_name, $route); } }
/** * {@inheritdoc} */ public function filter(RouteCollection $collection, Request $request) { // Generates a list of Symfony formats matching the acceptable MIME types. // @todo replace by proper content negotiation library. $acceptable_mime_types = $request->getAcceptableContentTypes(); $acceptable_formats = array_filter(array_map(array($request, 'getFormat'), $acceptable_mime_types)); $primary_format = $request->getRequestFormat(); foreach ($collection as $name => $route) { // _format could be a |-delimited list of supported formats. $supported_formats = array_filter(explode('|', $route->getRequirement('_format'))); if (empty($supported_formats)) { // No format restriction on the route, so it always matches. Move it to // the end of the collection by re-adding it. $collection->add($name, $route); } elseif (in_array($primary_format, $supported_formats)) { // Perfect match, which will get a higher priority by leaving the route // on top of the list. } elseif (in_array('*/*', $acceptable_mime_types) || array_intersect($acceptable_formats, $supported_formats)) { // Move it to the end of the list. $collection->add($name, $route); } else { // Remove the route if it does not match at all. $collection->remove($name); } } if (count($collection)) { return $collection; } // We do not throw a // \Symfony\Component\Routing\Exception\ResourceNotFoundException here // because we don't want to return a 404 status code, but rather a 406. throw new NotAcceptableHttpException(SafeMarkup::format('No route found for the specified formats @formats.', array('@formats' => implode(' ', $acceptable_mime_types)))); }
/** * {@inheritdoc} */ public function filter(RouteCollection $collection, Request $request) { // The Content-type header does not make sense on GET requests, because GET // requests do not carry any content. Nothing to filter in this case. if ($request->isMethod('GET')) { return $collection; } $format = $request->getContentType(); foreach ($collection as $name => $route) { $supported_formats = array_filter(explode('|', $route->getRequirement('_content_type_format'))); if (empty($supported_formats)) { // No restriction on the route, so we move the route to the end of the // collection by re-adding it. That way generic routes sink down in the // list and exact matching routes stay on top. $collection->add($name, $route); } elseif (!in_array($format, $supported_formats)) { $collection->remove($name); } } if (count($collection)) { return $collection; } // We do not throw a // \Symfony\Component\Routing\Exception\ResourceNotFoundException here // because we don't want to return a 404 status code, but rather a 415. throw new UnsupportedMediaTypeHttpException('No route found that matches "Content-Type: ' . $request->headers->get('Content-Type') . '"'); }
protected function storeRoutes(RouteCollection $collection) { $stored = []; foreach ($this->routeManager->getAllRoutes() as $routeName) { $stored[$routeName] = $collection->get($routeName); $collection->remove($routeName); } $this->storage->save($stored); $this->stored = true; }
public function addCollection(RouteCollection $collection) { foreach ($collection->all() as $name => $route) { $this->add($name, $route); } // remove all the routes so that we have an empty collection when calling parent method // and we are not re-adding the routes again $collection->remove(array_keys($collection->all())); // call parent method with empty collection to merge the collection's resources parent::addCollection($collection); }
/** * Loads the i18n routes. * * @param RouteCollection $routes A RouteCollection instance */ public function load(RouteCollection $routes) { // dump($routes->all());die; foreach ($routes->all() as $name => $route) { if ($this->routeExclusionStrategy->shouldExcludeRoute($name, $route)) { continue; } foreach ($this->pathGenerationStrategy->generateI18nPaths($name, $route) as $path => $locales) { foreach ($locales as $locale) { $localeRoute = clone $route; $localeRoute->setPath($path); $localeRoute->setDefault('_locale', $locale); $routes->add(sprintf('%s%s%s', $locale, self::ROUTING_PREFIX, $name), $localeRoute); } // Removes the default route, we do not need it any more $routes->remove($name); } } }
/** * {@inheritdoc} */ public function filter(RouteCollection $collection, Request $request) { $format = $request->getRequestFormat('html'); /** @var \Symfony\Component\Routing\Route $route */ foreach ($collection as $name => $route) { // If the route has no _format specification, we move it to the end. If it // does, then no match means the route is removed entirely. if ($supported_formats = array_filter(explode('|', $route->getRequirement('_format')))) { if (!in_array($format, $supported_formats)) { $collection->remove($name); } } else { $collection->add($name, $route); } } if (count($collection)) { return $collection; } // We do not throw a // \Symfony\Component\Routing\Exception\ResourceNotFoundException here // because we don't want to return a 404 status code, but rather a 406. throw new NotAcceptableHttpException("No route found for the specified format {$format}."); }
/** * {@inheritdoc} * * Invalid page manager routes will be removed. Routes not controlled by page * manager will be moved to the end of the collection. Once a valid page * manager route has been found, all other page manager routes will also be * removed. */ public function filter(RouteCollection $collection, Request $request) { // Only proceed if the collection is non-empty. if (!$collection->count()) { return $collection; } // Store the unaltered request attributes. $original_attributes = $request->attributes->all(); $page_manager_route_found = FALSE; foreach ($collection as $name => $route) { $attributes = $this->getRequestAttributes($route, $name, $request); // Add the enhanced attributes to the request. $request->attributes->add($attributes); if ($page_variant_id = $route->getDefault('page_manager_page_variant')) { // If a page manager route was already found, remove this one from the // collection. if ($page_manager_route_found) { $collection->remove($name); } elseif ($this->checkPageVariantAccess($page_variant_id)) { // Mark that a valid page manager route was found. $page_manager_route_found = TRUE; // Replace the original attributes with the newly processed attributes. $original_attributes = $request->attributes->all(); } else { // Remove routes for variants that fail access. $collection->remove($name); } } else { // If this route has no page variant, move it to the end of the list. $collection->add($name, $route); } // Restore the original request attributes. $request->attributes->replace($original_attributes); } return $collection; }
/** * @param RouteCollection $collection * * @return RouteCollection */ protected function removeMappingSourceRoutes($collection) { $appSlugs = $this->findAllApplicationSlugs($this->mappings); $trailingRoutesOrdering = 1000; foreach ($collection->all() as $name => $route) { if ($route->getOption('do_not_remove') || $route->getOption('force_mapping')) { continue; } // Remove the trailing routes and keep it in an array to place it back at the end of the Router if ($route->getOption('trailing_route')) { if (!($ordering = $route->getOption('ordering'))) { $ordering = $trailingRoutesOrdering; $trailingRoutesOrdering++; } $this->trailingRoutes[$ordering] = ['name' => $name, 'route' => $route]; $collection->remove($name); } if (preg_match('/' . static::ROUTING_PREFIX . 'unifik_|_' . implode('_|_', $appSlugs) . '_/', $name)) { $collection->remove($name); } } return $collection; }
public function testRemove() { $collection = new RouteCollection(); $collection->add('foo', $foo = new Route('/foo')); $collection1 = new RouteCollection(); $collection1->add('bar', $bar = new Route('/bar')); $collection->addCollection($collection1); $collection->add('last', $last = new Route('/last')); $collection->remove('foo'); $this->assertSame(array('bar' => $bar, 'last' => $last), $collection->all(), '->remove() can remove a single route'); $collection->remove(array('bar', 'last')); $this->assertSame(array(), $collection->all(), '->remove() accepts an array and can remove multiple routes at once'); }
/** * @override * @inheritDoc */ public function removeRoute($path) { $this->routes->remove($path); return $this; }
public function removeRoute($name) { $this->routeCollection->remove($name); }
/** * {@inheritdoc} */ public function alterRoutes(RouteCollection $collection) { $view_route_names = array(); $view_path = $this->getPath(); foreach ($collection->all() as $name => $route) { // Find all paths which match the path of the current display.. $route_path = RouteCompiler::getPathWithoutDefaults($route); $route_path = RouteCompiler::getPatternOutline($route_path); // Ensure that we don't override a route which is already controlled by // views. if (!$route->hasDefault('view_id') && '/' . $view_path == $route_path) { $parameters = $route->compile()->getPathVariables(); // @todo Figure out whether we need to merge some settings (like // requirements). // Replace the existing route with a new one based on views. $original_route = $collection->get($name); $collection->remove($name); $view_id = $this->view->storage->id(); $display_id = $this->display['id']; $route = $this->getRoute($view_id, $display_id); $path = $route->getPath(); // Replace the path with the original parameter names and add a mapping. $argument_map = array(); // We assume that the numeric ids of the parameters match the one from // the view argument handlers. foreach ($parameters as $position => $parameter_name) { $path = str_replace('{arg_' . $position . '}', '{' . $parameter_name . '}', $path); $argument_map['arg_' . $position] = $parameter_name; } // Copy the original options from the route, so for example we ensure // that parameter conversion options is carried over. $route->setOptions($route->getOptions() + $original_route->getOptions()); // Set the corrected path and the mapping to the route object. $route->setOption('_view_argument_map', $argument_map); $route->setPath($path); $collection->add($name, $route); $view_route_names[$view_id . '.' . $display_id] = $name; } } return $view_route_names; }
/** * Adds a name prefix to the route name of all collection routes. * * @param RouteCollection $collection Route collection * @param array $namePrefix NamePrefix to add in each route name of the route collection * * @return RouteCollection */ public function addParentNamePrefix(RouteCollection $collection, $namePrefix) { if (!isset($namePrefix) || ($namePrefix = trim($namePrefix)) === "") { return $collection; } $iterator = $collection->getIterator(); foreach ($iterator as $key1 => $route1) { $collection->add($namePrefix . $key1, $route1); $collection->remove($key1); } return $collection; }
/** * {@inheritdoc} * * Invalid page manager routes will be removed. Routes not controlled by page * manager will be moved to the end of the collection. Once a valid page * manager route has been found, all other page manager routes will also be * removed. */ public function filter(RouteCollection $collection, Request $request) { // Only proceed if the collection is non-empty. if (!$collection->count()) { return $collection; } // Store the unaltered request attributes. $original_attributes = $request->attributes->all(); // First get all routes and sort them by variant weight. Note that routes // without a weight will have an undefined order, they are ignored here. $routes = $collection->all(); uasort($routes, [$this, 'routeWeightSort']); // Find the first route that is accessible. $accessible_route_name = NULL; foreach ($routes as $name => $route) { $attributes = $this->getRequestAttributes($route, $name, $request); // Add the enhanced attributes to the request. $request->attributes->add($attributes); if ($page_variant_id = $route->getDefault('page_manager_page_variant')) { if ($this->checkPageVariantAccess($page_variant_id)) { // Access granted, use this route. Do not restore request attributes // but keep those from this route by breaking out. $accessible_route_name = $name; break; } } // Restore the original request attributes, this must be done in the loop // or the request attributes will not be calculated correctly for the // next route. $request->attributes->replace($original_attributes); } // Because the sort order of $routes is unreliable for a route without a // variant weight, rely on the original order of $collection here. foreach ($collection as $name => $route) { if ($route->getDefault('page_manager_page_variant')) { if ($accessible_route_name !== $name) { // Remove all other page manager routes. $collection->remove($name); } } else { // This is not page manager route, move it to the end of the collection, // those will only be used if there is no accessible variant route. $collection->add($name, $route); } } return $collection; }