create router for rewriting matching

This commit is contained in:
Manuel Raynaud
2013-09-05 20:46:46 +02:00
parent c63120478b
commit 00d7d9b29a
3 changed files with 222 additions and 9 deletions

View File

@@ -75,6 +75,10 @@
<argument key="debug">%kernel.debug%</argument>
</argument>
<argument type="service" id="request.context"/>
<tag name="router.register" priority="254"/>
</service>
<service id="router.rewrite" class="Thelia\Core\Routing\RewritingRouter">
<tag name="router.register" priority="255"/>
</service>

View File

@@ -60,13 +60,4 @@
<default key="_view">cart</default>
</route>
<!-- Empêche l'accès àl'admin, genre: http://localhost/thelia2/index_dev.php/admin/login amène sur l'accueuil de la boutique
<route id="url-rewriting.check" path="/{rewritten_url}" methods="GET">
<default key="_controller">Thelia\Controller\Front\UrlRewritingController::check</default>
<requirement key="rewritten_url">.*</requirement>
</route>
-->
</routes>

View File

@@ -0,0 +1,218 @@
<?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\Core\Routing;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;
use Thelia\Exception\RedirectException;
use Thelia\Exception\UrlRewritingException;
use Thelia\Model\ConfigQuery;
use Thelia\Tools\Redirect;
use Thelia\Tools\URL;
/**
* Class RewritingRouter
* @package Thelia\Core\Routing
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class RewritingRouter implements RouterInterface, RequestMatcherInterface
{
/**
* @var RequestContext The context
*/
protected $context;
/**
* @var options, don't use for now but mandatory
*/
protected $options;
/**
* Sets the request context.
*
* @param RequestContext $context The context
*
* @api
*/
public function setContext(RequestContext $context)
{
// TODO: Implement setContext() method.
$this->context = $context;
}
/**
* Gets the request context.
*
* @return RequestContext The context
*
* @api
*/
public function getContext()
{
// TODO: Implement getContext() method.
return $this->context;
}
public function setOption($key, $value)
{
//NOTHING TO DO FOR NOW
}
/**
* Gets the RouteCollection instance associated with this Router.
*
* @return RouteCollection A RouteCollection instance
*/
public function getRouteCollection()
{
return new RouteCollection();
}
/**
* 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)
{
// TODO: Implement generate() method.
}
/**
* Tries to match a URL path with a set of routes.
*
* If the matcher can not find information, it must throw one of the exceptions documented
* below.
*
* @param string $pathinfo The path info to be parsed (raw format, i.e. not urldecoded)
*
* @return array An array of parameters
*
* @throws ResourceNotFoundException If the resource could not be found
* @throws MethodNotAllowedException If the resource was found but the request method is not allowed
*
* @api
*/
public function match($pathinfo)
{
throw new ResourceNotFoundException("impossible to find route with this method, please use matchRequest method");
}
/**
* 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
*
* @throws \Exception|\Thelia\Exception\UrlRewritingException
* @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException
* @throws \Thelia\Exception\RedirectException
* @return array An array of parameters
*
*/
public function matchRequest(Request $request)
{
if(ConfigQuery::isRewritingEnable()) {
try {
$rewrittenUrlData = URL::getInstance()->resolve($request->getPathInfo());
var_dump($rewrittenUrlData); exit;
} catch(UrlRewritingException $e) {
switch($e->getCode()) {
case UrlRewritingException::URL_NOT_FOUND :
throw new ResourceNotFoundException();
break;
default:
throw $e;
}
}
/* is the URL redirected ? */
if(null !== $rewrittenUrlData->redirectedToUrl) {
$this->redirect($rewrittenUrlData->redirectedToUrl, 301);
}
/* define GET arguments in request */
if(null !== $rewrittenUrlData->view) {
$request->attributes->set('_view', $rewrittenUrlData->view);
if(null !== $rewrittenUrlData->viewId) {
$request->query->set($rewrittenUrlData->view . '_id', $rewrittenUrlData->viewId);
}
}
if(null !== $rewrittenUrlData->locale) {
$request->query->set('locale', $rewrittenUrlData->locale);
}
foreach($rewrittenUrlData->otherParameters as $parameter => $value) {
$request->query->set($parameter, $value);
}
return array (
'_controller' => 'Thelia\\Controller\\Front\\DefaultController::noAction',
'_route' => 'rewrite'
);
}
throw new ResourceNotFoundException();
}
protected function redirect($url, $code = 302)
{
Redirect::exec($url, $status);
}
}