Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Commit d9ece36

Browse files
committed
Enabled box to run PHP commands in addition to running Bash commands
1 parent 138077e commit d9ece36

File tree

4 files changed

+100
-6
lines changed

4 files changed

+100
-6
lines changed

guest/cli/box

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,29 @@ if [ "${WPLIB_BOX_COMMAND}" == "help" ]; then
2020
exit
2121
fi
2222

23-
if [ ! -f "${command_file}" ]; then
24-
echo
25-
echo -e "\tCommand not found: $1"
26-
echo
27-
exit
23+
if [ -f "${command_file}" ]; then
24+
command_type="bash"
25+
else
26+
if [ -f "${command_file}.php" ]; then
27+
command_type="php"
28+
else
29+
echo
30+
echo -e "\tCommand not found: $1"
31+
echo
32+
exit
33+
fi
2834
fi
2935

3036
echo_if_not_quiet "$*" "*Running $1..."
3137
initial_dir=$(pwd)
32-
source "${command_file}" "$2" "$3" "$4" "$5"
38+
case "${command_type}" in
39+
"bash")
40+
source "${command_file}" "$2" "$3" "$4" "$5"
41+
;;
42+
"php")
43+
44+
/usr/bin/php7.0 "${WPLIB_BOX_INCLUDES_DIR}/run-command.php" --args "${WPLIB_BOX_COMMAND}" "$2" "$3" "$4" "$5"
45+
;;
46+
esac
3347
cd "${initial_dir}"
3448
echo_if_not_quiet "$*" "*Done."

guest/cli/includes/constants

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env bash
22

3+
export WPLIB_BOX_PHPCLI=$(which php7.0)
4+
35
export WPLIB_BOX_DIR="/vagrant"
46
export WPLIB_BOX_LOGS_DIR="/vagrant/logs"
57
export WPLIB_BOX_WEB_ROOT_DIR="/var/www"

guest/cli/includes/functions.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
function is_quiet() {
4+
global $argv;
5+
$args = implode( '', $argv );
6+
return false !== strpos( $args, '--quiet' );
7+
}
8+
9+
function error_die( $message ) {
10+
$instruction = $message[0];
11+
$message = substr( $message, 1 );
12+
echo_message( "{$instruction} ERROR: {$message}" );
13+
die();
14+
}
15+
16+
function echo_if_not_quiet( $message ) {
17+
if ( is_quiet() ) {
18+
return;
19+
}
20+
echo_message( $message );
21+
}
22+
23+
function echo_message( $message ) {
24+
$instruction = $message[0];
25+
$message = substr( $message, 1 );
26+
switch ( $instruction ) {
27+
case '^':
28+
echo "{$message}\n\n";
29+
break;
30+
case '*':
31+
echo "\n{$message}\n\n";
32+
break;
33+
case '=':
34+
echo "{$message}\n";
35+
break;
36+
case '&':
37+
echo "\n{$message}\n";
38+
break;
39+
default:
40+
echo "\n\tCODING ERROR:";
41+
echo "\n\tYour message for echo_if_not_quiet() does not begin with one of: ^, *, = or &.\n";
42+
break;
43+
}
44+
45+
}
46+

guest/cli/includes/run-command.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Runs a command but strips off the calling @args and also includes includes/functions.php
4+
*/
5+
6+
$args = $GLOBALS['argv'];
7+
/*
8+
* Remove any args NOT means to be passed to the command.
9+
*/
10+
while ( count( $args ) && '--args' !== strtolower( $args[0] ) ) {
11+
array_shift( $args );
12+
}
13+
array_shift( $args );
14+
$GLOBALS['argv'] = $args;
15+
16+
/*
17+
* Include the functions file
18+
*/
19+
include "{$_SERVER['WPLIB_BOX_INCLUDES_DIR']}/functions.php";
20+
21+
$command = "{$args[0]}";
22+
$command_file = "{$_SERVER['WPLIB_BOX_COMMANDS_DIR']}/{$command}.php";
23+
if ( ! is_file( $command_file ) ) {
24+
error_die( "*Command {$command} does not exist.");
25+
}
26+
27+
/*
28+
* Run the command
29+
*/
30+
require $command_file;
31+
32+

0 commit comments

Comments
 (0)