2020-03-21 18:53:25 +00:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2020-05-20 17:53:53 +01:00
|
|
|
// {{{ License
|
2020-06-23 13:43:19 +01:00
|
|
|
|
2020-05-20 17:53:53 +01:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
2020-06-23 13:43:19 +01:00
|
|
|
|
2020-05-20 17:53:53 +01:00
|
|
|
// }}}
|
|
|
|
|
2020-03-21 18:53:25 +00:00
|
|
|
/**
|
2020-03-21 20:18:05 +00:00
|
|
|
* Dynamic router loader and URLMapper interface atop Symfony's router
|
2020-03-21 18:53:25 +00:00
|
|
|
*
|
|
|
|
* Converts a path into a set of parameters, and vice versa
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
|
|
|
* @category URL
|
|
|
|
*
|
2021-02-19 23:29:43 +00:00
|
|
|
* @author Hugo Sales <hugo@hsal.es>
|
|
|
|
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
|
2020-03-21 18:53:25 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2020-06-04 22:00:05 +01:00
|
|
|
namespace App\Core\Router;
|
2020-03-21 18:53:25 +00:00
|
|
|
|
2020-08-03 21:56:48 +01:00
|
|
|
use App\Core\Event;
|
2020-03-21 18:53:25 +00:00
|
|
|
use Symfony\Component\Config\Loader\Loader;
|
|
|
|
use Symfony\Component\Routing\Route;
|
|
|
|
use Symfony\Component\Routing\RouteCollection;
|
|
|
|
|
|
|
|
class RouteLoader extends Loader
|
|
|
|
{
|
|
|
|
private RouteCollection $rc;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Route loading entry point, called from `config/routes.php`
|
|
|
|
*
|
2020-05-10 21:43:15 +01:00
|
|
|
* Must conform to symfony's interface, but the $resource is unused
|
2020-03-21 18:53:25 +00:00
|
|
|
* and $type must not be null
|
|
|
|
*/
|
2020-06-23 13:43:19 +01:00
|
|
|
public function load($resource, ?string $type = null): RouteCollection
|
2020-03-21 18:53:25 +00:00
|
|
|
{
|
|
|
|
$this->rc = new RouteCollection();
|
|
|
|
|
|
|
|
$route_files = glob(INSTALLDIR . '/src/Routes/*.php');
|
2021-09-15 15:13:30 +01:00
|
|
|
$to_load = [];
|
2020-03-21 18:53:25 +00:00
|
|
|
foreach ($route_files as $file) {
|
|
|
|
require_once $file;
|
|
|
|
$ns = '\\App\\Routes\\' . basename($file, '.php');
|
2021-10-10 09:26:18 +01:00
|
|
|
if (\defined("{$ns}::LOAD_ORDER")) {
|
2021-09-15 15:13:30 +01:00
|
|
|
$to_load[$ns::LOAD_ORDER] = $ns;
|
|
|
|
} else {
|
|
|
|
$to_load[] = $ns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ksort($to_load);
|
|
|
|
foreach ($to_load as $ns) {
|
2020-03-21 18:53:25 +00:00
|
|
|
$ns::load($this);
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:22:59 +01:00
|
|
|
Event::handle('AddRoute', [&$this]);
|
2020-08-03 21:56:48 +01:00
|
|
|
|
2021-09-01 15:54:13 +01:00
|
|
|
// Sort routes so that whichever route has the smallest accept option matches first, as it's more specific
|
|
|
|
// This requires a copy, sadly, as it doesn't seem to be possible to modify the collection in-place
|
|
|
|
// However, this is fine since this gets cached
|
|
|
|
$it = $this->rc->getIterator();
|
2021-10-10 09:26:18 +01:00
|
|
|
$it->uasort(fn (Route $left, Route $right) => \count($left->getDefaults()['accept']) <=> \count($right->getDefaults()['accept']));
|
2021-09-01 15:54:13 +01:00
|
|
|
$this->rc = new RouteCollection();
|
|
|
|
foreach ($it as $id => $route) {
|
|
|
|
$this->rc->add($id, $route);
|
|
|
|
}
|
|
|
|
|
2020-03-21 18:53:25 +00:00
|
|
|
return $this->rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect a route to a controller
|
|
|
|
*
|
2020-06-21 15:00:14 +01:00
|
|
|
* @param string $id Route unique id, used to generate urls, for instance
|
|
|
|
* @param string $uri_path Path, possibly with {param}s
|
2020-06-23 13:43:19 +01:00
|
|
|
* @param mixed $target Some kind of callable, typically class with `__invoke` or [object, method]
|
2020-06-21 15:00:14 +01:00
|
|
|
* @param null|array $param_reqs Array of {param} => regex
|
2020-05-11 18:39:12 +01:00
|
|
|
* @param null|array $options Possible keys are ['condition', 'defaults', 'format',
|
2021-09-15 16:36:14 +01:00
|
|
|
* 'fragment', 'http-methods', 'locale', 'methods', 'schemes', 'accept', 'is_system_path']
|
2020-05-11 18:39:12 +01:00
|
|
|
* 'http-methods' and 'methods' are aliases
|
2020-03-21 18:53:25 +00:00
|
|
|
*/
|
2020-08-09 14:25:48 +01:00
|
|
|
public function connect(string $id, string $uri_path, $target, ?array $options = [], ?array $param_reqs = [])
|
2020-03-21 18:53:25 +00:00
|
|
|
{
|
2021-10-19 23:46:01 +01:00
|
|
|
// XXX: This hack can definitely be optimised by actually intersecting the arrays,
|
|
|
|
// maybe this helps: https://backbeat.tech/blog/symfony-routing-tricks-part-1
|
|
|
|
// see: https://symfony.com/index.php/doc/3.1/components/http_foundation.html#accessing-accept-headers-data
|
2021-10-21 14:45:18 +01:00
|
|
|
$accept_header_condition = '';
|
2021-10-19 23:46:01 +01:00
|
|
|
if (isset($options['accept'])) {
|
|
|
|
foreach ($options['accept'] as $accept) {
|
2021-10-21 14:45:18 +01:00
|
|
|
$accept_header_condition .= "('{$accept}' in request.getAcceptableContentTypes()) ||";
|
2021-10-19 23:46:01 +01:00
|
|
|
}
|
2021-10-21 14:45:18 +01:00
|
|
|
$accept_header_condition = mb_substr($accept_header_condition, 0, -3);
|
2021-10-19 23:46:01 +01:00
|
|
|
}
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
$this->rc->add(
|
|
|
|
$id,
|
2020-05-11 18:39:12 +01:00
|
|
|
new Route(
|
2020-06-23 13:43:19 +01:00
|
|
|
// path -- URI path
|
2021-07-28 22:36:24 +01:00
|
|
|
path: $uri_path,
|
2020-05-11 18:39:12 +01:00
|
|
|
// defaults = [] -- param default values,
|
|
|
|
// and special configuration options
|
2021-07-28 22:36:24 +01:00
|
|
|
defaults: array_merge(
|
2020-05-11 18:39:12 +01:00
|
|
|
[
|
2021-10-10 09:26:18 +01:00
|
|
|
'_controller' => \is_array($target) ? $target : [$target, '__invoke'],
|
2021-09-15 16:36:14 +01:00
|
|
|
'_format' => $options['format'] ?? 'html',
|
|
|
|
'_fragment' => $options['fragment'] ?? '',
|
|
|
|
'_locale' => $options['locale'] ?? 'en',
|
|
|
|
'template' => $options['template'] ?? '',
|
|
|
|
'accept' => $options['accept'] ?? [],
|
|
|
|
'is_system_path' => $options['is_system_path'] ?? true,
|
2020-05-11 18:39:12 +01:00
|
|
|
],
|
2021-10-10 09:26:18 +01:00
|
|
|
$options['defaults'] ?? [],
|
2020-08-09 14:25:48 +01:00
|
|
|
),
|
2020-05-11 18:39:12 +01:00
|
|
|
// requirements = [] -- param => regex
|
2021-07-28 22:36:24 +01:00
|
|
|
requirements: $param_reqs,
|
2020-05-11 18:39:12 +01:00
|
|
|
// options = [] -- possible keys: compiler_class:, utf8
|
|
|
|
// No need for a special compiler class for now,
|
|
|
|
// Enforce UTF8
|
2021-07-28 22:36:24 +01:00
|
|
|
options: ['utf8' => true],
|
2020-05-11 18:39:12 +01:00
|
|
|
// host = '' -- hostname (subdomain, for instance) to match,
|
|
|
|
// we don't want this
|
2021-07-28 22:36:24 +01:00
|
|
|
host: '',
|
2020-05-11 18:39:12 +01:00
|
|
|
// schemes = [] -- URI schemes (https, ftp and such)
|
2021-07-28 22:36:24 +01:00
|
|
|
schemes: $options['schemes'] ?? [],
|
2020-05-11 18:39:12 +01:00
|
|
|
// methods = [] -- HTTP methods
|
2021-07-28 22:36:24 +01:00
|
|
|
methods: $options['http-methods'] ?? $options['methods'] ?? [],
|
2020-05-11 18:39:12 +01:00
|
|
|
// condition = '' -- Symfony condition expression,
|
|
|
|
// see https://symfony.com/doc/current/routing.html#matching-expressions
|
2021-10-19 23:46:01 +01:00
|
|
|
condition: isset($options['accept']) ? $accept_header_condition : ($options['condition'] ?? ''),
|
2021-10-10 09:26:18 +01:00
|
|
|
),
|
2020-03-21 18:53:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this loader supports loading this route type
|
|
|
|
* Passed the arguments from the `RoutingConfigurator::import` call from
|
|
|
|
* `config/routes.php`
|
|
|
|
*
|
2021-07-28 22:36:24 +01:00
|
|
|
* @codeCoverageIgnore
|
2020-03-21 18:53:25 +00:00
|
|
|
*/
|
2020-06-23 13:43:19 +01:00
|
|
|
public function supports($resource, ?string $type = null): bool
|
2020-03-21 18:53:25 +00:00
|
|
|
{
|
|
|
|
return 'GNUsocial' === $type;
|
|
|
|
}
|
|
|
|
}
|