FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
phalcon-devtools/src/Script.php at master · marksda/phalcon-devtools · GitHub
marksda
/
phalcon-devtools
Public
forked from
phalcon/phalcon-devtools
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
phalcon-devtools
/
src
/
Script.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
218 lines (186 loc) · 5.62 KB
Breadcrumbs
phalcon-devtools
/
src
/
Script.php
Copy path
File metadata and controls
218 lines (186 loc) · 5.62 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
declare
(strict_types=
1
);
/**
* This file is part of the Phalcon Developer Tools.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace
Phalcon
\
DevTools
;
use
DirectoryIterator
;
use
Phalcon
\
DevTools
\
Builder
\
Exception
\
BuilderException
;
use
Phalcon
\
DevTools
\
Commands
\
Command
;
use
Phalcon
\
DevTools
\
Commands
\
CommandsException
;
use
Phalcon
\
DevTools
\
Script
\
Color
;
use
Phalcon
\
DevTools
\
Script
\
ScriptException
;
use
Phalcon
\
Events
\
Manager
as
EventsManager
;
/**
* Component that allows you to write scripts to use CLI.
*/
class
Script
{
/**
* Events Manager
*
* @var EventsManager
*/
protected
$
eventsManager
;
/**
* Commands attached to the Script
*
* @var Command[]
*/
protected
$
commands
;
/**
* Script Constructor
*
* @param EventsManager $eventsManager
*/
public
function
__construct
(
EventsManager
$
eventsManager
)
{
$
this
->
commands
= [];
$
this
->
eventsManager
=
$
eventsManager
;
}
/**
* Events Manager
*
* @param EventsManager $eventsManager
*/
public
function
setEventsManager
(
EventsManager
$
eventsManager
)
{
$
this
->
eventsManager
=
$
eventsManager
;
}
/**
* Returns the events manager
*
* @return EventsManager
*/
public
function
getEventsManager
()
{
return
$
this
->
eventsManager
;
}
/**
* Adds commands to the Script
*
* @param Command $command
*/
public
function
attach
(
Command
$
command
):
void
{
$
this
->
commands
[] =
$
command
;
}
/**
* Returns the commands registered in the script
*
* @return Command[]
*/
public
function
getCommands
():
array
{
return
$
this
->
commands
;
}
/**
* Dispatch the Command
*
* @param Command $command
* @return bool
*/
public
function
dispatch
(
Command
$
command
):
bool
{
// If beforeCommand fails abort
if
(
$
this
->
eventsManager
->
fire
(
'
command:beforeCommand
'
,
$
command
) ===
false
) {
return
false
;
}
// If run the commands fails abort too
try
{
$
return
=
true
;
$
command
->
run
(
$
command
->
getParameters
());
}
catch
(
BuilderException
$
builderException
) {
echo
Color::
error
(
$
builderException
->
getMessage
());
$
return
=
false
;
}
catch
(
CommandsException
$
commandsException
) {
echo
Color::
error
(
$
commandsException
->
getMessage
());
$
return
=
false
;
}
$
this
->
eventsManager
->
fire
(
'
command:afterCommand
'
,
$
command
);
return
$
return
;
}
/**
* Run the scripts
*
* @throws ScriptException
*/
public
function
run
()
{
if
(!
isset
(
$
_SERVER
[
'
argv
'
][
1
])) {
$
_SERVER
[
'
argv
'
][
1
] =
'
commands
'
;
}
$
input
=
$
_SERVER
[
'
argv
'
][
1
];
// Force `commands` command
if
(
in_array
(
strtolower
(
trim
(
$
input
)), [
'
-h
'
,
'
--help
'
,
'
help
'
],
true
)) {
$
input
=
$
_SERVER
[
'
argv
'
][
1
] =
'
commands
'
;
}
if
(
in_array
(
strtolower
(
trim
(
$
input
)), [
'
--version
'
,
'
-v
'
,
'
--info
'
],
true
)) {
$
input
=
$
_SERVER
[
'
argv
'
][
1
] =
'
info
'
;
}
// Try to dispatch the command
foreach
(
$
this
->
commands
as
$
command
) {
if
(
$
command
->
hasIdentifier
(
$
input
)) {
return
$
this
->
dispatch
(
$
command
);
}
}
// Check for alternatives
$
available
= [];
foreach
(
$
this
->
commands
as
$
command
) {
$
providedCommands
=
$
command
->
getCommands
();
foreach
(
$
providedCommands
as
$
alias
) {
$
soundex
=
soundex
(
$
alias
);
if
(!
isset
(
$
available
[
$
soundex
])) {
$
available
[
$
soundex
] = [];
}
$
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
])));
}
throw
new
ScriptException
(
$
message
);
}
public
function
loadUserScripts
():
void
{
if
(!
file_exists
(
'
.phalcon/project.ini
'
)) {
return
;
}
$
config
=
parse_ini_file
(
'
.phalcon/project.ini
'
);
if
(!
isset
(
$
config
[
'
scripts
'
])) {
return
;
}
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
() ||
$
item
->
isDot
()) {
continue
;
}
/** @noinspection PhpIncludeInspection */
require
$
item
->
getPathname
();
$
className
=
preg_replace
(
'
/\.php$/
'
,
''
,
$
item
->
getBasename
());
if
(!
class_exists
(
$
className
)) {
throw
new
ScriptException
(
sprintf
(
"
Expecting class '%s' to be located at '%s'
"
,
$
className
,
$
item
->
getPathname
()
)
);
}
$
this
->
attach
(
new
$
className
(
$
this
,
$
this
->
eventsManager
));
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL