1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Sulu. |
5
|
|
|
* |
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sulu\Bundle\ActivityLogBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
15
|
|
|
use Symfony\Component\Config\FileLocator; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
18
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This is the class that loads and manages activity log bundle configuration. |
23
|
|
|
*/ |
24
|
|
|
class SuluActivityLogExtension extends Extension |
25
|
|
|
{ |
26
|
|
|
const STORAGE_ELASTIC = 'elastic'; |
27
|
|
|
const STORAGE_ARRAY = 'array'; |
28
|
|
|
const STORAGE_CUSTOM = 'custom'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
1 |
|
public function load(array $configs, ContainerBuilder $container) |
34
|
|
|
{ |
35
|
1 |
|
$configuration = new Configuration(); |
36
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
37
|
|
|
|
38
|
1 |
|
switch ($config['storage']) { |
39
|
1 |
|
case self::STORAGE_ELASTIC: |
40
|
|
|
$id = $this->initElasticSearch($config['storages']['elastic'], $container); |
41
|
|
|
break; |
42
|
|
|
|
43
|
1 |
|
case self::STORAGE_ARRAY: |
44
|
1 |
|
$id = $this->initArray($config['storages']['array'], $container); |
45
|
1 |
|
break; |
46
|
|
|
|
47
|
|
|
case self::STORAGE_CUSTOM: |
48
|
|
|
$id = $this->initCustom($config['storages']['custom'], $container); |
49
|
|
|
break; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
$container->setAlias('sulu_activity_log.activity_log_storage', $id); |
|
|
|
|
53
|
|
|
|
54
|
1 |
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
55
|
1 |
|
$loader->load('services.xml'); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $config |
60
|
|
|
* @param ContainerBuilder $container |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
private function initElasticSearch(array $config, ContainerBuilder $container) |
65
|
|
|
{ |
66
|
|
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
67
|
|
|
$loader->load('storages/elastic.xml'); |
68
|
|
|
|
69
|
|
|
if (!$config['ongr_manager']) { |
70
|
|
|
$error = new InvalidConfigurationException(); |
71
|
|
|
$error->setPath('sulu_activity_log.storages.elastic.ongr_manager'); |
72
|
|
|
|
73
|
|
|
throw $error; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$container->getDefinition('sulu_activity_log.storage.elastic')->replaceArgument( |
77
|
|
|
0, |
78
|
|
|
new Reference('es.manager.' . $config['ongr_manager']) |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
return 'sulu_activity_log.storage.elastic'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array $config |
86
|
|
|
* @param ContainerBuilder $container |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
1 |
|
private function initArray(array $config, ContainerBuilder $container) |
91
|
|
|
{ |
92
|
1 |
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
93
|
1 |
|
$loader->load('storages/array.xml'); |
94
|
|
|
|
95
|
1 |
|
return 'sulu_activity_log.storage.array'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $config |
100
|
|
|
* @param ContainerBuilder $container |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
private function initCustom(array $config, ContainerBuilder $container) |
105
|
|
|
{ |
106
|
|
|
if (!$config['id']) { |
107
|
|
|
$error = new InvalidConfigurationException(); |
108
|
|
|
$error->setPath('sulu_activity_log.storages.custom.id'); |
109
|
|
|
|
110
|
|
|
throw $error; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $config['id']; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: