Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
MiddlewareExtensionsMapper
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
8.01
0.00% covered (danger)
0.00%
0 / 1
 map
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
8.01
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2022 (original work) Open Assessment Technologies SA;
19 *
20 * @author Gabriel Felipe Soares <gabriel.felipe.soares@taotesting.com>
21 */
22
23declare(strict_types=1);
24
25namespace oat\generis\model\Middleware;
26
27use common_ext_Extension;
28
29class MiddlewareExtensionsMapper
30{
31    public const MAP_KEY = 'middlewaresMap';
32
33    /**
34     * @param common_ext_Extension[] $extensions
35     */
36    public function map(array $extensions): array
37    {
38        $middlewareMap = [];
39
40        foreach ($extensions as $extension) {
41            foreach ($extension->getManifest()->getMiddlewares() as $map) {
42                if (!in_array(MiddlewareConfigInterface::class, class_implements($map), true)) {
43                    continue;
44                }
45
46                $middlewareConfigs = (new $map())();
47
48                /** @var MiddlewareMapInterface $middlewareConfig */
49                foreach ($middlewareConfigs as $middlewareConfig) {
50                    foreach ($middlewareConfig->getRoutes() as $route) {
51                        $methods = empty($middlewareConfig->getHttpMethods())
52                            ? '(.*)'
53                            : ('(' . implode('|', (array)$middlewareConfig->getHttpMethods()) . ')');
54
55                        $route = strpos($route, '/') > 0 ? ('/' . $route) : $route;
56                        $route = addslashes($route);
57                        $route = str_replace('/', '\/', $route);
58                        $route = '/^' . $methods . $route . '$/';
59
60                        $middlewareMap[$route] = $middlewareMap[$route] ?? [];
61                        $middlewareMap[$route][] = $middlewareConfig->jsonSerialize();
62                    }
63                }
64            }
65        }
66
67        return $middlewareMap;
68    }
69}