Completed
Push — master ( 9684f5...5d31f9 )
by Sander
36:56 queued 12:48
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
     * This will read the $name file and parse it to the PageTemplate
33
     *
34
     * @param string                               $name
35
     * @param PagePartAdminConfiguratorInterface[] $existing
36
     *
37
     * @return PagePartAdminConfiguratorInterface
38
     *
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
            $namespace = '';
56
            if (false !== strpos($name, ':')) {
57
                $namespace = substr($name, 0, strpos($name, ':') + 1);
58
            }
59
60
            foreach ((array) $value['extends'] as $extend) {
61
                if (false === strpos($extend, ':')) {
62
                    $extend = $namespace . $extend;
63
                }
64
65
                if (false === isset($existing[$extend])) {
66
                    $existing[$extend] = $this->parse($extend, $existing);
67
                }
68
69
                $value['types'] = array_merge($existing[$extend]->getPossiblePagePartTypes(), $value['types']);
70
            }
71
        }
72
73
        $types = [];
74
        foreach ($value['types'] as $type) {
75
            if ('' === (string) $type['class']) {
76
                unset($types[$type['name']]);
77
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
        array_pop($this->stack);
98
99
        return $result;
100
    }
101
102
    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...
103
    {
104
        if (isset($this->presets[$name])) {
105
            return $this->presets[$name];
106
        }
107
108
        $nameParts = explode(':', $name);
109
        if (2 !== count($nameParts)) {
110
            throw new \Exception(sprintf('Malformed namespaced configuration name "%s" (expecting "namespace:pagename").',
111
                $name));
112
        }
113
114
        list($namespace, $name) = $nameParts;
115
        $path = $this->kernel->locateResource('@' . $namespace . '/Resources/config/pageparts/' . $name . '.yml');
116
        $value = Yaml::parse(file_get_contents($path));
117
118
        return $value;
119
    }
120
}
121