Skip to content

Commit 030558c

Browse files
committed
Added command() common helper function to run a command programatically.
1 parent 2891585 commit 030558c

3 files changed

Lines changed: 96 additions & 21 deletions

File tree

system/CLI/CLI.php

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -175,21 +175,30 @@ class CLI
175175
*/
176176
public static function init()
177177
{
178-
// Readline is an extension for PHP that makes interactivity with PHP
179-
// much more bash-like.
180-
// http://www.php.net/manual/en/readline.installation.php
181-
static::$readline_support = extension_loaded('readline');
178+
if (is_cli())
179+
{
180+
// Readline is an extension for PHP that makes interactivity with PHP
181+
// much more bash-like.
182+
// http://www.php.net/manual/en/readline.installation.php
183+
static::$readline_support = extension_loaded('readline');
182184

183-
// clear segments & options to keep testing clean
184-
static::$segments = [];
185-
static::$options = [];
185+
// clear segments & options to keep testing clean
186+
static::$segments = [];
187+
static::$options = [];
186188

187-
// Check our stream resource for color support
188-
static::$isColored = static::hasColorSupport(STDOUT);
189+
// Check our stream resource for color support
190+
static::$isColored = static::hasColorSupport(STDOUT);
189191

190-
static::parseCommandLine();
192+
static::parseCommandLine();
191193

192-
static::$initialized = true;
194+
static::$initialized = true;
195+
}
196+
else
197+
{
198+
// If the command is being called from a controller
199+
// we need to define STDOUT ourselves
200+
define('STDOUT', 'php://output');
201+
}
193202
}
194203

195204
//--------------------------------------------------------------------
@@ -275,7 +284,7 @@ public static function prompt(string $field, $options = null, string $validation
275284
$default = $options[0];
276285
}
277286

278-
fwrite(STDOUT, $field . $extra_output . ': ');
287+
static::fwrite(STDOUT, $field . $extra_output . ': ');
279288

280289
// Read the input from keyboard.
281290
$input = trim(static::input()) ?: $default;
@@ -340,7 +349,7 @@ public static function print(string $text = '', string $foreground = null, strin
340349

341350
static::$lastWrite = null;
342351

343-
fwrite(STDOUT, $text);
352+
static::fwrite(STDOUT, $text);
344353
}
345354

346355
/**
@@ -363,7 +372,7 @@ public static function write(string $text = '', string $foreground = null, strin
363372
static::$lastWrite = 'write';
364373
}
365374

366-
fwrite(STDOUT, $text . PHP_EOL);
375+
static::fwrite(STDOUT, $text . PHP_EOL);
367376
}
368377

369378
//--------------------------------------------------------------------
@@ -386,7 +395,7 @@ public static function error(string $text, string $foreground = 'light_red', str
386395
$text = static::color($text, $foreground, $background);
387396
}
388397

389-
fwrite(STDERR, $text . PHP_EOL);
398+
static::fwrite(STDERR, $text . PHP_EOL);
390399

391400
// return STDOUT color support
392401
static::$isColored = $stdout;
@@ -421,7 +430,7 @@ public static function wait(int $seconds, bool $countdown = false)
421430

422431
while ($time > 0)
423432
{
424-
fwrite(STDOUT, $time . '... ');
433+
static::fwrite(STDOUT, $time . '... ');
425434
sleep(1);
426435
$time --;
427436
}
@@ -488,7 +497,7 @@ public static function clearScreen()
488497
// can handle CSI sequences. For lower than Win10 we just shove in 40 new lines.
489498
static::isWindows() && ! static::streamSupports('sapi_windows_vt100_support', STDOUT)
490499
? static::newLine(40)
491-
: fwrite(STDOUT, "\033[H\033[2J");
500+
: static::fwrite(STDOUT, "\033[H\033[2J");
492501
}
493502

494503
//--------------------------------------------------------------------
@@ -772,7 +781,7 @@ public static function showProgress($thisStep = 1, int $totalSteps = 10)
772781
// restore cursor position when progress is continuing.
773782
if ($inProgress !== false && $inProgress <= $thisStep)
774783
{
775-
fwrite(STDOUT, "\033[1A");
784+
static::fwrite(STDOUT, "\033[1A");
776785
}
777786
$inProgress = $thisStep;
778787

@@ -786,13 +795,13 @@ public static function showProgress($thisStep = 1, int $totalSteps = 10)
786795
$step = (int) round($percent / 10);
787796

788797
// Write the progress bar
789-
fwrite(STDOUT, "[\033[32m" . str_repeat('#', $step) . str_repeat('.', 10 - $step) . "\033[0m]");
798+
static::fwrite(STDOUT, "[\033[32m" . str_repeat('#', $step) . str_repeat('.', 10 - $step) . "\033[0m]");
790799
// Textual representation...
791-
fwrite(STDOUT, sprintf(' %3d%% Complete', $percent) . PHP_EOL);
800+
static::fwrite(STDOUT, sprintf(' %3d%% Complete', $percent) . PHP_EOL);
792801
}
793802
else
794803
{
795-
fwrite(STDOUT, "\007");
804+
static::fwrite(STDOUT, "\007");
796805
}
797806
}
798807

@@ -1121,6 +1130,28 @@ public static function table(array $tbody, array $thead = [])
11211130
}
11221131

11231132
//--------------------------------------------------------------------
1133+
1134+
/**
1135+
* While the library is intended for use on CLI commands,
1136+
* commands can be called from controllers and elsewhere
1137+
* so we need a way to allow them to still work.
1138+
*
1139+
* For now, just echo the content, but look into a better
1140+
* solution down the road.
1141+
*
1142+
* @param $handle
1143+
* @param string $string
1144+
*/
1145+
protected static function fwrite($handle, string $string)
1146+
{
1147+
if (is_cli())
1148+
{
1149+
fwrite($handle, $string);
1150+
return;
1151+
}
1152+
1153+
echo $string;
1154+
}
11241155
}
11251156

11261157
// Ensure the class is initialized. Done outside of code coverage

system/Common.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,34 @@ function cache(string $key = null)
112112
}
113113
}
114114

115+
if (! function_exists('command'))
116+
{
117+
/**
118+
* Runs a single command.
119+
* Input expected in a single string as would
120+
* be used on the command line itself:
121+
*
122+
* > command('migrate:create SomeMigration');
123+
*
124+
* @param string $command
125+
*
126+
* @return false|string
127+
*/
128+
function command(string $command)
129+
{
130+
$runner = service('commands');
131+
132+
$params = explode(' ', $command);
133+
$command = array_shift($params);
134+
135+
ob_start();
136+
$runner->run($command, $params);
137+
$output = ob_get_clean();
138+
139+
return $output;
140+
}
141+
}
142+
115143
if (! function_exists('config'))
116144
{
117145
/**

user_guide_src/source/cli/cli_commands.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ for the information it needs to run correctly::
3737
> php spark migrate:version
3838
> Version?
3939

40+
Calling Commands
41+
================
42+
43+
Commands can also be ran from within your own code. This is most often done within a controller for cronjob tasks,
44+
but they can be used at any time. You do this by using the ``command()`` function. This function is always available.
45+
46+
::
47+
48+
echo command('migrate:create TestMigration');
49+
50+
The only argument is string that is the command called and any parameters. This appears exactly as you would call
51+
it from the command line.
52+
53+
All output from the command that is ran is captured when not run from the command line. It is returned from the command
54+
so that you can choose to display it or not.
55+
4056
******************
4157
Using Help Command
4258
******************

0 commit comments

Comments
 (0)