123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace Symfony\Component\Routing\Loader;
- use Symfony\Component\Routing\RouteCollection;
- use Symfony\Component\Routing\Route;
- use Doctrine\Common\Annotations\AnnotationReader;
- use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
- use Symfony\Component\Routing\Loader\LoaderResolver;
- /*
- * This file is part of the Symfony framework.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- /**
- * AnnotationClassLoader loads routing information from a PHP class and its methods.
- *
- * You need to define an implementation for the getRouteDefaults() method. Most of the
- * time, this method should define some PHP callable to be called for the route
- * (a controller in MVC speak).
- *
- * The @Route annotation can be set on the class (for global parameters),
- * and on each method.
- *
- * The @Route annotation main value is the route pattern. The annotation also
- * recognizes three parameters: requirements, options, and name. The name parameter
- * is mandatory. Here is an example of how you should be able to use it:
- *
- * /**
- * * @Route("/Blog")
- * * /
- * class Blog
- * {
- * /**
- * * @Route("/", name="blog_index")
- * * /
- * public function index()
- * {
- * }
- *
- * /**
- * * @Route("/:id", name="blog_post", requirements = {"id" = "\d+"})
- * * /
- * public function show()
- * {
- * }
- * }
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
- abstract class AnnotationClassLoader implements LoaderInterface
- {
- protected $reader;
- /**
- * Constructor.
- */
- public function __construct(AnnotationReader $reader)
- {
- $this->reader = $reader;
- }
- /**
- * Loads from annotations from a class.
- *
- * @param string $class A class name
- *
- * @return RouteCollection A RouteCollection instance
- *
- * @throws \InvalidArgumentException When route can't be parsed
- */
- public function load($class)
- {
- if (!class_exists($class)) {
- throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
- }
- $class = new \ReflectionClass($class);
- $annotClass = 'Symfony\\Component\\Routing\\Annotation\\Route';
- $globals = array(
- 'pattern' => '',
- 'requirements' => array(),
- 'options' => array(),
- 'defaults' => array(),
- );
- if ($annot = $this->reader->getClassAnnotation($class, $annotClass)) {
- if (null !== $annot->getPattern()) {
- $globals['pattern'] = $annot->getPattern();
- }
- if (null !== $annot->getRequirements()) {
- $globals['requirements'] = $annot->getRequirements();
- }
- if (null !== $annot->getOptions()) {
- $globals['options'] = $annot->getOptions();
- }
- if (null !== $annot->getDefaults()) {
- $globals['defaults'] = $annot->getDefaults();
- }
- }
- $this->reader->setDefaultAnnotationNamespace('Symfony\\Component\\Routing\\Annotation\\');
- $collection = new RouteCollection();
- foreach ($class->getMethods() as $method) {
- if ($annot = $this->reader->getMethodAnnotation($method, $annotClass)) {
- if (null === $annot->getName()) {
- $annot->setName($this->getDefaultRouteName($class, $method));
- }
- $defaults = array_merge($globals['defaults'], $annot->getDefaults());
- $requirements = array_merge($globals['requirements'], $annot->getRequirements());
- $options = array_merge($globals['options'], $annot->getOptions());
- $route = new Route($globals['pattern'].$annot->getPattern(), $defaults, $requirements, $options);
- $this->configureRoute($route, $class, $method);
- $collection->add($annot->getName(), $route);
- }
- }
- return $collection;
- }
- protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
- {
- return strtolower(str_replace('\\', '_', $class->getName()).'_'.$method->getName());
- }
- /**
- * Returns true if this class supports the given resource.
- *
- * @param mixed $resource A resource
- *
- * @return Boolean true if this class supports the given resource, false otherwise
- */
- public function supports($resource)
- {
- return is_string($resource) && class_exists($resource);
- }
- /**
- * Sets the loader resolver.
- *
- * @param LoaderResolver $resolver A LoaderResolver instance
- */
- public function setResolver(LoaderResolver $resolver)
- {
- }
- /**
- * Gets the loader resolver.
- *
- * @return LoaderResolver A LoaderResolver instance
- */
- public function getResolver()
- {
- }
- abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method);
- }
|