Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
24 / 24 |
| RouteLoader | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
24 / 24 |
| load | |
100.00% |
1 / 1 |
2 | |
100.00% |
8 / 8 |
|||
| connect | |
100.00% |
1 / 1 |
3 | |
100.00% |
16 / 16 |
|||
| supports | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| 1 | <?php |
| 2 | |
| 3 | // {{{ License |
| 4 | |
| 5 | // This file is part of GNU social - https://www.gnu.org/software/social |
| 6 | // |
| 7 | // GNU social is free software: you can redistribute it and/or modify |
| 8 | // it under the terms of the GNU Affero General Public License as published by |
| 9 | // the Free Software Foundation, either version 3 of the License, or |
| 10 | // (at your option) any later version. |
| 11 | // |
| 12 | // GNU social is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU Affero General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU Affero General Public License |
| 18 | // along with GNU social. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | // }}} |
| 21 | |
| 22 | /** |
| 23 | * Dynamic router loader and URLMapper interface atop Symfony's router |
| 24 | * |
| 25 | * Converts a path into a set of parameters, and vice versa |
| 26 | * |
| 27 | * @package GNUsocial |
| 28 | * @category URL |
| 29 | * |
| 30 | * @author Hugo Sales <hugo@hsal.es> |
| 31 | * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org |
| 32 | * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later |
| 33 | */ |
| 34 | |
| 35 | namespace App\Core\Router; |
| 36 | |
| 37 | use App\Core\Event; |
| 38 | use Symfony\Component\Config\Loader\Loader; |
| 39 | use Symfony\Component\Routing\Route; |
| 40 | use Symfony\Component\Routing\RouteCollection; |
| 41 | |
| 42 | class RouteLoader extends Loader |
| 43 | { |
| 44 | private RouteCollection $rc; |
| 45 | |
| 46 | /** |
| 47 | * Route loading entry point, called from `config/routes.php` |
| 48 | * |
| 49 | * Must conform to symfony's interface, but the $resource is unused |
| 50 | * and $type must not be null |
| 51 | * |
| 52 | * @param mixed $resource |
| 53 | */ |
| 54 | public function load($resource, ?string $type = null): RouteCollection |
| 55 | { |
| 56 | $this->rc = new RouteCollection(); |
| 57 | |
| 58 | $route_files = glob(INSTALLDIR . '/src/Routes/*.php'); |
| 59 | foreach ($route_files as $file) { |
| 60 | require_once $file; |
| 61 | $ns = '\\App\\Routes\\' . basename($file, '.php'); |
| 62 | $ns::load($this); |
| 63 | } |
| 64 | |
| 65 | Event::handle('AddRoute', [&$this]); |
| 66 | |
| 67 | return $this->rc; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Connect a route to a controller |
| 72 | * |
| 73 | * @param string $id Route unique id, used to generate urls, for instance |
| 74 | * @param string $uri_path Path, possibly with {param}s |
| 75 | * @param mixed $target Some kind of callable, typically class with `__invoke` or [object, method] |
| 76 | * @param null|array $param_reqs Array of {param} => regex |
| 77 | * @param null|array $options Possible keys are ['condition', 'defaults', 'format', |
| 78 | * 'fragment', 'http-methods', 'locale', 'methods', 'schemes', 'accept'] |
| 79 | * 'http-methods' and 'methods' are aliases |
| 80 | */ |
| 81 | public function connect(string $id, string $uri_path, $target, ?array $options = [], ?array $param_reqs = []) |
| 82 | { |
| 83 | $this->rc->add($id, |
| 84 | new Route( |
| 85 | // path -- URI path |
| 86 | path: $uri_path, |
| 87 | // defaults = [] -- param default values, |
| 88 | // and special configuration options |
| 89 | defaults: array_merge( |
| 90 | [ |
| 91 | '_controller' => is_array($target) ? $target : [$target, '__invoke'], |
| 92 | '_format' => $options['format'] ?? 'html', |
| 93 | '_fragment' => $options['fragment'] ?? '', |
| 94 | '_locale' => $options['locale'] ?? 'en', |
| 95 | 'template' => $options['template'] ?? '', |
| 96 | ], |
| 97 | $options['defaults'] ?? [] |
| 98 | ), |
| 99 | // requirements = [] -- param => regex |
| 100 | requirements: $param_reqs, |
| 101 | // options = [] -- possible keys: compiler_class:, utf8 |
| 102 | // No need for a special compiler class for now, |
| 103 | // Enforce UTF8 |
| 104 | options: ['utf8' => true], |
| 105 | // host = '' -- hostname (subdomain, for instance) to match, |
| 106 | // we don't want this |
| 107 | host: '', |
| 108 | // schemes = [] -- URI schemes (https, ftp and such) |
| 109 | schemes: $options['schemes'] ?? [], |
| 110 | // methods = [] -- HTTP methods |
| 111 | methods: $options['http-methods'] ?? $options['methods'] ?? [], |
| 112 | // condition = '' -- Symfony condition expression, |
| 113 | // see https://symfony.com/doc/current/routing.html#matching-expressions |
| 114 | condition: isset($options['accept']) ? "request.headers.get('Accept') in " . json_encode($options['accept']) : ($options['condition'] ?? '') |
| 115 | ) |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Whether this loader supports loading this route type |
| 121 | * Passed the arguments from the `RoutingConfigurator::import` call from |
| 122 | * `config/routes.php` |
| 123 | * |
| 124 | * @param mixed $resource |
| 125 | * @codeCoverageIgnore |
| 126 | */ |
| 127 | public function supports($resource, ?string $type = null): bool |
| 128 | { |
| 129 | return 'GNUsocial' === $type; |
| 130 | } |
| 131 | } |