| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
declare(strict_types=1);
|
|
|
18 |
|
|
|
19 |
namespace core\router;
|
|
|
20 |
|
|
|
21 |
use core\di;
|
|
|
22 |
use Invoker\Invoker;
|
|
|
23 |
use Invoker\ParameterResolver\AssociativeArrayResolver;
|
|
|
24 |
use Invoker\ParameterResolver\Container\TypeHintContainerResolver;
|
|
|
25 |
use Invoker\ParameterResolver\DefaultValueResolver;
|
|
|
26 |
use Invoker\ParameterResolver\ResolverChain;
|
|
|
27 |
use Psr\Container\ContainerInterface;
|
|
|
28 |
use Slim\App;
|
|
|
29 |
use Slim\Factory\AppFactory;
|
|
|
30 |
use Slim\Interfaces\CallableResolverInterface;
|
|
|
31 |
use Slim\Interfaces\InvocationStrategyInterface;
|
|
|
32 |
|
|
|
33 |
// phpcs:disable moodle.NamingConventions.ValidFunctionName.LowercaseMethod
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* This factory creates a Slim application correctly configured with PHP-DI.
|
|
|
37 |
*
|
|
|
38 |
* To use this, replace `Slim\Factory\AppFactory::create()`
|
|
|
39 |
* with `DI\Bridge\Slim\Bridge::create()`.
|
|
|
40 |
*
|
|
|
41 |
* @package core
|
|
|
42 |
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
43 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
44 |
*/
|
|
|
45 |
class bridge {
|
|
|
46 |
/**
|
|
|
47 |
* Create a new Slim application with PHP-DI.
|
|
|
48 |
*
|
|
|
49 |
* @param ContainerInterface|null $container
|
|
|
50 |
* @return App
|
|
|
51 |
*/
|
|
|
52 |
public static function create(?ContainerInterface $container = null): App {
|
|
|
53 |
$container = $container ?: di::get_container();
|
|
|
54 |
|
|
|
55 |
di::set(
|
|
|
56 |
CallableResolverInterface::class,
|
|
|
57 |
new callable_resolver(new \Invoker\CallableResolver($container)),
|
|
|
58 |
);
|
|
|
59 |
|
|
|
60 |
$app = AppFactory::createFromContainer($container);
|
|
|
61 |
|
|
|
62 |
di::set(App::class, $app);
|
|
|
63 |
|
|
|
64 |
$controllerinvoker = static::create_controller_invoker($container);
|
|
|
65 |
$app->getRouteCollector()->setDefaultInvocationStrategy($controllerinvoker);
|
|
|
66 |
|
|
|
67 |
return $app;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Create a controller invoker
|
|
|
72 |
*
|
|
|
73 |
* @param ContainerInterface $container
|
|
|
74 |
* @return InvocationStrategyInterface
|
|
|
75 |
*/
|
|
|
76 |
protected static function create_controller_invoker(ContainerInterface $container): InvocationStrategyInterface {
|
|
|
77 |
$resolvers = [
|
|
|
78 |
// Inject parameters by name first.
|
|
|
79 |
new AssociativeArrayResolver(),
|
|
|
80 |
|
|
|
81 |
// Then inject services by type-hints for those that weren't resolved.
|
|
|
82 |
new TypeHintContainerResolver($container),
|
|
|
83 |
|
|
|
84 |
// Then fall back on parameters default values for optional route parameters.
|
|
|
85 |
new DefaultValueResolver(),
|
|
|
86 |
];
|
|
|
87 |
|
|
|
88 |
$invoker = new Invoker(new ResolverChain($resolvers), $container);
|
|
|
89 |
|
|
|
90 |
return new controller_invoker(
|
|
|
91 |
container: $container,
|
|
|
92 |
invoker: $invoker,
|
|
|
93 |
);
|
|
|
94 |
}
|
|
|
95 |
}
|