aboutsummaryrefslogtreecommitdiff
path: root/lib/plugins/core/DefaultLegacyRouteStrategy.php
blob: 289a689ad6fed183df825dec0e41e962f6f55604 (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
<?php

namespace Studip\Plugins;

use Closure;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use StudIPPlugin;

class DefaultLegacyRouteStrategy implements LegacyRouteStrategy
{
    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function getCallable(string $unconsumedPath): Closure
    {
        return function (Request $request, Response $response) use ($unconsumedPath) {
            ob_start();

            /** @var ContainerInterface $this */
            $plugin = $this->get(StudIPPlugin::class);
            $plugin->perform($unconsumedPath);
            $args = explode('/', $unconsumedPath);
            $action = $args[0] !== '' ? array_shift($args) . '_action' : 'show_action';
            $plugin->$action(...$args);

            $content = ob_get_clean();
            $response->getBody()->write($content);
            return $response;
        };
    }
}