From d53f1289c3479f95b34ec563861929099b0ec6c1 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 13 Aug 2013 12:09:10 +0200 Subject: [PATCH] allow to declare new router in modules --- core/lib/Thelia/Config/Resources/routing.xml | 25 +-- .../Thelia/Config/Resources/routing/front.xml | 5 + core/lib/Thelia/Controller/BaseController.php | 2 +- .../Controller/Front/CartController.php | 6 +- .../{ => Front}/DefaultController.php | 4 +- core/lib/Thelia/Core/Bundle/TheliaBundle.php | 9 +- .../Compiler/RegisterRouterPass.php} | 53 +++--- core/lib/Thelia/Routing/NullUrlGenerator.php | 95 ---------- .../Routing/TheliaMatcherCollection.php | 173 ------------------ 9 files changed, 47 insertions(+), 325 deletions(-) rename core/lib/Thelia/Controller/{ => Front}/DefaultController.php (96%) rename core/lib/Thelia/{Routing/Matcher/DefaultMatcher.php => Core/DependencyInjection/Compiler/RegisterRouterPass.php} (63%) mode change 100755 => 100644 delete mode 100755 core/lib/Thelia/Routing/NullUrlGenerator.php delete mode 100755 core/lib/Thelia/Routing/TheliaMatcherCollection.php diff --git a/core/lib/Thelia/Config/Resources/routing.xml b/core/lib/Thelia/Config/Resources/routing.xml index 8d1b0b7e1..9bb49451b 100755 --- a/core/lib/Thelia/Config/Resources/routing.xml +++ b/core/lib/Thelia/Config/Resources/routing.xml @@ -24,19 +24,9 @@ - - - - - - - - - - %thelia.core_dir%/Config/Resources/routing @@ -54,6 +44,7 @@ %kernel.debug% + @@ -64,25 +55,13 @@ %kernel.debug% + - - - -255 - - - - - 0 - - - - 255 - diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index 116517912..593863bb6 100644 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -4,6 +4,11 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> + + Thelia\Controller\Front\DefaultController::noAction + index + + Thelia\Controller\Front\CustomerController::createAction connexion diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index adeb3ef3b..7cea0e861 100755 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -55,7 +55,7 @@ class BaseController extends ContainerAware /** * Create an action event - * + * * @param string $action * @return EventDispatcher */ diff --git a/core/lib/Thelia/Controller/Front/CartController.php b/core/lib/Thelia/Controller/Front/CartController.php index e31e52d59..760dade39 100644 --- a/core/lib/Thelia/Controller/Front/CartController.php +++ b/core/lib/Thelia/Controller/Front/CartController.php @@ -35,7 +35,7 @@ class CartController extends BaseFrontController $this->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent); - $this->redirectInternal(); + $this->redirectSuccess(); } public function modifyArticle() @@ -44,7 +44,7 @@ class CartController extends BaseFrontController $this->dispatch(TheliaEvents::CART_CHANGEITEM, $cartEvent); - $this->redirectInternal(); + $this->redirectSuccess(); } public function deleteArticle() @@ -53,7 +53,7 @@ class CartController extends BaseFrontController $this->dispatch(TheliaEvents::CART_DELETEITEM, $cartEvent); - $this->redirectInternal(); + $this->redirectSuccess(); } protected function getCartEvent() diff --git a/core/lib/Thelia/Controller/DefaultController.php b/core/lib/Thelia/Controller/Front/DefaultController.php similarity index 96% rename from core/lib/Thelia/Controller/DefaultController.php rename to core/lib/Thelia/Controller/Front/DefaultController.php index e786fd0c9..dcdd24671 100755 --- a/core/lib/Thelia/Controller/DefaultController.php +++ b/core/lib/Thelia/Controller/Front/DefaultController.php @@ -20,7 +20,7 @@ /* along with this program. If not, see . */ /* */ /*************************************************************************************/ -namespace Thelia\Controller; +namespace Thelia\Controller\Front; use Thelia\Controller\NullControllerInterface; use Symfony\Component\HttpFoundation\Request; @@ -32,7 +32,7 @@ use Symfony\Component\HttpFoundation\Request; * @author Manuel Raynaud */ -class DefaultController implements NullControllerInterface +class DefaultController extends BaseFrontController { /** * diff --git a/core/lib/Thelia/Core/Bundle/TheliaBundle.php b/core/lib/Thelia/Core/Bundle/TheliaBundle.php index 9fb4ec37e..9c8b8fedf 100755 --- a/core/lib/Thelia/Core/Bundle/TheliaBundle.php +++ b/core/lib/Thelia/Core/Bundle/TheliaBundle.php @@ -28,6 +28,7 @@ use Symfony\Component\DependencyInjection\Scope; use Thelia\Core\DependencyInjection\Compiler\RegisterListenersPass; use Thelia\Core\DependencyInjection\Compiler\RegisterParserPluginPass; +use Thelia\Core\DependencyInjection\Compiler\RegisterRouterPass; /** * First Bundle use in Thelia @@ -55,7 +56,11 @@ class TheliaBundle extends Bundle $container->addScope(new Scope('request')); - $container->addCompilerPass(new RegisterListenersPass()); - $container->addCompilerPass(new RegisterParserPluginPass()); + $container + ->addCompilerPass(new RegisterListenersPass()) + ->addCompilerPass(new RegisterParserPluginPass()) + ->addCompilerPass(new RegisterRouterPass()) + ; + } } diff --git a/core/lib/Thelia/Routing/Matcher/DefaultMatcher.php b/core/lib/Thelia/Core/DependencyInjection/Compiler/RegisterRouterPass.php old mode 100755 new mode 100644 similarity index 63% rename from core/lib/Thelia/Routing/Matcher/DefaultMatcher.php rename to core/lib/Thelia/Core/DependencyInjection/Compiler/RegisterRouterPass.php index 6042a7cad..2f7624de5 --- a/core/lib/Thelia/Routing/Matcher/DefaultMatcher.php +++ b/core/lib/Thelia/Core/DependencyInjection/Compiler/RegisterRouterPass.php @@ -4,7 +4,7 @@ /* Thelia */ /* */ /* Copyright (c) OpenStudio */ -/* email : info@thelia.net */ +/* email : info@thelia.net */ /* web : http://www.thelia.net */ /* */ /* This program is free software; you can redistribute it and/or modify */ @@ -20,34 +20,35 @@ /* along with this program. If not, see . */ /* */ /*************************************************************************************/ -namespace Thelia\Routing\Matcher; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\Routing\Matcher\RequestMatcherInterface; -use Thelia\Controller\NullControllerInterface; +namespace Thelia\Core\DependencyInjection\Compiler; -/** - * Default matcher when no action is needed and there is no result for urlmatcher - * - * @author Manuel Raynaud - */ -class DefaultMatcher implements RequestMatcherInterface + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; + +class RegisterRouterPass implements CompilerPassInterface { - protected $controller; - public function __construct(NullControllerInterface $controller) + /** + * You can modify the container here before it is dumped to PHP code. + * + * @param ContainerBuilder $container + * + * @api + */ + public function process(ContainerBuilder $container) { - $this->controller = $controller; + if (!$container->hasDefinition("router.chainRequest")) { + return ; + } + + $chainRouter = $container->getDefinition("router.chainRequest"); + + foreach ($container->findTaggedServiceIds("router.register") as $id => $router) { + $priority = isset($router[0]['priority']) ? $router[0]['priority'] : 0; + $chainRouter->addMethodCall("add", array(new Reference($id), $priority)); + } } - - public function matchRequest(Request $request) - { - $objectInformation = new \ReflectionObject($this->controller); - - $parameter = array( - '_controller' => $objectInformation->getName().'::noAction' - ); - - return $parameter; - } -} +} \ No newline at end of file diff --git a/core/lib/Thelia/Routing/NullUrlGenerator.php b/core/lib/Thelia/Routing/NullUrlGenerator.php deleted file mode 100755 index f43f4ee5a..000000000 --- a/core/lib/Thelia/Routing/NullUrlGenerator.php +++ /dev/null @@ -1,95 +0,0 @@ -. */ -/* */ -/*************************************************************************************/ - -namespace Thelia\Routing; - - - -use Symfony\Component\Routing\Exception\InvalidParameterException; -use Symfony\Component\Routing\Exception\MissingMandatoryParametersException; -use Symfony\Component\Routing\Exception\RouteNotFoundException; -use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -use Symfony\Component\Routing\RequestContext; - -class NullUrlGenerator implements UrlGeneratorInterface -{ - - protected $context; - - /** - * Sets the request context. - * - * @param RequestContext $context The context - * - * @api - */ - public function setContext(RequestContext $context) - { - $this->context = $context; - } - - /** - * Gets the request context. - * - * @return RequestContext The context - * - * @api - */ - public function getContext() - { - return $this->context; - } - - /** - * Generates a URL or path for a specific route based on the given parameters. - * - * Parameters that reference placeholders in the route pattern will substitute them in the - * path or host. Extra params are added as query string to the URL. - * - * When the passed reference type cannot be generated for the route because it requires a different - * host or scheme than the current one, the method will return a more comprehensive reference - * that includes the required params. For example, when you call this method with $referenceType = ABSOLUTE_PATH - * but the route requires the https scheme whereas the current scheme is http, it will instead return an - * ABSOLUTE_URL with the https scheme and the current host. This makes sure the generated URL matches - * the route in any case. - * - * If there is no route with the given name, the generator must throw the RouteNotFoundException. - * - * @param string $name The name of the route - * @param mixed $parameters An array of parameters - * @param Boolean|string $referenceType The type of reference to be generated (one of the constants) - * - * @return string The generated URL - * - * @throws RouteNotFoundException If the named route doesn't exist - * @throws MissingMandatoryParametersException When some parameters are missing that are mandatory for the route - * @throws InvalidParameterException When a parameter value for a placeholder is not correct because - * it does not match the requirement - * - * @api - */ - public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) - { - throw new InvalidParameterException("this generator cannot be used"); - } -} \ No newline at end of file diff --git a/core/lib/Thelia/Routing/TheliaMatcherCollection.php b/core/lib/Thelia/Routing/TheliaMatcherCollection.php deleted file mode 100755 index b6769c148..000000000 --- a/core/lib/Thelia/Routing/TheliaMatcherCollection.php +++ /dev/null @@ -1,173 +0,0 @@ -. */ -/* */ -/*************************************************************************************/ -namespace Thelia\Routing; - -use Symfony\Component\Routing\Matcher\RequestMatcherInterface; -use Symfony\Component\Routing\Exception\ResourceNotFoundException; -use Symfony\Component\Routing\Exception\MethodNotAllowedException; -use Symfony\Component\Routing\RequestContextAwareInterface; -use Symfony\Component\Routing\RequestContext; -use Symfony\Component\HttpFoundation\Request; - -/** - * - * Collection of Matcher. - * - * Matcher resolve request into controller::method - * exemple of Matcher : UrlMatcher of HttpKernel component (but it implements UrlMatcherInterface and not RequestMatcherInterface. - * - * @author Manuel Raynaud - */ - -class TheliaMatcherCollection implements RequestMatcherInterface, RequestContextAwareInterface -{ - protected $context; - protected $matchers = array(); - protected $defaultMatcher; - - protected $sortedMatchers = array(); - - /** - * Constructor - * - * Check if this constructor is needed (is RequestContext needed ? ) - */ - public function __construct() - { - $this->context = new RequestContext(); - } - - /** - * allow to add a matcher routing class to the matchers collection - * matcher must implement RequestMatcherInterface - * - * priority can be fixed with $priority parameter - * - * @param RequestMatcherInterface $matcher - * @param int $priority set the priority of the added matcher - * - */ - public function add(RequestMatcherInterface $matcher, $priority = 0) - { - if (!is_object($matcher)) { - $matcher = new $matcher(); - } - - if (!isset($this->matchers[$priority])) { - $this->matchers[$priority] = array(); - } - - $this->matchers[$priority][] = $matcher; - $this->sortedMatchers = array(); - } - - /** - * - * Sort Matchers by priority - * - * @return array Array of matchers sorted by priority. - */ - public function getSortedMatchers() - { - if (empty($this->sortedMatchers)) { - $this->sortedMatchers = $this->sortMatchers(); - } - - return $this->sortedMatchers; - } - - /** - * - * Sort the matcher by priority - * - * @return array Array of matchers sorted by priority. - */ - public function sortMatchers() - { - $sortedMatchers = array(); - krsort($this->matchers); - - foreach ($this->matchers as $matcher) { - $sortedMatchers = array_merge($sortedMatchers,$matcher); - } - - return $sortedMatchers; - } - - /** - * Tries to match a request with a set of routes. - * - * If the matcher can not find information, it must throw one of the exceptions documented - * below. - * - * @param Request $request The request to match - * - * @return array An array of parameters - * - * @throws ResourceNotFoundException If no matching resource could be found - * @throws MethodNotAllowedException If a matching resource was found but the request method is not allowed - */ - public function matchRequest(Request $request) - { - if (empty($this->matchers)) { - throw new \InvalidArgumentException('there is no matcher added to the TheliaMatcherCollection'); - } - - foreach ($this->getSortedMatchers() as $matcher) { - try { - return $matcher->matchRequest($request); - } catch (ResourceNotFoundException $e) { - //no action, wait for next matcher - } catch (MethodNotAllowedException $e) { - /** - * @todo what todo with a MethodNotAllowedException ? - */ - } - } - - throw new ResourceNotFoundException('No one matcher in this collection matched the current request'); - } - - /** - * Sets the request context. - * - * @param RequestContext $context The context - * - */ - public function setContext(RequestContext $context) - { - $this->context = $context; - - } - - /** - * Gets the request context. - * - * @return RequestContext The context - * - */ - public function getContext() - { - return $this->context; - } -}