Merge branch 'routing'

This commit is contained in:
Manuel Raynaud
2013-08-14 08:23:59 +02:00
5 changed files with 46 additions and 80 deletions

View File

@@ -13,11 +13,6 @@
<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"/>
@@ -35,30 +30,40 @@
<service id="router.admin.fileLocator" class="Symfony\Component\Config\FileLocator">
<argument>%kernel.cache_dir%</argument>
<argument>%thelia.core_dir%/Config/Resources/routing</argument>
</service>
<service id="router.xmlLoader" class="Symfony\Component\Routing\Loader\XmlFileLoader">
<argument type="service" id="router.admin.fileLocator"/>
</service>
<service id="router" class="%router.class%">
<service id="router.admin" class="%router.class%">
<argument type="service" id="router.xmlLoader"/>
<argument>%router.xmlFileName%</argument>
<argument>admin.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="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,6 +59,7 @@ class TheliaBundle extends Bundle
$container
->addCompilerPass(new RegisterListenersPass())
->addCompilerPass(new RegisterParserPluginPass())
->addCompilerPass(new RegisterRouterPass())
;
}

View File

@@ -20,49 +20,43 @@
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\DependencyInjection\Loader;
namespace Thelia\Core\DependencyInjection\Compiler;
class CollectionXmlFileLoader {
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
protected $files = array();
protected $cacheDir;
protected $outputName;
class RegisterRouterPass implements CompilerPassInterface
{
public function __construct($cacheDir, $outputName, array $files = array())
/**
* You can modify the container here before it is dumped to PHP code.
*
* @param ContainerBuilder $container
*
* @api
*/
public function process(ContainerBuilder $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);
try {
$chainRouter = $container->getDefinition("router.chainRequest");
} catch(InvalidArgumentException $e) {
return;
}
foreach ($container->findTaggedServiceIds("router.register") as $id => $attributes) {
$priority = isset($attributes[0]["priority"]) ? $attributes[0]["priority"] : 0;
$router = $container->getDefinition($id);
$output = sprintf($outputPattern, implode($imports));
file_put_contents($this->cacheDir .'/'. $this->outputName, $output);
$router->addMethodCall("setOption", array("matcher_cache_class", $container::camelize("ProjectUrlMatcher".$id)));
$chainRouter->addMethodCall("add", array(new Reference($id), $priority));
}
}
}

View File

@@ -65,8 +65,6 @@ class XmlFileLoader extends FileLoader
$this->parseForms($xml);
$this->parseRouting($xml);
$this->parseDefinitions($xml, $path);
}
@@ -168,30 +166,6 @@ 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

@@ -150,14 +150,6 @@ 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;
}