Modules

Kohana_Minion_CLI

This class is a transparent base class for Minion_CLI and should not be accessed directly.

Class declared in MODPATH/minion/classes/Kohana/Minion/CLI.php on line 3.

Properties

public static $wait_msg

string(28) "Press any key to continue..."

protected static $background_colors

array(8) (
    "black" => string(2) "40"
    "red" => string(2) "41"
    "green" => string(2) "42"
    "yellow" => string(2) "43"
    "blue" => string(2) "44"
    "magenta" => string(2) "45"
    "cyan" => string(2) "46"
    "light_gray" => string(2) "47"
)

protected static $foreground_colors

array(16) (
    "black" => string(4) "0;30"
    "dark_gray" => string(4) "1;30"
    "blue" => string(4) "0;34"
    "light_blue" => string(4) "1;34"
    "green" => string(4) "0;32"
    "light_green" => string(4) "1;32"
    "cyan" => string(4) "0;36"
    "light_cyan" => string(4) "1;36"
    "red" => string(4) "0;31"
    "light_red" => string(4) "1;31"
    "purple" => string(4) "0;35"
    "light_purple" => string(4) "1;35"
    "brown" => string(4) "0;33"
    "yellow" => string(4) "1;33"
    "light_gray" => string(4) "0;37"
    "white" => string(4) "1;37"
)

Methods

public static color( string $text , atring $foreground [, string $background = NULL ] ) (defined in Kohana_Minion_CLI)

Returns the given text with the correct color codes for a foreground and optionally a background color.

Parameters

  • string $text required - The text to color
  • atring $foreground required - The foreground color
  • string $background = NULL - The background color

Tags

  • Author - Fuel Development Team
  • License - MIT License
  • Copyright - 2010 - 2011 Fuel Development Team
  • Link - http://fuelphp.com

Return Values

  • string - The color coded string

Source Code

public static function color($text, $foreground, $background = null)
{

	if (Kohana::$is_windows)
	{
		return $text;
	}

	if (!array_key_exists($foreground, Minion_CLI::$foreground_colors))
	{
		throw new Kohana_Exception('Invalid CLI foreground color: '.$foreground);
	}

	if ($background !== null and !array_key_exists($background, Minion_CLI::$background_colors))
	{
		throw new Kohana_Exception('Invalid CLI background color: '.$background);
	}

	$string = "\033[".Minion_CLI::$foreground_colors[$foreground]."m";

	if ($background !== null)
	{
		$string .= "\033[".Minion_CLI::$background_colors[$background]."m";
	}

	$string .= $text."\033[0m";

	return $string;
}

public static options( [ string $options = NULL ] ) (defined in Kohana_Minion_CLI)

Returns one or more command-line options. Options are specified using standard CLI syntax:

php index.php --username=john.smith --password=secret --var="some value with spaces"

// Get the values of "username" and "password"
$auth = Minion_CLI::options('username', 'password');

Parameters

  • string $options = NULL - ,... option name

Return Values

  • array

Source Code

public static function options($options = NULL)
{
	// Get all of the requested options
	$options = func_get_args();

	// Found option values
	$values = array();

	// Skip the first option, it is always the file executed
	for ($i = 1; $i < $_SERVER['argc']; $i++)
	{
		if ( ! isset($_SERVER['argv'][$i]))
		{
			// No more args left
			break;
		}

		// Get the option
		$opt = $_SERVER['argv'][$i];

		if (substr($opt, 0, 2) !== '--')
		{
			// This is a positional argument
			$values[] = $opt;
			continue;
		}

		// Remove the "--" prefix
		$opt = substr($opt, 2);

		if (strpos($opt, '='))
		{
			// Separate the name and value
			list ($opt, $value) = explode('=', $opt, 2);
		}
		else
		{
			$value = NULL;
		}

		$values[$opt] = $value;
	}

	if ($options)
	{
		foreach ($values as $opt => $value)
		{
			if ( ! in_array($opt, $options))
			{
				// Set the given value
				unset($values[$opt]);
			}
		}
	}

	return count($options) == 1 ? array_pop($values) : $values;
}

public static password( ) (defined in Kohana_Minion_CLI)

Experimental feature.

Reads hidden input from the user

Usage:

$password = Minion_CLI::password('Enter your password');

Tags

  • Author - Mathew Davies.

Return Values

  • string

Source Code

public static function password($text = '')
{
	$text .= ': ';

	if (Kohana::$is_windows)
	{
		$vbscript = sys_get_temp_dir().'Minion_CLI_Password.vbs';

		// Create temporary file
		file_put_contents($vbscript, 'wscript.echo(InputBox("'.addslashes($text).'"))');

		$password = shell_exec('cscript //nologo '.escapeshellarg($command));

		// Remove temporary file.
		unlink($vbscript);
	}
	else
	{
		$password = shell_exec('/usr/bin/env bash -c \'read -s -p "'.escapeshellcmd($text).'" var && echo $var\'');
	}

	Minion_CLI::write();

	return trim($password);
}

public static read( [ string $text = string(0) "" , array $options = NULL ] ) (defined in Kohana_Minion_CLI)

Reads input from the user. This can have either 1 or 2 arguments.

Usage:

// Waits for any key press Minion_CLI::read();

// Takes any input $color = Minion_CLI::read('What is your favorite color?');

// Will only accept the options in the array $ready = Minion_CLI::read('Are you ready?', array('y','n'));

Parameters

  • string $text = string(0) "" - Text to show user before waiting for input
  • array $options = NULL - Array of options the user is shown

Return Values

  • string - The user input

Source Code

public static function read($text = '', array $options = NULL)
{
	// If a question has been asked with the read
	$options_output = '';
	if ( ! empty($options))
	{
		$options_output = ' [ '.implode(', ', $options).' ]';
	}

	fwrite(STDOUT, $text.$options_output.': ');

	// Read the input from keyboard.
	$input = trim(fgets(STDIN));

	// If options are provided and the choice is not in the array, tell them to try again
	if ( ! empty($options) && ! in_array($input, $options))
	{
		Minion_CLI::write('This is not a valid option. Please try again.');

		$input = Minion_CLI::read($text, $options);
	}

	// Read the input
	return $input;
}

public static wait( [ int $seconds = integer 0 , bool $countdown = bool FALSE ] ) (defined in Kohana_Minion_CLI)

Waits a certain number of seconds, optionally showing a wait message and waiting for a key press.

Parameters

  • int $seconds = integer 0 - Number of seconds
  • bool $countdown = bool FALSE - Show a countdown or not

Tags

  • Author - Fuel Development Team
  • License - MIT License
  • Copyright - 2010 - 2011 Fuel Development Team
  • Link - http://fuelphp.com

Source Code

public static function wait($seconds = 0, $countdown = false)
{
	if ($countdown === true)
	{
		$time = $seconds;

		while ($time > 0)
		{
			fwrite(STDOUT, $time.'... ');
			sleep(1);
			$time--;
		}

		Minion_CLI::write();
	}
	else
	{
		if ($seconds > 0)
		{
			sleep($seconds);
		}
		else
		{
			Minion_CLI::write(Minion_CLI::$wait_msg);
			Minion_CLI::read();
		}
	}
}

public static write( [ string|array $text = string(0) "" ] ) (defined in Kohana_Minion_CLI)

Outputs a string to the cli. If you send an array it will implode them with a line break.

Parameters

  • string|array $text = string(0) "" - The text to output, or array of lines

Source Code

public static function write($text = '')
{
	if (is_array($text))
	{
		foreach ($text as $line)
		{
			Minion_CLI::write($line);
		}
	}
	else
	{
		fwrite(STDOUT, $text.PHP_EOL);
	}
}

public static write_replace( [ string $text = string(0) "" , boolean $end_line = bool FALSE ] ) (defined in Kohana_Minion_CLI)

Outputs a replacable line to the cli. You can continue replacing the line until TRUE is passed as the second parameter in order to indicate you are done modifying the line.

// Sample progress indicator
Minion_CLI::write_replace('0%');
Minion_CLI::write_replace('25%');
Minion_CLI::write_replace('50%');
Minion_CLI::write_replace('75%');
// Done writing this line
Minion_CLI::write_replace('100%', TRUE);

Parameters

  • string $text = string(0) "" - The text to output
  • boolean $end_line = bool FALSE - Whether the line is done being replaced

Source Code

public static function write_replace($text = '', $end_line = FALSE)
{
	// Append a newline if $end_line is TRUE
	$text = $end_line ? $text.PHP_EOL : $text;
	fwrite(STDOUT, "\r\033[K".$text);
}