rewriting
This commit is contained in:
@@ -36,6 +36,8 @@ use Thelia\Form\BaseForm;
|
||||
use Thelia\Form\Exception\FormValidationException;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Thelia\Core\Event\DefaultActionEvent;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -200,4 +202,14 @@ class BaseController extends ContainerAware
|
||||
|
||||
if (null !== $url) $this->redirect($url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a 404 error
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
protected function pageNotFound()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,10 +49,10 @@ class DefaultController extends BaseFrontController
|
||||
if(ConfigQuery::isRewritingEnable()) {
|
||||
|
||||
/* Does the query GET parameters match a rewritten URL ? */
|
||||
$rewrittenUrl = URL::retrieveCurrent($request, true);
|
||||
if($rewrittenUrl !== null) {
|
||||
$rewrittenUrl = URL::init()->retrieveCurrent($request);
|
||||
if($rewrittenUrl->rewrittenUrl !== null) {
|
||||
/* 301 redirection to rewritten URL */
|
||||
$this->redirect($rewrittenUrl, 301);
|
||||
$this->redirect($rewrittenUrl->url, 301);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,13 +22,6 @@
|
||||
/*************************************************************************************/
|
||||
namespace Thelia\Controller\Front;
|
||||
|
||||
//use Propel\Runtime\Exception\PropelException;
|
||||
//use Thelia\Form\Exception\FormValidationException;
|
||||
//use Thelia\Core\Event\CartEvent;
|
||||
//use Thelia\Core\Event\TheliaEvents;
|
||||
//use Symfony\Component\HttpFoundation\Request;
|
||||
//use Thelia\Form\CartAdd;
|
||||
|
||||
use Thelia\Core\HttpFoundation\Request;
|
||||
use Thelia\Exception\UrlRewritingException;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
@@ -36,36 +29,40 @@ use Thelia\Tools\URL;
|
||||
|
||||
class UrlRewritingController extends BaseFrontController
|
||||
{
|
||||
public function check(Request $request)
|
||||
public function check(Request $request, $rewritten_url)
|
||||
{
|
||||
if(ConfigQuery::isRewritingEnable()) {
|
||||
try {
|
||||
$rewrittentUrlData = URL::resolveCurrent($request);
|
||||
$rewrittenUrlData = URL::init()->resolve($rewritten_url);
|
||||
} catch(UrlRewritingException $e) {
|
||||
$code = $e->getCode();
|
||||
switch($e->getCode()) {
|
||||
case UrlRewritingException::URL_NOT_FOUND :
|
||||
/* TODO : redirect 404 */
|
||||
throw $e;
|
||||
return $this->pageNotFound();
|
||||
break;
|
||||
default:
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/* is the URL redirected ? */
|
||||
|
||||
if(null !== $rewrittenUrlData->redirectedToUrl) {
|
||||
$this->redirect($rewrittenUrlData->redirectedToUrl, 301);
|
||||
}
|
||||
|
||||
/* define GET arguments in request */
|
||||
|
||||
if(null !== $rewrittentUrlData->view) {
|
||||
$request->query->set('view', $rewrittentUrlData->view);
|
||||
if(null !== $rewrittentUrlData->viewId) {
|
||||
$request->query->set($rewrittentUrlData->view . '_id', $rewrittentUrlData->viewId);
|
||||
if(null !== $rewrittenUrlData->view) {
|
||||
$request->query->set('view', $rewrittenUrlData->view);
|
||||
if(null !== $rewrittenUrlData->viewId) {
|
||||
$request->query->set($rewrittenUrlData->view . '_id', $rewrittenUrlData->viewId);
|
||||
}
|
||||
}
|
||||
if(null !== $rewrittentUrlData->locale) {
|
||||
$request->query->set('locale', $rewrittentUrlData->locale);
|
||||
if(null !== $rewrittenUrlData->locale) {
|
||||
$request->query->set('locale', $rewrittenUrlData->locale);
|
||||
}
|
||||
|
||||
foreach($rewrittentUrlData->otherParameters as $parameter => $value) {
|
||||
foreach($rewrittenUrlData->otherParameters as $parameter => $value) {
|
||||
$request->query->set($parameter, $value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ class UrlGenerator extends AbstractSmartyPlugin
|
||||
{
|
||||
return array(
|
||||
"current" => "getCurrentUrl",
|
||||
"return_to" => "getReturnToUrl",
|
||||
"return_to" => "getReturnToUrl",
|
||||
"index" => "getIndexUrl",
|
||||
);
|
||||
}
|
||||
@@ -169,7 +169,9 @@ class UrlGenerator extends AbstractSmartyPlugin
|
||||
|
||||
protected function getCurrentUrl()
|
||||
{
|
||||
return URL::retrieveCurrent($this->request);
|
||||
$retriever = URL::init()->retrieveCurrent($this->request);
|
||||
|
||||
return $retriever->rewrittenUrl === null ? $retriever->url : $retriever->rewrittenUrl ;
|
||||
}
|
||||
|
||||
protected function getReturnToUrl()
|
||||
|
||||
@@ -31,6 +31,8 @@ class UrlRewritingException extends \Exception
|
||||
|
||||
const URL_NOT_FOUND = 404;
|
||||
|
||||
const RESOLVER_NULL_SEARCH = 800;
|
||||
|
||||
public function __construct($message, $code = null, $previous = null) {
|
||||
if($code === null) {
|
||||
$code = self::UNKNOWN_EXCEPTION;
|
||||
|
||||
@@ -22,7 +22,7 @@ class Category extends BaseCategory
|
||||
|
||||
public function getUrl($locale)
|
||||
{
|
||||
return URL::retrieve('category', $this->getId(), $locale);
|
||||
return URL::init()->retrieve('category', $this->getId(), $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -9,6 +9,6 @@ class Content extends BaseContent
|
||||
{
|
||||
public function getUrl($locale)
|
||||
{
|
||||
return URL::retrieve('content', $this->getId(), $locale);
|
||||
return URL::init()->retrieve('content', $this->getId(), $locale);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ class Folder extends BaseFolder
|
||||
|
||||
public function getUrl($locale)
|
||||
{
|
||||
return URL::retrieve('folder', $this->getId(), $locale);
|
||||
return URL::init()->retrieve('folder', $this->getId(), $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -9,6 +9,6 @@ class Product extends BaseProduct
|
||||
{
|
||||
public function getUrl($locale)
|
||||
{
|
||||
return URL::retrieve('product', $this->getId(), $locale);
|
||||
return URL::init()->retrieve('product', $this->getId(), $locale);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
namespace Thelia\Model;
|
||||
|
||||
use Propel\Runtime\ActiveQuery\Criteria;
|
||||
use Propel\Runtime\ActiveQuery\Join;
|
||||
use Thelia\Model\Base\RewritingUrlQuery as BaseRewritingUrlQuery;
|
||||
|
||||
use Thelia\Model\Map\RewritingUrlTableMap;
|
||||
|
||||
/**
|
||||
* Skeleton subclass for performing query and update operations on the 'rewriting_url' table.
|
||||
@@ -15,6 +17,96 @@ use Thelia\Model\Base\RewritingUrlQuery as BaseRewritingUrlQuery;
|
||||
* long as it does not already exist in the output directory.
|
||||
*
|
||||
*/
|
||||
class RewritingUrlQuery extends BaseRewritingUrlQuery {
|
||||
class RewritingUrlQuery extends BaseRewritingUrlQuery
|
||||
{
|
||||
/**
|
||||
* @param $rewrittenUrl
|
||||
*
|
||||
* @return array|mixed|\Propel\Runtime\Collection\ObjectCollection
|
||||
*/
|
||||
public function getResolverSearch($rewrittenUrl)
|
||||
{
|
||||
$redirectedJoin = new Join();
|
||||
$redirectedJoin->addExplicitCondition(RewritingUrlTableMap::TABLE_NAME, 'REDIRECTED', 'ru', RewritingUrlTableMap::TABLE_NAME, 'ID', 'is_redirected');
|
||||
$redirectedJoin->setJoinType(Criteria::LEFT_JOIN);
|
||||
|
||||
$search = RewritingArgumentQuery::create()
|
||||
->joinRewritingUrl('ru', Criteria::RIGHT_JOIN)
|
||||
->addJoinObject($redirectedJoin)
|
||||
->where('`ru`.URL = ?', $rewrittenUrl, \PDO::PARAM_STR)
|
||||
->withColumn('`ru`.URL', 'ru_url')
|
||||
->withColumn('`ru`.VIEW', 'ru_view')
|
||||
->withColumn('`ru`.VIEW_LOCALE', 'ru_locale')
|
||||
->withColumn('`ru`.VIEW_ID', 'ru_viewId')
|
||||
->withColumn('`is_redirected`.URL', 'ru_redirected_to_url')
|
||||
->find();
|
||||
|
||||
return $search;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $view
|
||||
* @param $viewId
|
||||
* @param $viewLocale
|
||||
*
|
||||
* @return null|RewritingUrl
|
||||
*/
|
||||
public function getViewUrlQuery($view, $viewLocale, $viewId)
|
||||
{
|
||||
return RewritingUrlQuery::create()
|
||||
->joinRewritingArgument('ra', Criteria::LEFT_JOIN)
|
||||
->where('ISNULL(`ra`.REWRITING_URL_ID)')
|
||||
->filterByView($view)
|
||||
->filterByViewLocale($viewLocale)
|
||||
->filterByViewId($viewId)
|
||||
->filterByRedirected(null)
|
||||
->orderByUpdatedAt(Criteria::DESC)
|
||||
->findOne();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $view
|
||||
* @param $viewLocale
|
||||
* @param $viewId
|
||||
* @param $viewOtherParameters
|
||||
*
|
||||
* @return null|RewritingUrl
|
||||
*/
|
||||
public function getSpecificUrlQuery($view, $viewLocale, $viewId, $viewOtherParameters)
|
||||
{
|
||||
$urlQuery = RewritingUrlQuery::create()
|
||||
->joinRewritingArgument('ra', Criteria::LEFT_JOIN)
|
||||
->withColumn('`ra`.REWRITING_URL_ID', 'ra_REWRITING_URL_ID')
|
||||
->filterByView($view)
|
||||
->filterByViewLocale($viewLocale)
|
||||
->filterByViewId($viewId)
|
||||
->filterByRedirected(null)
|
||||
->orderByUpdatedAt(Criteria::DESC);
|
||||
|
||||
$otherParametersCount = count($viewOtherParameters);
|
||||
if($otherParametersCount > 0) {
|
||||
$parameterConditions = array();
|
||||
|
||||
foreach($viewOtherParameters as $parameter => $value) {
|
||||
$conditionName = 'other_parameter_condition_' . count($parameterConditions);
|
||||
$urlQuery->condition('parameter_condition', '`ra`.PARAMETER= ?', $parameter, \PDO::PARAM_STR)
|
||||
->condition('value_condition', '`ra`.VALUE = ?', $value, \PDO::PARAM_STR)
|
||||
->combine(array('parameter_condition', 'value_condition'), Criteria::LOGICAL_AND, $conditionName);
|
||||
$parameterConditions[] = $conditionName;
|
||||
}
|
||||
|
||||
$urlQuery->where($parameterConditions, Criteria::LOGICAL_OR);
|
||||
|
||||
$urlQuery->groupBy(RewritingUrlTableMap::ID);
|
||||
|
||||
$urlQuery->condition('count_condition_1', 'COUNT(' . RewritingUrlTableMap::ID . ') = ?', $otherParametersCount, \PDO::PARAM_INT) // ensure we got all the asked parameters (provided by the query)
|
||||
->condition('count_condition_2', 'COUNT(' . RewritingUrlTableMap::ID . ') = (SELECT COUNT(*) FROM rewriting_argument WHERE rewriting_argument.REWRITING_URL_ID = ra_REWRITING_URL_ID)'); // ensure we don't miss any parameters (needed to match the rewritten url)
|
||||
|
||||
$urlQuery->having(array('count_condition_1', 'count_condition_2'), Criteria::LOGICAL_AND);
|
||||
} else {
|
||||
$urlQuery->where('ISNULL(`ra`.REWRITING_URL_ID)');
|
||||
}
|
||||
|
||||
return $urlQuery->findOne();
|
||||
}
|
||||
} // RewritingUrlQuery
|
||||
|
||||
@@ -23,9 +23,11 @@
|
||||
namespace Thelia\Rewriting;
|
||||
|
||||
use Propel\Runtime\ActiveQuery\Criteria;
|
||||
use Propel\Runtime\ActiveQuery\Join;
|
||||
use Thelia\Exception\RewritingUrlException;
|
||||
use Thelia\Exception\UrlRewritingException;
|
||||
use Thelia\Model\RewritingArgumentQuery;
|
||||
use Thelia\Model\RewritingUrlQuery;
|
||||
use Thelia\Model\Map\RewritingUrlTableMap;
|
||||
|
||||
/**
|
||||
* Class RewritingResolver
|
||||
@@ -36,13 +38,19 @@ use Thelia\Model\RewritingArgumentQuery;
|
||||
*/
|
||||
class RewritingResolver
|
||||
{
|
||||
protected $search = null;
|
||||
protected $rewritingUrlQuery = null;
|
||||
|
||||
public $view;
|
||||
public $viewId;
|
||||
public $locale;
|
||||
public $otherParameters;
|
||||
public $redirectedToUrl;
|
||||
|
||||
public function __construct($url = null)
|
||||
{
|
||||
$this->rewritingUrlQuery = new RewritingUrlQuery();
|
||||
|
||||
if($url !== null) {
|
||||
$this->load($url);
|
||||
}
|
||||
@@ -50,22 +58,28 @@ class RewritingResolver
|
||||
|
||||
public function load($rewrittenUrl)
|
||||
{
|
||||
$search = RewritingArgumentQuery::create()
|
||||
//->filterByUrl($rewrittenUrl)
|
||||
->joinRewritingUrl('ru', Criteria::RIGHT_JOIN)
|
||||
->where('`ru`.URL = ?', $rewrittenUrl, \PDO::PARAM_STR)
|
||||
->withColumn('`ru`.URL', 'ru_url')
|
||||
->withColumn('`ru`.VIEW', 'ru_view')
|
||||
->withColumn('`ru`.VIEW_LOCALE', 'ru_locale')
|
||||
->withColumn('`ru`.VIEW_ID', 'ru_viewId')
|
||||
->find();
|
||||
$this->search = $this->rewritingUrlQuery->getResolverSearch($rewrittenUrl);
|
||||
|
||||
if($search->count() == 0) {
|
||||
if($this->search->count() == 0) {
|
||||
throw new UrlRewritingException('URL NOT FOUND', UrlRewritingException::URL_NOT_FOUND);
|
||||
}
|
||||
|
||||
$this->view = $this->search->getFirst()->getVirtualColumn('ru_view');
|
||||
$this->viewId = $this->search->getFirst()->getVirtualColumn('ru_viewId');
|
||||
$this->locale = $this->search->getFirst()->getVirtualColumn('ru_locale');
|
||||
$this->redirectedToUrl = $this->search->getFirst()->getVirtualColumn('ru_redirected_to_url');
|
||||
|
||||
$this->otherParameters = $this->getOtherParameters();
|
||||
}
|
||||
|
||||
protected function getOtherParameters()
|
||||
{
|
||||
if($this->search === null) {
|
||||
throw new UrlRewritingException('RESOLVER NULL SEARCH', UrlRewritingException::RESOLVER_NULL_SEARCH);
|
||||
}
|
||||
|
||||
$otherParameters = array();
|
||||
foreach($search as $result) {
|
||||
foreach($this->search as $result) {
|
||||
$parameter = $result->getParameter();
|
||||
$value = $result->getValue();
|
||||
|
||||
@@ -74,9 +88,8 @@ class RewritingResolver
|
||||
}
|
||||
}
|
||||
|
||||
$this->view = $search->getFirst()->getVirtualColumn('ru_view');
|
||||
$this->viewId = $search->getFirst()->getVirtualColumn('ru_viewId');
|
||||
$this->locale = $search->getFirst()->getVirtualColumn('ru_locale');
|
||||
$this->otherParameters = $otherParameters;
|
||||
return $otherParameters;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -23,8 +23,9 @@
|
||||
namespace Thelia\Rewriting;
|
||||
|
||||
use Propel\Runtime\ActiveQuery\Criteria;
|
||||
use Thelia\Model\Base\RewritingUrlQuery;
|
||||
use Thelia\Model\RewritingUrlQuery;
|
||||
use Thelia\Model\Map\RewritingUrlTableMap;
|
||||
use Thelia\Tools\URL;
|
||||
|
||||
/**
|
||||
* Class RewritingRetriever
|
||||
@@ -35,38 +36,40 @@ use Thelia\Model\Map\RewritingUrlTableMap;
|
||||
*/
|
||||
class RewritingRetriever
|
||||
{
|
||||
/**
|
||||
* @param $view
|
||||
* @param $viewLocale
|
||||
* @param $viewId
|
||||
*
|
||||
* @return null|$url
|
||||
*/
|
||||
public function getViewUrl($view, $viewLocale, $viewId)
|
||||
{
|
||||
$url = $this->getViewUrlQuery($view, $viewId, $viewLocale);
|
||||
protected $search = null;
|
||||
protected $rewritingUrlQuery = null;
|
||||
|
||||
return $url === null ? null : $url->getUrl();
|
||||
public $url;
|
||||
public $rewrittenUrl;
|
||||
|
||||
public function __construct($view = null, $viewLocale = null, $viewId = null)
|
||||
{
|
||||
$this->rewritingUrlQuery = new RewritingUrlQuery();
|
||||
|
||||
if($view !== null && $viewLocale !== null) {
|
||||
$this->load($view, $viewLocale, $viewId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $view
|
||||
* @param $viewId
|
||||
* @param $viewLocale
|
||||
*
|
||||
* @return null|RewritingUrl
|
||||
* @param $view
|
||||
* @param $viewLocale
|
||||
* @param null $viewId
|
||||
*/
|
||||
protected function getViewUrlQuery($view, $viewId, $viewLocale)
|
||||
public function loadViewUrl($view, $viewLocale, $viewId = null)
|
||||
{
|
||||
return RewritingUrlQuery::create()
|
||||
->joinRewritingArgument('ra', Criteria::LEFT_JOIN)
|
||||
->where('ISNULL(`ra`.REWRITING_URL_ID)')
|
||||
->filterByView($view)
|
||||
->filterByViewLocale($viewLocale)
|
||||
->filterByViewId($viewId)
|
||||
->filterByRedirected(null)
|
||||
->orderByUpdatedAt(Criteria::DESC)
|
||||
->findOne();
|
||||
$this->search = $this->rewritingUrlQuery->getViewUrlQuery($view, $viewLocale, $viewId);
|
||||
|
||||
$allParametersWithoutView = array();
|
||||
$allParametersWithoutView['locale'] = $viewLocale;
|
||||
if(null !== $viewId) {
|
||||
$allParametersWithoutView[$view . '_id'] = $viewId;
|
||||
}
|
||||
|
||||
$this->url = URL::viewUrl($view, $allParametersWithoutView);
|
||||
if($this->search !== null) {
|
||||
$this->rewrittenUrl = $this->search->getUrl();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,50 +77,25 @@ class RewritingRetriever
|
||||
* @param $viewLocale
|
||||
* @param null $viewId
|
||||
* @param array $viewOtherParameters
|
||||
*
|
||||
* @return null|$url
|
||||
*/
|
||||
public function getSpecificUrl($view, $viewLocale, $viewId = null, $viewOtherParameters = array())
|
||||
public function loadSpecificUrl($view, $viewLocale, $viewId = null, $viewOtherParameters = array())
|
||||
{
|
||||
if(empty($viewOtherParameters)) {
|
||||
return $this->getViewUrl($view, $viewLocale, $viewId);
|
||||
$this->loadViewUrl($view, $viewLocale, $viewId);
|
||||
return;
|
||||
}
|
||||
|
||||
$urlQuery = RewritingUrlQuery::create()
|
||||
->joinRewritingArgument('ra', Criteria::LEFT_JOIN)
|
||||
->withColumn('`ra`.REWRITING_URL_ID', 'ra_REWRITING_URL_ID')
|
||||
->filterByView($view)
|
||||
->filterByViewLocale($viewLocale)
|
||||
->filterByViewId($viewId)
|
||||
->filterByRedirected(null)
|
||||
->orderByUpdatedAt(Criteria::DESC);
|
||||
$this->search = $this->rewritingUrlQuery->getSpecificUrlQuery($view, $viewLocale, $viewId, $viewOtherParameters);
|
||||
|
||||
$otherParametersCount = count($viewOtherParameters);
|
||||
if($otherParametersCount > 0) {
|
||||
$parameterConditions = array();
|
||||
|
||||
foreach($viewOtherParameters as $parameter => $value) {
|
||||
$conditionName = 'other_parameter_condition_' . count($parameterConditions);
|
||||
$urlQuery->condition('parameter_condition', '`ra`.PARAMETER= ?', $parameter, \PDO::PARAM_STR)
|
||||
->condition('value_condition', '`ra`.VALUE = ?', $value, \PDO::PARAM_STR)
|
||||
->combine(array('parameter_condition', 'value_condition'), Criteria::LOGICAL_AND, $conditionName);
|
||||
$parameterConditions[] = $conditionName;
|
||||
}
|
||||
|
||||
$urlQuery->where($parameterConditions, Criteria::LOGICAL_OR);
|
||||
|
||||
$urlQuery->groupBy(RewritingUrlTableMap::ID);
|
||||
|
||||
$urlQuery->condition('count_condition_1', 'COUNT(' . RewritingUrlTableMap::ID . ') = ?', $otherParametersCount, \PDO::PARAM_INT) // ensure we got all the asked parameters (provided by the query)
|
||||
->condition('count_condition_2', 'COUNT(' . RewritingUrlTableMap::ID . ') = (SELECT COUNT(*) FROM rewriting_argument WHERE rewriting_argument.REWRITING_URL_ID = ra_REWRITING_URL_ID)'); // ensure we don't miss any parameters (needed to match the rewritten url)
|
||||
|
||||
$urlQuery->having(array('count_condition_1', 'count_condition_2'), Criteria::LOGICAL_AND);
|
||||
} else {
|
||||
$urlQuery->where('ISNULL(`ra`.REWRITING_URL_ID)');
|
||||
$allParametersWithoutView = $viewOtherParameters;
|
||||
$allParametersWithoutView['locale'] = $viewLocale;
|
||||
if(null !== $viewId) {
|
||||
$allParametersWithoutView[$view . '_id'] = $viewId;
|
||||
}
|
||||
|
||||
$url = $urlQuery->findOne();
|
||||
|
||||
return $url === null ? null : $url->getUrl();
|
||||
$this->url = URL::viewUrl($view, $allParametersWithoutView);
|
||||
if($this->search !== null) {
|
||||
$this->rewrittenUrl = $this->search->getUrl();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
119
core/lib/Thelia/Tests/Rewriting/RewritingResolverTest.php
Executable file
119
core/lib/Thelia/Tests/Rewriting/RewritingResolverTest.php
Executable file
@@ -0,0 +1,119 @@
|
||||
<?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\Tests\Rewriting;
|
||||
|
||||
use Thelia\Model\RewritingArgument;
|
||||
use Thelia\Rewriting\RewritingResolver;
|
||||
use Propel\Runtime\Collection\ObjectCollection;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Etienne Roudeix <eroudeix@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class RewritingResolverTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected function getMethod($name)
|
||||
{
|
||||
$class = new \ReflectionClass('\Thelia\Rewriting\RewritingResolver');
|
||||
$method = $class->getMethod($name);
|
||||
$method->setAccessible(true);
|
||||
|
||||
return $method;
|
||||
}
|
||||
|
||||
protected function getProperty($name)
|
||||
{
|
||||
$class = new \ReflectionClass('\Thelia\Rewriting\RewritingResolver');
|
||||
$property = $class->getProperty($name);
|
||||
$property->setAccessible(true);
|
||||
|
||||
return $property;
|
||||
}
|
||||
|
||||
public function testGetOtherParameters()
|
||||
{
|
||||
$rewritingArguments = array(
|
||||
array('Parameter' => 'foo0', 'Value' => 'bar0'),
|
||||
array('Parameter' => 'foo1', 'Value' => 'bar1'),
|
||||
array('Parameter' => 'foo2', 'Value' => 'bar2'),
|
||||
);
|
||||
$searchResult = new ObjectCollection();
|
||||
$searchResult->setModel('\Thelia\Model\RewritingArgument');
|
||||
$searchResult->fromArray($rewritingArguments);
|
||||
|
||||
$resolver = new RewritingResolver();
|
||||
|
||||
$search = $this->getProperty('search');
|
||||
$search->setValue($resolver, $searchResult);
|
||||
|
||||
$method = $this->getMethod('getOtherParameters');
|
||||
$actual = $method->invoke($resolver);
|
||||
|
||||
$expected = array(
|
||||
'foo0' => 'bar0',
|
||||
'foo1' => 'bar1',
|
||||
'foo2' => 'bar2',
|
||||
);
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
public function testLoad()
|
||||
{
|
||||
$collection = new ObjectCollection();
|
||||
$collection->setModel('\Thelia\Model\RewritingArgument');
|
||||
|
||||
for($i=0; $i<3; $i++) {
|
||||
$ra = new RewritingArgument();
|
||||
$ra->setParameter('foo' . $i);
|
||||
$ra->setValue('bar' . $i);
|
||||
$ra->setVirtualColumn('ru_view', 'view');
|
||||
$ra->setVirtualColumn('ru_viewId', 'viewId');
|
||||
$ra->setVirtualColumn('ru_locale', 'locale');
|
||||
$ra->setVirtualColumn('ru_redirected_to_url', null);
|
||||
|
||||
$collection->append($ra);
|
||||
}
|
||||
|
||||
|
||||
$resolverQuery = $this->getMock('\Thelia\Model\RewritingUrlQuery', array('getResolverSearch'));
|
||||
$resolverQuery->expects($this->any())
|
||||
->method('getResolverSearch')
|
||||
->with('foo.html')
|
||||
->will($this->returnValue($collection));
|
||||
|
||||
$resolver = new RewritingResolver();
|
||||
|
||||
$rewritingUrlQuery = $this->getProperty('rewritingUrlQuery');
|
||||
$rewritingUrlQuery->setValue($resolver, $resolverQuery);
|
||||
|
||||
$resolver->load('foo.html');
|
||||
|
||||
$this->assertEquals('view', $resolver->view);
|
||||
$this->assertEquals('viewId', $resolver->viewId);
|
||||
$this->assertEquals('locale', $resolver->locale);
|
||||
$this->assertEquals(array('foo0' => 'bar0', 'foo1' => 'bar1', 'foo2' => 'bar2'), $resolver->otherParameters);
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,9 @@
|
||||
|
||||
namespace Thelia\Tests\Rewriting;
|
||||
|
||||
use Thelia\Model\RewritingUrl;
|
||||
use Thelia\Rewriting\RewritingRetriever;
|
||||
use Thelia\Tools\URL;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -32,13 +34,65 @@ use Thelia\Rewriting\RewritingRetriever;
|
||||
*/
|
||||
class RewritingRetrieverTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testGetViewUrl()
|
||||
protected function getMethod($name)
|
||||
{
|
||||
$class = new \ReflectionClass('\Thelia\Rewriting\RewritingRetriever');
|
||||
$method = $class->getMethod($name);
|
||||
$method->setAccessible(true);
|
||||
|
||||
return $method;
|
||||
}
|
||||
|
||||
public function testGetSpecificUrl()
|
||||
protected function getProperty($name)
|
||||
{
|
||||
$class = new \ReflectionClass('\Thelia\Rewriting\RewritingRetriever');
|
||||
$property = $class->getProperty($name);
|
||||
$property->setAccessible(true);
|
||||
|
||||
return $property;
|
||||
}
|
||||
|
||||
public function testLoadViewUrl()
|
||||
{
|
||||
$searchResult = new RewritingUrl();
|
||||
$searchResult->setUrl('foo.html');
|
||||
|
||||
$retrieverQuery = $this->getMock('\Thelia\Model\RewritingUrlQuery', array('getViewUrlQuery'));
|
||||
$retrieverQuery->expects($this->any())
|
||||
->method('getViewUrlQuery')
|
||||
->with('view', 'fr_FR', 1)
|
||||
->will($this->returnValue($searchResult));
|
||||
|
||||
$retriever = new RewritingRetriever();
|
||||
|
||||
$rewritingUrlQuery = $this->getProperty('rewritingUrlQuery');
|
||||
$rewritingUrlQuery->setValue($retriever, $retrieverQuery);
|
||||
|
||||
$retriever->loadViewUrl('view', 'fr_FR', 1);
|
||||
|
||||
$this->assertEquals('foo.html', $retriever->rewrittenUrl);
|
||||
$this->assertEquals(URL::viewUrl('view', array('locale' => 'fr_FR', 'view_id' => 1)), $retriever->url);
|
||||
}
|
||||
|
||||
public function testLoadSpecificUrl()
|
||||
{
|
||||
$searchResult = new RewritingUrl();
|
||||
$searchResult->setUrl('foo.html');
|
||||
|
||||
$retrieverQuery = $this->getMock('\Thelia\Model\RewritingUrlQuery', array('getSpecificUrlQuery'));
|
||||
$retrieverQuery->expects($this->any())
|
||||
->method('getSpecificUrlQuery')
|
||||
->with('view', 'fr_FR', 1, array('foo0' => 'bar0', 'foo1' => 'bar1'))
|
||||
->will($this->returnValue($searchResult));
|
||||
|
||||
$retriever = new RewritingRetriever();
|
||||
|
||||
$rewritingUrlQuery = $this->getProperty('rewritingUrlQuery');
|
||||
$rewritingUrlQuery->setValue($retriever, $retrieverQuery);
|
||||
|
||||
$retriever->loadSpecificUrl('view', 'fr_FR', 1, array('foo0' => 'bar0', 'foo1' => 'bar1'));
|
||||
|
||||
$this->assertEquals('foo.html', $retriever->rewrittenUrl);
|
||||
$this->assertEquals(URL::viewUrl('view', array('foo0' => 'bar0', 'foo1' => 'bar1', 'locale' => 'fr_FR', 'view_id' => 1)), $retriever->url);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,14 +30,28 @@ use Thelia\Rewriting\RewritingRetriever;
|
||||
|
||||
class URL
|
||||
{
|
||||
protected $resolver = null;
|
||||
protected $retriever = null;
|
||||
|
||||
const PATH_TO_FILE = true;
|
||||
const WITH_INDEX_PAGE = false;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->retriever = new RewritingRetriever();
|
||||
$this->resolver = new RewritingResolver();
|
||||
}
|
||||
|
||||
public static function getIndexPage()
|
||||
{
|
||||
return ConfigQuery::read('base_url', '/') . "index_dev.php"; // FIXME !
|
||||
}
|
||||
|
||||
public static function init()
|
||||
{
|
||||
return new URL();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Absolute URL for a given path relative to web root. By default,
|
||||
* the index.php (or index_dev.php) script name is added to the URL, use
|
||||
@@ -104,7 +118,7 @@ class URL
|
||||
*/
|
||||
public static function viewUrl($viewName, array $parameters = array())
|
||||
{
|
||||
$path = sprintf("%s?view=%s", self::getIndexPage(), $viewName);
|
||||
$path = sprintf("?view=%s", $viewName);
|
||||
|
||||
return self::absoluteUrl($path, $parameters);
|
||||
}
|
||||
@@ -116,18 +130,17 @@ class URL
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
public static function retrieve($view, $viewId, $viewLocale)
|
||||
public function retrieve($view, $viewId, $viewLocale)
|
||||
{
|
||||
$rewrittenUrl = null;
|
||||
if(ConfigQuery::isRewritingEnable()) {
|
||||
$retriever = new RewritingRetriever();
|
||||
$rewrittenUrl = $retriever->getViewUrl($view, $viewLocale, $viewId);
|
||||
$rewrittenUrl = $this->retriever->loadViewUrl($view, $viewLocale, $viewId);
|
||||
}
|
||||
|
||||
return $rewrittenUrl === null ? self::viewUrl($view, array($view . '_id' => $viewId, 'locale' => $viewLocale)) : $rewrittenUrl;
|
||||
}
|
||||
|
||||
public static function retrieveCurrent(Request $request, $rewrittenUrlOnly = false)
|
||||
public function retrieveCurrent(Request $request)
|
||||
{
|
||||
$rewrittenUrl = null;
|
||||
if(ConfigQuery::isRewritingEnable()) {
|
||||
@@ -135,12 +148,10 @@ class URL
|
||||
$viewLocale = $request->query->get('locale', null);
|
||||
$viewId = $view === null ? null : $request->query->get($view . '_id', null);
|
||||
|
||||
$allParameters = $request->query->all();
|
||||
$allParametersWithoutView = $allParameters;
|
||||
$allOtherParameters = $request->query->all();
|
||||
if($view !== null) {
|
||||
unset($allParametersWithoutView['view']);
|
||||
unset($allOtherParameters['view']);
|
||||
}
|
||||
$allOtherParameters = $allParametersWithoutView;
|
||||
if($viewLocale !== null) {
|
||||
unset($allOtherParameters['locale']);
|
||||
}
|
||||
@@ -148,29 +159,15 @@ class URL
|
||||
unset($allOtherParameters[$view . '_id']);
|
||||
}
|
||||
|
||||
$retriever = new RewritingRetriever();
|
||||
$rewrittenUrl = $retriever->getSpecificUrl($view, $viewLocale, $viewId, $allOtherParameters);
|
||||
$this->retriever->loadSpecificUrl($view, $viewLocale, $viewId, $allOtherParameters);
|
||||
}
|
||||
|
||||
if($rewrittenUrlOnly) {
|
||||
return $rewrittenUrl !== null ? $rewrittenUrl : null;
|
||||
} else {
|
||||
return $rewrittenUrl !== null ? $rewrittenUrl : self::viewUrl($view, $allParametersWithoutView);
|
||||
}
|
||||
return $this->retriever;
|
||||
}
|
||||
|
||||
public static function resolve($url)
|
||||
public function resolve($url)
|
||||
{
|
||||
$resolver = new RewritingResolver($url);
|
||||
return $resolver;
|
||||
}
|
||||
|
||||
public static function resolveCurrent(Request $request)
|
||||
{
|
||||
if(ConfigQuery::isRewritingEnable()) {
|
||||
return self::resolve($request->get('rewritten_url'));
|
||||
}
|
||||
|
||||
return null;
|
||||
$this->resolver->load($url);
|
||||
return $this->resolver;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user