-
Notifications
You must be signed in to change notification settings - Fork 68
/
command.php
380 lines (307 loc) · 10.4 KB
/
command.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
* Fuel is a fast, lightweight, community driven PHP 5.4+ framework.
*
* @package Fuel
* @version 1.9-dev
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2019 Fuel Development Team
* @link https://fuelphp.com
*/
namespace Oil;
/**
* Oil\Cli Class
*
* @package Fuel
* @subpackage Oil
* @category Core
*/
class Command
{
public static function init($args)
{
\Config::load('oil', true);
// Remove flag options from the main argument list
$args = self::_clear_args($args);
try
{
if ( ! isset($args[1]))
{
if (\Cli::option('v', \Cli::option('version')))
{
\Cli::write('Fuel: '.\Fuel::VERSION.' running in "'.\Fuel::$env.'" mode');
return;
}
static::help();
return;
}
switch ($args[1])
{
case 'g':
case 'generate':
$action = isset($args[2]) ? $args[2] : 'help';
$subfolder = 'orm';
if (is_int(strpos($action, '/')))
{
list($action, $subfolder)=explode('/', $action);
}
switch ($action)
{
case 'config':
case 'controller':
case 'model':
case 'module':
case 'migration':
case 'task':
case 'package':
call_user_func('Oil\Generate::'.$action, array_slice($args, 3));
break;
case 'views':
call_user_func('Oil\Generate::views', array_slice($args, 3), $subfolder);
break;
case 'admin':
call_user_func('Oil\Generate_Admin::forge', array_slice($args, 3), $subfolder);
break;
case 'scaffold':
call_user_func('Oil\Generate_Scaffold::forge', array_slice($args, 3), $subfolder);
break;
default:
Generate::help();
}
break;
case 'c':
case 'console':
if (isset($args[2]) and $args[2] == 'help')
{
Console::help();
}
else
{
new Console;
}
break;
case 'p':
case 'package':
$action = isset($args[2]) ? $args[2] : 'help';
switch ($action)
{
case 'install':
case 'uninstall':
call_fuel_func_array('Oil\Package::'.$action, array_slice($args, 3));
break;
default:
Package::help();
}
break;
case 'r':
case 'refine':
$task = isset($args[2]) ? $args[2] : null;
call_user_func('Oil\Refine::run', $task, array_slice($args, 3));
break;
case 't':
case 'test':
if (isset($args[2]) and $args[2] == 'help')
{
$output = <<<HELP
Usage:
php oil [t|test]
Runtime options:
--file=<file> # Run a test on a specific file only.
--group=<group> # Only runs tests from the specified group(s).
--exclude-group=<group> # Exclude tests from the specified group(s).
--testsuite=<testsuite> # Only runs tests from the specified testsuite(s).
--coverage-clover=<file> # Generate code coverage report in Clover XML format.
--coverage-html=<dir> # Generate code coverage report in HTML format.
--coverage-php=<file> # Serialize PHP_CodeCoverage object to file.
--coverage-text=<file> # Generate code coverage report in text format.
--log-junit=<file> # Generate report of test execution in JUnit XML format to file.
--debug # Display debugging information during test execution.
Description:
Run phpunit on all or a subset of tests defined for the current application.
Examples:
php oil test
Documentation:
https://fuelphp.com/docs/packages/oil/test.html
HELP;
\Cli::write($output);
}
else
{
$phpunit_command = \Config::get('oil.phpunit.binary_path', 'phpunit');
// Check if we might be using the phar library
$is_phar = false;
foreach(explode(':', getenv('PATH')) as $path)
{
if (is_file($path.DS.$phpunit_command))
{
$handle = fopen($path.DS.$phpunit_command, 'r');
$is_phar = fread($handle, 18) == '#!/usr/bin/env php';
fclose($handle);
if ($is_phar)
{
break;
}
}
}
// Suppressing this because if the file does not exist... well thats a bad thing and we can't really check
// I know that supressing errors is bad, but if you're going to complain: shut up. - Phil
$phpunit_autoload_path = \Config::get('oil.phpunit.autoload_path', 'PHPUnit/Autoload.php' );
if (file_exists($phpunit_autoload_path))
{
include_once $phpunit_autoload_path;
}
// Attempt to load PHUnit. If it fails, we are done.
if ( ! $is_phar and ! (class_exists('PHPUnit_Framework_TestCase') or class_exists('PHPUnit\Framework\TestCase')))
{
throw new Exception('PHPUnit does not appear to be installed.'.PHP_EOL.PHP_EOL."\tPlease visit https://phpunit.de and install.");
}
// Check for a custom phpunit config, but default to the one from core
if (is_file(APPPATH.'phpunit.xml'))
{
$phpunit_config = APPPATH.'phpunit.xml';
}
else
{
$phpunit_config = COREPATH.'phpunit.xml';
}
// CD to the root of Fuel and call up phpunit with the path to our config
$command = 'cd '.DOCROOT.'; '.$phpunit_command.' -c "'.$phpunit_config.'"';
// Respect the group options
\Cli::option('group') and $command .= ' --group '.\Cli::option('group');
\Cli::option('exclude-group') and $command .= ' --exclude-group '.\Cli::option('exclude-group');
// Respect the testsuite options
\Cli::option('testsuite') and $command .= ' --testsuite '.\Cli::option('testsuite');
// Respect the debug options
\Cli::option('debug') and $command .= ' --debug';
// Respect the coverage-html option
\Cli::option('coverage-html') and $command .= ' --coverage-html '.\Cli::option('coverage-html');
\Cli::option('coverage-clover') and $command .= ' --coverage-clover '.\Cli::option('coverage-clover');
\Cli::option('coverage-text') and $command .= ' --coverage-text='.\Cli::option('coverage-text');
\Cli::option('coverage-php') and $command .= ' --coverage-php '.\Cli::option('coverage-php');
\Cli::option('log-junit') and $command .= ' --log-junit '.\Cli::option('log-junit');
\Cli::option('file') and $command .= ' '.\Cli::option('file');
\Cli::write('Tests Running...This may take a few moments.', 'green');
$return_code = 0;
foreach(explode(';', $command) as $c)
{
passthru($c, $return_code_task);
// Return failure if any subtask fails
$return_code |= $return_code_task;
}
exit($return_code);
}
break;
case 's':
case 'server':
if (isset($args[2]) and $args[2] == 'help')
{
$output = <<<HELP
Usage:
php oil [s|server]
Runtime options:
--php=<file> # The full pathname of your PHP-CLI binary if it's not in the path.
--port=<port> # TCP port number the webserver should listen too. Defaults to 8000.
--host=<host> # Hostname the webserver should run at. Defaults to "localhost".
--docroot=<dir> # Your FuelPHP docroot. Defaults to "public".
--router=<file> # PHP router script. Defaults to "fuel/packages/oil/phpserver.php".
Description:
Starts a local webserver to run your FuelPHP application, using PHP 5.4+ internal webserver.
Examples:
php oil server -p=8080
Documentation:
https://fuelphp.com/docs/packages/oil/server.html
HELP;
\Cli::write($output);
}
else
{
if (version_compare(PHP_VERSION, '5.4.0') < 0)
{
\Cli::write('The PHP built-in webserver is only available on PHP 5.4+', 'red');
break;
}
$php = \Cli::option('php', 'php');
$port = \Cli::option('p', \Cli::option('port', '8000'));
$host = \Cli::option('h', \Cli::option('host', 'localhost'));
$docroot = \Cli::option('d', \Cli::option('docroot', 'public'));
$router = \Cli::option('r', \Cli::option('router', __DIR__.DS.'..'.DS.'phpserver.php'));
\Cli::write("Listening on http://$host:$port");
\Cli::write("Document root is $docroot");
\Cli::write("Press Ctrl-C to quit.");
passthru("$php -S $host:$port -t $docroot $router");
}
break;
case 'create':
\Cli::write('You can not use "oil create", a valid FuelPHP installation already exists in this directory', 'red');
break;
default:
static::help();
}
}
catch (\Exception $e)
{
static::print_exception($e);
exit(1);
}
}
protected static function print_exception(\Exception $ex)
{
// create the error message, log and display it
$msg = $ex->getCode().' - '.$ex->getMessage().' in '.$ex->getFile().' on line '.$ex->getLine();
logger(\Fuel::L_ERROR, $msg);
\Cli::error('Uncaught exception '.get_class($ex).': '.$msg);
// print a trace if not in production, don't want to spoil external logging
if (\Fuel::$env != \Fuel::PRODUCTION)
{
\Cli::error('Callstack: ');
\Cli::error($ex->getTraceAsString());
}
\Cli::beep();
\Cli::option('speak') and `say --voice="Trinoids" "{$ex->getMessage()}"`;
// print any previous exception(s) too...
if (($previous = $ex->getPrevious()) != null)
{
\Cli::error('');
\Cli::error('Previous exception: ');
static::print_exception($previous);
}
}
public static function help()
{
echo <<<HELP
Usage:
php oil [console|generate|package|refine|help|server|test]
Runtime options:
-f, [--force] # Overwrite files that already exist
-s, [--skip] # Skip files that already exist
-q, [--quiet] # Supress status output
-t, [--speak] # Speak errors in a robot voice
Description:
The 'oil' command can be used in several ways to facilitate quick development, help with
testing your application and for running Tasks.
Environment:
If you want to specify a specific environment oil has to run in, overload the environment
variable on the commandline: FUEL_ENV=staging php oil <commands>
More information:
You can pass the parameter "help" to each of the defined command to get information
about that specific command: php oil package help
Documentation:
https://docs.fuelphp.com/packages/oil/intro.html
HELP;
}
protected static function _clear_args($actions = array())
{
foreach ($actions as $key => $action)
{
if (substr($action, 0, 1) === '-')
{
unset($actions[$key]);
}
// get rid of any junk added by Powershell on Windows...
isset($actions[$key]) and $actions[$key] = trim($actions[$key]);
}
return $actions;
}
}
/* End of file oil/classes/command.php */