allow possibility to add multiple files for touring

This commit is contained in:
Manuel Raynaud
2013-08-13 16:04:28 +02:00
parent d53f1289c3
commit c284a756b6
7 changed files with 98 additions and 94 deletions

View File

@@ -10,8 +10,14 @@
<parameter key="router.dynamicRouter.class">Symfony\Cmf\Component\Routing\DynamicRouter</parameter>
<parameter key="router.chainRouter.class">Symfony\Cmf\Component\Routing\ChainRouter</parameter>
<parameter key="router.class">Symfony\Component\Routing\Router</parameter>
<parameter key="router.xmlFileName">routing.xml</parameter>
</parameters>
<routing>
<file path="%thelia.core_dir%/Config/Resources/routing/admin.xml"/>
<file path="%thelia.core_dir%/Config/Resources/routing/front.xml"/>
</routing>
<services>
<service id="thelia.listener.view" class="Thelia\Core\EventListener\ViewListener">
<tag name="kernel.event_subscriber"/>
@@ -29,39 +35,30 @@
<service id="router.admin.fileLocator" class="Symfony\Component\Config\FileLocator">
<argument>%thelia.core_dir%/Config/Resources/routing</argument>
<argument>%kernel.cache_dir%</argument>
</service>
<service id="router.xmlLoader" class="Symfony\Component\Routing\Loader\XmlFileLoader">
<argument type="service" id="router.admin.fileLocator"/>
</service>
<service id="router.admin" class="%router.class%">
<service id="router" class="%router.class%">
<argument type="service" id="router.xmlLoader"/>
<argument>admin.xml</argument>
<argument>%router.xmlFileName%</argument>
<argument type="collection">
<argument key="cache_dir">%kernel.cache_dir%</argument>
<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%">
<argument type="service" id="router.xmlLoader"/>
<argument>front.xml</argument>
<argument type="collection">
<argument key="cache_dir">%kernel.cache_dir%</argument>
<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"/>
</call>
</service>
<service id="listener.router" class="Symfony\Component\HttpKernel\EventListener\RouterListener">

View File

@@ -59,7 +59,6 @@ class TheliaBundle extends Bundle
$container
->addCompilerPass(new RegisterListenersPass())
->addCompilerPass(new RegisterParserPluginPass())
->addCompilerPass(new RegisterRouterPass())
;
}

View File

@@ -1,54 +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\Core\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class RegisterRouterPass implements CompilerPassInterface
{
/**
* You can modify the container here before it is dumped to PHP code.
*
* @param ContainerBuilder $container
*
* @api
*/
public function process(ContainerBuilder $container)
{
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));
}
}
}

View File

@@ -20,34 +20,49 @@
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\DependencyInjection\Loader;
namespace Thelia\Core\DependencyInjection;
class CollectionXmlFileLoader {
protected $files = array();
protected $cacheDir;
protected $outputName;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Thelia\Core\Context;
class ContainerAwareAdmin implements ContainerAwareInterface {
/**
* @var ContainerInterface
*
* @api
*/
protected $container;
/**
* Sets the Container.
*
* @param ContainerInterface|null $container A ContainerInterface instance or null
*
* @api
*/
public function setContainer(ContainerInterface $container = null)
public function __construct($cacheDir, $outputName, array $files = array())
{
$container->get('thelia.envContext')->setContext(Context::CONTEXT_BACK_OFFICE);
$this->container = $container;
$this->cacheDir = $cacheDir;
$this->outputName = $outputName;
$this->files = $files;
}
public function addFile($file)
{
$this->files[] = $file;
}
public function process()
{
$pattern = '<import resource="%s" />';
$outputPattern = <<<EOF
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
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">
%s
</routes>
EOF;
$imports = array();
foreach ($this->files as $file) {
$imports[] = sprintf($pattern, $file);
}
$output = sprintf($outputPattern, implode($imports));
file_put_contents($this->cacheDir .'/'. $this->outputName, $output);
}
}

View File

@@ -65,6 +65,8 @@ class XmlFileLoader extends FileLoader
$this->parseForms($xml);
$this->parseRouting($xml);
$this->parseDefinitions($xml, $path);
}
@@ -166,6 +168,30 @@ class XmlFileLoader extends FileLoader
$this->container->setParameter("Thelia.parser.filters", $filterConfig);
}
/**
* parse routing properties
*
* @param SimpleXMLElement $xml
*/
protected function parseRouting(SimpleXMLElement $xml)
{
if (false === $routing = $xml->xpath("//config:routing/config:file")) {
return;
}
try {
$routingConfig = $this->container->getParameter("thelia.routing.files");
} catch (ParameterNotFoundException $e) {
$routingConfig = array();
}
foreach ($routing as $file) {
$routingConfig[] = $file->getAttributeAsPhp("path");
}
$this->container->setParameter("thelia.routing.files", $routingConfig);
}
/**
* parse BaseParams property
*

View File

@@ -16,6 +16,7 @@
<xsd:element name="parameters" type="parameters"/>
<xsd:element name="commands" type="commands"/>
<xsd:element name="forms" type="forms" />
<xsd:element name="routing" type="routing" />
</xsd:choice>
</xsd:complexType>
@@ -54,6 +55,16 @@
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="routing">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="file" type="file"/>
</xsd:choice>
</xsd:complexType>
<xsd:complexType name="file">
<xsd:attribute name="path" type="xsd:string" use="required" />
</xsd:complexType>
<xsd:complexType name="loops">
<xsd:choice minOccurs="0" maxOccurs="unbounded" >
<xsd:element name="loop" type="loop"/>

View File

@@ -39,6 +39,7 @@ use Symfony\Component\Yaml\Yaml;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Thelia\Core\Bundle;
use Thelia\Core\DependencyInjection\Loader\CollectionXmlFileLoader;
use Thelia\Log\Tlog;
use Thelia\Config\DatabaseConfiguration;
use Thelia\Config\DefinePropel;
@@ -149,6 +150,14 @@ class Thelia extends Kernel
$this->loadConfiguration($container);
$container->customCompile();
$collectionFileLoader = new CollectionXmlFileLoader(
$container->getParameter("kernel.cache_dir"),
$container->getParameter("router.xmlFileName"),
$container->getParameter("thelia.routing.files")
);
$collectionFileLoader->process();
return $container;
}
@@ -196,6 +205,7 @@ class Thelia extends Kernel
$parameters["thelia.root_dir"] = THELIA_ROOT;
$parameters["thelia.core_dir"] = THELIA_ROOT . "core/lib/Thelia";
$parameters["thelia.module_dir"] = THELIA_MODULE_DIR;
return $parameters;
}