allow to declare new router in modules
This commit is contained in:
@@ -24,19 +24,9 @@
|
||||
<argument type="service" id="thelia.parser"/>
|
||||
</service>
|
||||
|
||||
<service id="matcher.default" class="Thelia\Routing\Matcher\DefaultMatcher">
|
||||
<argument type="service" id="controller.default"/>
|
||||
</service>
|
||||
|
||||
<service id="request.context" class="%router.request_context.class%" />
|
||||
|
||||
<service id="router.null_generator" class="%router.null_generator.class%"/>
|
||||
|
||||
<service id="router.default_route" class="%router.dynamicRouter.class%">
|
||||
<argument type="service" id="request.context"/>
|
||||
<argument type="service" id="matcher.default"/>
|
||||
<argument type="service" id="router.null_generator"/>
|
||||
</service>
|
||||
|
||||
<service id="router.admin.fileLocator" class="Symfony\Component\Config\FileLocator">
|
||||
<argument>%thelia.core_dir%/Config/Resources/routing</argument>
|
||||
@@ -54,6 +44,7 @@
|
||||
<argument key="debug">%kernel.debug%</argument>
|
||||
</argument>
|
||||
<argument type="service" id="request.context"/>
|
||||
<tag name="router.register" priority="0"/>
|
||||
</service>
|
||||
|
||||
<service id="router.front" class="%router.class%">
|
||||
@@ -64,25 +55,13 @@
|
||||
<argument key="debug">%kernel.debug%</argument>
|
||||
</argument>
|
||||
<argument type="service" id="request.context"/>
|
||||
<tag name="router.register" priority="255"/>
|
||||
</service>
|
||||
|
||||
<service id="router.chainRequest" class="%router.chainRouter.class%">
|
||||
<call method="setContext">
|
||||
<argument type="service" id="request.context"/>
|
||||
</call>
|
||||
<call method="add">
|
||||
<argument type="service" id="router.default_route"/>
|
||||
<argument>-255</argument>
|
||||
</call>
|
||||
|
||||
<call method="add">
|
||||
<argument type="service" id="router.admin"/>
|
||||
<argument>0</argument>
|
||||
</call>
|
||||
<call method="add">
|
||||
<argument type="service" id="router.front"/>
|
||||
<argument>255</argument>
|
||||
</call>
|
||||
</service>
|
||||
|
||||
<service id="listener.router" class="Symfony\Component\HttpKernel\EventListener\RouterListener">
|
||||
|
||||
@@ -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">
|
||||
|
||||
<route id="home" path="/" >
|
||||
<default key="_controller">Thelia\Controller\Front\DefaultController::noAction</default>
|
||||
<default key="_view">index</default>
|
||||
</route>
|
||||
|
||||
<route id="customer.create.process" path="/customer/create" methods="post">
|
||||
<default key="_controller">Thelia\Controller\Front\CustomerController::createAction</default>
|
||||
<default key="_view">connexion</default>
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
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 <mraynadu@openstudio.fr>
|
||||
*/
|
||||
|
||||
class DefaultController implements NullControllerInterface
|
||||
class DefaultController extends BaseFrontController
|
||||
{
|
||||
/**
|
||||
*
|
||||
@@ -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())
|
||||
;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
47
core/lib/Thelia/Routing/Matcher/DefaultMatcher.php → core/lib/Thelia/Core/DependencyInjection/Compiler/RegisterRouterPass.php
Executable file → Normal file
47
core/lib/Thelia/Routing/Matcher/DefaultMatcher.php → core/lib/Thelia/Core/DependencyInjection/Compiler/RegisterRouterPass.php
Executable file → Normal file
@@ -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 <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
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 <mraynaud@openstudio.fr>
|
||||
*/
|
||||
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 ;
|
||||
}
|
||||
|
||||
public function matchRequest(Request $request)
|
||||
{
|
||||
$objectInformation = new \ReflectionObject($this->controller);
|
||||
$chainRouter = $container->getDefinition("router.chainRequest");
|
||||
|
||||
$parameter = array(
|
||||
'_controller' => $objectInformation->getName().'::noAction'
|
||||
);
|
||||
|
||||
return $parameter;
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -1,173 +0,0 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
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 <mraynaud@openstudio.fr>
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user