aboutsummaryrefslogtreecommitdiff
path: root/lib/plugins/core/TrailsLegacyRouteStrategy.php
blob: 7bcb0001196e26f7c4b5757ae09c9f7c0af609ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php

namespace Studip\Plugins;

use Closure;
use Exception;
use PluginDispatcher;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use StudIPPlugin;
use Trails\Exceptions\UnknownAction;

class TrailsLegacyRouteStrategy implements LegacyRouteStrategy
{
    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function getCallable(string $unconsumedPath): Closure
    {
        return function (Request $request, Response $response, array $args) use ($unconsumedPath) {
            try {
                /** @var ContainerInterface $this */
                $plugin = $this->get(StudIPPlugin::class);
                $plugin->perform($unconsumedPath);

                $dispatcher = $this->get(PluginDispatcher::class);
                $handler = $dispatcher->getRouteCallable($unconsumedPath);

                return $handler($request, $response, $args);
            } catch (UnknownAction $exception) {
                $args = explode('/', $unconsumedPath);
                if ($args[0] !== '') {
                    $args = array_slice($args, 1);
                }
                throw count($args) ? $exception : new Exception(_('unbekannte Plugin-Aktion: ') . $unconsumedPath);
            }
        };
    }
}