aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/StudipDispatcher.php
blob: a54c45c4455a538736d145da2330b8b970d60dc6 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

use Psr\Container\ContainerInterface;
use Slim\Routing\RouteContext;
use Trails\Exception;

/**
 * StudipDispatcher.php - create the default Trails dispatcher
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      <mlunzena@uos.de>
 * @copyright   2013 Stud.IP Core-Group
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 */

/**
 * Use this subclass to easily get an Stud.IP specific
 * Trails\Dispatcher.
 *
 * Example of use:
 *
 * @code
 * // deep in the Stud.IP jungle
 * $dispatcher = new StudipDispatcher();
 * $dispatcher->dispatch($requested_uri);
 * @endcode
 */
class StudipDispatcher extends Trails\Dispatcher
{
    /**
     * This variable contains the DI-Container.
     *
     * @var ContainerInterface
     */
    protected $container;

    /**
     * Create a new Trails\Dispatcher with Stud.IP specific parameters
     * for: trails_root is "$STUDIP_BASE_PATH/app", trails_uri is
     * "dispatch.php" and default_controller is "default" (which does
     * not map to anything).
     */
    public function __construct(ContainerInterface $container)
    {
        global $STUDIP_BASE_PATH, $ABSOLUTE_URI_STUDIP;

        $trails_root = $STUDIP_BASE_PATH . DIRECTORY_SEPARATOR . 'app';
        $trails_uri = rtrim($ABSOLUTE_URI_STUDIP, '/') . '/dispatch.php';
        $default_controller = 'default';
        $this->container = $container;

        parent::__construct($trails_root, $trails_uri, $default_controller);
    }

    /**
     * Adapted error method that just passes the exception to stud.ip's
     * exception instead of the standard trails handling.
     *
     * @param Exception $exception The exception that occured
     *
     * @throws Exception
     */
    public function trails_error($exception)
    {
        throw $exception;
    }

    /**
     * Loads the controller file for a given controller path and return an
     * instance of that controller. If an error occures, an exception will be
     * thrown.
     *
     * @param string $controller the relative controller path
     * @return Trails\Controller  an instance of that controller
     * @throws \Trails\Exceptions\UnknownController
     */
    public function load_controller($controller)
    {
        require_once "{$this->trails_root}/controllers/{$controller}.php";
        $class = Trails\Inflector::camelize($controller) . 'Controller';
        if (!class_exists($class)) {
            throw new Trails\Exceptions\UnknownController("Controller missing: '$class'");
        }

        return $this->container->make($class, ['dispatcher' => $this]);
    }

    public function getRouteCallable($uri): callable
    {
        try {
            $uri = $this->clean_request_uri((string) $uri);
            [$controller_path, $unconsumed] = '' === $uri ? $this->default_route() : $this->parse($uri);
            $controller = $this->load_controller($controller_path);
            return function (
                \Psr\Http\Message\ServerRequestInterface $request,
                \Psr\Http\Message\ResponseInterface $response,
                array $args
            ) use ($controller, $unconsumed): \Psr\Http\Message\ResponseInterface {
                $controller->injectResponse($response);
                $response = $controller->perform($unconsumed);
                return $response->getPsrResponse();
            } ;
        } catch (Exception $e) {
            return function () use ($e) {
                throw $e;
            };
        }
    }
}