Bläddra i källkod

[DependencyInjection] added an extension for the Symfony Templating Component

Fabien Potencier 15 år sedan
förälder
incheckning
5e555d3d1a

+ 121 - 0
src/Symfony/Components/DependencyInjection/Loader/Extension/SymfonyTemplatingExtension.php

@@ -0,0 +1,121 @@
+<?php
+
+namespace Symfony\Components\DependencyInjection\Loader\Extension;
+
+use Symfony\Components\DependencyInjection\Loader\LoaderExtension;
+use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
+use Symfony\Components\DependencyInjection\BuilderConfiguration;
+use Symfony\Components\DependencyInjection\Reference;
+
+/*
+ * This file is part of the symfony framework.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * SymfonyTemplatingExtension is an extension for the Symfony Templating Component.
+ *
+ * @package    symfony
+ * @subpackage dependency_injection
+ * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class SymfonyTemplatingExtension extends LoaderExtension
+{
+  /**
+   * Loads the template configuration.
+   *
+   * @param array $config A configuration array
+   *
+   * @return BuilderConfiguration A BuilderConfiguration instance
+   */
+  public function templatingLoad($config)
+  {
+    $configuration = new BuilderConfiguration();
+
+    $loader = new XmlFileLoader(__DIR__.'/xml/symfony');
+    $configuration->merge($loader->load('templating-1.0.xml'));
+
+    // path for the filesystem loader
+    if (isset($config['path']))
+    {
+      $configuration->setParameter('symfony.templating.loader.filesystem.path', $config['path']);
+    }
+
+    // loaders
+    if (isset($config['loader']))
+    {
+      $loaders = array();
+      $ids = is_array($config['loader']) ? $config['loader'] : array($config['loader']);
+      foreach ($ids as $id)
+      {
+        $loaders[] = new Reference($id);
+      }
+    }
+    else
+    {
+      $loaders = array(
+        new Reference('symfony.templating.loader.filesystem'),
+      );
+    }
+
+    $configuration->setParameter('symfony.templating.loader.chain.loaders', $loaders);
+    $configuration->setAlias('symfony.templating.loader', 'symfony.templating.loader.chain');
+
+    // helpers
+    if (isset($config['helper']))
+    {
+      $helpers = array();
+      $ids = is_array($config['helper']) ? $config['helper'] : array($config['helper']);
+      foreach ($ids as $id)
+      {
+        $helpers[] = new Reference($id);
+      }
+    }
+    else
+    {
+      $helpers = array(
+        new Reference('symfony.templating.helper.javascripts'),
+        new Reference('symfony.templating.helper.stylesheets'),
+      );
+    }
+
+    $configuration->setParameter('symfony.templating.helpers', $helpers);
+
+    // cache?
+    if (isset($config['cache']))
+    {
+      // wrap the loader with some cache
+      $configuration->setAlias('symfony.templating.loader.wrapped', $configuration->getAlias('symfony.templating.loader'));
+      $configuration->setAlias('symfony.templating.loader', 'symfony.templating.loader.cache');
+      $configuration->setParameter('symfony.templating.loader.cache.path', $config['cache']);
+    }
+
+    return $configuration;
+  }
+
+  /**
+   * Returns the namespace to be used for this extension (XML namespace).
+   *
+   * @return string The XML namespace
+   */
+  public function getNamespace()
+  {
+    return 'http://www.symfony-project.org/schema/symfony';
+  }
+
+  /**
+   * Returns the recommanded alias to use in XML.
+   *
+   * This alias is also the mandatory prefix to use when using YAML.
+   *
+   * @return string The alias
+   */
+  public function getAlias()
+  {
+    return 'symfony';
+  }
+}

+ 46 - 0
src/Symfony/Components/DependencyInjection/Loader/Extension/xml/symfony/templating-1.0.xml

@@ -0,0 +1,46 @@
+<?xml version="1.0" ?>
+
+<container xmlns="http://www.symfony-project.org/schema/services"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://www.symfony-project.org/schema/services http://www.symfony-project.org/schema/services/services-1.0.xsd">
+
+  <parameters>
+    <parameter key="symfony.templating.engine.class">Symfony\Components\Templating\Engine</parameter>
+    <parameter key="symfony.templating.helperset.class">Symfony\Components\Templating\Helper\HelperSet</parameter>
+    <parameter key="symfony.templating.loader.filesystem.class">Symfony\Components\Templating\Loader\FilesystemLoader</parameter>
+    <parameter key="symfony.templating.loader.cache.class">Symfony\Components\Templating\Loader\CacheLoader</parameter>
+    <parameter key="symfony.templating.loader.Chain.class">Symfony\Components\Templating\Loader\ChainLoader</parameter>
+    <parameter key="symfony.templating.helper.javascripts.class">Symfony\Components\Templating\Helper\JavascriptsHelper</parameter>
+    <parameter key="symfony.templating.helper.stylesheets.class">Symfony\Components\Templating\Helper\StylesheetsHelper</parameter>
+  </parameters>
+
+  <services>
+    <service id="symfony.templating.engine" class="%symfony.templating.engine.class%">
+      <argument type="service" id="symfony.templating.loader" />
+      <argument type="collection"></argument>
+      <argument type="service" id="symfony.templating.helperset" />
+    </service>
+
+    <service id="symfony.templating.helperset" class="%symfony.templating.helperset.class%">
+      <argument>%symfony.templating.helpers%</argument>
+    </service>
+
+    <service id="symfony.templating.loader.filesystem" class="%symfony.templating.loader.filesystem.class%">
+      <argument>%symfony.templating.loader.filesystem.path%</argument>
+    </service>
+
+    <service id="symfony.templating.loader.cache" class="%symfony.templating.loader.cache.class%">
+      <argument type="service" id="symfony.templating.loader.wrapped" />
+      <argument>%symfony.templating.loader.cache.path%</argument>
+    </service>
+
+    <service id="symfony.templating.loader.chain" class="%symfony.templating.loader.chain.class%">
+      <argument>%symfony.templating.loader.chain.loaders%</argument>
+    </service>
+
+    <service id="symfony.templating.helper.javascripts" class="%symfony.templating.helper.javascripts.class%" />
+    <service id="symfony.templating.helper.stylesheets" class="%symfony.templating.helper.stylesheets.class%" />
+
+    <service id="symfony.templating.loader" alias="symfony.templating.loader.filesystem" />
+  </services>
+</container>

+ 19 - 0
src/Symfony/Components/DependencyInjection/Loader/schema/symfony/symfony-1.0.xsd

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<xsd:schema xmlns="http://www.symfony-project.org/schema/symfony"
+    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+    targetNamespace="http://www.symfony-project.org/schema/symfony"
+    elementFormDefault="qualified">
+
+  <xsd:element name="templating" type="templating" />
+
+  <xsd:complexType name="templating">
+    <xsd:sequence>
+      <xsd:element name="loader" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
+      <xsd:element name="helper" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
+    </xsd:sequence>
+
+    <xsd:attribute name="path" type="xsd:string" />
+    <xsd:attribute name="cache" type="xsd:string" />
+  </xsd:complexType>
+</xsd:schema>