1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the XabbuhPandaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Flothmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xabbuh\PandaBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\FileLocator; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
17
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
19
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* XabbuhPandaExtension. |
23
|
|
|
* |
24
|
|
|
* @author Christian Flothmann <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class XabbuhPandaExtension extends Extension |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritDoc} |
30
|
|
|
*/ |
31
|
4 |
|
public function load(array $configs, ContainerBuilder $container) |
32
|
|
|
{ |
33
|
4 |
|
$configuration = new Configuration(); |
34
|
4 |
|
$config = $this->processConfiguration($configuration, $configs); |
35
|
|
|
|
36
|
4 |
|
$container->setParameter( |
37
|
4 |
|
'xabbuh_panda.video_uploader.multiple_files', |
38
|
4 |
|
$config['video_uploader']['multiple_files'] |
39
|
4 |
|
); |
40
|
4 |
|
$container->setParameter( |
41
|
4 |
|
'xabbuh_panda.video_uploader.cancel_button', |
42
|
4 |
|
$config['video_uploader']['cancel_button'] |
43
|
4 |
|
); |
44
|
4 |
|
$container->setParameter( |
45
|
4 |
|
'xabbuh_panda.video_uploader.progress_bar', |
46
|
4 |
|
$config['video_uploader']['progress_bar'] |
47
|
4 |
|
); |
48
|
|
|
|
49
|
4 |
|
$container->setParameter('xabbuh_panda.account.default', $config['default_account']); |
50
|
4 |
|
$container->setParameter('xabbuh_panda.cloud.default', $config['default_cloud']); |
51
|
|
|
|
52
|
|
|
// and load the service definitions |
53
|
4 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
54
|
4 |
|
$loader->load('account_manager.xml'); |
55
|
4 |
|
$loader->load('cloud_manager.xml'); |
56
|
4 |
|
$loader->load('cloud_factory.xml'); |
57
|
4 |
|
$loader->load('controller.xml'); |
58
|
4 |
|
$loader->load('transformers.xml'); |
59
|
4 |
|
$loader->load('video_uploader_extension.xml'); |
60
|
|
|
|
61
|
|
|
// Add the tag for the form type extension without using deprecated APIs |
62
|
4 |
|
if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) { |
63
|
4 |
|
$extendedType = 'Symfony\Component\Form\Extension\Core\Type\FileType'; |
64
|
4 |
|
} else { |
65
|
|
|
$extendedType = 'file'; |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
$container->getDefinition('xabbuh_panda.video_uploader_extension') |
69
|
4 |
|
->addTag('form.type_extension', array('alias' => $extendedType)); |
70
|
|
|
|
71
|
4 |
|
$this->loadAccounts($config['accounts'], $container); |
72
|
4 |
|
$this->loadClouds($config['clouds'], $container); |
73
|
4 |
|
$this->registerSerializerFactory($container); |
74
|
4 |
|
} |
75
|
|
|
|
76
|
4 |
|
private function loadAccounts(array $accounts, ContainerBuilder $container) |
77
|
|
|
{ |
78
|
4 |
|
$accountManagerDefinition = $container->getDefinition('xabbuh_panda.account_manager'); |
79
|
|
|
|
80
|
4 |
|
foreach ($accounts as $name => $accountConfig) { |
81
|
|
|
// register each account as a service |
82
|
1 |
|
$accountDefinition = new Definition( |
83
|
1 |
|
'Xabbuh\PandaClient\Api\Account', |
84
|
|
|
array( |
85
|
1 |
|
$accountConfig['access_key'], |
86
|
1 |
|
$accountConfig['secret_key'], |
87
|
1 |
|
$accountConfig['api_host'] |
88
|
1 |
|
) |
89
|
1 |
|
); |
90
|
1 |
|
$id = 'xabbuh_panda.'.strtr($name, ' -', '_').'_account'; |
91
|
1 |
|
$container->setDefinition($id, $accountDefinition); |
92
|
|
|
|
93
|
|
|
// and pass it to the manager's registerAccount() method |
94
|
1 |
|
$accountManagerDefinition->addMethodCall( |
95
|
1 |
|
'registerAccount', |
96
|
1 |
|
array($name, new Reference($id)) |
97
|
1 |
|
); |
98
|
4 |
|
} |
99
|
4 |
|
} |
100
|
|
|
|
101
|
4 |
|
private function loadClouds(array $clouds, ContainerBuilder $container) |
102
|
|
|
{ |
103
|
4 |
|
$cloudManagerDefinition = $container->getDefinition('xabbuh_panda.cloud_manager'); |
104
|
|
|
|
105
|
4 |
|
foreach ($clouds as $name => $cloudConfig) { |
106
|
|
|
// register each cloud as a service |
107
|
2 |
|
$cloudDefinition = new Definition( |
108
|
2 |
|
'Xabbuh\PandaClient\Api\Cloud', |
109
|
|
|
array( |
110
|
2 |
|
$cloudConfig['id'], |
111
|
2 |
|
isset($cloudConfig['account']) ? $cloudConfig['account'] : null, |
112
|
|
|
) |
113
|
2 |
|
); |
114
|
|
|
|
115
|
2 |
|
if (method_exists($cloudDefinition, 'setFactory')) { |
116
|
2 |
|
$cloudDefinition->setFactory(array(new Reference('xabbuh_panda.cloud_factory'), 'get')); |
117
|
2 |
|
} else { |
118
|
|
|
$cloudDefinition->setFactoryService('xabbuh_panda.cloud_factory'); |
119
|
|
|
$cloudDefinition->setFactoryMethod('get'); |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
$id = 'xabbuh_panda.'.strtr($name, ' -', '_').'_cloud'; |
123
|
2 |
|
$container->setDefinition($id, $cloudDefinition); |
124
|
|
|
|
125
|
|
|
// and pass it to the manager's registerAccount() method |
126
|
2 |
|
$cloudManagerDefinition->addMethodCall( |
127
|
2 |
|
'registerCloud', |
128
|
2 |
|
array($name, new Reference($id)) |
129
|
2 |
|
); |
130
|
4 |
|
} |
131
|
4 |
|
} |
132
|
|
|
|
133
|
4 |
|
private function registerSerializerFactory(ContainerBuilder $container) |
134
|
|
|
{ |
135
|
|
|
$serializers = array( |
136
|
4 |
|
'xabbuh_panda.serializer.cloud' => 'getCloudSerializer', |
137
|
4 |
|
'xabbuh_panda.serializer.encoding' => 'getEncodingSerializer', |
138
|
4 |
|
'xabbuh_panda.serializer.profile' => 'getProfileSerializer', |
139
|
4 |
|
'xabbuh_panda.serializer.video' => 'getVideoSerializer', |
140
|
4 |
|
); |
141
|
|
|
|
142
|
4 |
|
foreach ($serializers as $serviceId => $factoryMethod) { |
143
|
4 |
|
$definition = $container->getDefinition($serviceId); |
144
|
|
|
|
145
|
4 |
|
if (method_exists($definition, 'setFactory')) { |
146
|
4 |
|
$definition->setFactory(array('%xabbuh_panda.serializer.factory.class%', $factoryMethod)); |
147
|
4 |
|
} else { |
148
|
|
|
$definition->setFactoryClass('%xabbuh_panda.serializer.factory.class%'); |
149
|
|
|
$definition->setFactoryMethod($factoryMethod); |
150
|
|
|
} |
151
|
4 |
|
} |
152
|
4 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritDoc} |
156
|
|
|
*/ |
157
|
4 |
|
public function getNamespace() |
158
|
|
|
{ |
159
|
4 |
|
return 'http://xabbuh.de/schema/dic/xabbuh/panda'; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritDoc} |
164
|
|
|
*/ |
165
|
|
|
public function getXsdValidationBasePath() |
166
|
|
|
{ |
167
|
|
|
return __DIR__.'/../Resources/config/schema'; |
|
|
|
|
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.