Completed
Push — 6.7 ( 470b16...7283a5 )
by André
13:35
created

EzPublishCoreExtension::buildPolicyMap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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 Symfony\Component\Config\Resource\FileResource;
19
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\DependencyInjection\Loader;
23
use Symfony\Component\DependencyInjection\Loader\FileLoader;
24
use Symfony\Component\Config\FileLocator;
25
use InvalidArgumentException;
26
use Symfony\Component\Yaml\Yaml;
27
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface;
28
29
class EzPublishCoreExtension extends Extension implements PrependExtensionInterface
30
{
31
    /**
32
     * @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Suggestion\Collector\SuggestionCollector
33
     */
34
    private $suggestionCollector;
35
36
    /**
37
     * @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface
38
     */
39
    private $mainConfigParser;
40
41
    /**
42
     * @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface[]
43
     */
44
    private $configParsers;
45
46
    /**
47
     * @var PolicyProviderInterface[]
48
     */
49
    private $policyProviders = [];
50
51
    /**
52
     * Holds a collection of YAML files, as an array with directory path as a
53
     * key to the array of contained file names.
54
     *
55
     * @var array
56
     */
57
    private $defaultSettingsCollection = [];
58
59
    public function __construct(array $configParsers = array())
60
    {
61
        $this->configParsers = $configParsers;
62
        $this->suggestionCollector = new SuggestionCollector();
63
    }
64
65
    public function getAlias()
66
    {
67
        return 'ezpublish';
68
    }
69
70
    /**
71
     * Loads a specific configuration.
72
     *
73
     * @param mixed[] $configs An array of configuration values
74
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container A ContainerBuilder instance
75
     *
76
     * @throws \InvalidArgumentException When provided tag is not defined in this extension
77
     *
78
     * @api
79
     */
80
    public function load(array $configs, ContainerBuilder $container)
81
    {
82
        $loader = new Loader\YamlFileLoader(
83
            $container,
84
            new FileLocator(__DIR__ . '/../Resources/config')
85
        );
86
87
        $configuration = $this->getConfiguration($configs, $container);
88
89
        // Note: this is where the transformation occurs
90
        $config = $this->processConfiguration($configuration, $configs);
91
92
        // Base services and services overrides
93
        $loader->load('services.yml');
94
        // Security services
95
        $loader->load('security.yml');
96
        // Slots
97
        $loader->load('slot.yml');
98
99
        // Default settings
100
        $this->handleDefaultSettingsLoading($container, $loader);
101
102
        $this->registerRepositoriesConfiguration($config, $container);
103
        $this->registerSiteAccessConfiguration($config, $container);
104
        $this->registerImageMagickConfiguration($config, $container);
105
        $this->registerPageConfiguration($config, $container);
106
107
        // Routing
108
        $this->handleRouting($config, $container, $loader);
109
        // Public API loading
110
        $this->handleApiLoading($container, $loader);
111
        $this->handleTemplating($container, $loader);
112
        $this->handleSessionLoading($container, $loader);
113
        $this->handleCache($config, $container, $loader);
114
        $this->handleLocale($config, $container, $loader);
115
        $this->handleHelpers($config, $container, $loader);
116
        $this->handleImage($config, $container, $loader);
117
118
        // Map settings
119
        $processor = new ConfigurationProcessor($container, 'ezsettings');
120
        $processor->mapConfig($config, $this->getMainConfigParser());
121
122
        if ($this->suggestionCollector->hasSuggestions()) {
123
            $message = '';
124
            $suggestionFormatter = new YamlSuggestionFormatter();
125
            foreach ($this->suggestionCollector->getSuggestions() as $suggestion) {
126
                $message .= $suggestionFormatter->format($suggestion) . "\n\n";
127
            }
128
129
            throw new InvalidArgumentException($message);
130
        }
131
132
        $this->handleSiteAccessesRelation($container);
133
        $this->buildPolicyMap($container);
134
    }
135
136
    /**
137
     * @param array $config
138
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
139
     *
140
     * @return \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration
141
     */
142
    public function getConfiguration(array $config, ContainerBuilder $container)
143
    {
144
        return new Configuration($this->getMainConfigParser(), $this->suggestionCollector);
145
    }
146
147
    /**
148
     * @return \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface
149
     */
150
    private function getMainConfigParser()
151
    {
152
        if ($this->mainConfigParser === null) {
153
            foreach ($this->configParsers as $parser) {
154
                if ($parser instanceof SuggestionCollectorAwareInterface) {
155
                    $parser->setSuggestionCollector($this->suggestionCollector);
156
                }
157
            }
158
159
            $this->mainConfigParser = new ConfigParser($this->configParsers);
160
        }
161
162
        return $this->mainConfigParser;
163
    }
164
165
    /**
166
     * Handle default settings.
167
     *
168
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
169
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
170
     */
171
    private function handleDefaultSettingsLoading(ContainerBuilder $container, FileLoader $loader)
172
    {
173
        $loader->load('default_settings.yml');
174
175
        foreach ($this->defaultSettingsCollection as $fileLocation => $files) {
176
            $externalLoader = new Loader\YamlFileLoader($container, new FileLocator($fileLocation));
177
            foreach ($files as $file) {
178
                $externalLoader->load($file);
179
            }
180
        }
181
    }
182
183
    private function registerRepositoriesConfiguration(array $config, ContainerBuilder $container)
184
    {
185
        if (!isset($config['repositories'])) {
186
            $config['repositories'] = array();
187
        }
188
189
        foreach ($config['repositories'] as $name => &$repository) {
190
            if (empty($repository['fields_groups']['list'])) {
191
                $repository['fields_groups']['list'] = $container->getParameter('ezsettings.default.content.field_groups.list');
192
            }
193
        }
194
195
        $container->setParameter('ezpublish.repositories', $config['repositories']);
196
    }
197
198
    private function registerSiteAccessConfiguration(array $config, ContainerBuilder $container)
199
    {
200
        if (!isset($config['siteaccess'])) {
201
            $config['siteaccess'] = array();
202
            $config['siteaccess']['list'] = array('setup');
203
            $config['siteaccess']['default_siteaccess'] = 'setup';
204
            $config['siteaccess']['groups'] = array();
205
            $config['siteaccess']['match'] = null;
206
        }
207
208
        $container->setParameter('ezpublish.siteaccess.list', $config['siteaccess']['list']);
209
        ConfigurationProcessor::setAvailableSiteAccesses($config['siteaccess']['list']);
210
        $container->setParameter('ezpublish.siteaccess.default', $config['siteaccess']['default_siteaccess']);
211
        $container->setParameter('ezpublish.siteaccess.match_config', $config['siteaccess']['match']);
212
213
        // Register siteaccess groups + reverse
214
        $container->setParameter('ezpublish.siteaccess.groups', $config['siteaccess']['groups']);
215
        $groupsBySiteaccess = array();
216 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...
217
            foreach ($groupMembers as $member) {
218
                if (!isset($groupsBySiteaccess[$member])) {
219
                    $groupsBySiteaccess[$member] = array();
220
                }
221
222
                $groupsBySiteaccess[$member][] = $groupName;
223
            }
224
        }
225
        $container->setParameter('ezpublish.siteaccess.groups_by_siteaccess', $groupsBySiteaccess);
226
        ConfigurationProcessor::setGroupsBySiteAccess($groupsBySiteaccess);
227
    }
228
229
    private function registerImageMagickConfiguration(array $config, ContainerBuilder $container)
230
    {
231
        if (isset($config['imagemagick'])) {
232
            $container->setParameter('ezpublish.image.imagemagick.enabled', $config['imagemagick']['enabled']);
233
            if ($config['imagemagick']['enabled']) {
234
                $container->setParameter('ezpublish.image.imagemagick.executable_path', dirname($config['imagemagick']['path']));
235
                $container->setParameter('ezpublish.image.imagemagick.executable', basename($config['imagemagick']['path']));
236
            }
237
        }
238
239
        $filters = isset($config['imagemagick']['filters']) ? $config['imagemagick']['filters'] : array();
240
        $filters = $filters + $container->getParameter('ezpublish.image.imagemagick.filters');
241
        $container->setParameter('ezpublish.image.imagemagick.filters', $filters);
242
    }
243
244
    private function registerPageConfiguration(array $config, ContainerBuilder $container)
245
    {
246
        if (isset($config['ezpage']['layouts'])) {
247
            $container->setParameter(
248
                'ezpublish.ezpage.layouts',
249
                $config['ezpage']['layouts'] + $container->getParameter('ezpublish.ezpage.layouts')
250
            );
251
        }
252
        if (isset($config['ezpage']['blocks'])) {
253
            $container->setParameter(
254
                'ezpublish.ezpage.blocks',
255
                $config['ezpage']['blocks'] + $container->getParameter('ezpublish.ezpage.blocks')
256
            );
257
        }
258
        if (isset($config['ezpage']['enabledLayouts'])) {
259
            $container->setParameter(
260
                'ezpublish.ezpage.enabledLayouts',
261
                $config['ezpage']['enabledLayouts'] + $container->getParameter('ezpublish.ezpage.enabledLayouts')
262
            );
263
        }
264
        if (isset($config['ezpage']['enabledBlocks'])) {
265
            $container->setParameter(
266
                'ezpublish.ezpage.enabledBlocks',
267
                $config['ezpage']['enabledBlocks'] + $container->getParameter('ezpublish.ezpage.enabledBlocks')
268
            );
269
        }
270
    }
271
272
    /**
273
     * Handle routing parameters.
274
     *
275
     * @param array $config
276
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
277
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
278
     */
279
    private function handleRouting(array $config, ContainerBuilder $container, FileLoader $loader)
280
    {
281
        $loader->load('routing.yml');
282
        $container->setAlias('router', 'ezpublish.chain_router');
283
284
        if (isset($config['router']['default_router']['non_siteaccess_aware_routes'])) {
285
            $container->setParameter(
286
                'ezpublish.default_router.non_siteaccess_aware_routes',
287
                array_merge(
288
                    $container->getParameter('ezpublish.default_router.non_siteaccess_aware_routes'),
289
                    $config['router']['default_router']['non_siteaccess_aware_routes']
290
                )
291
            );
292
        }
293
    }
294
295
    /**
296
     * Handle public API loading.
297
     *
298
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
299
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
300
     */
301
    private function handleApiLoading(ContainerBuilder $container, FileLoader $loader)
302
    {
303
        // Loading configuration from Core/settings
304
        $coreLoader = new Loader\YamlFileLoader(
305
            $container,
306
            new FileLocator(__DIR__ . '/../../../Publish/Core/settings')
307
        );
308
        $coreLoader->load('repository.yml');
309
        $coreLoader->load('fieldtype_external_storages.yml');
310
        $coreLoader->load('fieldtypes.yml');
311
        $coreLoader->load('indexable_fieldtypes.yml');
312
        $coreLoader->load('roles.yml');
313
        $coreLoader->load('storage_engines/common.yml');
314
        $coreLoader->load('storage_engines/cache.yml');
315
        $coreLoader->load('storage_engines/legacy.yml');
316
        $coreLoader->load('storage_engines/shortcuts.yml');
317
        $coreLoader->load('search_engines/common.yml');
318
        $coreLoader->load('utils.yml');
319
        $coreLoader->load('io.yml');
320
321
        // Public API services
322
        $loader->load('papi.yml');
323
324
        // Built-in field types
325
        $loader->load('fieldtype_services.yml');
326
327
        // Storage engine
328
        $loader->load('storage_engines.yml');
329
    }
330
331
    /**
332
     * Handle templating parameters.
333
     *
334
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
335
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
336
     */
337
    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...
338
    {
339
        $loader->load('templating.yml');
340
    }
341
342
    /**
343
     * Handle session parameters.
344
     *
345
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
346
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
347
     */
348
    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...
349
    {
350
        $loader->load('session.yml');
351
    }
352
353
    /**
354
     * Handle cache parameters.
355
     *
356
     * @param array $config
357
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
358
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
359
     *
360
     * @throws \InvalidArgumentException
361
     */
362
    private function handleCache(array $config, ContainerBuilder $container, FileLoader $loader)
363
    {
364
        $loader->load('cache.yml');
365
366
        $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...
367
        if (isset($config['http_cache']['purge_type'])) {
368
            $container->setParameter('ezpublish.http_cache.purge_type', $config['http_cache']['purge_type']);
369
        }
370
    }
371
372
    /**
373
     * Handle locale parameters.
374
     *
375
     * @param array $config
376
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
377
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
378
     */
379
    private function handleLocale(array $config, ContainerBuilder $container, FileLoader $loader)
380
    {
381
        $loader->load('locale.yml');
382
        $container->setParameter(
383
            'ezpublish.locale.conversion_map',
384
            $config['locale_conversion'] + $container->getParameter('ezpublish.locale.conversion_map')
385
        );
386
    }
387
388
    /**
389
     * Handle helpers.
390
     *
391
     * @param array $config
392
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
393
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
394
     */
395
    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...
396
    {
397
        $loader->load('helpers.yml');
398
    }
399
400
    /**
401
     * Handles relation between SiteAccesses.
402
     * Related SiteAccesses share the same repository and root location id.
403
     *
404
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
405
     */
406
    private function handleSiteAccessesRelation(ContainerBuilder $container)
407
    {
408
        $configResolver = $container->get('ezpublish.config.resolver.core');
409
        $configResolver->setContainer($container);
410
411
        $saRelationMap = array();
412
        $saList = $container->getParameter('ezpublish.siteaccess.list');
413
        // First build the SiteAccess relation map, indexed by repository and rootLocationId.
414
        foreach ($saList as $sa) {
415
            $repository = $configResolver->getParameter('repository', 'ezsettings', $sa);
416
            if (!isset($saRelationMap[$repository])) {
417
                $saRelationMap[$repository] = array();
418
            }
419
420
            $rootLocationId = $configResolver->getParameter('content.tree_root.location_id', 'ezsettings', $sa);
421
            if (!isset($saRelationMap[$repository][$rootLocationId])) {
422
                $saRelationMap[$repository][$rootLocationId] = array();
423
            }
424
            $saRelationMap[$repository][$rootLocationId][] = $sa;
425
        }
426
        $container->setParameter('ezpublish.siteaccess.relation_map', $saRelationMap);
427
428
        // Now build the related SiteAccesses list, based on the relation map.
429
        foreach ($saList as $sa) {
430
            $repository = $configResolver->getParameter('repository', 'ezsettings', $sa);
431
            $rootLocationId = $configResolver->getParameter('content.tree_root.location_id', 'ezsettings', $sa);
432
            $container->setParameter(
433
                "ezsettings.$sa.related_siteaccesses",
434
                $saRelationMap[$repository][$rootLocationId]
435
            );
436
        }
437
    }
438
439
    /**
440
     * @param array $config
441
     * @param ContainerBuilder $container
442
     * @param FileLoader $loader
443
     */
444
    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...
445
    {
446
        $loader->load('image.yml');
447
    }
448
449
    private function buildPolicyMap(ContainerBuilder $container)
450
    {
451
        $policiesBuilder = new PoliciesConfigBuilder($container);
452
        foreach ($this->policyProviders as $provider) {
453
            $provider->addPolicies($policiesBuilder);
454
        }
455
    }
456
457 View Code Duplication
    public function prepend(ContainerBuilder $container)
458
    {
459
        // Default settings for FOSHttpCacheBundle
460
        $configFile = __DIR__ . '/../Resources/config/fos_http_cache.yml';
461
        $config = Yaml::parse(file_get_contents($configFile));
462
        $container->prependExtensionConfig('fos_http_cache', $config);
463
        $container->addResource(new FileResource($configFile));
464
    }
465
466
    /**
467
     * Adds a new policy provider to the internal collection.
468
     * One can call this method from a bundle `build()` method.
469
     *
470
     * ```php
471
     * public function build(ContainerBuilder $container)
472
     * {
473
     *     $ezExtension = $container->getExtension('ezpublish');
474
     *     $ezExtension->addPolicyProvider($myPolicyProvider);
475
     * }
476
     * ```
477
     *
478
     * @since 6.0
479
     *
480
     * @param PolicyProviderInterface $policyProvider
481
     */
482
    public function addPolicyProvider(PolicyProviderInterface $policyProvider)
483
    {
484
        $this->policyProviders[] = $policyProvider;
485
    }
486
487
    /**
488
     * Adds a new config parser to the internal collection.
489
     * One can call this method from a bundle `build()` method.
490
     *
491
     * ```php
492
     * public function build(ContainerBuilder $container)
493
     * {
494
     *     $ezExtension = $container->getExtension('ezpublish');
495
     *     $ezExtension->addConfigParser($myConfigParser);
496
     * }
497
     * ```
498
     *
499
     * @since 6.0
500
     *
501
     * @param \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface $configParser
502
     */
503
    public function addConfigParser(ParserInterface $configParser)
504
    {
505
        $this->configParsers[] = $configParser;
506
    }
507
508
    /**
509
     * Adds new default settings to the internal collection.
510
     * One can call this method from a bundle `build()` method.
511
     *
512
     * ```php
513
     * public function build(ContainerBuilder $container)
514
     * {
515
     *     $ezExtension = $container->getExtension('ezpublish');
516
     *     $ezExtension->addDefaultSettings(
517
     *         __DIR__ . '/Resources/config',
518
     *         ['default_settings.yml']
519
     *     );
520
     * }
521
     * ```
522
     *
523
     * @since 6.0
524
     *
525
     * @param string $fileLocation
526
     * @param array $files
527
     */
528
    public function addDefaultSettings($fileLocation, array $files)
529
    {
530
        $this->defaultSettingsCollection[$fileLocation] = $files;
531
    }
532
}
533