Преглед на файлове

[FrameworkBundle] updated configuration

Johannes Schmitt преди 14 години
родител
ревизия
d29c7811aa

+ 22 - 0
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

@@ -55,6 +55,7 @@ class Configuration implements ConfigurationInterface
         $this->addTemplatingSection($rootNode);
         $this->addTranslatorSection($rootNode);
         $this->addValidationSection($rootNode);
+        $this->addAnnotationsSection($rootNode);
 
         return $treeBuilder;
     }
@@ -253,4 +254,25 @@ class Configuration implements ConfigurationInterface
             ->end()
         ;
     }
+
+    private function addAnnotationsSection(ArrayNodeDefinition $rootNode)
+    {
+        $rootNode
+            ->children()
+                ->arrayNode('annotations')
+                    ->addDefaultsIfNotSet()
+                    ->children()
+                        ->scalarNode('cache')->defaultValue('file')->end()
+                        ->arrayNode('file_cache')
+                            ->addDefaultsIfNotSet()
+                            ->children()
+                                ->scalarNode('dir')->defaultValue('%kernel.cache_dir%/annotations')->end()
+                                ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
+                            ->end()
+                        ->end()
+                    ->end()
+                ->end()
+            ->end()
+        ;
+    }
 }

+ 29 - 0
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

@@ -11,6 +11,8 @@
 
 namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
 
+use Symfony\Component\Config\Loader\LoaderInterface;
+
 use Symfony\Component\DependencyInjection\ContainerBuilder;
 use Symfony\Component\DependencyInjection\Definition;
 use Symfony\Component\DependencyInjection\Parameter;
@@ -119,6 +121,8 @@ class FrameworkExtension extends Extension
             $this->registerValidationConfiguration($config['validation'], $container, $loader);
         }
 
+        $this->registerAnnotationsConfiguration($config['annotations'], $container, $loader);
+
         $this->addClassesToCompile(array(
             'Symfony\\Component\\HttpFoundation\\ParameterBag',
             'Symfony\\Component\\HttpFoundation\\HeaderBag',
@@ -532,6 +536,31 @@ class FrameworkExtension extends Extension
         return $files;
     }
 
+    private function registerAnnotationsConfiguration(array $config, ContainerBuilder $container,$loader)
+    {
+        $loader->load('annotations.xml');
+
+        if ('file' === $config['cache']) {
+            $cacheDir = $container->getParameterBag()->resolveValue($config['file_cache']['dir']);
+            if (!is_dir($cacheDir) && false === @mkdir($cacheDir, 0777, true)) {
+                throw new \RuntimeException(sprintf('Could not create cache directory "%s".', $cacheDir));
+            }
+
+            $container
+                ->getDefinition('annotations.cache.file_cache')
+                ->replaceArgument(0, $cacheDir)
+                ->replaceArgument(1, $config['file_cache']['debug'])
+            ;
+        } else if ('none' === $config['cache']) {
+            $container->setAlias('annotation_reader', 'annotations.reader');
+        } else {
+            $container
+                ->getDefinition('annotations.cached_reader')
+                ->replaceArgument(1, new Reference($config['cache']))
+            ;
+        }
+    }
+
     /**
      * Returns the base path for the XSD files.
      *

+ 29 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml

@@ -0,0 +1,29 @@
+<?xml version="1.0" ?>
+
+<container xmlns="http://symfony.com/schema/dic/services"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
+
+    <parameters>
+        <parameter key="annotations.cache.file_cache.class">Annotations\Cache\FileCache</parameter>
+
+        <parameter key="annotations.reader.class">Annotations\Reader</parameter>
+        <parameter key="annotations.cached_reader.class">Annotations\CachedReader</parameter>
+    </parameters>
+
+    <services>
+        <service id="annotations.cache.file_cache" class="%annotations.cache.file_cache.class%" public="false">
+            <argument /><!-- Cache Directory -->
+            <argument /><!-- Debug-Flag -->
+        </service>
+
+        <service id="annotations.reader" class="%annotations.reader.class%" public="false" />
+
+        <service id="annotations.cached_reader" class="%annotations.cached_reader.class%" public="false">
+            <argument type="service" id="annotations.reader" />
+            <argument type="service" id="annotations.cache.file_cache" />
+        </service>
+
+        <service id="annotation_reader" alias="annotations.cached_reader" />
+    </services>
+</container>

+ 14 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

@@ -17,6 +17,7 @@
             <xsd:element name="templating" type="templating" minOccurs="0" maxOccurs="1" />
             <xsd:element name="translator" type="translator" minOccurs="0" maxOccurs="1" />
             <xsd:element name="validation" type="validation" minOccurs="0" maxOccurs="1" />
+            <xsd:element name="annotations" type="annotations" minOccurs="0" maxOccurs="1" />
         </xsd:all>
 
         <xsd:attribute name="cache-warmer" type="cache_warmer" />
@@ -120,4 +121,17 @@
         <xsd:attribute name="cache" type="xsd:string" />
         <xsd:attribute name="enable-annotations" type="xsd:boolean" />
     </xsd:complexType>
+    
+    <xsd:complexType name="annotations">
+        <xsd:sequence>
+            <xsd:element name="file-cache" type="annotations.file_cache" minOccurs="0" maxOccurs="1" />
+        </xsd:sequence>
+
+        <xsd:attribute name="cache" type="xsd:string" />
+    </xsd:complexType>
+    
+    <xsd:complexType name="annotations.file_cache">
+        <xsd:attribute name="dir" type="xsd:string" />
+        <xsd:attribute name="debug" type="xsd:string" />
+    </xsd:complexType>
 </xsd:schema>

+ 0 - 4
src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml

@@ -11,7 +11,6 @@
         <parameter key="filesystem.class">Symfony\Component\HttpKernel\Util\Filesystem</parameter>
         <parameter key="cache_warmer.class">Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate</parameter>
         <parameter key="file_locator.class">Symfony\Component\HttpKernel\Config\FileLocator</parameter>
-        <parameter key="annotation_reader.class">Annotations\Reader</parameter>
     </parameters>
 
     <services>
@@ -52,8 +51,5 @@
             <argument type="service" id="kernel" />
             <argument>%kernel.root_dir%/Resources</argument>
         </service>
-        
-        <service id="annotation_reader" class="%annotation_reader.class%" public="false">
-        </service>
     </services>
 </container>

+ 7 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php

@@ -56,4 +56,11 @@ $container->loadFromExtension('framework', array(
         'enabled' => true,
         'cache'   => 'apc',
     ),
+    'annotations' => array(
+        'cache' => 'file',
+        'file_cache' => array(
+            'dir'   => '%kernel.cache_dir%/annotations',
+            'debug' => true,
+        )
+    ),
 ));

+ 3 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml

@@ -30,5 +30,8 @@
         </framework:templating>
         <framework:translator enabled="true" fallback="fr" />
         <framework:validation enabled="true" cache="apc" />
+        <framework:annotations cache="file">
+            <framework:file-cache dir="%kernel.cache_dir%/annotations" debug="true" />
+        </framework:annotations>
     </framework:config>
 </container>

+ 5 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml

@@ -42,3 +42,8 @@ framework:
     validation:
         enabled: true
         cache:   apc
+    annotations:
+        cache:   file
+        file_cache:
+            dir:   %kernel.cache_dir%/annotations
+            debug: true

+ 8 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

@@ -161,6 +161,14 @@ abstract class FrameworkExtensionTest extends TestCase
         );
     }
 
+    public function testAnnotations()
+    {
+        $container = $this->createContainerFromFile('full');
+
+        $this->assertEquals($container->getParameter('kernel.cache_dir').'/annotations', $container->getDefinition('annotations.cache.file_cache')->getArgument(0));
+        $this->assertEquals('annotations.cached_reader', (string) $container->getAlias('annotation_reader'));
+    }
+
     public function testValidationAnnotations()
     {
         $container = $this->createContainerFromFile('validation_annotations');