Merge branch 'master' of https://github.com/thelia/thelia into coupon

# By Manuel Raynaud (18) and others
# Via franck (9) and others
* 'master' of https://github.com/thelia/thelia: (39 commits)
  Working :
  Working :
  Working :
  Working :
  Fixed minor visual glitches
  Working : Resize countries flag + Add bootstrap-switch
  fix test suite
  clear asset cache in cache:cler command
  Added a 'development mode' to assetic smarty plugin
  rewriting router
  use good Request object
  Added Tools\URL test case, and a test case superclass for initializing Tools\URL
  remove unused UrlWritin controller
  create router for rewriting matching
  customer substitutions
  fix typo in front id
  Working : For attributes on labels
  Working : For attributes on labels
  add label_attr attribute to form smarty plugin
  start refactorin rewriting routing
  ...

Conflicts:
	core/lib/Thelia/Config/Resources/routing/front.xml
	templates/admin/default/assets/less/thelia/bootstrap-editable.less
	templates/admin/default/categories.html
This commit is contained in:
gmorel
2013-09-06 19:36:52 +02:00
118 changed files with 4702 additions and 1831 deletions

View File

@@ -23,38 +23,89 @@
namespace Thelia\Tools;
use Symfony\Component\HttpFoundation\Request;
use Thelia\Model\ConfigQuery;
use Thelia\Rewriting\RewritingResolver;
use Thelia\Rewriting\RewritingRetriever;
use Thelia\Core\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\RequestContext;
class URL
{
protected $resolver = null;
protected $retriever = null;
protected $requestContext;
const PATH_TO_FILE = true;
const WITH_INDEX_PAGE = false;
public function __construct()
protected static $instance = null;
public function __construct(ContainerInterface $container)
{
// Allow singleton style calls once intanciated.
// For this to work, the URL service has to be instanciated very early. This is done manually
// in TheliaHttpKernel, by calling $this->container->get('thelia.url.manager');
self::$instance = $this;
$this->requestContext = $container->get('router.admin')->getContext();
$this->retriever = new RewritingRetriever();
$this->resolver = new RewritingResolver();
}
public static function getIndexPage()
{
return ConfigQuery::read('base_url', '/') . "index_dev.php"; // FIXME !
/**
* Return this class instance, only once instanciated.
*
* @throws \RuntimeException if the class has not been instanciated.
* @return \Thelia\Tools\URL the instance.
*/
public static function getInstance() {
if (self::$instance == null) throw new \RuntimeException("URL instance is not initialized.");
return self::$instance;
}
public static function init()
/**
* Return the base URL, either the base_url defined in Config, or the URL
* of the current language, if 'one_domain_foreach_lang' is enabled.
*
* @return string the base URL, with a trailing '/'
*/
public function getBaseUrl()
{
return new URL();
if ($host = $this->requestContext->getHost()) {
$scheme = $this->requestContext->getScheme();
$port = '';
if ('http' === $scheme && 80 != $this->requestContext->getHttpPort()) {
$port = ':'.$this->requestContext->getHttpPort();
} elseif ('https' === $scheme && 443 != $this->requestContext->getHttpsPort()) {
$port = ':'.$this->requestContext->getHttpsPort();
}
$schemeAuthority = "$scheme://$host"."$port";
}
return $schemeAuthority.$this->requestContext->getBaseUrl();
}
/**
* @return string the index page, which is in fact the base URL.
*/
public function getIndexPage()
{
// The index page is the base URL :)
return $this->getBaseUrl();
}
/**
* 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
* the script name (index_dev.php) is added to the URL in dev_environment, use
* $path_only = true to get a path without the index script.
*
* @param string $path the relative path
@@ -63,19 +114,21 @@ class URL
*
* @return string The generated URL
*/
public static function absoluteUrl($path, array $parameters = null, $path_only = self::WITH_INDEX_PAGE)
public function absoluteUrl($path, array $parameters = null, $path_only = self::WITH_INDEX_PAGE)
{
// Already absolute ?
if (substr($path, 0, 4) != 'http') {
/**
* @etienne : can't be done here for it's already done in ::viewUrl / ::adminViewUrl
* @franck : should be done, as absoluteUrl() is sometimes called directly (see UrlGenerator::generateUrlFunction())
*/
$root = $path_only == self::PATH_TO_FILE ? ConfigQuery::read('base_url', '/') : self::getIndexPage();
//$root = $path_only == self::PATH_TO_FILE ? ConfigQuery::read('base_url', '/') : '';
$base_url = $this->getBaseUrl();
$base = rtrim($root, '/') . '/' . ltrim($path, '/');
// If only a path is requested, be sure to remove the script name (index.php or index_dev.php), if any.
if ($path_only == self::PATH_TO_FILE) {
// As the base_url always ends with '/', if we don't find / at the end, we have a script.
if (substr($base_url, -1) != '/') $base_url = dirname($base_url);
}
// Normalize the given path
$base = rtrim($base_url, '/') . '/' . ltrim($path, '/');
} else
$base = $path;
@@ -90,6 +143,7 @@ class URL
$sepChar = strstr($base, '?') === false ? '?' : '&';
if ('' !== $queryString = rtrim($queryString, "&")) $queryString = $sepChar . $queryString;
return $base . $queryString;
}
@@ -101,11 +155,11 @@ class URL
*
* @return string The generated URL
*/
public static function adminViewUrl($viewName, array $parameters = array())
public function adminViewUrl($viewName, array $parameters = array())
{
$path = sprintf("%s/admin/%s", self::getIndexPage(), $viewName); // FIXME ! view= should not be required, check routing parameters
$path = sprintf("%s/admin/%s", $this->getIndexPage(), $viewName);
return self::absoluteUrl($path, $parameters);
return $this->absoluteUrl($path, $parameters);
}
/**
@@ -116,72 +170,71 @@ class URL
*
* @return string The generated URL
*/
public static function viewUrl($viewName, array $parameters = array())
public function viewUrl($viewName, array $parameters = array())
{
$path = sprintf("?view=%s", $viewName);
return self::absoluteUrl($path, $parameters);
return $this->absoluteUrl($path, $parameters);
}
/**
* Retrieve a rewritten URL from a view, a view id and a locale
*
* @param $view
* @param $viewId
* @param $viewLocale
*
* @return RewritingRetriever You can access $url and $rewrittenUrl properties
*/
public function retrieve($view, $viewId, $viewLocale)
{
if(ConfigQuery::isRewritingEnable()) {
$this->retriever->loadViewUrl($view, $viewLocale, $viewId);
}
return $this->retriever;
}
/**
* Retrieve a rewritten URL from a view, a view id and a locale
*
* @param $view
* @param $viewId
* @param $viewLocale
*
* @return RewritingRetriever You can access $url and $rewrittenUrl properties
*/
public function retrieve($view, $viewId, $viewLocale)
{
if(ConfigQuery::isRewritingEnable()) {
$this->retriever->loadViewUrl($view, $viewLocale, $viewId);
}
/**
* Retrieve a rewritten URL from the current GET parameters
*
* @param Request $request
*
* @return RewritingRetriever You can access $url and $rewrittenUrl properties or use toString method
*/
public function retrieveCurrent(Request $request)
{
if(ConfigQuery::isRewritingEnable()) {
$view = $request->attributes->get('_view', null);
$viewLocale = $request->query->get('locale', null);
$viewId = $view === null ? null : $request->query->get($view . '_id', null);
return $this->retriever;
}
$allOtherParameters = $request->query->all();
if($view !== null) {
unset($allOtherParameters['view']);
if($viewId !== null) {
unset($allOtherParameters[$view . '_id']);
}
}
if($viewLocale !== null) {
unset($allOtherParameters['locale']);
}
/**
* Retrieve a rewritten URL from the current GET parameters
*
* @param Request $request
*
* @return RewritingRetriever You can access $url and $rewrittenUrl properties or use toString method
*/
public function retrieveCurrent(Request $request)
{
if(ConfigQuery::isRewritingEnable()) {
$view = $request->query->get('view', null);
$viewLocale = $request->query->get('locale', null);
$viewId = $view === null ? null : $request->query->get($view . '_id', null);
$this->retriever->loadSpecificUrl($view, $viewLocale, $viewId, $allOtherParameters);
}
$allOtherParameters = $request->query->all();
if($view !== null) {
unset($allOtherParameters['view']);
}
if($viewLocale !== null) {
unset($allOtherParameters['locale']);
}
if($viewId !== null) {
unset($allOtherParameters[$view . '_id']);
}
return $this->retriever;
}
$this->retriever->loadSpecificUrl($view, $viewLocale, $viewId, $allOtherParameters);
}
return $this->retriever;
}
/**
* Retrieve a rewritten URL from the current GET parameters or use toString method
*
* @param $url
*
* @return RewritingResolver
*/
public function resolve($url)
{
$this->resolver->load($url);
return $this->resolver;
}
}
/**
* Retrieve a rewritten URL from the current GET parameters or use toString method
*
* @param $url
*
* @return RewritingResolver
*/
public function resolve($url)
{
$this->resolver->load($url);
return $this->resolver;
}
}