Passed
Push — master ( 5a0590...0c08dd )
by Andreas
49:13 queued 24:34
created

loader::get_yaml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.routing
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\routing;
10
11
use Symfony\Component\Config\Loader\Loader as base;
12
use Symfony\Component\Routing\RouteCollection;
13
use Symfony\Component\Routing\Route;
14
use midcom;
15
use midcom_baseclasses_components_configuration;
16
use Symfony\Component\Routing\Loader\YamlFileLoader;
17
use Symfony\Component\Config\FileLocator;
18
19
/**
20
 * @package midcom.routing
21
 */
22
class loader extends base
23
{
24
    /**
25
     * @var YamlFileLoader
26
     */
27
    private $yaml_loader;
28
29
    /**
30
     * {@inheritDoc}
31
     * @see \Symfony\Component\Config\Loader\LoaderInterface::load()
32
     */
33 78
    public function load($input, $type = null)
34
    {
35 78
        if (is_string($input)) {
36 33
            if (!$this->is_legacy($input)) {
37 32
                return $this->get_yaml()->load($this->get_path($input, 'yml'), $type);
38
            }
39 1
            $input = $this->get_legacy_routes($input);
40
        }
41
42 46
        $collection = new RouteCollection();
43
44 46
        foreach ($input as $name => $config) {
45 45
            $path = '/';
46 45
            $requirements = [];
47
48 45
            if (empty($config['fixed_args'])) {
49 45
                $config['fixed_args'] = [];
50
            } else {
51 45
                $config['fixed_args'] = (array) $config['fixed_args'];
52 45
                $path = '/' . implode('/', $config['fixed_args']) . '/';
53
            }
54
55 45
            if (!array_key_exists('variable_args', $config)) {
56 45
                $config['variable_args'] = 0;
57
            }
58 45
            for ($i = 0; $i < $config['variable_args']; $i++) {
59 45
                $path .= '{args_' . $i . '}/';
60 45
                if (!empty($config['validation'][$i])) {
61 17
                    $requirements['args_' . $i] = $this->translate_validation($config['validation'][$i]);
62
                }
63
            }
64
65
            $defaults = [
66 45
                '_controller' => implode('::', (array) $config['handler'])
67
            ];
68
69 45
            $route = new Route($path, $defaults, $requirements);
70 45
            $collection->add($name, $route);
71
        }
72
73 46
        return $collection;
74
    }
75
76
    /**
77
     * Small transitory helper for old-style route validation configs
78
     *
79
     * @param array $input
80
     */
81 17
    private function translate_validation(array $input) : string
82
    {
83 17
        foreach ($input as &$value) {
84 17
            if (in_array($value, ['is_numeric', 'is_int'])) {
85 17
                $value = '\d+';
86
            } elseif ($value == 'mgd_is_guid') {
87
                $value = '[0-9a-f]{21,80}';
88
            }
89
        }
90 17
        return implode('|', $input);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function supports($resource, $type = null)
97
    {
98
        if (is_string($resource)) {
99
            if (!$this->is_legacy($resource)) {
100
                return $this->get_yaml()->supports($this->get_path($resource, 'yml'), $type);
101
            }
102
            return (!$type || 'string' === $type);
103
        }
104
105
        return is_array($resource) && (!$type || 'array' === $type);
106
    }
107
108
    /**
109
     * @param string $component
110
     */
111 341
    private function is_legacy(string $component) : bool
112
    {
113 341
        return !file_exists($this->get_path($component, 'yml'));
114
    }
115
116 32
    private function get_yaml() : YamlFileLoader
117
    {
118 32
        if (empty($this->yaml_loader)) {
119 32
            $this->yaml_loader = new YamlFileLoader(new FileLocator);
120
        }
121 32
        return $this->yaml_loader;
122
    }
123
124 341
    private function get_path(string $component, string $suffix) : string
125
    {
126 341
        return midcom::get()->componentloader->path_to_snippetpath($component) . '/config/routes.' . $suffix;
127
    }
128
129
    /**
130
     * @param string $component
131
     */
132 341
    public function get_legacy_routes($component) : array
133
    {
134 341
        if (!$this->is_legacy($component)) {
135 221
            return [];
136
        }
137
138 121
        return $this->load_routes($component);
139
    }
140
141 121
    private function load_routes(string $component) : array
142
    {
143 121
        $path = $this->get_path($component, 'inc');
144
        // Load and parse the global config
145 121
        return midcom_baseclasses_components_configuration::read_array_from_file($path);
146
    }
147
}
148