forked from UnionOfRAD/lithium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDispatcher.php
202 lines (186 loc) · 7.27 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
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2014, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace lithium\console;
use lithium\core\Libraries;
use lithium\core\Environment;
use UnexpectedValueException;
/**
* The `Dispatcher` is the outermost layer of the framework, responsible for both receiving the
* initial console request and returning back a response at the end of the request's life cycle.
*
* The console dispatcher is responsible for accepting requests from scripts called from the command
* line, and executing the appropriate `Command` class(es). The `run()` method accepts an instance
* of `lithium\console\Request`, which encapsulates the console environment and any command-line
* parameters passed to the script. `Dispatcher` then invokes `lithium\console\Router` to determine
* the correct `Command` class to invoke, and which method should be called.
*/
class Dispatcher extends \lithium\core\StaticObject {
/**
* Fully-namespaced router class reference.
*
* Class must implement a `parse()` method, which must return an array with (at a minimum)
* 'command' and 'action' keys.
*
* @see lithium\console\Router::parse()
* @var array
*/
protected static $_classes = array(
'request' => 'lithium\console\Request',
'router' => 'lithium\console\Router'
);
/**
* Contains pre-process format strings for changing Dispatcher's behavior based on 'rules'.
*
* Each key in the array represents a 'rule'; if a key that matches the rule is present (and
* not empty) in a route, (i.e. the result of `lithium\console\Router::parse()`) then the rule's
* value will be applied to the route before it is dispatched. When applying a rule, any array
* elements array elements of the flag which are present in the route will be modified using a
* `lithium\util\String::insert()`-formatted string.
*
* @see lithium\console\Dispatcher::config()
* @see lithium\util\String::insert()
* @var array
*/
protected static $_rules = array(
'command' => array(array('lithium\util\Inflector', 'camelize')),
'action' => array(array('lithium\util\Inflector', 'camelize', array(false)))
);
/**
* Used to set configuration parameters for the Dispatcher.
*
* @param array $config Optional configuration params.
* @return array If no parameters are passed, returns an associative array with the
* current configuration, otherwise returns null.
*/
public static function config($config = array()) {
if (!$config) {
return array('rules' => static::$_rules);
}
foreach ($config as $key => $val) {
if (isset(static::${'_' . $key})) {
static::${'_' . $key} = $val + static::${'_' . $key};
}
}
}
/**
* Dispatches a request based on a request object (an instance of `lithium\console\Request`).
*
* If `$request` is `null`, a new request object is instantiated based on the value of the
* `'request'` key in the `$_classes` array.
*
* @param object $request An instance of a request object with console request information. If
* `null`, an instance will be created.
* @param array $options
* @return object The command action result which is an instance of `lithium\console\Response`.
*/
public static function run($request = null, $options = array()) {
$defaults = array('request' => array());
$options += $defaults;
$classes = static::$_classes;
$params = compact('request', 'options');
return static::_filter(__FUNCTION__, $params, function($self, $params) use ($classes) {
$request = $params['request'];
$options = $params['options'];
$router = $classes['router'];
$request = $request ?: new $classes['request']($options['request']);
$request->params = $router::parse($request);
$params = $self::applyRules($request->params);
Environment::set($request);
try {
$callable = $self::invokeMethod('_callable', array($request, $params, $options));
return $self::invokeMethod('_call', array($callable, $request, $params));
} catch (UnexpectedValueException $e) {
return (object) array('status' => $e->getMessage() . "\n");
}
});
}
/**
* Determines which command to use for current request.
*
* @param object $request An instance of a `Request` object.
* @param array $params Request params that can be accessed inside the filter.
* @param array $options
* @return class lithium\console\Command Returns the instantiated command object.
*/
protected static function _callable($request, $params, $options) {
$params = compact('request', 'params', 'options');
return static::_filter(__FUNCTION__, $params, function($self, $params) {
$request = $params['request'];
$params = $params['params'];
$name = $params['command'];
if (!$name) {
$request->params['args'][0] = $name;
$name = 'lithium\console\command\Help';
}
if (class_exists($class = Libraries::locate('command', $name))) {
return new $class(compact('request'));
}
throw new UnexpectedValueException("Command `{$name}` not found.");
});
}
/**
* Attempts to apply a set of formatting rules from `$_rules` to a `$params` array.
*
* Each formatting rule is applied if the key of the rule in `$_rules` is present and not empty
* in `$params`. Also performs sanity checking against `$params` to ensure that no value
* matching a rule is present unless the rule check passes.
*
* @param array $params An array of route parameters to which rules will be applied.
* @return array Returns the `$params` array with formatting rules applied to array values.
*/
public static function applyRules($params) {
$result = array();
if (!$params) {
return false;
}
foreach (static::$_rules as $name => $rules) {
foreach ($rules as $rule) {
if (!empty($params[$name]) && isset($rule[0])) {
$options = array_merge(
array($params[$name]), isset($rule[2]) ? (array) $rule[2] : array()
);
$result[$name] = call_user_func_array(array($rule[0], $rule[1]), $options);
}
}
}
return $result + array_diff_key($params, $result);
}
/**
* Calls a given command with the appropriate action.
*
* This method is responsible for calling a `$callable` command and returning its result.
*
* @param string $callable The callable command.
* @param string $request The associated `Request` object.
* @param string $params Additional params that should be passed along.
* @return mixed Returns the result of the called action, typically `true` or `false`.
*/
protected static function _call($callable, $request, $params) {
$params = compact('callable', 'request', 'params');
return static::_filter(__FUNCTION__, $params, function($self, $params) {
if (is_callable($callable = $params['callable'])) {
$request = $params['request'];
$params = $params['params'];
if (!method_exists($callable, $params['action'])) {
array_unshift($params['args'], $request->params['action']);
$params['action'] = 'run';
}
$isHelp = (
!empty($params['help']) || !empty($params['h']) ||
!method_exists($callable, $params['action'])
);
if ($isHelp) {
$params['action'] = '_help';
}
return $callable($params['action'], $params['args']);
}
throw new UnexpectedValueException("Callable `{$callable}` is actually not callable.");
});
}
}
?>