From 40bb56c00376ff35c92a686e8c2b6b337913d44e Mon Sep 17 00:00:00 2001 From: lordjancso Date: Fri, 23 Feb 2018 14:35:51 +0100 Subject: [PATCH 1/5] Extend from Symfony base router --- src/Routing/Annotation/I18nRoute.php | 73 +++++----------------------- 1 file changed, 12 insertions(+), 61 deletions(-) diff --git a/src/Routing/Annotation/I18nRoute.php b/src/Routing/Annotation/I18nRoute.php index 689d0d17d..a6f32123e 100644 --- a/src/Routing/Annotation/I18nRoute.php +++ b/src/Routing/Annotation/I18nRoute.php @@ -2,23 +2,20 @@ namespace BeSimple\I18nRoutingBundle\Routing\Annotation; +use Symfony\Component\Routing\Annotation\Route as BaseRoute; + /** * Annotation class for @I18nRoute(). * * @Annotation * @Target({"CLASS", "METHOD"}) */ -class I18nRoute +class I18nRoute extends BaseRoute { - private $locales; - private $name; - private $requirements = array(); - private $options = array(); - private $defaults = array(); - private $host; - private $methods = array(); - private $schemes = array(); - private $condition; + protected $locales; + protected $requirements = array(); + protected $methods = array(); + protected $schemes = array(); /** * Constructor. @@ -34,13 +31,7 @@ public function __construct(array $data) unset($data['value']); } - foreach ($data as $key => $value) { - $method = 'set'.str_replace('_', '', $key); - if (!method_exists($this, $method)) { - throw new \BadMethodCallException(sprintf('Unknown property "%s" on annotation "%s".', $key, get_class($this))); - } - $this->$method($value); - } + parent::__construct($data); } public function setLocales($locales) @@ -53,24 +44,14 @@ public function getLocales() return $this->locales; } - public function setHost($pattern) - { - $this->host = $pattern; - } - - public function getHost() - { - return $this->host; - } - - public function setName($name) + public function setPath($path) { - $this->name = $name; + throw new \Exception('Bad method call.'); } - public function getName() + public function getPath() { - return $this->name; + throw new \Exception('Bad method call.'); } public function setRequirements($requirements) @@ -95,26 +76,6 @@ public function getRequirements() return $this->requirements; } - public function setOptions($options) - { - $this->options = $options; - } - - public function getOptions() - { - return $this->options; - } - - public function setDefaults($defaults) - { - $this->defaults = $defaults; - } - - public function getDefaults() - { - return $this->defaults; - } - public function setSchemes($schemes) { $this->schemes = is_array($schemes) ? $schemes : array($schemes); @@ -134,14 +95,4 @@ public function getMethods() { return $this->methods; } - - public function setCondition($condition) - { - $this->condition = $condition; - } - - public function getCondition() - { - return $this->condition; - } } From aded5ff86d68cf2e91b613a563b3628461a3920e Mon Sep 17 00:00:00 2001 From: lordjancso Date: Fri, 23 Feb 2018 14:36:11 +0100 Subject: [PATCH 2/5] Read all annontation routes --- src/Routing/Loader/AnnotatedRouteControllerLoader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Routing/Loader/AnnotatedRouteControllerLoader.php b/src/Routing/Loader/AnnotatedRouteControllerLoader.php index 8aa940b3a..a37c10311 100644 --- a/src/Routing/Loader/AnnotatedRouteControllerLoader.php +++ b/src/Routing/Loader/AnnotatedRouteControllerLoader.php @@ -26,7 +26,7 @@ public function __construct(Reader $reader, RouteGeneratorInterface $routeGenera parent::__construct($reader); $this->routeGenerator = $routeGenerator ?: new I18nRouteGenerator(); - $this->setRouteAnnotationClass('BeSimple\\I18nRoutingBundle\\Routing\\Annotation\\I18nRoute'); + $this->setRouteAnnotationClass('Symfony\\Component\\Routing\\Annotation\\Route'); } protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method) From 0e95173464e53289bda390c8a2059dfebbc312bb Mon Sep 17 00:00:00 2001 From: lordjancso Date: Fri, 23 Feb 2018 14:57:54 +0100 Subject: [PATCH 3/5] Allow path methods --- src/Routing/Annotation/I18nRoute.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Routing/Annotation/I18nRoute.php b/src/Routing/Annotation/I18nRoute.php index a6f32123e..730a18458 100644 --- a/src/Routing/Annotation/I18nRoute.php +++ b/src/Routing/Annotation/I18nRoute.php @@ -44,16 +44,6 @@ public function getLocales() return $this->locales; } - public function setPath($path) - { - throw new \Exception('Bad method call.'); - } - - public function getPath() - { - throw new \Exception('Bad method call.'); - } - public function setRequirements($requirements) { if (isset($requirements['_method'])) { From 50cf28a71dc77725bf42257a4d2113d1a351e2a6 Mon Sep 17 00:00:00 2001 From: lordjancso Date: Fri, 23 Feb 2018 14:58:15 +0100 Subject: [PATCH 4/5] Handle symfony routes --- .../Loader/AnnotatedRouteControllerLoader.php | 47 ++++--------------- 1 file changed, 8 insertions(+), 39 deletions(-) diff --git a/src/Routing/Loader/AnnotatedRouteControllerLoader.php b/src/Routing/Loader/AnnotatedRouteControllerLoader.php index a37c10311..56850735a 100644 --- a/src/Routing/Loader/AnnotatedRouteControllerLoader.php +++ b/src/Routing/Loader/AnnotatedRouteControllerLoader.php @@ -31,7 +31,12 @@ public function __construct(Reader $reader, RouteGeneratorInterface $routeGenera protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method) { - /** @var \BeSimple\I18nRoutingBundle\Routing\Annotation\I18nRoute $annot */ + if (!$annot instanceof I18nRoute) { + parent::addRoute($collection, $annot, $globals, $class, $method); + + return; + } + $name = $annot->getName(); if (null === $name) { $name = $this->getDefaultRouteName($class, $method); @@ -115,51 +120,15 @@ protected function getGlobals(\ReflectionClass $class) { $globals = array( 'locales' => '', - 'requirements' => array(), - 'options' => array(), - 'defaults' => array(), - 'schemes' => array(), - 'methods' => array(), - 'host' => '', - 'condition' => '', ); - /** @var \BeSimple\I18nRoutingBundle\Routing\Annotation\I18nRoute $annot */ if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) { - if (null !== $annot->getLocales()) { + if ($annot instanceof I18nRoute && null !== $annot->getLocales()) { $globals['locales'] = $annot->getLocales(); } - - if (null !== $annot->getRequirements()) { - $globals['requirements'] = $annot->getRequirements(); - } - - if (null !== $annot->getOptions()) { - $globals['options'] = $annot->getOptions(); - } - - if (null !== $annot->getDefaults()) { - $globals['defaults'] = $annot->getDefaults(); - } - - if (null !== $annot->getSchemes()) { - $globals['schemes'] = $annot->getSchemes(); - } - - if (null !== $annot->getMethods()) { - $globals['methods'] = $annot->getMethods(); - } - - if (null !== $annot->getHost()) { - $globals['host'] = $annot->getHost(); - } - - if (null !== $annot->getCondition()) { - $globals['condition'] = $annot->getCondition(); - } } - return $globals; + return array_merge(parent::getGlobals($class), $globals); } /** From 69a137bc295e5e919c664bf3d75de98556287807 Mon Sep 17 00:00:00 2001 From: lordjancso Date: Fri, 23 Feb 2018 15:04:15 +0100 Subject: [PATCH 5/5] Missing use class added --- src/Routing/Loader/AnnotatedRouteControllerLoader.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Routing/Loader/AnnotatedRouteControllerLoader.php b/src/Routing/Loader/AnnotatedRouteControllerLoader.php index 56850735a..80a7fc1ce 100644 --- a/src/Routing/Loader/AnnotatedRouteControllerLoader.php +++ b/src/Routing/Loader/AnnotatedRouteControllerLoader.php @@ -2,6 +2,7 @@ namespace BeSimple\I18nRoutingBundle\Routing\Loader; +use BeSimple\I18nRoutingBundle\Routing\Annotation\I18nRoute; use BeSimple\I18nRoutingBundle\Routing\Exception\MissingLocaleException; use BeSimple\I18nRoutingBundle\Routing\Exception\MissingRouteLocaleException; use BeSimple\I18nRoutingBundle\Routing\RouteGenerator\I18nRouteGenerator;