Example #1
0
 protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
 {
     if ($annot instanceof BrickRoute) {
         $route->setDefault('_controller', $annot->getService() . ':' . $method->getName());
     } else {
         $route->setDefault('_controller', $class->getName() . '::' . $method->getName());
     }
 }
 protected function revisionDeleteRoute(EntityTypeInterface $entity_type)
 {
     $route = new Route($entity_type->getLinkTemplate('revision-delete'));
     $route->setDefault('_form', 'Drupal\\content_entity_base\\Entity\\Form\\EntityRevisionDeleteForm');
     $route->setDefault('_title', 'Delete earlier revision');
     $route->setRequirement('_entity_access_revision', $entity_type->id() . '.delete');
     $route->setOption('parameters', [$entity_type->id() => ['type' => 'entity:' . $entity_type->id()], $entity_type->id() . '_revision' => ['type' => 'entity_revision:' . $entity_type->id()]]);
     return $route;
 }
 protected function addFormRoute(EntityTypeInterface $entity_type)
 {
     $route = new Route('entity.' . $entity_type->id() . '.add-form');
     $route->setDefault('_controller', '\\Drupal\\content_entity_base\\Entity\\Controller\\EntityBaseController::addForm');
     $route->setDefault('_title_callback', '\\Drupal\\content_entity_base\\Entity\\Controller\\EntityBaseController::getAddFormTitle');
     $route->setDefault('entity_type', $entity_type->id());
     $route->setRequirement('_entity_create_access', $entity_type->id());
     return $route;
 }
 /**
  * Returns the delete multiple form route.
  *
  * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  *   The entity type.
  *
  * @return \Symfony\Component\Routing\Route|null
  *   The generated route, if available.
  */
 protected function deleteMultipleFormRoute(EntityTypeInterface $entity_type)
 {
     if ($entity_type->hasLinkTemplate('delete-multiple-form')) {
         $route = new Route($entity_type->getLinkTemplate('delete-multiple-form'));
         $route->setDefault('_form', '\\Drupal\\entity\\Form\\DeleteMultiple');
         $route->setDefault('entity_type_id', $entity_type->id());
         $route->setRequirement('_permission', $entity_type->getAdminPermission());
         return $route;
     }
 }
 /**
  * Returns the add form route.
  *
  * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  *   The entity type.
  *
  * @return \Symfony\Component\Routing\Route|null
  *   The generated route, if available.
  */
 protected function addFormRoute(EntityTypeInterface $entity_type)
 {
     if ($entity_type->hasLinkTemplate('add-form')) {
         $route = new Route($entity_type->getLinkTemplate('add-form'));
         $route->setDefault('_controller', '\\Drupal\\entity\\Controller\\EntityCreateController::addForm');
         $route->setDefault('_title_callback', '\\Drupal\\entity\\Controller\\EntityCreateController::addFormTitle');
         $route->setDefault('entity_type_id', $entity_type->id());
         $route->setRequirement('_entity_create_access', $entity_type->id());
         return $route;
     }
 }
 /**
  * Configures the _controller default parameter and eventually the _method 
  * requirement of a given Route instance.
  *
  * @param Route $route A Route instance
  * @param ReflectionClass $class A ReflectionClass instance
  * @param ReflectionMethod $method A ReflectionClass method
  */
 protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
 {
     // controller
     if ($service = $annot->getService()) {
         $route->setDefault('_controller', $service . ':' . $method->getName());
     } else {
         $route->setDefault('_controller', $class->getName() . '::' . $method->getName());
     }
     // requirements (@extra:Method)
     foreach ($this->configReader->getMethodAnnotations($method) as $configuration) {
         if ($configuration instanceof Method) {
             $route->setRequirement('_method', implode('|', $configuration->getMethods()));
         }
     }
 }
 /**
  * Given an array of RAML\Resources, this function will add each Resource
  * into the Symfony Route Collection, and set the corresponding method.
  *
  * @param BasicRoute[Resource] $resources
  *  Associative array where the key is the method and full path, and the value contains
  *  the path, method type (GET/POST etc.) and then the Raml\Method object
  *
  * @return array
  */
 public function format(array $resources)
 {
     // Loop over the Resources
     foreach ($resources as $path => $resource) {
         // This is the path from the RAML, with or without a /.
         $path = $resource->getUri() . ($this->addTrailingSlash ? '/' : '');
         // This is the baseUri + path, the complete URL.
         $url = $resource->getBaseUrl() . $path;
         // Now remove the host away, so we have the FULL path to the resource.
         // baseUri may also contain path that has been omitted for brevity in the
         // RAML creation.
         $host = parse_url($url, PHP_URL_HOST);
         $fullPath = substr($url, strpos($url, $host) + strlen($host));
         // Now build our Route class.
         $route = new Route($fullPath);
         $route->setMethods($resource->getMethod()->getType());
         $route->setSchemes($resource->getProtocols());
         // Loop over each of the URI Parameters searching for validation patterns
         // or default values for parameters.
         foreach ($resource->getUriParameters() as $name => $param) {
             $route->setRequirement($name, $param->getValidationPattern());
             if ($default = $param->getDefault()) {
                 $route->setDefault($name, $default);
             }
         }
         $this->routes->add($resource->getType() . ' ' . $path, $route);
     }
     return $resources;
 }
 /**
  * @param Method $method
  * @param string $endpoint
  *
  * @return RpcApiDoc
  */
 protected function processMethod(Method $method, $endpoint)
 {
     /** @var string[] $views */
     $views = $method->getContext();
     if ($method->includeDefaultContext()) {
         $views[] = 'Default';
     }
     $views[] = 'default';
     $request = new Request($method, [], new ParameterBag(['_controller' => $method->getController()]));
     /** @var array $controller */
     $controller = $this->resolver->getController($request);
     $refl = new \ReflectionMethod($controller[0], $controller[1]);
     /** @var RpcApiDoc $methodDoc */
     $methodDoc = $this->reader->getMethodAnnotation($refl, RpcApiDoc::class);
     if (null === $methodDoc) {
         $methodDoc = new RpcApiDoc(['resource' => $endpoint]);
     }
     $methodDoc = clone $methodDoc;
     $methodDoc->setEndpoint($endpoint);
     $methodDoc->setRpcMethod($method);
     if (null === $methodDoc->getSection()) {
         $methodDoc->setSection($endpoint);
     }
     foreach ($views as $view) {
         $methodDoc->addView($view);
     }
     $route = new Route($endpoint);
     $route->setMethods([$endpoint]);
     $route->setDefault('_controller', get_class($controller[0]) . '::' . $controller[1]);
     $methodDoc->setRoute($route);
     return $methodDoc;
 }
 /**
  * Adds a route and returns it for future modification.
  *
  * @param string      $path       The route path
  * @param string      $controller The route's controller
  * @param string|null $name       The name to give this route
  *
  * @return Route
  */
 public function add($path, $controller, $name = null)
 {
     $route = new Route($path);
     $route->setDefault('_controller', $controller);
     $this->addRoute($route, $name);
     return $route;
 }
 protected function configureRoute(\Symfony\Component\Routing\Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
 {
     // defines the controller
     $route->setDefault('_controller', $class->getName() . '::' . $method->getName());
     // verify the other callbacks
     $options = $annot->getOptions();
     foreach ($options as $prop => &$values) {
         if (!in_array($prop, array('_after_middlewares', '_before_middlewares', '_converters'))) {
             continue;
         }
         if (empty($values)) {
             continue;
         }
         foreach ($values as &$value) {
             if (is_string($value) && $class->hasMethod($value)) {
                 // call static method from class
                 $value = array($class->getName(), $value);
             }
         }
         unset($value);
         // clear reference
     }
     unset($values);
     $route->setOptions($options);
 }
 /**
  * @param ApiResource $apiResource
  * @param RouteCollection $routes
  * @param ApiResource $parentResource
  */
 private function addRoute(ApiResource $apiResource, RouteCollection $routes, $parentResource = null)
 {
     foreach ($apiResource->getActions() as $action) {
         if (!$parentResource && !$apiResource->isMainResource()) {
             continue;
         }
         $route = new Route($this->getUrl($action, $parentResource));
         $route->setDefault('_api_resource', $apiResource->getName())->setDefault('_controller', $action->getControllerAction())->setDefault('_entity', $apiResource->getEntityClass())->setDefault('_security', $this->expressionsToSecurity($action->getSecurityExpression()))->setDefault('_identifier', $apiResource->getIdentifier())->setMethods($action->getMethods());
         if ($action instanceof Index) {
             $route->setDefault('_indexGetterMethod', $action->getResourceGetterMethod());
             $route->setDefault('_limit', $action->getDefaultLimit());
         }
         $routes->add($action->getRouteName($parentResource), $route);
     }
     foreach ($apiResource->getSubResources() as $subResource) {
         $this->addRoute($subResource, $routes, $apiResource);
     }
 }
 /**
  * Configures the _controller default parameter and eventually the HTTP method
  * requirement of a given Route instance.
  *
  * @param Route             $route  A route instance
  * @param \ReflectionClass  $class  A ReflectionClass instance
  * @param \ReflectionMethod $method A ReflectionClass method
  * @param mixed             $annot  The annotation class instance
  *
  * @throws \LogicException When the service option is specified on a method
  */
 protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
 {
     // controller
     $classAnnot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass);
     if ($classAnnot instanceof FrameworkExtraBundleRoute && ($service = $classAnnot->getService())) {
         $route->setDefault('_controller', $service . ':' . $method->getName());
     } else {
         $route->setDefault('_controller', $class->getName() . '::' . $method->getName());
     }
     // requirements (@Method)
     foreach ($this->reader->getMethodAnnotations($method) as $configuration) {
         if ($configuration instanceof Method) {
             $route->setMethods(implode('|', $configuration->getMethods()));
         } elseif ($configuration instanceof FrameworkExtraBundleRoute && $configuration->getService()) {
             throw new \LogicException('The service option can only be specified at class level.');
         }
     }
 }
 private function convertController(Route $route)
 {
     $nameParser = $this->getContainer()->get('controller_name_converter');
     if ($route->hasDefault('_controller')) {
         try {
             $route->setDefault('_controller', $nameParser->build($route->getDefault('_controller')));
         } catch (\InvalidArgumentException $e) {
         }
     }
 }
Example #14
0
 /**
  * @covers ::buildBasicRenderable
  */
 public function testBuildBasicRenderable()
 {
     $route = new Route('/test-view');
     $route->setDefault('view_id', 'test_view');
     $route->setOption('_view_display_plugin_id', 'page');
     $route->setOption('_view_display_show_admin_links', TRUE);
     $result = Page::buildBasicRenderable('test_view', 'page_1', [], $route);
     $this->assertEquals('test_view', $result['#view_id']);
     $this->assertEquals('page', $result['#view_display_plugin_id']);
     $this->assertEquals(TRUE, $result['#view_display_show_admin_links']);
 }
 private function processRoute(Route $importedRoute, $serviceName, $controllerClass)
 {
     $controllerName = $importedRoute->getDefault('_controller');
     if ($this->containsClassNameNotServiceName($controllerName)) {
         // service has already been assigned to this controller -> skip
         return;
     }
     $controllerClassLength = strlen($controllerClass);
     $controllerClassFromName = substr($controllerName, 0, $controllerClassLength);
     if ($controllerClassFromName !== $controllerClass) {
         throw new \InvalidArgumentException('Something is wrong with controller class: ' . $controllerClass);
     }
     $importedRoute->setDefault('_controller', $this->getServiceControllerName($serviceName, $controllerName, $controllerClassLength));
 }
Example #16
0
 public function load($resource, $type = null)
 {
     $collection = new RouteCollection();
     $structureRepository = $this->getEntityManager()->getRepository('OctavaStructureBundle:Structure');
     $tree = $structureRepository->getFlatTree();
     foreach ($tree as $item) {
         if ($item->getType() == 'page' && $structureRepository->getCombinedState($item)) {
             $routePattern = $item->getPath();
             if ('/' != substr($routePattern, -1, 1) && false === strpos(basename($routePattern), '.')) {
                 $routePattern .= '/';
             }
             $route = new Route($routePattern);
             $route->setDefault('_controller', 'OctavaStructureBundle:Default:index')->setDefault('id', $item->getId())->setDefault(Structure::ROUTING_ID_NAME, $item->getId())->setDefault('_structure_type', 'page')->setOption('translatable_path', $structureRepository->getTranslatablePath($item));
             $collection->add($item->getRouteName(), $route);
         }
     }
     $collection->addResource(new FileResource(__FILE__));
     return $collection;
 }
Example #17
0
 /**
  * {@inheritdoc}
  */
 protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
 {
     $route->setDefault('_controller', $class->name . '::' . $method->name);
 }
Example #18
0
 /**
  * @param array $options
  *
  * @return Route
  */
 protected function destroyRoute(array $options)
 {
     $route = new Route('/{id}/destroy');
     $route->setMethods(['GET', 'POST']);
     $route->setDefault('_controller', $options['controller'] . ':destroy');
     $route->setRequirement('id', '\\d+');
     return $route;
 }
Example #19
0
 /**
  * Gets the add-form route.
  *
  * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  *   The entity type.
  *
  * @return \Symfony\Component\Routing\Route|null
  *   The generated route, if available.
  */
 protected function getAddFormRoute(EntityTypeInterface $entity_type)
 {
     if ($entity_type->hasLinkTemplate('add-form')) {
         $entity_type_id = $entity_type->id();
         $route = new Route($entity_type->getLinkTemplate('add-form'));
         // Use the add form handler, if available, otherwise default.
         $operation = 'default';
         if ($entity_type->getFormClass('add')) {
             $operation = 'add';
         }
         $route->setDefaults(['_entity_form' => "{$entity_type_id}.{$operation}", 'entity_type_id' => $entity_type_id]);
         // If the entity has bundles, we can provide a bundle-specific title
         // and access requirements.
         $expected_parameter = $entity_type->getBundleEntityType() ?: $entity_type->getKey('bundle');
         // @todo: We have to check if a route contains a bundle in its path as
         //   test entities have inconsistent usage of "add-form" link templates.
         //   Fix it in https://www.drupal.org/node/2699959.
         if (($bundle_key = $entity_type->getKey('bundle')) && strpos($route->getPath(), '{' . $expected_parameter . '}') !== FALSE) {
             $route->setDefault('_title_callback', EntityController::class . '::addBundleTitle');
             // If the bundles are entities themselves, we can add parameter
             // information to the route options.
             if ($bundle_entity_type_id = $entity_type->getBundleEntityType()) {
                 $bundle_entity_type = $this->entityTypeManager->getDefinition($bundle_entity_type_id);
                 $route->setDefault('bundle_parameter', $bundle_entity_type_id)->setRequirement('_entity_create_access', $entity_type_id . ':{' . $bundle_entity_type_id . '}');
                 // Entity types with serial IDs can specify this in their route
                 // requirements, improving the matching process.
                 if ($this->getEntityTypeIdKeyType($bundle_entity_type) === 'integer') {
                     $route->setRequirement($entity_type_id, '\\d+');
                 }
                 $bundle_entity_parameter = ['type' => 'entity:' . $bundle_entity_type_id];
                 if ($bundle_entity_type instanceof ConfigEntityTypeInterface) {
                     // The add page might be displayed on an admin path. Even then, we
                     // need to load configuration overrides so that, for example, the
                     // bundle label gets translated correctly.
                     // @see \Drupal\Core\ParamConverter\AdminPathConfigEntityConverter
                     $bundle_entity_parameter['with_config_overrides'] = TRUE;
                 }
                 $route->setOption('parameters', [$bundle_entity_type_id => $bundle_entity_parameter]);
             } else {
                 // If the bundles are not entities, the bundle key is used as the
                 // route parameter name directly.
                 $route->setDefault('bundle_parameter', $bundle_key)->setRequirement('_entity_create_access', $entity_type_id . ':{' . $bundle_key . '}');
             }
         } else {
             $route->setDefault('_title_callback', EntityController::class . '::addTitle')->setRequirement('_entity_create_access', $entity_type_id);
         }
         return $route;
     }
 }
 /**
  * Maps a path to a callable.
  *
  * @param  string $path
  * @param  string $name
  * @param  mixed  $callback
  * @return Route
  */
 public function map($path, $name, $callback)
 {
     $route = new Route($path);
     $route->setDefault('_controller', '::' . $name);
     $this->routes->add($name, $route);
     $this->callbacks[$name] = $callback;
     return $route;
 }
 /**
  * Apply the parameter map to a Drupal 8 route, modifying it as needed.
  *
  * @param \Symfony\Component\Routing\Route $route
  *  The route to process.
  */
 public function applyRoute(Drupal8Route $route)
 {
     $this->applyPath($this->path);
     foreach ($this as $key => $binding) {
         $parameter = $binding->getParameter();
         /** @var ParameterBinding $binding */
         if (is_integer($key)) {
             if ($parameter->isOptional()) {
                 // @todo Don't use eval().
                 $value = eval('return ' . $parameter->getValue() . ';');
                 $route->setDefault($parameter->getName(), $value);
             }
         } elseif ($binding->hasArgument()) {
             $route->setDefault($parameter->getName(), $binding->getValue());
         }
     }
     $route->setPath($this->path->__toString());
 }
 /**
  * @param Route $route
  */
 protected function setFormatAttribute(Route $route)
 {
     $route->setRequirement(self::FORMAT_ATTRIBUTE, $this->formats);
     $route->setDefault(self::FORMAT_ATTRIBUTE, $this->defaultFormat);
 }
 /**
  * Configure the route, should be overridden in subclasses.
  *
  * @param Route            $route
  * @param ReflectionClass  $class
  * @param ReflectionMethod $method
  * @param array            $options
  */
 protected function configureRoute(Route $route, ReflectionClass $class, ReflectionMethod $method, array $options)
 {
     $route->setDefault('_controller', $class->name . '::' . $method->name);
     $this->events->dispatch('route.configure', new ConfigureRouteEvent($route, $class, $method, $options));
 }
Example #24
0
 /**
  * Sets a default value.
  * @return RouteEx
  */
 public function setDefault($name, $default)
 {
     parent::setDefault($name, $default);
     return $this;
 }