/** * Gets all routes for controller. * * @return array */ public function getRelatedRoutes() { $annotatedControllerRouteLoader = new AnnotatedRouteControllerLoader(new AnnotationReader()); $routeCollection = $annotatedControllerRouteLoader->load(get_class($this)); $routes = []; foreach ($routeCollection->all() as $routeName => $routeDetails) { $explodedController = explode(':', $routeDetails->getDefaults()['_controller']); $routes[str_replace('Action', '', $explodedController[count($explodedController) - 1])] = $routeName; } return $routes; }
public function testLoad() { $loader = new AnnotatedRouteControllerLoader(new AnnotationReader()); AnnotationRegistry::registerLoader('class_exists'); $rc = $loader->load('Sensio\\Bundle\\FrameworkExtraBundle\\Tests\\Routing\\Fixtures\\FoobarController'); $this->assertInstanceOf('Symfony\\Component\\Routing\\RouteCollection', $rc); $this->assertCount(2, $rc); $this->assertInstanceOf('Symfony\\Component\\Routing\\Route', $rc->get('index')); $this->assertEquals(array('GET'), $rc->get('index')->getMethods()); $this->assertInstanceOf('Symfony\\Component\\Routing\\Route', $rc->get('new')); $this->assertEquals(array('POST'), $rc->get('new')->getMethods()); }
public function testLoad() { $loader = new AnnotatedRouteControllerLoader(new AnnotationReader()); AnnotationRegistry::registerLoader('class_exists'); $rc = $loader->load('Sensio\\Bundle\\FrameworkExtraBundle\\Tests\\Routing\\Fixtures\\FoobarController'); $this->assertInstanceOf('Symfony\\Component\\Routing\\RouteCollection', $rc); $this->assertCount(2, $rc); $this->assertInstanceOf('Symfony\\Component\\Routing\\Route', $rc->get('index')); // depending on the Symfony version, it can return GET or an empty array (on 2.3) // which has the same behavior anyway $methods = $rc->get('index')->getMethods(); $this->assertTrue(empty($methods) || array('GET') == $methods); $this->assertInstanceOf('Symfony\\Component\\Routing\\Route', $rc->get('new')); $this->assertEquals(array('POST'), $rc->get('new')->getMethods()); }
/** * For all route annotations using FOM\ManagerBundle\Configuration\Route, this adds the configured prefix. * * @inheritdoc */ protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot) { parent::configureRoute($route, $class, $method, $annot); if (is_a($annot, 'FOM\\ManagerBundle\\Configuration\\Route')) { $route->setPattern($this->prefix . $route->getPattern()); } }
/** * Configures the CSRF token options * * @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) { parent::configureRoute($route, $class, $method, $annot); /** @var \Fantoine\CsrfRouteBundle\Annotation\CsrfToken */ $annotation = $this->reader->getMethodAnnotation($method, '\\Fantoine\\CsrfRouteBundle\\Annotation\\CsrfToken'); if (null !== $annotation) { // Store the CsrfToken options on Route options $route->setOption(CsrfTokenManager::OPTION_NAME, $annotation->toOption()); } }
/** * {@inheritdoc} */ protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method) { // If the annotation is not the Multisite's one, call parent method if (!$annot instanceof RouteAnnotation) { return parent::addRoute($collection, $annot, $globals, $class, $method); } // mono-route if (null === $annot->getPaths()) { return parent::addRoute($collection, $annot, $globals, $class, $method); } return $this->addMultisiteRoute($collection, $annot, $globals, $class, $method); }
/** * 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) { parent::configureRoute($route, $class, $method, $annot); // Symfony 2.1.x if (!method_exists($route, 'getPath')) { $pattern = $route->getPattern(); if (!empty($pattern) && '/' === $pattern[0] && (isset($pattern[1]) && '.' === $pattern[1] || isset($pattern[1]))) { $r = new \ReflectionProperty('Symfony\\Component\\Routing\\Route', 'pattern'); $r->setAccessible(true); $r->setValue($route, substr($pattern, 1)); } } else { $path = $route->getPath(); if (!empty($path) && '/' === $path[0] && (isset($path[1]) && '.' === $path[1] || !isset($path[1]))) { $r = new \ReflectionProperty('Symfony\\Component\\Routing\\Route', 'path'); $r->setAccessible(true); $r->setValue($route, substr($path, 1)); } } }
/** * @param RouteCollection $collection * @param $annot * @param $globals * @param \ReflectionClass $class * @param \ReflectionMethod $method */ protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method) { $i18n = isset($annot->data['i18n']) ? $annot->data['i18n'] : true; unset($annot->data['i18n']); foreach ($this->registry->getRegisteredLocales() as $locale) { $i18nAnnot = new Route($annot->data); $i18nGlobals = $globals; if ($i18n) { $i18nAnnot->setName($this->helper->alterName($i18nAnnot->getName(), $locale)); $i18nAnnot->setPath($this->helper->alterPath($i18nAnnot->getPath(), $locale)); $i18nAnnot->setDefaults($this->helper->alterDefaults($i18nAnnot->getDefaults(), $locale)); if (isset($i18nGlobals['path']) && !empty($i18nGlobals['path'])) { $i18nGlobals['path'] = '/' . $locale . '/' . ltrim($this->helper->alterPath($i18nGlobals['path'], $locale), '/'); } else { $i18nGlobals['path'] = '/' . $locale; } } parent::addRoute($collection, $i18nAnnot, $i18nGlobals, $class, $method); } }