summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Controller/Dispatcher.php
blob: e2dfb80f5939a9536df9e414c1c5a4a7a9280ec4 (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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Web\Controller;

use Exception;
use Icinga\Util\StringHelper;
use Zend_Controller_Action;
use Zend_Controller_Action_Interface;
use Zend_Controller_Dispatcher_Exception;
use Zend_Controller_Dispatcher_Standard;
use Zend_Controller_Request_Abstract;
use Zend_Controller_Response_Abstract;

/**
 * Dispatcher supporting Zend-style and namespaced controllers
 *
 * Does not support a namespaced default controller in combination w/ the Zend parameter useDefaultControllerAlways.
 */
class Dispatcher extends Zend_Controller_Dispatcher_Standard
{
    /**
     * Controller namespace
     *
     * @var string
     */
    const CONTROLLER_NAMESPACE = 'Controllers';

    /**
     * Dispatch request to a controller and action
     *
     * @param   Zend_Controller_Request_Abstract  $request
     * @param   Zend_Controller_Response_Abstract $response
     *
     * @throws  Zend_Controller_Dispatcher_Exception    If the controller is not an instance of
     *                                                  Zend_Controller_Action_Interface
     * @throws  Exception                               If dispatching the request fails
     */
    public function dispatch(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response)
    {
        $this->setResponse($response);
        $controllerName = $request->getControllerName();
        if (! $controllerName) {
            parent::dispatch($request, $response);
            return;
        }
        $controllerName = StringHelper::cname($controllerName, '-') . 'Controller';
        $moduleName = $request->getModuleName();
        if ($moduleName === null || $moduleName === $this->_defaultModule) {
            $controllerClass = 'Icinga\\' . self::CONTROLLER_NAMESPACE . '\\' . $controllerName;
        } else {
            $controllerClass = 'Icinga\\Module\\' . ucfirst($moduleName) . '\\' . self::CONTROLLER_NAMESPACE . '\\'
                . $controllerName;
        }
        if (! class_exists($controllerClass)) {
            parent::dispatch($request, $response);
            return;
        }
        $controller = new $controllerClass($request, $response, $this->getParams());
        if (! $controller instanceof Zend_Controller_Action
            && ! $controller instanceof Zend_Controller_Action_Interface
        ) {
            throw new Zend_Controller_Dispatcher_Exception(
                'Controller "' . $controllerClass . '" is not an instance of Zend_Controller_Action_Interface'
            );
        }
        $action = $this->getActionMethod($request);
        $request->setDispatched(true);
        // Buffer output by default
        $disableOb = $this->getParam('disableOutputBuffering');
        $obLevel = ob_get_level();
        if (empty($disableOb)) {
            ob_start();
        }
        try {
            $controller->dispatch($action);
        } catch (Exception $e) {
            // Clean output buffer on error
            $curObLevel = ob_get_level();
            if ($curObLevel > $obLevel) {
                do {
                    ob_get_clean();
                    $curObLevel = ob_get_level();
                } while ($curObLevel > $obLevel);
            }
            throw $e;
        }
        if (empty($disableOb)) {
            $content = ob_get_clean();
            $response->appendBody($content);
        }
    }
}