aboutsummaryrefslogtreecommitdiff
path: root/lib/plugins/core/CustomPerformLegacyRouteStrategy.php
blob: 58f3d2fe8dcba61ad02b1ff7a217da2afcf6a5e2 (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
<?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 CustomPerformLegacyRouteStrategy 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);
            $content = ob_get_clean();
            $response->getBody()->write($content);
            $responseCode = http_response_code();
            if (!is_bool($responseCode)) {
                $response = $response->withStatus($responseCode);
            }
            return $response;
        };
    }
}