Completed
Push — foshttpcache_remove ( fa6c21 )
by André
19:18 queued 02:14
created

EzPublishCoreExtension::load()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 33
nc 6
nop 2
dl 0
loc 57
rs 9.0309
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * File containing the EzPublishCoreExtension class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection;
10
11
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigParser;
12
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\ConfigurationProcessor;
13
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Suggestion\Collector\SuggestionCollector;
14
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Suggestion\Collector\SuggestionCollectorAwareInterface;
15
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Suggestion\Formatter\YamlSuggestionFormatter;
16
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Security\PolicyProvider\PoliciesConfigBuilder;
17
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Security\PolicyProvider\PolicyProviderInterface;
18
use eZ\Bundle\EzPublishCoreBundle\SiteAccess\SiteAccessConfigurationFilter;
19
use Symfony\Component\Config\Resource\FileResource;
20
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
21
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
22
use Symfony\Component\DependencyInjection\ContainerBuilder;
23
use Symfony\Component\DependencyInjection\Loader;
24
use Symfony\Component\DependencyInjection\Loader\FileLoader;
25
use Symfony\Component\Config\FileLocator;
26
use InvalidArgumentException;
27
use Symfony\Component\Yaml\Yaml;
28
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface;
29
30
class EzPublishCoreExtension extends Extension implements PrependExtensionInterface
31
{
32
    /**
33
     * @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Suggestion\Collector\SuggestionCollector
34
     */
35
    private $suggestionCollector;
36
37
    /**
38
     * @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface
39
     */
40
    private $mainConfigParser;
41
42
    /**
43
     * @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface[]
44
     */
45
    private $configParsers;
46
47
    /**
48
     * @var PolicyProviderInterface[]
49
     */
50
    private $policyProviders = [];
51
52
    /**
53
     * Holds a collection of YAML files, as an array with directory path as a
54
     * key to the array of contained file names.
55
     *
56
     * @var array
57
     */
58
    private $defaultSettingsCollection = [];
59
60
    /**
61
     * @var \eZ\Bundle\EzPublishCoreBundle\SiteAccess\SiteAccessConfigurationFilter[]
62
     */
63
    private $siteaccessConfigurationFilters = [];
64
65
    public function __construct(array $configParsers = array())
66
    {
67
        $this->configParsers = $configParsers;
68
        $this->suggestionCollector = new SuggestionCollector();
69
    }
70
71
    public function getAlias()
72
    {
73
        return 'ezpublish';
74
    }
75
76
    /**
77
     * Loads a specific configuration.
78
     *
79
     * @param mixed[] $configs An array of configuration values
80
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container A ContainerBuilder instance
81
     *
82
     * @throws \InvalidArgumentException When provided tag is not defined in this extension
83
     *
84
     * @api
85
     */
86
    public function load(array $configs, ContainerBuilder $container)
87
    {
88
        $loader = new Loader\YamlFileLoader(
89
            $container,
90
            new FileLocator(__DIR__ . '/../Resources/config')
91
        );
92
93
        $configuration = $this->getConfiguration($configs, $container);
94
95
        // Note: this is where the transformation occurs
96
        $config = $this->processConfiguration($configuration, $configs);
97
98
        // Base services and services overrides
99
        $loader->load('services.yml');
100
        // Security services
101
        $loader->load('security.yml');
102
103
        if (interface_exists('FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface')) {
104
            $loader->load('routing/js_routing.yml');
105
        }
106
107
        // Default settings
108
        $this->handleDefaultSettingsLoading($container, $loader);
109
110
        $this->registerRepositoriesConfiguration($config, $container);
111
        $this->registerSiteAccessConfiguration($config, $container);
112
        $this->registerImageMagickConfiguration($config, $container);
113
        $this->registerPageConfiguration($config, $container);
114
115
        // Routing
116
        $this->handleRouting($config, $container, $loader);
117
        // Public API loading
118
        $this->handleApiLoading($container, $loader);
119
        $this->handleTemplating($container, $loader);
120
        $this->handleSessionLoading($container, $loader);
121
        $this->handleCache($config, $container, $loader);
122
        $this->handleLocale($config, $container, $loader);
123
        $this->handleHelpers($config, $container, $loader);
124
        $this->handleImage($config, $container, $loader);
125
126
        // Map settings
127
        $processor = new ConfigurationProcessor($container, 'ezsettings');
128
        $processor->mapConfig($config, $this->getMainConfigParser());
129
130
        if ($this->suggestionCollector->hasSuggestions()) {
131
            $message = '';
132
            $suggestionFormatter = new YamlSuggestionFormatter();
133
            foreach ($this->suggestionCollector->getSuggestions() as $suggestion) {
134
                $message .= $suggestionFormatter->format($suggestion) . "\n\n";
135
            }
136
137
            throw new InvalidArgumentException($message);
138
        }
139
140
        $this->handleSiteAccessesRelation($container);
141
        $this->buildPolicyMap($container);
142
    }
143
144
    /**
145
     * @param array $config
146
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
147
     *
148
     * @return \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration
149
     */
150
    public function getConfiguration(array $config, ContainerBuilder $container)
151
    {
152
        $configuration = new Configuration($this->getMainConfigParser(), $this->suggestionCollector);
153
        $configuration->setSiteAccessConfigurationFilters($this->siteaccessConfigurationFilters);
154
155
        return $configuration;
156
    }
157
158
    /**
159
     * @return \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface
160
     */
161
    private function getMainConfigParser()
162
    {
163
        if ($this->mainConfigParser === null) {
164
            foreach ($this->configParsers as $parser) {
165
                if ($parser instanceof SuggestionCollectorAwareInterface) {
166
                    $parser->setSuggestionCollector($this->suggestionCollector);
167
                }
168
            }
169
170
            $this->mainConfigParser = new ConfigParser($this->configParsers);
171
        }
172
173
        return $this->mainConfigParser;
174
    }
175
176
    /**
177
     * Handle default settings.
178
     *
179
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
180
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
181
     */
182
    private function handleDefaultSettingsLoading(ContainerBuilder $container, FileLoader $loader)
183
    {
184
        $loader->load('default_settings.yml');
185
186
        foreach ($this->defaultSettingsCollection as $fileLocation => $files) {
187
            $externalLoader = new Loader\YamlFileLoader($container, new FileLocator($fileLocation));
188
            foreach ($files as $file) {
189
                $externalLoader->load($file);
190
            }
191
        }
192
    }
193
194
    private function registerRepositoriesConfiguration(array $config, ContainerBuilder $container)
195
    {
196
        if (!isset($config['repositories'])) {
197
            $config['repositories'] = array();
198
        }
199
200
        foreach ($config['repositories'] as $name => &$repository) {
201
            if (empty($repository['fields_groups']['list'])) {
202
                $repository['fields_groups']['list'] = $container->getParameter('ezsettings.default.content.field_groups.list');
203
            }
204
        }
205
206
        $container->setParameter('ezpublish.repositories', $config['repositories']);
207
    }
208
209
    private function registerSiteAccessConfiguration(array $config, ContainerBuilder $container)
210
    {
211
        if (!isset($config['siteaccess'])) {
212
            $config['siteaccess'] = array();
213
            $config['siteaccess']['list'] = array('setup');
214
            $config['siteaccess']['default_siteaccess'] = 'setup';
215
            $config['siteaccess']['groups'] = array();
216
            $config['siteaccess']['match'] = null;
217
        }
218
219
        $container->setParameter('ezpublish.siteaccess.list', $config['siteaccess']['list']);
220
        ConfigurationProcessor::setAvailableSiteAccesses($config['siteaccess']['list']);
221
        $container->setParameter('ezpublish.siteaccess.default', $config['siteaccess']['default_siteaccess']);
222
        $container->setParameter('ezpublish.siteaccess.match_config', $config['siteaccess']['match']);
223
224
        // Register siteaccess groups + reverse
225
        $container->setParameter('ezpublish.siteaccess.groups', $config['siteaccess']['groups']);
226
        $groupsBySiteaccess = array();
227 View Code Duplication
        foreach ($config['siteaccess']['groups'] as $groupName => $groupMembers) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
228
            foreach ($groupMembers as $member) {
229
                if (!isset($groupsBySiteaccess[$member])) {
230
                    $groupsBySiteaccess[$member] = array();
231
                }
232
233
                $groupsBySiteaccess[$member][] = $groupName;
234
            }
235
        }
236
        $container->setParameter('ezpublish.siteaccess.groups_by_siteaccess', $groupsBySiteaccess);
237
        ConfigurationProcessor::setGroupsBySiteAccess($groupsBySiteaccess);
238
    }
239
240
    private function registerImageMagickConfiguration(array $config, ContainerBuilder $container)
241
    {
242
        if (isset($config['imagemagick'])) {
243
            $container->setParameter('ezpublish.image.imagemagick.enabled', $config['imagemagick']['enabled']);
244
            if ($config['imagemagick']['enabled']) {
245
                $container->setParameter('ezpublish.image.imagemagick.executable_path', dirname($config['imagemagick']['path']));
246
                $container->setParameter('ezpublish.image.imagemagick.executable', basename($config['imagemagick']['path']));
247
            }
248
        }
249
250
        $filters = isset($config['imagemagick']['filters']) ? $config['imagemagick']['filters'] : array();
251
        $filters = $filters + $container->getParameter('ezpublish.image.imagemagick.filters');
252
        $container->setParameter('ezpublish.image.imagemagick.filters', $filters);
253
    }
254
255
    private function registerPageConfiguration(array $config, ContainerBuilder $container)
256
    {
257
        if (isset($config['ezpage']['layouts'])) {
258
            $container->setParameter(
259
                'ezpublish.ezpage.layouts',
260
                $config['ezpage']['layouts'] + $container->getParameter('ezpublish.ezpage.layouts')
261
            );
262
        }
263
        if (isset($config['ezpage']['blocks'])) {
264
            $container->setParameter(
265
                'ezpublish.ezpage.blocks',
266
                $config['ezpage']['blocks'] + $container->getParameter('ezpublish.ezpage.blocks')
267
            );
268
        }
269
        if (isset($config['ezpage']['enabledLayouts'])) {
270
            $container->setParameter(
271
                'ezpublish.ezpage.enabledLayouts',
272
                $config['ezpage']['enabledLayouts'] + $container->getParameter('ezpublish.ezpage.enabledLayouts')
273
            );
274
        }
275
        if (isset($config['ezpage']['enabledBlocks'])) {
276
            $container->setParameter(
277
                'ezpublish.ezpage.enabledBlocks',
278
                $config['ezpage']['enabledBlocks'] + $container->getParameter('ezpublish.ezpage.enabledBlocks')
279
            );
280
        }
281
    }
282
283
    /**
284
     * Handle routing parameters.
285
     *
286
     * @param array $config
287
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
288
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
289
     */
290
    private function handleRouting(array $config, ContainerBuilder $container, FileLoader $loader)
291
    {
292
        $loader->load('routing.yml');
293
        $container->setAlias('router', 'ezpublish.chain_router');
294
295
        if (isset($config['router']['default_router']['non_siteaccess_aware_routes'])) {
296
            $container->setParameter(
297
                'ezpublish.default_router.non_siteaccess_aware_routes',
298
                array_merge(
299
                    $container->getParameter('ezpublish.default_router.non_siteaccess_aware_routes'),
300
                    $config['router']['default_router']['non_siteaccess_aware_routes']
301
                )
302
            );
303
        }
304
    }
305
306
    /**
307
     * Handle public API loading.
308
     *
309
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
310
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
311
     */
312
    private function handleApiLoading(ContainerBuilder $container, FileLoader $loader)
313
    {
314
        // Loading configuration from Core/settings
315
        $coreLoader = new Loader\YamlFileLoader(
316
            $container,
317
            new FileLocator(__DIR__ . '/../../../Publish/Core/settings')
318
        );
319
        $coreLoader->load('repository.yml');
320
        $coreLoader->load('repository/inner.yml');
321
        $coreLoader->load('repository/signalslot.yml');
322
        $coreLoader->load('fieldtype_external_storages.yml');
323
        $coreLoader->load('fieldtypes.yml');
324
        $coreLoader->load('indexable_fieldtypes.yml');
325
        $coreLoader->load('roles.yml');
326
        $coreLoader->load('storage_engines/common.yml');
327
        $coreLoader->load('storage_engines/cache.yml');
328
        $coreLoader->load('storage_engines/legacy.yml');
329
        $coreLoader->load('storage_engines/shortcuts.yml');
330
        $coreLoader->load('search_engines/common.yml');
331
        $coreLoader->load('utils.yml');
332
        $coreLoader->load('io.yml');
333
334
        // Public API services
335
        $loader->load('papi.yml');
336
337
        // Built-in field types
338
        $loader->load('fieldtype_services.yml');
339
340
        // Storage engine
341
        $loader->load('storage_engines.yml');
342
    }
343
344
    /**
345
     * Handle templating parameters.
346
     *
347
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
348
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
349
     */
350
    private function handleTemplating(ContainerBuilder $container, FileLoader $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
351
    {
352
        $loader->load('templating.yml');
353
    }
354
355
    /**
356
     * Handle session parameters.
357
     *
358
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
359
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
360
     */
361
    private function handleSessionLoading(ContainerBuilder $container, FileLoader $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
362
    {
363
        $loader->load('session.yml');
364
    }
365
366
    /**
367
     * Handle cache parameters.
368
     *
369
     * @param array $config
370
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
371
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
372
     *
373
     * @throws \InvalidArgumentException
374
     */
375
    private function handleCache(array $config, ContainerBuilder $container, FileLoader $loader)
376
    {
377
        $loader->load('cache.yml');
378
379
        $purgeService = null;
0 ignored issues
show
Unused Code introduced by
$purgeService is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
380
        if (isset($config['http_cache']['purge_type'])) {
381
            $container->setParameter('ezpublish.http_cache.purge_type', $config['http_cache']['purge_type']);
382
        }
383
    }
384
385
    /**
386
     * Handle locale parameters.
387
     *
388
     * @param array $config
389
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
390
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
391
     */
392
    private function handleLocale(array $config, ContainerBuilder $container, FileLoader $loader)
393
    {
394
        $loader->load('locale.yml');
395
        $container->setParameter(
396
            'ezpublish.locale.conversion_map',
397
            $config['locale_conversion'] + $container->getParameter('ezpublish.locale.conversion_map')
398
        );
399
    }
400
401
    /**
402
     * Handle helpers.
403
     *
404
     * @param array $config
405
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
406
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
407
     */
408
    private function handleHelpers(array $config, ContainerBuilder $container, FileLoader $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
409
    {
410
        $loader->load('helpers.yml');
411
    }
412
413
    /**
414
     * Handles relation between SiteAccesses.
415
     * Related SiteAccesses share the same repository and root location id.
416
     *
417
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
418
     */
419
    private function handleSiteAccessesRelation(ContainerBuilder $container)
420
    {
421
        $configResolver = $container->get('ezpublish.config.resolver.core');
422
        $configResolver->setContainer($container);
423
424
        $saRelationMap = array();
425
        $saList = $container->getParameter('ezpublish.siteaccess.list');
426
        // First build the SiteAccess relation map, indexed by repository and rootLocationId.
427
        foreach ($saList as $sa) {
428
            $repository = $configResolver->getParameter('repository', 'ezsettings', $sa);
429
            if (!isset($saRelationMap[$repository])) {
430
                $saRelationMap[$repository] = array();
431
            }
432
433
            $rootLocationId = $configResolver->getParameter('content.tree_root.location_id', 'ezsettings', $sa);
434
            if (!isset($saRelationMap[$repository][$rootLocationId])) {
435
                $saRelationMap[$repository][$rootLocationId] = array();
436
            }
437
            $saRelationMap[$repository][$rootLocationId][] = $sa;
438
        }
439
        $container->setParameter('ezpublish.siteaccess.relation_map', $saRelationMap);
440
441
        // Now build the related SiteAccesses list, based on the relation map.
442
        foreach ($saList as $sa) {
443
            $repository = $configResolver->getParameter('repository', 'ezsettings', $sa);
444
            $rootLocationId = $configResolver->getParameter('content.tree_root.location_id', 'ezsettings', $sa);
445
            $container->setParameter(
446
                "ezsettings.$sa.related_siteaccesses",
447
                $saRelationMap[$repository][$rootLocationId]
448
            );
449
        }
450
    }
451
452
    /**
453
     * @param array $config
454
     * @param ContainerBuilder $container
455
     * @param FileLoader $loader
456
     */
457
    private function handleImage(array $config, ContainerBuilder $container, FileLoader $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
458
    {
459
        $loader->load('image.yml');
460
    }
461
462
    private function buildPolicyMap(ContainerBuilder $container)
463
    {
464
        $policiesBuilder = new PoliciesConfigBuilder($container);
465
        foreach ($this->policyProviders as $provider) {
466
            $provider->addPolicies($policiesBuilder);
467
        }
468
    }
469
470
    public function prepend(ContainerBuilder $container)
471
    {
472
        // Default settings for FOSHttpCacheBundle
473
        $configFile = __DIR__ . '/../Resources/config/fos_http_cache.yml';
474
        $config = Yaml::parse(file_get_contents($configFile));
475
        $container->prependExtensionConfig('fos_http_cache', $config);
476
        $container->addResource(new FileResource($configFile));
477
    }
478
479
    /**
480
     * Adds a new policy provider to the internal collection.
481
     * One can call this method from a bundle `build()` method.
482
     *
483
     * ```php
484
     * public function build(ContainerBuilder $container)
485
     * {
486
     *     $ezExtension = $container->getExtension('ezpublish');
487
     *     $ezExtension->addPolicyProvider($myPolicyProvider);
488
     * }
489
     * ```
490
     *
491
     * @since 6.0
492
     *
493
     * @param PolicyProviderInterface $policyProvider
494
     */
495
    public function addPolicyProvider(PolicyProviderInterface $policyProvider)
496
    {
497
        $this->policyProviders[] = $policyProvider;
498
    }
499
500
    /**
501
     * Adds a new config parser to the internal collection.
502
     * One can call this method from a bundle `build()` method.
503
     *
504
     * ```php
505
     * public function build(ContainerBuilder $container)
506
     * {
507
     *     $ezExtension = $container->getExtension('ezpublish');
508
     *     $ezExtension->addConfigParser($myConfigParser);
509
     * }
510
     * ```
511
     *
512
     * @since 6.0
513
     *
514
     * @param \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface $configParser
515
     */
516
    public function addConfigParser(ParserInterface $configParser)
517
    {
518
        $this->configParsers[] = $configParser;
519
    }
520
521
    /**
522
     * Adds new default settings to the internal collection.
523
     * One can call this method from a bundle `build()` method.
524
     *
525
     * ```php
526
     * public function build(ContainerBuilder $container)
527
     * {
528
     *     $ezExtension = $container->getExtension('ezpublish');
529
     *     $ezExtension->addDefaultSettings(
530
     *         __DIR__ . '/Resources/config',
531
     *         ['default_settings.yml']
532
     *     );
533
     * }
534
     * ```
535
     *
536
     * @since 6.0
537
     *
538
     * @param string $fileLocation
539
     * @param array $files
540
     */
541
    public function addDefaultSettings($fileLocation, array $files)
542
    {
543
        $this->defaultSettingsCollection[$fileLocation] = $files;
544
    }
545
546
    public function addSiteAccessConfigurationFilter(SiteAccessConfigurationFilter $filter)
547
    {
548
        $this->siteaccessConfigurationFilters[] = $filter;
549
    }
550
}
551