1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Majora\Bundle\FrameworkExtraBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
6
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
7
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
11
|
|
|
use Symfony\Component\Yaml\Yaml; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Compiler pass which guess and load all memory data stores. |
15
|
|
|
*/ |
16
|
|
|
class InMemoryDataLoadCompilerPass implements CompilerPassInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var FileLocatorInterface |
20
|
|
|
*/ |
21
|
|
|
protected $fileLocator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var OptionsResolver |
25
|
|
|
*/ |
26
|
|
|
protected $optionsResolver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Construct. |
30
|
|
|
* |
31
|
|
|
* @param FileLocator $fileLocator |
32
|
|
|
*/ |
33
|
2 |
|
public function __construct(FileLocatorInterface $fileLocator) |
34
|
|
|
{ |
35
|
2 |
|
$this->fileLocator = $fileLocator; |
36
|
2 |
|
$this->optionsResolver = new OptionsResolver(); |
37
|
2 |
|
$this->optionsResolver->setDefaults(array( |
38
|
2 |
|
'callback' => 'registerData', |
39
|
1 |
|
)); |
40
|
2 |
|
$this->optionsResolver->setDefined('file'); |
41
|
2 |
|
$this->optionsResolver->setDefined('parameter'); |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
2 |
|
public function process(ContainerBuilder $container) |
48
|
|
|
{ |
49
|
2 |
|
$inMemoryFilesTags = $container->findTaggedServiceIds('majora.loader.in_memory'); |
50
|
|
|
|
51
|
2 |
|
foreach ($inMemoryFilesTags as $serviceId => $tags) { |
52
|
|
|
foreach ($tags as $attributes) { |
53
|
|
|
$options = $this->optionsResolver->resolve($attributes); |
54
|
|
|
$loaderDefinition = $container->getDefinition($serviceId); |
55
|
|
|
|
56
|
|
|
// File loading |
57
|
|
|
if (!empty($options['file'])) { |
58
|
|
|
$file = new SplFileInfo( |
59
|
|
|
$this->fileLocator->locate($options['file']), |
|
|
|
|
60
|
|
|
'', |
61
|
|
|
'' |
62
|
|
|
); |
63
|
|
|
$loaderDefinition->addMethodCall($options['callback'], array( |
64
|
|
|
Yaml::parse($file->getContents()), |
65
|
|
|
)); |
66
|
|
|
|
67
|
|
|
$container->addResource(new FileResource($file->getRealPath())); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Parameter loading |
71
|
|
|
if (!empty($options['parameter'])) { |
72
|
|
|
$loaderDefinition->addMethodCall($options['callback'], array( |
73
|
|
|
$container->getParameter($options['parameter']), |
74
|
|
|
)); |
75
|
|
|
$container->getParameterBag()->remove($options['parameter']); |
76
|
|
|
} |
77
|
|
|
} |
78
|
1 |
|
} |
79
|
2 |
|
} |
80
|
|
|
} |
81
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.