-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Dispatcher.php
219 lines (190 loc) · 5.15 KB
/
Dispatcher.php
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\CMS\Dispatcher;
defined('_JEXEC') or die;
use Joomla\CMS\Access\Exception\NotAllowed;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Form\FormFactoryAwareInterface;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\Input\Input;
use Joomla\CMS\MVC\Factory\MVCFactory;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\FormFactoryInterface;
/**
* Base class for a Joomla Dispatcher
*
* Dispatchers are responsible for checking ACL of a component if appropriate and
* choosing an appropriate controller (and if necessary, a task) and executing it.
*
* @since 4.0.0
*/
abstract class Dispatcher implements DispatcherInterface
{
/**
* The URL option for the component.
*
* @var string
* @since 4.0.0
*/
protected $option;
/**
* The extension namespace
*
* @var string
* @since 4.0.0
*/
protected $namespace;
/**
* The application instance
*
* @var CMSApplication
* @since 4.0.0
*/
protected $app;
/**
* The input instance
*
* @var Input
* @since 4.0.0
*/
protected $input;
/**
* Constructor for Dispatcher
*
* @param CMSApplication $app The application instance
* @param Input $input The input instance
*
* @since 4.0.0
*/
public function __construct(CMSApplication $app, Input $input = null)
{
if (empty($this->namespace))
{
throw new \RuntimeException('Namespace can not be empty!');
}
$this->app = $app;
$this->input = $input ?: $app->input;
// If option is not provided, detect it from dispatcher class name, ie ContentDispatcher
if (empty($this->option))
{
$className = get_class($this);
$pos = strpos($className, 'Dispatcher');
if ($pos !== false)
{
$this->option = 'com_' . strtolower(substr($className, 0, $pos));
}
}
}
/**
* Load the language
*
* @return void
*
* @since 4.0.0
*/
protected function loadLanguage()
{
// Load common and local language files.
$this->app->getLanguage()->load($this->option, JPATH_BASE, null, false, true) ||
$this->app->getLanguage()->load($this->option, JPATH_COMPONENT, null, false, true);
}
/**
* Method to check component access permission
*
* @return void
*
* @since 4.0.0
*/
protected function checkAccess()
{
// Check the user has permission to access this component if in the backend
if ($this->app->isClient('administrator') && !$this->app->getIdentity()->authorise('core.manage', $this->option))
{
throw new NotAllowed($this->app->getLanguage()->_('JERROR_ALERTNOAUTHOR'), 403);
}
}
/**
* Dispatch a controller task. Redirecting the user if appropriate.
*
* @return void
*
* @since 4.0.0
*/
public function dispatch()
{
$this->loadLanguage();
// Check component access permission
$this->checkAccess();
$command = $this->input->getCmd('task', 'display');
// Check for a controller.task command.
if (strpos($command, '.') !== false)
{
// Explode the controller.task command.
list ($controller, $task) = explode('.', $command);
$this->input->set('controller', $controller);
$this->input->set('task', $task);
}
else
{
// Do we have a controller?
$controller = $this->input->get('controller', 'display');
$task = $command;
}
// Build controller config data
$config['option'] = $this->option;
// Set name of controller if it is passed in the request
if ($this->input->exists('controller'))
{
$config['name'] = strtolower($this->input->get('controller'));
}
// Execute the task for this component
$controller = $this->getController($controller, ucfirst($this->app->getName()), $config);
$controller->execute($task);
$controller->redirect();
}
/**
* The application the dispatcher is working with.
*
* @return CMSApplication
*
* @since 4.0.0
*/
protected function getApplication(): CMSApplication
{
return $this->app;
}
/**
* Get a controller from the component
*
* @param string $name Controller name
* @param string $client Optional client (like Administrator, Site etc.)
* @param array $config Optional controller config
*
* @return BaseController
*
* @since 4.0.0
*/
public function getController(string $name, string $client = '', array $config = array()): BaseController
{
// Set up the namespace
$namespace = rtrim($this->namespace, '\\') . '\\';
// Set up the client
$client = $client ?: ucfirst($this->app->getName());
$controllerClass = $namespace . $client . '\\Controller\\' . ucfirst($name) . 'Controller';
if (!class_exists($controllerClass))
{
throw new \InvalidArgumentException(\JText::sprintf('JLIB_APPLICATION_ERROR_INVALID_CONTROLLER_CLASS', $controllerClass));
}
$controller = new $controllerClass($config, new MVCFactory($namespace, $this->app), $this->app, $this->input);
if ($controller instanceof FormFactoryAwareInterface)
{
$controller->setFormFactory(Factory::getContainer()->get(FormFactoryInterface::class));
}
return $controller;
}
}