Completed
Push — ezp30878_cant_add_image_with_p... ( e19ea7...263f1b )
by
unknown
20:16
created

EzPublishCoreExtension::addDefaultSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
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 = [])
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
        // Stash Cache
118
        $this->handleStashCache($config, $container, $loader);
119
120
        // Map settings
121
        $processor = new ConfigurationProcessor($container, 'ezsettings');
122
        $processor->mapConfig($config, $this->getMainConfigParser());
123
124
        if ($this->suggestionCollector->hasSuggestions()) {
125
            $message = '';
126
            $suggestionFormatter = new YamlSuggestionFormatter();
127
            foreach ($this->suggestionCollector->getSuggestions() as $suggestion) {
128
                $message .= $suggestionFormatter->format($suggestion) . "\n\n";
129
            }
130
131
            throw new InvalidArgumentException($message);
132
        }
133
134
        $this->handleSiteAccessesRelation($container);
135
        $this->buildPolicyMap($container);
136
    }
137
138
    /**
139
     * @param array $config
140
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
141
     *
142
     * @return \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration
143
     */
144
    public function getConfiguration(array $config, ContainerBuilder $container)
145
    {
146
        return new Configuration($this->getMainConfigParser(), $this->suggestionCollector);
147
    }
148
149
    /**
150
     * @return \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface
151
     */
152
    private function getMainConfigParser()
153
    {
154
        if ($this->mainConfigParser === null) {
155
            foreach ($this->configParsers as $parser) {
156
                if ($parser instanceof SuggestionCollectorAwareInterface) {
157
                    $parser->setSuggestionCollector($this->suggestionCollector);
158
                }
159
            }
160
161
            $this->mainConfigParser = new ConfigParser($this->configParsers);
162
        }
163
164
        return $this->mainConfigParser;
165
    }
166
167
    /**
168
     * Handle default settings.
169
     *
170
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
171
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
172
     */
173
    private function handleDefaultSettingsLoading(ContainerBuilder $container, FileLoader $loader)
174
    {
175
        $loader->load('default_settings.yml');
176
177
        foreach ($this->defaultSettingsCollection as $fileLocation => $files) {
178
            $externalLoader = new Loader\YamlFileLoader($container, new FileLocator($fileLocation));
179
            foreach ($files as $file) {
180
                $externalLoader->load($file);
181
            }
182
        }
183
    }
184
185
    private function registerRepositoriesConfiguration(array $config, ContainerBuilder $container)
186
    {
187
        if (!isset($config['repositories'])) {
188
            $config['repositories'] = [];
189
        }
190
191
        foreach ($config['repositories'] as $name => &$repository) {
192
            if (empty($repository['fields_groups']['list'])) {
193
                $repository['fields_groups']['list'] = $container->getParameter('ezsettings.default.content.field_groups.list');
194
            }
195
        }
196
197
        $container->setParameter('ezpublish.repositories', $config['repositories']);
198
    }
199
200
    private function registerSiteAccessConfiguration(array $config, ContainerBuilder $container)
201
    {
202
        if (!isset($config['siteaccess'])) {
203
            $config['siteaccess'] = [];
204
            $config['siteaccess']['list'] = ['setup'];
205
            $config['siteaccess']['default_siteaccess'] = 'setup';
206
            $config['siteaccess']['groups'] = [];
207
            $config['siteaccess']['match'] = null;
208
        }
209
210
        $container->setParameter('ezpublish.siteaccess.list', $config['siteaccess']['list']);
211
        ConfigurationProcessor::setAvailableSiteAccesses($config['siteaccess']['list']);
212
        $container->setParameter('ezpublish.siteaccess.default', $config['siteaccess']['default_siteaccess']);
213
        $container->setParameter('ezpublish.siteaccess.match_config', $config['siteaccess']['match']);
214
215
        // Register siteaccess groups + reverse
216
        $container->setParameter('ezpublish.siteaccess.groups', $config['siteaccess']['groups']);
217
        $groupsBySiteaccess = [];
218 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...
219
            foreach ($groupMembers as $member) {
220
                if (!isset($groupsBySiteaccess[$member])) {
221
                    $groupsBySiteaccess[$member] = [];
222
                }
223
224
                $groupsBySiteaccess[$member][] = $groupName;
225
            }
226
        }
227
        $container->setParameter('ezpublish.siteaccess.groups_by_siteaccess', $groupsBySiteaccess);
228
        ConfigurationProcessor::setGroupsBySiteAccess($groupsBySiteaccess);
229
    }
230
231
    private function registerImageMagickConfiguration(array $config, ContainerBuilder $container)
232
    {
233
        if (isset($config['imagemagick'])) {
234
            $container->setParameter('ezpublish.image.imagemagick.enabled', $config['imagemagick']['enabled']);
235
            if ($config['imagemagick']['enabled']) {
236
                $container->setParameter('ezpublish.image.imagemagick.executable_path', dirname($config['imagemagick']['path']));
237
                $container->setParameter('ezpublish.image.imagemagick.executable', basename($config['imagemagick']['path']));
238
            }
239
        }
240
241
        $filters = isset($config['imagemagick']['filters']) ? $config['imagemagick']['filters'] : [];
242
        $filters = $filters + $container->getParameter('ezpublish.image.imagemagick.filters');
243
        $container->setParameter('ezpublish.image.imagemagick.filters', $filters);
244
    }
245
246
    private function registerPageConfiguration(array $config, ContainerBuilder $container)
247
    {
248
        if (isset($config['ezpage']['layouts'])) {
249
            $container->setParameter(
250
                'ezpublish.ezpage.layouts',
251
                $config['ezpage']['layouts'] + $container->getParameter('ezpublish.ezpage.layouts')
252
            );
253
        }
254
        if (isset($config['ezpage']['blocks'])) {
255
            $container->setParameter(
256
                'ezpublish.ezpage.blocks',
257
                $config['ezpage']['blocks'] + $container->getParameter('ezpublish.ezpage.blocks')
258
            );
259
        }
260
        if (isset($config['ezpage']['enabledLayouts'])) {
261
            $container->setParameter(
262
                'ezpublish.ezpage.enabledLayouts',
263
                $config['ezpage']['enabledLayouts'] + $container->getParameter('ezpublish.ezpage.enabledLayouts')
264
            );
265
        }
266
        if (isset($config['ezpage']['enabledBlocks'])) {
267
            $container->setParameter(
268
                'ezpublish.ezpage.enabledBlocks',
269
                $config['ezpage']['enabledBlocks'] + $container->getParameter('ezpublish.ezpage.enabledBlocks')
270
            );
271
        }
272
    }
273
274
    /**
275
     * Handle routing parameters.
276
     *
277
     * @param array $config
278
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
279
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
280
     */
281
    private function handleRouting(array $config, ContainerBuilder $container, FileLoader $loader)
282
    {
283
        $loader->load('routing.yml');
284
        $container->setAlias('router', 'ezpublish.chain_router');
285
286
        if (isset($config['router']['default_router']['non_siteaccess_aware_routes'])) {
287
            $container->setParameter(
288
                'ezpublish.default_router.non_siteaccess_aware_routes',
289
                array_merge(
290
                    $container->getParameter('ezpublish.default_router.non_siteaccess_aware_routes'),
291
                    $config['router']['default_router']['non_siteaccess_aware_routes']
292
                )
293
            );
294
        }
295
    }
296
297
    /**
298
     * Handle public API loading.
299
     *
300
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
301
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
302
     */
303
    private function handleApiLoading(ContainerBuilder $container, FileLoader $loader)
304
    {
305
        // Loading configuration from Core/settings
306
        $coreLoader = new Loader\YamlFileLoader(
307
            $container,
308
            new FileLocator(__DIR__ . '/../../../Publish/Core/settings')
309
        );
310
        $coreLoader->load('repository.yml');
311
        $coreLoader->load('fieldtype_external_storages.yml');
312
        $coreLoader->load('fieldtypes.yml');
313
        $coreLoader->load('indexable_fieldtypes.yml');
314
        $coreLoader->load('roles.yml');
315
        $coreLoader->load('storage_engines/common.yml');
316
        $coreLoader->load('storage_engines/cache.yml');
317
        $coreLoader->load('storage_engines/legacy.yml');
318
        $coreLoader->load('storage_engines/shortcuts.yml');
319
        $coreLoader->load('search_engines/common.yml');
320
        $coreLoader->load('utils.yml');
321
        $coreLoader->load('io.yml');
322
323
        // Public API services
324
        $loader->load('papi.yml');
325
326
        // Built-in field types
327
        $loader->load('fieldtype_services.yml');
328
329
        // Storage engine
330
        $loader->load('storage_engines.yml');
331
    }
332
333
    /**
334
     * Handle templating parameters.
335
     *
336
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
337
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
338
     */
339
    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...
340
    {
341
        $loader->load('templating.yml');
342
    }
343
344
    /**
345
     * Handle session parameters.
346
     *
347
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
348
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
349
     */
350
    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...
351
    {
352
        $loader->load('session.yml');
353
    }
354
355
    /**
356
     * Handle cache parameters.
357
     *
358
     * @param array $config
359
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
360
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
361
     *
362
     * @throws \InvalidArgumentException
363
     */
364
    private function handleCache(array $config, ContainerBuilder $container, FileLoader $loader)
365
    {
366
        $loader->load('cache.yml');
367
368
        $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...
369
        if (isset($config['http_cache']['purge_type'])) {
370
            $container->setParameter('ezpublish.http_cache.purge_type', $config['http_cache']['purge_type']);
371
        }
372
    }
373
374
    /**
375
     * Handle locale parameters.
376
     *
377
     * @param array $config
378
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
379
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
380
     */
381
    private function handleLocale(array $config, ContainerBuilder $container, FileLoader $loader)
382
    {
383
        $loader->load('locale.yml');
384
        $container->setParameter(
385
            'ezpublish.locale.conversion_map',
386
            $config['locale_conversion'] + $container->getParameter('ezpublish.locale.conversion_map')
387
        );
388
    }
389
390
    /**
391
     * Handle helpers.
392
     *
393
     * @param array $config
394
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
395
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
396
     */
397
    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...
398
    {
399
        $loader->load('helpers.yml');
400
    }
401
402
    /**
403
     * Handles relation between SiteAccesses.
404
     * Related SiteAccesses share the same repository and root location id.
405
     *
406
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
407
     */
408
    private function handleSiteAccessesRelation(ContainerBuilder $container)
409
    {
410
        $configResolver = $container->get('ezpublish.config.resolver.core');
411
        $configResolver->setContainer($container);
412
413
        $saRelationMap = [];
414
        $saList = $container->getParameter('ezpublish.siteaccess.list');
415
        // First build the SiteAccess relation map, indexed by repository and rootLocationId.
416
        foreach ($saList as $sa) {
417
            $repository = $configResolver->getParameter('repository', 'ezsettings', $sa);
418
            if (!isset($saRelationMap[$repository])) {
419
                $saRelationMap[$repository] = [];
420
            }
421
422
            $rootLocationId = $configResolver->getParameter('content.tree_root.location_id', 'ezsettings', $sa);
423
            if (!isset($saRelationMap[$repository][$rootLocationId])) {
424
                $saRelationMap[$repository][$rootLocationId] = [];
425
            }
426
            $saRelationMap[$repository][$rootLocationId][] = $sa;
427
        }
428
        $container->setParameter('ezpublish.siteaccess.relation_map', $saRelationMap);
429
430
        // Now build the related SiteAccesses list, based on the relation map.
431
        foreach ($saList as $sa) {
432
            $repository = $configResolver->getParameter('repository', 'ezsettings', $sa);
433
            $rootLocationId = $configResolver->getParameter('content.tree_root.location_id', 'ezsettings', $sa);
434
            $container->setParameter(
435
                "ezsettings.$sa.related_siteaccesses",
436
                $saRelationMap[$repository][$rootLocationId]
437
            );
438
        }
439
    }
440
441
    /**
442
     * @param array $config
443
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
444
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
445
     */
446
    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...
447
    {
448
        $loader->load('image.yml');
449
    }
450
451
    /**
452
     * @param array $config
453
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
454
     * @param \Symfony\Component\DependencyInjection\Loader\FileLoader $loader
455
     */
456
    private function handleStashCache(array $config, ContainerBuilder $container, FileLoader $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $loader 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...
457
    {
458
        if (isset($config['stash_cache']['igbinary'])) {
459
            $container->setParameter('ezpublish.stash_cache.igbinary', $config['stash_cache']['igbinary']);
460
        }
461
462
        if (isset($config['stash_cache']['lzf'])) {
463
            $container->setParameter('ezpublish.stash_cache.lzf', $config['stash_cache']['lzf']);
464
        }
465
    }
466
467
    private function buildPolicyMap(ContainerBuilder $container)
468
    {
469
        $policiesBuilder = new PoliciesConfigBuilder($container);
470
        foreach ($this->policyProviders as $provider) {
471
            $provider->addPolicies($policiesBuilder);
472
        }
473
    }
474
475 View Code Duplication
    public function prepend(ContainerBuilder $container)
476
    {
477
        // Default settings for FOSHttpCacheBundle
478
        $configFile = __DIR__ . '/../Resources/config/fos_http_cache.yml';
479
        $config = Yaml::parse(file_get_contents($configFile));
480
        $container->prependExtensionConfig('fos_http_cache', $config);
481
        $container->addResource(new FileResource($configFile));
482
    }
483
484
    /**
485
     * Adds a new policy provider to the internal collection.
486
     * One can call this method from a bundle `build()` method.
487
     *
488
     * ```php
489
     * public function build(ContainerBuilder $container)
490
     * {
491
     *     $ezExtension = $container->getExtension('ezpublish');
492
     *     $ezExtension->addPolicyProvider($myPolicyProvider);
493
     * }
494
     * ```
495
     *
496
     * @since 6.0
497
     *
498
     * @param PolicyProviderInterface $policyProvider
499
     */
500
    public function addPolicyProvider(PolicyProviderInterface $policyProvider)
501
    {
502
        $this->policyProviders[] = $policyProvider;
503
    }
504
505
    /**
506
     * Adds a new config parser to the internal collection.
507
     * One can call this method from a bundle `build()` method.
508
     *
509
     * ```php
510
     * public function build(ContainerBuilder $container)
511
     * {
512
     *     $ezExtension = $container->getExtension('ezpublish');
513
     *     $ezExtension->addConfigParser($myConfigParser);
514
     * }
515
     * ```
516
     *
517
     * @since 6.0
518
     *
519
     * @param \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ParserInterface $configParser
520
     */
521
    public function addConfigParser(ParserInterface $configParser)
522
    {
523
        $this->configParsers[] = $configParser;
524
    }
525
526
    /**
527
     * Adds new default settings to the internal collection.
528
     * One can call this method from a bundle `build()` method.
529
     *
530
     * ```php
531
     * public function build(ContainerBuilder $container)
532
     * {
533
     *     $ezExtension = $container->getExtension('ezpublish');
534
     *     $ezExtension->addDefaultSettings(
535
     *         __DIR__ . '/Resources/config',
536
     *         ['default_settings.yml']
537
     *     );
538
     * }
539
     * ```
540
     *
541
     * @since 6.0
542
     *
543
     * @param string $fileLocation
544
     * @param array $files
545
     */
546
    public function addDefaultSettings($fileLocation, array $files)
547
    {
548
        $this->defaultSettingsCollection[$fileLocation] = $files;
549
    }
550
}
551