Skip to content

Commit 2e4ced3

Browse files
committed
[DI] Add ContainerBuilder::fileExists()
Update TwigExtension
1 parent 10c9d19 commit 2e4ced3

File tree

6 files changed

+76
-33
lines changed

6 files changed

+76
-33
lines changed

src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/DoctrineValidationPass.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16-
use Symfony\Component\Config\Resource\FileResource;
1716

1817
/**
1918
* Registers additional validators.
@@ -60,9 +59,8 @@ private function updateValidatorMappingFiles(ContainerBuilder $container, $mappi
6059

6160
foreach ($container->getParameter('kernel.bundles') as $bundle) {
6261
$reflection = new \ReflectionClass($bundle);
63-
if (is_file($file = dirname($reflection->getFileName()).'/'.$validationPath)) {
62+
if ($container->fileExists($file = dirname($reflection->getFileName()).'/'.$validationPath)) {
6463
$files[] = $file;
65-
$container->addResource(new FileResource($file));
6664
}
6765
}
6866

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+10-22
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use Symfony\Component\DependencyInjection\Reference;
2525
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2626
use Symfony\Component\Config\Resource\FileResource;
27-
use Symfony\Component\Config\Resource\DirectoryResource;
2827
use Symfony\Component\Finder\Finder;
2928
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
3029
use Symfony\Component\Config\FileLocator;
@@ -877,32 +876,28 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
877876
}
878877
$rootDir = $container->getParameter('kernel.root_dir');
879878
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
880-
if (is_dir($dir = $bundle['path'].'/Resources/translations')) {
879+
if ($container->fileExists($dir = $bundle['path'].'/Resources/translations')) {
881880
$dirs[] = $dir;
882881
}
883-
if (is_dir($dir = $rootDir.sprintf('/Resources/%s/translations', $name))) {
882+
if ($container->fileExists($dir = $rootDir.sprintf('/Resources/%s/translations', $name))) {
884883
$dirs[] = $dir;
885884
}
886885
}
887886

888887
foreach ($config['paths'] as $dir) {
889-
if (is_dir($dir)) {
888+
if ($container->fileExists($dir)) {
890889
$dirs[] = $dir;
891890
} else {
892891
throw new \UnexpectedValueException(sprintf('%s defined in translator.paths does not exist or is not a directory', $dir));
893892
}
894893
}
895894

896-
if (is_dir($dir = $rootDir.'/Resources/translations')) {
895+
if ($container->fileExists($dir = $rootDir.'/Resources/translations')) {
897896
$dirs[] = $dir;
898897
}
899898

900899
// Register translation resources
901900
if ($dirs) {
902-
foreach ($dirs as $dir) {
903-
$container->addResource(new DirectoryResource($dir));
904-
}
905-
906901
$files = array();
907902
$finder = Finder::create()
908903
->followLinks()
@@ -1008,19 +1003,16 @@ private function getValidatorMappingFiles(ContainerBuilder $container, array &$f
10081003
foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
10091004
$dirname = $bundle['path'];
10101005

1011-
if (is_file($file = $dirname.'/Resources/config/validation.yml')) {
1006+
if ($container->fileExists($file = $dirname.'/Resources/config/validation.yml', false)) {
10121007
$files['yml'][] = $file;
1013-
$container->addResource(new FileResource($file));
10141008
}
10151009

1016-
if (is_file($file = $dirname.'/Resources/config/validation.xml')) {
1010+
if ($container->fileExists($file = $dirname.'/Resources/config/validation.xml', false)) {
10171011
$files['xml'][] = $file;
1018-
$container->addResource(new FileResource($file));
10191012
}
10201013

1021-
if (is_dir($dir = $dirname.'/Resources/config/validation')) {
1014+
if ($container->fileExists($dir = $dirname.'/Resources/config/validation')) {
10221015
$this->getValidatorMappingFilesFromDir($dir, $files);
1023-
$container->addResource(new DirectoryResource($dir));
10241016
}
10251017
}
10261018
}
@@ -1204,23 +1196,21 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
12041196
foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
12051197
$dirname = $bundle['path'];
12061198

1207-
if (is_file($file = $dirname.'/Resources/config/serialization.xml')) {
1199+
if ($container->fileExists($file = $dirname.'/Resources/config/serialization.xml')) {
12081200
$definition = new Definition('Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader', array($file));
12091201
$definition->setPublic(false);
12101202

12111203
$serializerLoaders[] = $definition;
1212-
$container->addResource(new FileResource($file));
12131204
}
12141205

1215-
if (is_file($file = $dirname.'/Resources/config/serialization.yml')) {
1206+
if ($container->fileExists($file = $dirname.'/Resources/config/serialization.yml', false)) {
12161207
$definition = new Definition('Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader', array($file));
12171208
$definition->setPublic(false);
12181209

12191210
$serializerLoaders[] = $definition;
1220-
$container->addResource(new FileResource($file));
12211211
}
12221212

1223-
if (is_dir($dir = $dirname.'/Resources/config/serialization')) {
1213+
if ($container->fileExists($dir = $dirname.'/Resources/config/serialization')) {
12241214
foreach (Finder::create()->followLinks()->files()->in($dir)->name('*.xml') as $file) {
12251215
$definition = new Definition('Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader', array($file->getPathname()));
12261216
$definition->setPublic(false);
@@ -1233,8 +1223,6 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
12331223

12341224
$serializerLoaders[] = $definition;
12351225
}
1236-
1237-
$container->addResource(new DirectoryResource($dir));
12381226
}
12391227
}
12401228

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Bundle\TwigBundle\DependencyInjection;
1313

1414
use Symfony\Component\Config\FileLocator;
15-
use Symfony\Component\Config\Resource\FileExistenceResource;
1615
use Symfony\Component\DependencyInjection\ContainerBuilder;
1716
use Symfony\Component\DependencyInjection\Reference;
1817
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -108,10 +107,9 @@ public function load(array $configs, ContainerBuilder $container)
108107
}
109108
}
110109

111-
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/views')) {
110+
if ($container->fileExists($dir = $container->getParameter('kernel.root_dir').'/Resources/views', false)) {
112111
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
113112
}
114-
$container->addResource(new FileExistenceResource($dir));
115113

116114
if (!empty($config['globals'])) {
117115
$def = $container->getDefinition('twig');
@@ -164,15 +162,13 @@ private function getBundleHierarchy(ContainerBuilder $container)
164162
);
165163
}
166164

167-
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$name.'/views')) {
165+
if ($container->fileExists($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$name.'/views', false)) {
168166
$bundleHierarchy[$name]['paths'][] = $dir;
169167
}
170-
$container->addResource(new FileExistenceResource($dir));
171168

172-
if (is_dir($dir = $bundle['path'].'/Resources/views')) {
169+
if ($container->fileExists($dir = $bundle['path'].'/Resources/views', false)) {
173170
$bundleHierarchy[$name]['paths'][] = $dir;
174171
}
175-
$container->addResource(new FileExistenceResource($dir));
176172

177173
if (null === $bundle['parent']) {
178174
continue;

src/Symfony/Bundle/TwigBundle/composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"require-dev": {
2727
"symfony/asset": "~2.8|~3.0",
2828
"symfony/stopwatch": "~2.8|~3.0",
29-
"symfony/dependency-injection": "~2.8|~3.0",
29+
"symfony/dependency-injection": "~3.3",
3030
"symfony/expression-language": "~2.8|~3.0",
3131
"symfony/finder": "~2.8|~3.0",
3232
"symfony/form": "~2.8|~3.0",
@@ -36,6 +36,9 @@
3636
"symfony/framework-bundle": "^3.2.2",
3737
"doctrine/annotations": "~1.0"
3838
},
39+
"conflict": {
40+
"symfony/dependency-injection": "<3.3"
41+
},
3942
"autoload": {
4043
"psr-4": { "Symfony\\Bundle\\TwigBundle\\": "" },
4144
"exclude-from-classmap": [

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

+38
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2626
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
2727
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
28+
use Symfony\Component\Config\Resource\DirectoryResource;
29+
use Symfony\Component\Config\Resource\FileExistenceResource;
2830
use Symfony\Component\Config\Resource\FileResource;
2931
use Symfony\Component\Config\Resource\ResourceInterface;
3032
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface;
@@ -1193,6 +1195,42 @@ public static function getServiceConditionals($value)
11931195
return $services;
11941196
}
11951197

1198+
/**
1199+
* Checks whether the requested file or directory exists and registers the result for resource tracking.
1200+
*
1201+
* @param string $path The file or directory path for which to check the existence
1202+
* @param bool|string $trackContents Whether to track contents of the given resource. If a string is passed,
1203+
* it will be used as pattern for tracking contents of the requested directory
1204+
*
1205+
* @return bool
1206+
*
1207+
* @final
1208+
*/
1209+
public function fileExists($path, $trackContents = true)
1210+
{
1211+
$exists = file_exists($path);
1212+
1213+
if (!$this->trackResources) {
1214+
return $exists;
1215+
}
1216+
1217+
if (!$exists) {
1218+
$this->addResource(new FileExistenceResource($path));
1219+
1220+
return $exists;
1221+
}
1222+
1223+
if ($trackContents) {
1224+
if (is_file($path)) {
1225+
$this->addResource(new FileResource($path));
1226+
} else {
1227+
$this->addResource(new DirectoryResource($path, is_string($trackContents) ? $trackContents : null));
1228+
}
1229+
}
1230+
1231+
return $exists;
1232+
}
1233+
11961234
/**
11971235
* Retrieves the currently set proxy instantiator or instantiates one.
11981236
*

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require_once __DIR__.'/Fixtures/includes/ProjectExtension.php';
1616

1717
use Symfony\Component\Config\Resource\ResourceInterface;
18+
use Symfony\Component\Config\Resource\DirectoryResource;
1819
use Symfony\Component\DependencyInjection\Alias;
1920
use Symfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
2021
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
@@ -682,6 +683,25 @@ public function testResources()
682683
$this->assertEquals(array(), $container->getResources());
683684
}
684685

686+
public function testFileExists()
687+
{
688+
$container = new ContainerBuilder();
689+
$a = new FileResource(__DIR__.'/Fixtures/xml/services1.xml');
690+
$b = new FileResource(__DIR__.'/Fixtures/xml/services2.xml');
691+
$c = new DirectoryResource($dir = dirname($b));
692+
693+
$this->assertTrue($container->fileExists((string) $a) && $container->fileExists((string) $b) && $container->fileExists($dir));
694+
695+
$resources = array();
696+
foreach ($container->getResources() as $resource) {
697+
if (false === strpos($resource, '.php')) {
698+
$resources[] = $resource;
699+
}
700+
}
701+
702+
$this->assertEquals(array($a, $b, $c), $resources, '->getResources() returns an array of resources read for the current configuration');
703+
}
704+
685705
public function testExtension()
686706
{
687707
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)