CoreExtension::loadWorkflowDefinitions()   A
last analyzed

Complexity

Conditions 4
Paths 21

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 21
nop 1
dl 0
loc 18
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\CoreBundle\DependencyInjection;
15
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Extension\Extension;
19
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
20
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
21
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
22
use Symfony\Component\Finder\Finder;
23
use Zikula\CoreBundle\Api\LocaleApi;
24
use Zikula\CoreBundle\EventSubscriber\ClickjackProtectionSubscriber;
25
use Zikula\CoreBundle\EventSubscriber\SiteOffSubscriber;
26
use Zikula\CoreBundle\Site\SiteDefinition;
27
28
class CoreExtension extends Extension implements PrependExtensionInterface
29
{
30
    private array $workflowDirectories = [];
31
32
    public function load(array $configs, ContainerBuilder $container): void
33
    {
34
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
35
        $loader->load('services.yaml');
36
37
        $configuration = new Configuration();
38
        $config = $this->processConfiguration($configuration, $configs);
39
40
        $container->setParameter('data_directory', $config['datadir']);
41
42
        $container->getDefinition(ClickjackProtectionSubscriber::class)
43
            ->setArgument('$xFrameOptions', $config['x_frame_options']);
44
45
        $container->getDefinition(SiteOffSubscriber::class)
46
            ->setArgument('$maintenanceModeEnabled', $config['maintenance_mode']['enabled'])
47
            ->setArgument('$maintenanceReason', $config['maintenance_mode']['reason']);
48
49
        $container->setParameter('enable_mail_logging', $config['enable_mail_logging']);
50
51
        $container->getDefinition(LocaleApi::class)
52
            ->setArgument('$multiLingualEnabled', $config['multilingual']);
53
54
        $container->getDefinition(SiteDefinition::class)
55
            ->setArgument('$siteData', $config['site_data']);
56
    }
57
58
    public function getNamespace(): string
59
    {
60
        return 'http://symfony.com/schema/dic/symfony';
61
    }
62
63
    public function prepend(ContainerBuilder $container): void
64
    {
65
        // bundles may define their workflows in: <bundlePath>/Resources/workflows/
66
        $bundleMetaData = $container->getParameter('kernel.bundles_metadata');
67
        foreach ($bundleMetaData as $bundleName => $metaData) {
68
            $workflowPath = $metaData['path'] . '/Resources/workflows';
69
            if (!file_exists($workflowPath)) {
70
                continue;
71
            }
72
            $this->workflowDirectories[] = $workflowPath;
73
        }
74
75
        // also it is possible to define custom workflows in: config/workflows/
76
        $this->workflowDirectories[] = $container->getParameter('kernel.project_dir') . '/config/workflows';
77
78
        $this->loadWorkflowDefinitions($container);
79
    }
80
81
    /**
82
     * Loads workflow files from given directories.
83
     */
84
    private function loadWorkflowDefinitions(ContainerBuilder $container): void
85
    {
86
        try {
87
            $finder = new Finder();
88
            $finder->files()->name(['*.yml', '*.yaml'])->in($this->workflowDirectories);
89
            foreach ($finder as $file) {
90
                $filePath = $file->getPath();
91
                $loader = new YamlFileLoader($container, new FileLocator($filePath));
92
                $loader->load($file->getFilename());
93
            }
94
95
            $finder = new Finder();
96
            $finder->files()->name('*.xml')->in($this->workflowDirectories);
97
            foreach ($finder as $file) {
98
                $loader = new XmlFileLoader($container, new FileLocator($file->getPath()));
99
                $loader->load($file->getFilename());
100
            }
101
        } catch (\InvalidArgumentException) {
102
            // no bundle with a workflow directory exists, ignore
103
        }
104
    }
105
}
106