Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ControllerResolver.php « Http « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6a4c32cd183ec54b3dcc97babea7b59a182ae20c (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Http;

use DI\FactoryInterface;
use Exception;
use Piwik\Plugin\Controller;
use Piwik\Plugin\Report;
use Piwik\Plugin\Widgets;
use Piwik\Session;

/**
 * Resolves the controller that will handle the request.
 *
 * A controller is a PHP callable.
 */
class ControllerResolver
{
    /**
     * @var FactoryInterface
     */
    private $abstractFactory;

    public function __construct(FactoryInterface $abstractFactory)
    {
        $this->abstractFactory = $abstractFactory;
    }

    /**
     * @param string $module
     * @param string|null $action
     * @param array $parameters
     * @throws Exception Controller not found.
     * @return callable The controller is a PHP callable.
     */
    public function getController($module, $action, array &$parameters)
    {
        // Controller action
        $controllerClass = $this->getControllerClass($module);
        if (class_exists($controllerClass)) {
            $controller = $this->createPluginController($controllerClass, $action);
            if ($controller) {
                return $controller;
            }
        }

        // Widget action
        /** @var Widgets|null $widget */
        $widget = Widgets::factory($module, $action);
        if ($widget) {
            return $this->createWidgetController($widget, $action, $parameters);
        }

        // Report action
        $report = Report::factory($module, $action);
        if ($report) {
            return $this->createReportController($report, $parameters);
        }

        // Report menu action
        if ($this->isReportMenuAction($action)) {
            $controller = $this->createReportMenuController($module, $action, $parameters);
            if ($controller) {
                return $controller;
            }
        }

        throw new Exception(sprintf("Action '%s' not found in the module '%s'", $action, $module));
    }

    private function getControllerClass($module)
    {
        return "Piwik\\Plugins\\$module\\Controller";
    }

    private function createPluginController($controllerClass, $action)
    {
        /** @var $controller Controller */
        $controller = $this->abstractFactory->make($controllerClass);

        $action = $action ?: $controller->getDefaultAction();

        if (!is_callable(array($controller, $action))) {
            return null;
        }

        return array($controller, $action);
    }

    private function createWidgetController(Widgets $widget, $action, array &$parameters)
    {
        $parameters['widget'] = $widget;
        $parameters['method'] = $action;

        return array($this->abstractFactory->make('Piwik\Plugins\CoreHome\Controller'), 'renderWidget');
    }

    private function createReportController(Report $report, array &$parameters)
    {
        $parameters['report'] = $report;

        return array($this->abstractFactory->make('Piwik\Plugins\CoreHome\Controller'), 'renderReportWidget');
    }

    private function createReportMenuController($module, $action, array &$parameters)
    {
        $action = lcfirst(substr($action, 4)); // menuGetPageUrls => getPageUrls
        $report = Report::factory($module, $action);

        if (!$report) {
            return null;
        }

        $parameters['report'] = $report;

        return array($this->abstractFactory->make('Piwik\Plugins\CoreHome\Controller'), 'renderReportMenu');
    }

    private function isReportMenuAction($action)
    {
        $startsWithMenu = (Report::PREFIX_ACTION_IN_MENU === substr($action, 0, strlen(Report::PREFIX_ACTION_IN_MENU)));

        return !empty($action) && $startsWithMenu;
    }
}