Completed
Push — master ( 5c7245...c04fc7 )
by Craig
06:16
created

ZikulaWorkflowExtension::loadWorkflowDefinitions()   C

Complexity

Conditions 7
Paths 66

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 24
nc 66
nop 1
dl 0
loc 38
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
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 Zikula\Bundle\WorkflowBundle\DependencyInjection;
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
17
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
19
use Symfony\Component\Finder\Finder;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
use Symfony\Component\Workflow;
22
23
/**
24
 * Class ZikulaWorkflowExtension
25
 */
26
class ZikulaWorkflowExtension extends Extension implements PrependExtensionInterface
27
{
28
    private $workflowDirectories = [];
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function load(array $configs, ContainerBuilder $container)
34
    {
35
        // unrequired
36
        //$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
37
        //$loader->load('services.yml');
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function prepend(ContainerBuilder $container)
44
    {
45
        // central workflows in the core system are placed in: lib/Zikula/Bundle/CoreBundle/Resources/workflows/
46
        $this->workflowDirectories[] = __DIR__ . '/../Resources/workflows';
47
48
        $rootDirectory = $container->getParameter('kernel.root_dir');
49
50
        // Modules can define their own workflows in: modules/Acme/MyBundle/Resources/workflows/
51
        $this->workflowDirectories[] = $rootDirectory . '/../system/*/Resources/workflows';
52
        $this->workflowDirectories[] = $rootDirectory . '/../modules/*/*/Resources/workflows';
53
54
        // also it is possible to define custom workflows (or override existing ones) in: app/Resources/workflows/
55
        $this->workflowDirectories[] = $rootDirectory . '/Resources/workflows';
56
57
        $this->loadWorkflowDefinitions($container);
58
    }
59
60
    /**
61
     * Loads workflow files from given directories.
62
     *
63
     * @param ContainerBuilder $container
64
     *
65
     * @todo replace Finder usage by glob pattern in master branch
66
     * @see http://symfony.com/blog/new-in-symfony-3-3-import-config-files-with-glob-patterns
67
     */
68
    private function loadWorkflowDefinitions(ContainerBuilder $container)
69
    {
70
        // get all bundles
71
        $bundleNames = [];
72
        $bundles = $container->getParameter('kernel.bundles');
73
        foreach ($bundles as $bundleName => $bundle) {
74
            $bundleNames[] = $bundleName;
75
        }
76
77
        try {
78
            $finder = new Finder();
79
            $finder->files()->name('*.yml')->in($this->workflowDirectories);
80
            foreach ($finder as $file) {
81
                $filePath = $file->getPath();
82
                if (false !== strpos($filePath, 'modules/')) {
83
                    // fallback for uninstalled modules
84
                    $directoryParts = explode('/', str_replace('/Resources/workflows', '', $filePath));
85
                    // @todo this does not work if the module is installed in a custom folder
86
                    $moduleName = array_pop($directoryParts);
87
                    $moduleName = array_pop($directoryParts) . $moduleName;
88
                    if (!in_array($moduleName, $bundleNames)) {
89
                        continue;
90
                    }
91
                }
92
                $loader = new YamlFileLoader($container, new FileLocator($filePath));
93
                $loader->load($file->getFilename());
94
            }
95
96
            $finder = new Finder();
97
            $finder->files()->name('*.xml')->in($this->workflowDirectories);
98
            foreach ($finder as $file) {
99
                $loader = new XmlFileLoader($container, new FileLocator($file->getPath()));
100
                $loader->load($file->getFilename());
101
            }
102
        } catch (\InvalidArgumentException $e) {
103
            // no module with a workflow directory exists, ignore
104
        }
105
    }
106
}
107