Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

PagePartConfigurationParser.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\PagePartBundle\PagePartConfigurationReader;
4
5
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdminConfigurator;
6
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdminConfiguratorInterface;
7
use Symfony\Component\HttpKernel\KernelInterface;
8
use Symfony\Component\Yaml\Yaml;
9
10
class PagePartConfigurationParser implements PagePartConfigurationParserInterface
11
{
12
    /**
13
     * @var KernelInterface
14
     */
15
    private $kernel;
16
17
    private $presets = [];
18
19
    private $stack = [];
20
21
    /**
22
     * @param KernelInterface $kernel
23
     * @param array           $presets
24
     */
25
    public function __construct(KernelInterface $kernel, array $presets = [])
26
    {
27
        $this->kernel = $kernel;
28
        $this->presets = $presets;
29
    }
30
31
32
    /**
33
     * This will read the $name file and parse it to the PageTemplate
34
     *
35
     * @param string $name
36
     * @param PagePartAdminConfiguratorInterface[] $existing
37
     *
38
     * @return PagePartAdminConfiguratorInterface
39
     * @throws \Exception
40
     */
41
    public function parse($name, array $existing = [])
42
    {
43
        if (in_array($name, $this->stack)) {
44
            throw new \RuntimeException(sprintf('Recursion detected when parsing %s -> %s', implode(' -> ', $this->stack), $name));
45
        }
46
        $this->stack[] = $name;
47
48
        $value = $this->getValue($name);
49
50
        if (!array_key_exists('types', $value)) {
51
            $value['types'] = [];
52
        }
53
54
        if (array_key_exists('extends', $value)) {
55
56
            $namespace = '';
57
            if (false !== strpos($name, ':')) {
58
                $namespace = substr($name, 0, strpos($name, ':') + 1);
59
            }
60
61
            foreach ((array)$value['extends'] as $extend) {
62
                if (false === strpos($extend, ':')) {
63
                    $extend = $namespace . $extend;
64
                }
65
66
                if (false === isset($existing[$extend])) {
67
                    $existing[$extend] = $this->parse($extend, $existing);
68
                }
69
70
                $value['types'] = array_merge($existing[$extend]->getPossiblePagePartTypes(), $value['types']);
71
            }
72
        }
73
74
        $types = [];
75
        foreach ($value['types'] as $type) {
76
            if ("" === (string)$type['class']) {
77
                unset($types[$type['name']]);
78
                continue;
79
            }
80
81
            $types[$type['name']] = ['name' => $type['name'], 'class' => $type['class'], 'preview' => array_key_exists('preview', $type) ? $type['preview'] : ""];
82
            if (isset($type['pagelimit'])) {
83
                $types[$type['name']]['pagelimit'] = $type['pagelimit'];
84
            }
85
        }
86
87
        $result = new PagePartAdminConfigurator();
88
        $result->setName($value['name']);
89
        $result->setInternalName($name);
90
        $result->setPossiblePagePartTypes(array_values($types));
91
        $result->setContext($value['context']);
92
93
        if (isset($value['widget_template'])) {
94
            $result->setWidgetTemplate($value['widget_template']);
95
        }
96
97
98
        array_pop($this->stack);
99
100
        return $result;
101
    }
102
103
    private function getValue($name)
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
104
    {
105
        if (isset($this->presets[$name])) {
106
            return $this->presets[$name];
107
        }
108
109
        $nameParts = explode(':', $name);
110
        if (2 !== count($nameParts)) {
111
            throw new \Exception(sprintf('Malformed namespaced configuration name "%s" (expecting "namespace:pagename").',
112
                $name));
113
        }
114
115
        list ($namespace, $name) = $nameParts;
116
        $path = $this->kernel->locateResource('@' . $namespace . '/Resources/config/pageparts/' . $name . '.yml');
117
        $value = Yaml::parse(file_get_contents($path));
118
119
        return $value;
120
121
    }
122
}
123