forked from phalcon/phalcon-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScript.php
207 lines (179 loc) · 5.96 KB
/
Script.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
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Developer Tools |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <[email protected]> |
| Eduar Carvajal <[email protected]> |
| Serghei Iakovlev <[email protected]> |
+------------------------------------------------------------------------+
*/
namespace Phalcon;
use Phalcon\Commands\Command;
use Phalcon\Script\ScriptException;
use Phalcon\Events\Manager as EventsManager;
use DirectoryIterator;
/**
* Script Class
*
* Component that allows you to write scripts to use CLI.
*
* @package Phalcon
*/
class Script
{
/**
* Phalcon 2.0.0
* @type int
*/
const COMPATIBLE_VERSION = 2000023;
const DOC_DOWNLOAD_URL = 'http://phalconphp.com/download';
/**
* Events Manager
*
* @var \Phalcon\Events\Manager
*/
protected $_eventsManager;
/**
* Commands attached to the Script
*
* @var \Phalcon\Commands\Command[]
*/
protected $_commands;
/**
* Script Constructor
*
* @param \Phalcon\Events\Manager $eventsManager
*/
public function __construct(EventsManager $eventsManager)
{
$this->_commands = array();
$this->_eventsManager = $eventsManager;
}
/**
* Events Manager
*
* @param \Phalcon\Events\Manager $eventsManager
*/
public function setEventsManager(EventsManager $eventsManager)
{
$this->_eventsManager = $eventsManager;
}
/**
* Returns the events manager
*
* @return \Phalcon\Events\Manager
*/
public function getEventsManager()
{
return $this->_eventsManager;
}
/**
* Adds commands to the Script
*
* @param \Phalcon\Commands\Command $command
*/
public function attach(Command $command)
{
$this->_commands[] = $command;
}
/**
* Returns the commands registered in the script
*
* @return \Phalcon\Commands\Command[]
*/
public function getCommands()
{
return $this->_commands;
}
/**
* Dispatch the Command
*
* @param Command $command
* @return bool
*/
public function dispatch(Command $command)
{
// If beforeCommand fails abort
if ($this->_eventsManager->fire('command:beforeCommand', $command) === false) {
return false;
}
// If run the commands fails abort too
if ($command->run($command->getParameters()) === false) {
return false;
}
$this->_eventsManager->fire('command:afterCommand', $command);
return true;
}
/**
* Run the scripts
*
* @throws ScriptException
*/
public function run()
{
if (!isset($_SERVER['argv'][1])) {
$_SERVER['argv'][1] = 'commands';
}
$input = $_SERVER['argv'][1];
// Try to dispatch the command
foreach ($this->_commands as $command) {
if ($command->hasIdentifier($input)) {
return $this->dispatch($command);
}
}
// Check for alternatives
$available = array();
foreach ($this->_commands as $command) {
$providedCommands = $command->getCommands();
foreach ($providedCommands as $alias) {
$soundex = soundex($alias);
if (!isset($available[$soundex])) {
$available[$soundex] = array();
}
$available[$soundex][] = $alias;
}
}
// Show exception with/without alternatives
$soundex = soundex($input);
$message = sprintf('%s is not a recognized command.', $input);
if (isset($available[$soundex])) {
throw new ScriptException(sprintf('%s Did you mean: %s?', $message, join(' or ', $available[$soundex])));
} else {
throw new ScriptException($message);
}
}
public function loadUserScripts()
{
if (file_exists('.phalcon/project.ini')) {
$config = parse_ini_file('.phalcon/project.ini');
if (isset($config['scripts'])) {
foreach (explode(',', $config['scripts']) as $directory) {
if (!is_dir($directory)) {
throw new ScriptException("Cannot load user scripts in directory '" . $directory . "'");
}
$iterator = new DirectoryIterator($directory);
foreach ($iterator as $item) {
if (!$item->isDir()) {
require $item->getPathName();
$className = preg_replace('/\.php$/', '', $item->getBaseName());
if (!class_exists($className)) {
throw new ScriptException("Expecting class '" . $className . "' to be located at '" . $item->getPathName() . '"');
}
$this->attach(new $className($this, $this->_eventsManager));
}
}
}
}
}
}
}