1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\MediaBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Processor; |
6
|
|
|
use Symfony\Component\Config\FileLocator; |
7
|
|
|
use Symfony\Component\DependencyInjection\Container; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
11
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
12
|
|
|
use Symfony\Component\Yaml\Yaml; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This is the class that loads and manages your bundle configuration. |
16
|
|
|
* |
17
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
18
|
|
|
*/ |
19
|
|
|
class VictoireMediaExtension extends Extension implements PrependExtensionInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Loads configuration. |
23
|
|
|
* |
24
|
|
|
* @param array $configs Configuration |
25
|
|
|
* @param ContainerBuilder $container Container |
26
|
|
|
*/ |
27
|
|
|
public function load(array $configs, ContainerBuilder $container) |
28
|
|
|
{ |
29
|
|
|
$processor = new Processor(); |
30
|
|
|
$configuration = new Configuration(); |
31
|
|
|
|
32
|
|
|
$processor->processConfiguration($configuration, $configs); |
33
|
|
|
|
34
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
35
|
|
|
|
36
|
|
|
$container->setParameter('twig.form.resources', array_merge( |
37
|
|
|
$container->getParameter('twig.form.resources'), |
38
|
|
|
['VictoireMediaBundle:Form:formWidgets.html.twig'] |
39
|
|
|
)); |
40
|
|
|
|
41
|
|
|
$loader->load('services.yml'); |
42
|
|
|
$loader->load('handlers.yml'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function prepend(ContainerBuilder $container) |
46
|
|
|
{ |
47
|
|
|
if (!$container->hasParameter('victoire_media.upload_dir')) { |
48
|
|
|
$container->setParameter('victoire_media.upload_dir', '/uploads/media/'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$twigConfig['globals']['upload_dir'] = $container->getParameter('victoire_media.upload_dir'); |
|
|
|
|
52
|
|
|
$twigConfig['globals']['mediabundleisactive'] = true; |
53
|
|
|
$twigConfig['globals']['mediamanager'] = '@victoire_media.media_manager'; |
54
|
|
|
$container->prependExtensionConfig('twig', $twigConfig); |
55
|
|
|
|
56
|
|
|
$liipConfig = Yaml::parse(file_get_contents(__DIR__.'/../Resources/config/liip_imagine.yml')); |
57
|
|
|
$container->prependExtensionConfig('liip_imagine', $liipConfig['liip_imagine']); |
58
|
|
|
|
59
|
|
|
$configs = $container->getExtensionConfig($this->getAlias()); |
60
|
|
|
$this->processConfiguration(new Configuration(), $configs); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getAlias() |
67
|
|
|
{ |
68
|
|
|
return 'victoire_media'; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.