Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
namespace core\router;
18
 
19
use Invoker\Exception\NotCallableException;
20
use Psr\Http\Server\MiddlewareInterface;
21
use Psr\Http\Server\RequestHandlerInterface;
22
use Slim\Interfaces\AdvancedCallableResolverInterface;
23
 
24
// phpcs:disable moodle.NamingConventions.ValidVariableName.VariableNameLowerCase
25
// phpcs:disable moodle.NamingConventions.ValidFunctionName.LowercaseMethod
26
 
27
/**
28
 * Resolve middleware and route callables using PHP-DI.
29
 *
30
 * @package    core
31
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class callable_resolver implements AdvancedCallableResolverInterface {
35
    /**
36
     * Create a new instance of the Callable Resolver.
37
     *
38
     * @param \Invoker\CallableResolver $callableresolver The DI Callable Resolver instance
39
     */
40
    public function __construct(
41
        /** @var \Invoker\CallableResolver The DI Callable Resolver instance */
42
        protected \Invoker\CallableResolver $callableresolver,
43
    ) {
44
    }
45
 
46
    #[\Override]
47
    public function resolve($toResolve): callable {
48
        return $this->callableresolver->resolve($this->translate_notation($toResolve));
49
    }
50
 
51
    #[\Override]
52
    public function resolveRoute($toResolve): callable {
53
        return $this->resolve_possible_signature($toResolve, 'handle', RequestHandlerInterface::class);
54
    }
55
 
56
    #[\Override]
57
    public function resolveMiddleware($toResolve): callable {
58
        return $this->resolve_possible_signature($toResolve, 'process', MiddlewareInterface::class);
59
    }
60
 
61
    /**
62
     * Translate Slim string callable notation ('nameOrKey:method') to PHP-DI notation ('nameOrKey::method').
63
     *
64
     * For a full list of supported callables, see the Slim Docs at
65
     * https://www.slimframework.com/docs/v4/objects/routing.html#container-resolution.
66
     *
67
     * @param mixed $toresolve
68
     * @return mixed
69
     */
70
    private function translate_notation(mixed $toresolve): mixed {
71
        if (is_string($toresolve) && preg_match(\Slim\CallableResolver::$callablePattern, $toresolve)) {
72
            $toresolve = str_replace(':', '::', $toresolve);
73
        }
74
 
75
        return $toresolve;
76
    }
77
 
78
    /**
79
     * Resolve a possible signature for a callable.
80
     *
81
     * @param mixed $toresolve The callable to resolve
82
     * @param string $method The method to resolve
83
     * @param string $typename The type name to resolve
84
     */
85
    private function resolve_possible_signature(
86
        mixed $toresolve,
87
        string $method,
88
        string $typename,
89
    ): callable {
90
        if (is_string($toresolve)) {
91
            $toresolve = $this->translate_notation($toresolve);
92
 
93
            try {
94
                $callable = $this->callableresolver->resolve([$toresolve, $method]);
95
 
96
                if (is_array($callable) && $callable[0] instanceof $typename) {
97
                    return $callable;
98
                }
99
            } catch (NotCallableException $e) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
100
                // Fall back to looking for a generic callable.
101
            }
102
        }
103
 
104
        return $this->callableresolver->resolve($toresolve);
105
    }
106
}