Skip to content

Commit 5e555d3

Browse files
committed
[DependencyInjection] added an extension for the Symfony Templating Component
1 parent 392ab46 commit 5e555d3

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace Symfony\Components\DependencyInjection\Loader\Extension;
4+
5+
use Symfony\Components\DependencyInjection\Loader\LoaderExtension;
6+
use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
7+
use Symfony\Components\DependencyInjection\BuilderConfiguration;
8+
use Symfony\Components\DependencyInjection\Reference;
9+
10+
/*
11+
* This file is part of the symfony framework.
12+
*
13+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
14+
*
15+
* This source file is subject to the MIT license that is bundled
16+
* with this source code in the file LICENSE.
17+
*/
18+
19+
/**
20+
* SymfonyTemplatingExtension is an extension for the Symfony Templating Component.
21+
*
22+
* @package symfony
23+
* @subpackage dependency_injection
24+
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
25+
*/
26+
class SymfonyTemplatingExtension extends LoaderExtension
27+
{
28+
/**
29+
* Loads the template configuration.
30+
*
31+
* @param array $config A configuration array
32+
*
33+
* @return BuilderConfiguration A BuilderConfiguration instance
34+
*/
35+
public function templatingLoad($config)
36+
{
37+
$configuration = new BuilderConfiguration();
38+
39+
$loader = new XmlFileLoader(__DIR__.'/xml/symfony');
40+
$configuration->merge($loader->load('templating-1.0.xml'));
41+
42+
// path for the filesystem loader
43+
if (isset($config['path']))
44+
{
45+
$configuration->setParameter('symfony.templating.loader.filesystem.path', $config['path']);
46+
}
47+
48+
// loaders
49+
if (isset($config['loader']))
50+
{
51+
$loaders = array();
52+
$ids = is_array($config['loader']) ? $config['loader'] : array($config['loader']);
53+
foreach ($ids as $id)
54+
{
55+
$loaders[] = new Reference($id);
56+
}
57+
}
58+
else
59+
{
60+
$loaders = array(
61+
new Reference('symfony.templating.loader.filesystem'),
62+
);
63+
}
64+
65+
$configuration->setParameter('symfony.templating.loader.chain.loaders', $loaders);
66+
$configuration->setAlias('symfony.templating.loader', 'symfony.templating.loader.chain');
67+
68+
// helpers
69+
if (isset($config['helper']))
70+
{
71+
$helpers = array();
72+
$ids = is_array($config['helper']) ? $config['helper'] : array($config['helper']);
73+
foreach ($ids as $id)
74+
{
75+
$helpers[] = new Reference($id);
76+
}
77+
}
78+
else
79+
{
80+
$helpers = array(
81+
new Reference('symfony.templating.helper.javascripts'),
82+
new Reference('symfony.templating.helper.stylesheets'),
83+
);
84+
}
85+
86+
$configuration->setParameter('symfony.templating.helpers', $helpers);
87+
88+
// cache?
89+
if (isset($config['cache']))
90+
{
91+
// wrap the loader with some cache
92+
$configuration->setAlias('symfony.templating.loader.wrapped', $configuration->getAlias('symfony.templating.loader'));
93+
$configuration->setAlias('symfony.templating.loader', 'symfony.templating.loader.cache');
94+
$configuration->setParameter('symfony.templating.loader.cache.path', $config['cache']);
95+
}
96+
97+
return $configuration;
98+
}
99+
100+
/**
101+
* Returns the namespace to be used for this extension (XML namespace).
102+
*
103+
* @return string The XML namespace
104+
*/
105+
public function getNamespace()
106+
{
107+
return 'http://www.symfony-project.org/schema/symfony';
108+
}
109+
110+
/**
111+
* Returns the recommanded alias to use in XML.
112+
*
113+
* This alias is also the mandatory prefix to use when using YAML.
114+
*
115+
* @return string The alias
116+
*/
117+
public function getAlias()
118+
{
119+
return 'symfony';
120+
}
121+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://www.symfony-project.org/schema/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.symfony-project.org/schema/services http://www.symfony-project.org/schema/services/services-1.0.xsd">
6+
7+
<parameters>
8+
<parameter key="symfony.templating.engine.class">Symfony\Components\Templating\Engine</parameter>
9+
<parameter key="symfony.templating.helperset.class">Symfony\Components\Templating\Helper\HelperSet</parameter>
10+
<parameter key="symfony.templating.loader.filesystem.class">Symfony\Components\Templating\Loader\FilesystemLoader</parameter>
11+
<parameter key="symfony.templating.loader.cache.class">Symfony\Components\Templating\Loader\CacheLoader</parameter>
12+
<parameter key="symfony.templating.loader.Chain.class">Symfony\Components\Templating\Loader\ChainLoader</parameter>
13+
<parameter key="symfony.templating.helper.javascripts.class">Symfony\Components\Templating\Helper\JavascriptsHelper</parameter>
14+
<parameter key="symfony.templating.helper.stylesheets.class">Symfony\Components\Templating\Helper\StylesheetsHelper</parameter>
15+
</parameters>
16+
17+
<services>
18+
<service id="symfony.templating.engine" class="%symfony.templating.engine.class%">
19+
<argument type="service" id="symfony.templating.loader" />
20+
<argument type="collection"></argument>
21+
<argument type="service" id="symfony.templating.helperset" />
22+
</service>
23+
24+
<service id="symfony.templating.helperset" class="%symfony.templating.helperset.class%">
25+
<argument>%symfony.templating.helpers%</argument>
26+
</service>
27+
28+
<service id="symfony.templating.loader.filesystem" class="%symfony.templating.loader.filesystem.class%">
29+
<argument>%symfony.templating.loader.filesystem.path%</argument>
30+
</service>
31+
32+
<service id="symfony.templating.loader.cache" class="%symfony.templating.loader.cache.class%">
33+
<argument type="service" id="symfony.templating.loader.wrapped" />
34+
<argument>%symfony.templating.loader.cache.path%</argument>
35+
</service>
36+
37+
<service id="symfony.templating.loader.chain" class="%symfony.templating.loader.chain.class%">
38+
<argument>%symfony.templating.loader.chain.loaders%</argument>
39+
</service>
40+
41+
<service id="symfony.templating.helper.javascripts" class="%symfony.templating.helper.javascripts.class%" />
42+
<service id="symfony.templating.helper.stylesheets" class="%symfony.templating.helper.stylesheets.class%" />
43+
44+
<service id="symfony.templating.loader" alias="symfony.templating.loader.filesystem" />
45+
</services>
46+
</container>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<xsd:schema xmlns="http://www.symfony-project.org/schema/symfony"
4+
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
5+
targetNamespace="http://www.symfony-project.org/schema/symfony"
6+
elementFormDefault="qualified">
7+
8+
<xsd:element name="templating" type="templating" />
9+
10+
<xsd:complexType name="templating">
11+
<xsd:sequence>
12+
<xsd:element name="loader" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
13+
<xsd:element name="helper" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
14+
</xsd:sequence>
15+
16+
<xsd:attribute name="path" type="xsd:string" />
17+
<xsd:attribute name="cache" type="xsd:string" />
18+
</xsd:complexType>
19+
</xsd:schema>

0 commit comments

Comments
 (0)