-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathConsole.php
More file actions
616 lines (518 loc) · 21.3 KB
/
Console.php
File metadata and controls
616 lines (518 loc) · 21.3 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<?php
/*!
* Console Class
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Handles the set-up of the console commands, options, and documentation
* Heavily influenced by the symfony/console output format
*
*/
namespace PatternLab;
use \Colors\Color;
use \PatternLab\Console\Event as ConsoleEvent;
use \PatternLab\Dispatcher;
use \PatternLab\Timer;
use \Symfony\Component\Process\PhpExecutableFinder;
class Console {
protected static $commands = array();
protected static $commandInstances = array();
protected static $options = array();
protected static $optionsShort = "";
protected static $optionsLong = array();
private static $color;
private static $self = "";
private static $zTracker = 1;
public static function init() {
// double-check this is being run from the command line
if (php_sapi_name() != 'cli') {
print "The console can only be run from the command line.\n";
exit;
}
self::$self = $_SERVER["PHP_SELF"];
// set-up the cli coloring
self::$color = new Color();
// define the pattern lab color theme
$colorTheme = array();
$colorTheme["h1"] = "bold";
$colorTheme["h2"] = "underline";
$colorTheme["optional"] = "italic";
$colorTheme["desc"] = "green";
$colorTheme["path"] = "green";
$colorTheme["enter"] = "blue";
$colorTheme["ok"] = "green";
$colorTheme["options"] = "magenta";
$colorTheme["info"] = "cyan";
$colorTheme["warning"] = "yellow";
$colorTheme["error"] = "red";
$colorTheme["strong"] = "bold";
$colorTheme["fire1"] = "color[196]";
$colorTheme["fire2"] = "color[202]";
$colorTheme["fire3"] = "color[208]";
$colorTheme["fire4"] = "color[214]";
$colorTheme["fire5"] = "color[220]";
$colorTheme["fire6"] = "color[226]";
$colorTheme["cool"] = "color[19]";
self::$color->setTheme($colorTheme);
}
public static function run() {
// send out an event
$event = new ConsoleEvent($options = array());
Dispatcher::getInstance()->dispatch("console.loadCommandsStart",$event);
// loadCommands
self::loadCommands();
// send out an event
Dispatcher::getInstance()->dispatch("console.loadCommandsEnd",$event);
// get what was passed on the command line
self::$options = getopt(self::$optionsShort,self::$optionsLong);
// test and run the given command
$commandFound = false;
$commandSent = self::getCommand();
foreach (self::$commandInstances as $command) {
if ($command->test($commandSent)) {
$command->run();
$commandFound = true;
}
}
// no command was found so just draw the help by default
if (!$commandFound) {
self::writeHelp();
}
}
/**
* See if a particular command was passed to the script via the command line and return a boolean. Can either be the short or long version
* @param {String} list of arguments to check
*
* @return {Boolean} if the command has been passed to the script via the command line
*/
public static function findCommand($args) {
$args = explode("|",$args);
foreach ($args as $arg) {
if (isset(self::$options[$arg])) {
return true;
}
}
return false;
}
/**
* See if a particular command was passed to the script via the command line and return a value. Can either be the short or long version
* @param {String} list of arguments to check
*
* @return {String} the value that was passed via the command line
*/
public static function findCommandValue($args) {
$args = explode("|",$args);
foreach ($args as $arg) {
if (isset(self::$options[$arg])) {
return self::$options[$arg];
}
}
return false;
}
/**
* Find the short command for a given long gommand
* @param {String} long command to search for
*
* @return {String} the search command
*/
public static function findCommandLong($arg) {
foreach (self::$commands as $command => $commandOptions) {
if (($commandOptions["commandLong"] == $arg) || ($commandOptions["commandShort"] == $arg)) {
return $command;
}
}
return false;
}
/**
* Return the command that was given in the command line arguments
*
* @return {String} the command. passes false if no command was found
*/
public static function getCommand() {
foreach (self::$commands as $command => $attributes) {
if (isset(self::$options[$command]) || isset(self::$options[$attributes["commandShort"]])) {
return $command;
}
}
return false;
}
/**
* Get the path to PHP binary
*
* @return {String} the path to the PHP executable
*/
public static function getPathPHP() {
$manualPHP = Config::getOption("phpBin");
$autoPHP = new PhpExecutableFinder();
$path = $manualPHP ? $manualPHP : $autoPHP->find();
if (!$path) {
$configPath = Console::getHumanReadablePath(Config::getOption("configPath"));
$examplePHP = (DIRECTORY_SEPARATOR === "/") ? "C:\wamp\bin\php\php5.5.12" : "/opt/local/lib/php54";
Console::writeError("can't find PHP. add the path to PHP by adding the option \"phpBin\" to <path>".$configPath."</path>. it should look like \"phpBin\": \"".$examplePHP."\"");
}
return $path;
}
/**
* Get the path to the calling script. Should be core/console but let's not make that assumption
*
* @return {String} the path to the calling script
*/
public static function getPathConsole() {
$console = isset($_SERVER["SCRIPT_NAME"]) ? $_SERVER["SCRIPT_NAME"] : Config::getOption("phpScriptName");
if (!$console) {
$configPath = Console::getHumanReadablePath(Config::getOption("configPath"));
Console::writeError("please add the option `phpScriptName` with the path to your console option (e.g. core".DIRECTORY_SEPARATOR."console) to <path>".$configPath."</path> to run this process...");
}
return Config::getOption("baseDir").$console;
}
/**
* Load all of the rules related to Pattern Data
*/
public static function loadCommands() {
foreach (glob(__DIR__."/Console/Commands/*.php") as $filename) {
$command = str_replace(".php","",str_replace(__DIR__."/Console/Commands/","",$filename));
if ($command[0] != "_") {
$commandClass = "\PatternLab\Console\Commands\\".$command;
self::$commandInstances[] = new $commandClass();
}
}
}
/**
* Set-up the command so it can be used from the command line
* @param {String} the single character version of the command
* @param {String} the long version of the command
* @param {String} the description to be used in the "available commands" section of writeHelp()
* @param {String} the description to be used in the "help" section of writeHelpCommand()
*/
public static function setCommand($long,$desc,$help,$short = "") {
if (!empty($short)) {
self::$optionsShort .= $short;
}
self::$optionsLong[] = $long;
$short = str_replace(":","",$short);
$long = str_replace(":","",$long);
self::$commands[$long] = array("commandShort" => $short, "commandLong" => $long, "commandLongLength" => strlen($long), "commandDesc" => $desc, "commandHelp" => $help, "commandOptions" => array(), "commandExamples" => array());
}
/**
* Set a sample for a specific command
* @param {String} the long version of the command that this option is related to
* @param {String} the sample to be used in the "sample" section of writeHelpCommand()
* @param {String} the extra info to be used in the example command for the "sample" section of writeHelpCommand()
*/
public static function setCommandSample($command,$sample,$extra) {
$command = str_replace(":","",$command);
self::$commands[$command]["commandExamples"][] = array("exampleSample" => $sample, "exampleExtra" => $extra);
}
/**
* See if a particular option was passed to the script via the command line and return a boolean. Can either be the short or long version
* @param {String} list of arguments to check
*
* @return {Boolean} if the command has been passed to the script via the command line
*/
public static function findCommandOption($args) {
$args = explode("|",$args);
foreach ($args as $arg) {
if (isset(self::$options[$arg])) {
return true;
}
}
return false;
}
/**
* See if a particular option was passed to the script via the command line and return a value. Can either be the short or long version
* @param {String} list of arguments to check
*
* @return {String} the value that was passed via the command line
*/
public static function findCommandOptionValue($args) {
$args = explode("|",$args);
foreach ($args as $arg) {
if (isset(self::$options[$arg])) {
return self::$options[$arg];
}
}
return false;
}
/**
* Set-up an option for a given command so it can be used from the command line
* @param {String} the long version of the command that this option is related to
* @param {String} the long version of the option
* @param {String} the description to be used in the "available options" section of writeHelpCommand()
* @param {String} the sample to be used in the "sample" section of writeHelpCommand()
* @param {String} the single character version of the option
* @param {String} the extra info to be used in the example command for the "sample" section of writeHelpCommand()
*/
public static function setCommandOption($command,$long,$desc,$sample,$short = "",$extra = "") {
if (($short != "") && ($short != "z") && (strpos(self::$optionsShort,$short) === false)) {
self::$optionsShort .= $short;
}
if (!in_array($long,self::$optionsLong)) {
self::$optionsLong[] = $long;
}
$short = str_replace(":","",$short);
$long = str_replace(":","",$long);
if ($short == "z") {
$short = "z".self::$zTracker;
self::$zTracker++;
}
self::$commands[$command]["commandOptions"][$long] = array("optionShort" => $short, "optionLong" => $long, "optionLongLength" => strlen($long), "optionDesc" => $desc, "optionSample" => $sample, "optionExtra" => $extra);
}
/**
* Write out the generic help
*/
public static function writeHelp() {
/*
The generic help follows this format:
Pattern Lab Console Options
Usage:
php core/console command [options]
Available commands:
--build (-b) Build Pattern Lab
--watch (-w) Build Pattern Lab and watch for changes and rebuild as necessary
--version (-v) Display the version number
--help (-h) Display this help message.
*/
// find length of longest command
$lengthLong = 0;
foreach (self::$commands as $command => $attributes) {
$lengthLong = ($attributes["commandLongLength"] > $lengthLong) ? $attributes["commandLongLength"] : $lengthLong;
}
// write out the generic usage info
self::writeLine("");
self::writeLine("<h1>Pattern Lab Console Options</h1>",true,true);
self::writeLine("<h2>Usage</h2>:",true,true);
self::writeLine(" php ".self::$self." command <optional>[options]</optional>",true,true);
self::writeLine("<h2>Available commands</h2>:",true,true);
// write out the commands
foreach (self::$commands as $command => $attributes) {
$spacer = self::getSpacer($lengthLong,$attributes["commandLongLength"]);
self::writeLine(" --".$attributes["commandLong"].$spacer." <desc>".$attributes["commandDesc"]."</desc>",true);
}
// write out how to get more help
self::writeLine("");
self::writeLine("<h2>Get options for a specific command:</h2>",true,true);
self::writeLine(" php ".self::$self." --help --command",true);
self::writeLine("");
}
/**
* Write out the command-specific help
* @param {String} the single character of the command that this option is related to
*/
public static function writeHelpCommand($command = "") {
/*
The command help follows this format:
Build Command Options
Usage:
php core/console --build [--patternsonly|-p] [--nocache|-n] [--enablecss|-c]
Available options:
--patternsonly (-p) Build only the patterns. Does NOT clean public/.
--nocache (-n) Set the cacheBuster value to 0.
--enablecss (-c) Generate CSS for each pattern. Resource intensive.
--help (-h) Display this help message.
Help:
The build command builds an entire site a single time. It compiles the patterns and moves content from source/ into public/
Samples:
To run and generate the CSS for each pattern:
php core/console --build -c
To build only the patterns and not move other files from source/ to public/
php core/console --build -p
To turn off the cacheBuster
php core/console --build -n
*/
// if given an empty command or the command doesn't exist in the lists give the generic help
if (empty($command)) {
self::writeHelp();
return;
}
$commandLong = self::$commands[$command]["commandLong"];
$commandLongUC = ucfirst($commandLong);
$commandHelp = self::$commands[$command]["commandHelp"];
$commandExtra = isset(self::$commands[$command]["commandExtra"]) ? self::$commands[$command]["commandExtra"] : "";
$commandOptions = self::$commands[$command]["commandOptions"];
$commandExamples = self::$commands[$command]["commandExamples"];
$commandShort = self::$commands[$command]["commandShort"];
$commandShortInc = ($commandShort != "") ? "|-".$commandShort : "";
// write out the option list and get the longest item
$optionList = "";
$lengthLong = 0;
foreach ($commandOptions as $option => $attributes) {
$optionShort = (!empty($attributes["optionShort"][0]) && (($attributes["optionShort"][0] != "z") || ($attributes["optionShort"] != ""))) ? "|-".$attributes["optionShort"] : "";
$optionExtra = (!empty($attributes["optionExtra"])) ? " ".$attributes["optionExtra"] : "";
$optionList .= "[--".$attributes["optionLong"].$optionShort.$optionExtra."] ";
$lengthLong = ($attributes["optionLongLength"] > $lengthLong) ? $attributes["optionLongLength"] : $lengthLong;
}
$commandExampleList = "";
if (count($commandExamples) > 0) {
foreach ($commandExamples as $example => $attributes) {
$commandExampleList .= $attributes["exampleExtra"]." ";
}
}
// write out the generic usage info
self::writeLine("");
self::writeLine("<h1>".$commandLongUC." Command Options</h1>",true,true);
self::writeLine("<h2>Usage</h2>:",true,true);
self::writeLine(" php ".self::$self." --".$commandLong.$commandShortInc." ".$optionList,true,true);
// write out the available options
if (count($commandOptions) > 0) {
self::writeLine("<h2>Available options</h2>:",true,true);
foreach ($commandOptions as $option => $attributes) {
$optionShort = (!empty($attributes["optionShort"]) && (($attributes["optionShort"][0] != "z") || ($attributes["optionShort"] != ""))) ? "(-".$attributes["optionShort"].")" : " ";
$spacer = self::getSpacer($lengthLong,$attributes["optionLongLength"]);
self::writeLine(" --".$attributes["optionLong"].$spacer.$optionShort." <desc>".$attributes["optionDesc"]."</desc>",true);
}
self::writeLine("");
}
self::writeLine("<h2>Help</h2>:",true,true);
self::writeLine(" ".$commandHelp,true,true);
// write out the samples
if ((count($commandOptions) > 0) || (count($commandExamples) > 0)) {
self::writeLine("<h2>Samples</h2>:",true,true);
}
if (count($commandExamples) > 0) {
foreach ($commandExamples as $example => $attributes) {
self::writeLine(" ".$attributes["exampleSample"],true,true);
self::writeLine(" <desc>php ".self::$self." --".$commandLong." ".$attributes["exampleExtra"]."</desc>",true,true);
}
}
if (count($commandOptions) > 0) {
foreach ($commandOptions as $option => $attributes) {
self::writeLine(" ".$attributes["optionSample"],true,true);
self::writeLine(" <desc>php ".self::$self." --".$commandLong." --".$attributes["optionLong"]." ".$attributes["optionExtra"]."</desc>",true,true);
}
}
}
/**
* Make sure the space is properly set between long command options and short command options
* @param {Integer} the longest length of the command's options
* @param {Integer} the character length of the given option
*/
public static function getSpacer($lengthLong,$itemLongLength) {
$i = 0;
$spacer = " ";
$spacerLength = $lengthLong - $itemLongLength;
while ($i < $spacerLength) {
$spacer .= " ";
$i++;
}
return $spacer;
}
/**
* Make a path human readable by dropping the base dir
* @param {String} the path to clean
*
* @return {String} cleaned up path
*/
public static function getHumanReadablePath($path) {
return str_replace(Config::getOption("baseDir"), ".".DIRECTORY_SEPARATOR, $path);
}
/**
* Modify a line to include the given tag by default
* @param {String} the content to be written out
*/
public static function addTags($line,$tag) {
$lineFinal = "<".$tag.">".preg_replace("/<[A-z0-9-_]{1,}>[^<]{1,}<\/[A-z0-9-_]{1,}>/","</".$tag.">$0<".$tag.">",$line)."</".$tag.">";
return $lineFinal;
}
/**
* Write out a line to the console with error tags. It forces an exit of the script
* @param {String} the content to be written out
* @param {Boolean} if there should be two spaces added to the beginning of the line
* @param {Boolean} if there should be two breaks added to the end of the line
*/
public static function writeError($line,$doubleSpace = false,$doubleBreak = false) {
$lineFinal = self::addTags($line,"error");
self::writeLine($lineFinal,$doubleSpace,$doubleBreak);
exit(1);
}
/**
* Write out a line to the console with info tags
* @param {String} the content to be written out
* @param {Boolean} if there should be two spaces added to the beginning of the line
* @param {Boolean} if there should be two breaks added to the end of the line
*/
public static function writeInfo($line,$doubleSpace = false,$doubleBreak = false) {
$lineFinal = self::addTags($line,"info");
self::writeLine($lineFinal,$doubleSpace,$doubleBreak);
}
/**
* Alias for writeInfo because I keep wanting to use it
* @param {String} the content to be written out
* @param {Boolean} if there should be two spaces added to the beginning of the line
* @param {Boolean} if there should be two breaks added to the end of the line
*/
public static function log($line,$doubleSpace = false,$doubleBreak = false) {
self::writeInfo($line,$doubleSpace = false,$doubleBreak = false);
}
/**
* Write out a line to the console
* @param {String} the content to be written out
* @param {Boolean} if there should be two spaces added to the beginning of the line
* @param {Boolean} if there should be two breaks added to the end of the line
*/
public static function writeLine($line,$doubleSpace = false,$doubleBreak = false) {
$break = ($doubleBreak) ? PHP_EOL.PHP_EOL : PHP_EOL;
if (strpos($line,"<nophpeol>") !== false) {
$break = "";
$line = str_replace("<nophpeol>","",$line);
}
$space = ($doubleSpace) ? " " : "";
$c = self::$color;
print $space.$c($line)->colorize().$break;
}
/**
* Write out a line to the console with a specific tag
* @param {String} the tag to add to the line
* @param {String} the content to be written out
* @param {Boolean} if there should be two spaces added to the beginning of the line
* @param {Boolean} if there should be two breaks added to the end of the line
*/
public static function writeTag($tag,$line,$doubleSpace = false,$doubleBreak = false) {
$lineFinal = self::addTags($line,$tag);
self::writeLine($lineFinal,$doubleSpace,$doubleBreak);
}
/**
* Write out a line to the console with warning tags
* @param {String} the content to be written out
* @param {Boolean} if there should be two spaces added to the beginning of the line
* @param {Boolean} if there should be two breaks added to the end of the line
*/
public static function writeWarning($line,$doubleSpace = false,$doubleBreak = false) {
$lineFinal = self::addTags($line,"warning");
self::writeLine($lineFinal,$doubleSpace,$doubleBreak);
}
/**
* Prompt the user for some input
* @param {String} the text for the prompt
* @param {String} the text for the options
* @param {String} the text for the default option
* @param {Boolean} if we should lowercase the input before sending it back
* @param {String} the tag that should be used when drawing the content
*
* @return {String} trimmed input given by the user
*/
public static function promptInput($prompt = "", $options = "", $default = "", $lowercase = true, $tag = "info") {
// check prompt
if (empty($prompt)) {
Console::writeError("an input prompt requires prompt text...");
}
// if there are suggested options add them
if (!empty($options)) {
$prompt .= " <options>".$options."</options> >";
}
// make sure no end-of-line is added
$prompt .= " <nophpeol>";
// make sure we're not running in no interaction mode. if so just use the default for the input
if (InstallerUtil::$isInteractive) {
// open the terminal and wait for feedback
$stdin = fopen("php://stdin", "r");
Console::writeTag($tag,$prompt);
$input = trim(fgets($stdin));
fclose($stdin);
} else {
$input = $default;
}
// check to see if it should be lowercased before sending back
return ($lowercase) ? strtolower($input) : $input;
}
}