BadRequestException.php
1 year ago
ControllerResolver.php
4 months ago
HttpCodeException.php
2 years ago
Router.php
1 year ago
ControllerResolver.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Http; |
| 10 | |
| 11 | use Matomo\Dependencies\DI\FactoryInterface; |
| 12 | use Piwik\Exception\ThingNotFoundException; |
| 13 | use Piwik\Plugin\ReportsProvider; |
| 14 | use Piwik\Plugin\WidgetsProvider; |
| 15 | /** |
| 16 | * Resolves the controller that will handle the request. |
| 17 | * |
| 18 | * A controller is a PHP callable. |
| 19 | */ |
| 20 | class ControllerResolver |
| 21 | { |
| 22 | /** |
| 23 | * @var FactoryInterface |
| 24 | */ |
| 25 | private $abstractFactory; |
| 26 | /** |
| 27 | * @var WidgetsProvider |
| 28 | */ |
| 29 | private $widgets; |
| 30 | public function __construct(FactoryInterface $abstractFactory, WidgetsProvider $widgets) |
| 31 | { |
| 32 | $this->abstractFactory = $abstractFactory; |
| 33 | $this->widgets = $widgets; |
| 34 | } |
| 35 | /** |
| 36 | * @param string $module |
| 37 | * @param string|null $action |
| 38 | * @param array $parameters |
| 39 | * @throws ThingNotFoundException Controller not found. |
| 40 | * @return callable The controller is a PHP callable. |
| 41 | */ |
| 42 | public function getController($module, $action, array &$parameters) |
| 43 | { |
| 44 | $controller = $this->createPluginController($module, $action); |
| 45 | if ($controller) { |
| 46 | return $controller; |
| 47 | } |
| 48 | $controller = $this->createWidgetController($module, $action, $parameters); |
| 49 | if ($controller) { |
| 50 | return $controller; |
| 51 | } |
| 52 | $controller = $this->createReportController($module, $action, $parameters); |
| 53 | if ($controller) { |
| 54 | return $controller; |
| 55 | } |
| 56 | throw new ThingNotFoundException(sprintf("Action '%s' not found in the module '%s'", $action, $module)); |
| 57 | } |
| 58 | private function createPluginController($module, $action) |
| 59 | { |
| 60 | $controllerClass = "Piwik\\Plugins\\{$module}\\Controller"; |
| 61 | if (!class_exists($controllerClass)) { |
| 62 | return null; |
| 63 | } |
| 64 | /** @var $controller Controller */ |
| 65 | $controller = $this->abstractFactory->make($controllerClass); |
| 66 | $action = $action ?: $controller->getDefaultAction(); |
| 67 | if (!is_callable(array($controller, $action)) || !in_array($action, get_class_methods($controller))) { |
| 68 | return null; |
| 69 | } |
| 70 | return array($controller, $action); |
| 71 | } |
| 72 | private function createWidgetController($module, $action, array &$parameters) |
| 73 | { |
| 74 | $widget = $this->widgets->factory($module, $action); |
| 75 | if (!$widget) { |
| 76 | return; |
| 77 | } |
| 78 | $parameters['widget'] = $widget; |
| 79 | return array($this->createCoreHomeController(), 'renderWidget'); |
| 80 | } |
| 81 | private function createReportController($module, $action, array &$parameters) |
| 82 | { |
| 83 | $report = ReportsProvider::factory($module, $action); |
| 84 | if (!$report) { |
| 85 | return null; |
| 86 | } |
| 87 | $parameters['report'] = $report; |
| 88 | return array($this->createCoreHomeController(), 'renderReportWidget'); |
| 89 | } |
| 90 | private function createCoreHomeController() |
| 91 | { |
| 92 | return $this->abstractFactory->make('Piwik\\Plugins\\CoreHome\\Controller'); |
| 93 | } |
| 94 | } |
| 95 |