From a1b391fb0025cb8f76a917f690a39035dc446db0 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Fri, 1 Sep 2017 17:54:17 -0400 Subject: [PATCH] Add default templates directory and option to configure it --- src/Symfony/Bundle/TwigBundle/CHANGELOG.md | 1 + .../DependencyInjection/Configuration.php | 4 ++++ .../DependencyInjection/TwigExtension.php | 14 ++++++++++++-- .../Resources/config/schema/twig-1.0.xsd | 1 + .../DependencyInjection/Fixtures/php/full.php | 1 + .../templates/bundles/TwigBundle/layout.html.twig | 1 + .../Fixtures/templates/layout.html.twig | 1 + .../DependencyInjection/Fixtures/xml/full.xml | 2 +- .../DependencyInjection/Fixtures/yml/full.yml | 1 + .../DependencyInjection/TwigExtensionTest.php | 2 ++ 10 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/templates/bundles/TwigBundle/layout.html.twig create mode 100644 src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/templates/layout.html.twig diff --git a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md index 58a6a72296..8ba6647306 100644 --- a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGELOG * deprecated `Symfony\Bundle\TwigBundle\Command\DebugCommand`, use `Symfony\Bridge\Twig\Command\DebugCommand` instead * deprecated relying on the `ContainerAwareInterface` implementation for `Symfony\Bundle\TwigBundle\Command\LintCommand` + * added option to configure default path templates (via `default_path`) 3.3.0 ----- diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php index 4880d2534d..f4488c0d5c 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php @@ -130,6 +130,10 @@ class Configuration implements ConfigurationInterface ->booleanNode('strict_variables')->end() ->scalarNode('auto_reload')->end() ->integerNode('optimizations')->min(-1)->end() + ->scalarNode('default_path') + ->info('The default path used to load templates') + ->defaultValue('%kernel.project_dir%/templates') + ->end() ->arrayNode('paths') ->normalizeKeys(false) ->useAttributeAsKey('paths') diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php index ca0c68d49b..718767324a 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php @@ -109,7 +109,7 @@ class TwigExtension extends Extension $container->getDefinition('twig.cache_warmer')->replaceArgument(2, $config['paths']); $container->getDefinition('twig.template_iterator')->replaceArgument(2, $config['paths']); - $bundleHierarchy = $this->getBundleHierarchy($container); + $bundleHierarchy = $this->getBundleHierarchy($container, $config); foreach ($bundleHierarchy as $name => $bundle) { $namespace = $this->normalizeBundleName($name); @@ -130,6 +130,11 @@ class TwigExtension extends Extension } $container->addResource(new FileExistenceResource($dir)); + if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']))) { + $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir)); + } + $container->addResource(new FileExistenceResource($dir)); + if (!empty($config['globals'])) { $def = $container->getDefinition('twig'); foreach ($config['globals'] as $key => $global) { @@ -174,7 +179,7 @@ class TwigExtension extends Extension } } - private function getBundleHierarchy(ContainerBuilder $container) + private function getBundleHierarchy(ContainerBuilder $container, array $config) { $bundleHierarchy = array(); @@ -192,6 +197,11 @@ class TwigExtension extends Extension } $container->addResource(new FileExistenceResource($dir)); + if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name)) { + $bundleHierarchy[$name]['paths'][] = $dir; + } + $container->addResource(new FileExistenceResource($dir)); + if (file_exists($dir = $bundle['path'].'/Resources/views')) { $bundleHierarchy[$name]['paths'][] = $dir; } diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd b/src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd index 61b57798b2..3e491f7029 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd @@ -26,6 +26,7 @@ + diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/php/full.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/php/full.php index 9385131730..839d037fdc 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/php/full.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/php/full.php @@ -17,6 +17,7 @@ $container->loadFromExtension('twig', array( 'charset' => 'ISO-8859-1', 'debug' => true, 'strict_variables' => true, + 'default_path' => '%kernel.root_dir%/templates', 'paths' => array( 'path1', 'path2', diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/templates/bundles/TwigBundle/layout.html.twig b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/templates/bundles/TwigBundle/layout.html.twig new file mode 100644 index 0000000000..bb07ecfe55 --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/templates/bundles/TwigBundle/layout.html.twig @@ -0,0 +1 @@ +This is a layout diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/templates/layout.html.twig b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/templates/layout.html.twig new file mode 100644 index 0000000000..bb07ecfe55 --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/templates/layout.html.twig @@ -0,0 +1 @@ +This is a layout diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/xml/full.xml b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/xml/full.xml index 14c95af97e..702e34617d 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/xml/full.xml +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/xml/full.xml @@ -6,7 +6,7 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd"> - + MyBundle::form.html.twig @@qux diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/yml/full.yml b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/yml/full.yml index 68ecf463f0..07d4abe767 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/yml/full.yml +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/yml/full.yml @@ -13,6 +13,7 @@ twig: charset: ISO-8859-1 debug: true strict_variables: true + default_path: '%kernel.root_dir%/templates' paths: path1: '' path2: '' diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php index cdd6860294..9d47948d67 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php @@ -196,6 +196,7 @@ class TwigExtensionTest extends TestCase array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'Twig'), array(__DIR__.'/Fixtures/Bundle/ChildTwigBundle/Resources/views', 'Twig'), array(__DIR__.'/Fixtures/Resources/TwigBundle/views', 'Twig'), + array(__DIR__.'/Fixtures/templates/bundles/TwigBundle', 'Twig'), array(realpath(__DIR__.'/../..').'/Resources/views', 'Twig'), array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildTwig'), array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildTwig'), @@ -205,6 +206,7 @@ class TwigExtensionTest extends TestCase array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildChildTwig'), array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'ChildChildTwig'), array(__DIR__.'/Fixtures/Resources/views'), + array(__DIR__.'/Fixtures/templates'), ), $paths); }