ContainerBuilder::getThirdPartyBlocks()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 8
nop 0
1
<?php
2
3
/**
4
 * This file is part of Bldr.io
5
 *
6
 * (c) Aaron Scherer <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE
10
 */
11
12
namespace Bldr\DependencyInjection;
13
14
use Bldr\Application;
15
use Bldr\Block;
16
use Bldr\Config;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\DependencyInjection\ContainerBuilder as BaseContainerBuilder;
20
use Symfony\Component\Yaml\Yaml;
21
22
/**
23
 * @author Aaron Scherer <[email protected]>
24
 */
25
class ContainerBuilder extends BaseContainerBuilder
26
{
27
    /**
28
     * @param Application     $application
29
     * @param InputInterface  $input
30
     * @param OutputInterface $output
31
     */
32
    public function __construct(Application $application, InputInterface $input, OutputInterface $output)
33
    {
34
        parent::__construct();
35
36
        $this->set('application', $application);
37
        $this->set('helper_set', $application->getHelperSet());
38
        $this->set('input', $input);
39
        $this->set('output', $output);
40
    }
41
42
    /**
43
     * Compiles the container with third party blocks
44
     */
45
    public function compile()
46
    {
47
        if (null !== $this->parameterBag) {
48
            $blocks = $this->getCoreBlocks();
49
            $blocks = array_merge($blocks, $this->getThirdPartyBlocks());
50
            foreach ($blocks as $block) {
51
                $this->prepareBlock($block);
52
            }
53
        }
54
55
        Config::read($this);
56
57
        parent::compile();
58
    }
59
60
    /**
61
     * Gets all the third party blocks from .bldr/blocks.yml
62
     *
63
     * @return array
64
     */
65
    public function getThirdPartyBlocks()
66
    {
67
        /** @var Application $application */
68
        $application = $this->get('application');
69
        $embeddedComposer = $application->getEmbeddedComposer();
70
        $config = $embeddedComposer->getExternalComposerConfig();
71
        $loadBlock = $config->has('block-loader') ? $config->get('block-loader') : '.bldr/blocks.yml';
72
        $blockFile = $embeddedComposer->getExternalRootDirectory().DIRECTORY_SEPARATOR.$loadBlock;
73
74
        if (!file_exists($blockFile)) {
75
            return [];
76
        }
77
78
        $blockNames = Yaml::parse(file_get_contents($blockFile));
79
        if (null === $blockNames) {
80
            return [];
81
        }
82
83
        $blocks     = [];
84
        foreach ($blockNames as $block) {
0 ignored issues
show
Bug introduced by
The expression $blockNames of type array|string|object<stdClass> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
85
            $blocks[] = new $block();
86
        }
87
88
        return $blocks;
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    private function getCoreBlocks()
95
    {
96
        return [
97
            new Block\Core\BldrBlock(),
98
            new Block\Execute\ExecuteBlock(),
99
            new Block\Filesystem\FilesystemBlock(),
100
            new Block\Notify\NotifyBlock(),
101
            new Block\Watch\WatchBlock(),
102
            new Block\Miscellaneous\MiscellaneousBlock(),
103
            new Block\Frontend\FrontendBlock(),
104
            new Block\Remote\RemoteBlock()
105
        ];
106
    }
107
108
    /**
109
     * @param AbstractBlock $block
110
     */
111
    private function prepareBlock(AbstractBlock $block)
112
    {
113
        $this->registerExtension($block);
114
        $this->loadFromExtension($block->getAlias());
115
        foreach ($block->getCompilerPasses() as $pass) {
116
            $this->addCompilerPass($pass);
117
        }
118
    }
119
}
120