forked from liip/LiipImagineBundle
/
FileSystemLoaderFactory.php
165 lines (147 loc) · 5.58 KB
/
FileSystemLoaderFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Liip\ImagineBundle\DependencyInjection\Factory\Loader;
use Liip\ImagineBundle\Exception\InvalidArgumentException;
use Liip\ImagineBundle\Utility\Framework\SymfonyFramework;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class FileSystemLoaderFactory extends AbstractLoaderFactory
{
/**
* {@inheritdoc}
*/
public function create(ContainerBuilder $container, $loaderName, array $config)
{
$definition = $this->getChildLoaderDefinition();
$definition->replaceArgument(2, new Reference(sprintf('liip_imagine.binary.locator.%s', $config['locator'])));
$definition->replaceArgument(3, $this->resolveDataRoots($config['data_root'], $config['bundle_resources'], $container));
return $this->setTaggedLoaderDefinition($loaderName, $definition, $container);
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'filesystem';
}
/**
* {@inheritdoc}
*/
public function addConfiguration(ArrayNodeDefinition $builder)
{
$builder
->children()
->enumNode('locator')
->values(['filesystem', 'filesystem_insecure'])
->info('Using the "filesystem_insecure" locator is not recommended due to a less secure resolver mechanism, but is provided for those using heavily symlinked projects.')
->defaultValue('filesystem')
->end()
->arrayNode('data_root')
->beforeNormalization()
->ifString()
->then(function ($value) {
return [$value];
})
->end()
->treatNullLike([])
->treatFalseLike([])
->defaultValue([SymfonyFramework::getContainerResolvableRootWebPath()])
->prototype('scalar')
->cannotBeEmpty()
->end()
->end()
->arrayNode('bundle_resources')
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')
->defaultFalse()
->end()
->enumNode('access_control_type')
->values(['blacklist', 'whitelist'])
->info('Sets the access control method applied to bundle names in "access_control_list" into a blacklist or whitelist.')
->defaultValue('blacklist')
->end()
->arrayNode('access_control_list')
->defaultValue([])
->prototype('scalar')
->cannotBeEmpty()
->end()
->end()
->end()
->end()
->end();
}
/*
* @param string[] $staticPaths
* @param array $config
* @param ContainerBuilder $container
*
* @return string[]
*/
private function resolveDataRoots(array $staticPaths, array $config, ContainerBuilder $container)
{
if (false === $config['enabled']) {
return $staticPaths;
}
$resourcePaths = [];
foreach ($this->getBundleResourcePaths($container) as $name => $path) {
if (('whitelist' === $config['access_control_type']) === in_array($name, $config['access_control_list'], true) && is_dir($path)) {
$resourcePaths[$name] = $path;
}
}
return array_merge($staticPaths, $resourcePaths);
}
/**
* @param ContainerBuilder $container
*
* @return string[]
*/
private function getBundleResourcePaths(ContainerBuilder $container)
{
if ($container->hasParameter('kernel.bundles_metadata')) {
$paths = $this->getBundlePathsUsingMetadata($container->getParameter('kernel.bundles_metadata'));
} else {
$paths = $this->getBundlePathsUsingNamedObj($container->getParameter('kernel.bundles'));
}
return array_map(function ($path) {
return $path.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'public';
}, $paths);
}
/**
* @param array[] $metadata
*
* @return string[]
*/
private function getBundlePathsUsingMetadata(array $metadata)
{
return array_combine(array_keys($metadata), array_map(function ($data) {
return $data['path'];
}, $metadata));
}
/**
* @param string[] $classes
*
* @return string[]
*/
private function getBundlePathsUsingNamedObj(array $classes)
{
$paths = [];
foreach ($classes as $c) {
try {
$r = new \ReflectionClass($c);
} catch (\ReflectionException $exception) {
throw new InvalidArgumentException(sprintf('Unable to resolve bundle "%s" while auto-registering bundle resource paths.', $c), null, $exception);
}
$paths[$r->getShortName()] = dirname($r->getFileName());
}
return $paths;
}
}