Skip to content

Commit

Permalink
starting to update help menu in \console
Browse files Browse the repository at this point in the history
  • Loading branch information
gwoo committed Jan 22, 2010
1 parent ad115f4 commit 2764bb0
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 13 deletions.
26 changes: 18 additions & 8 deletions libraries/lithium/console/Command.php
Expand Up @@ -18,6 +18,13 @@
* The base class to inherit when writing console scripts in Lithium.
*/
class Command extends \lithium\core\Object {

/**
* If -h or --help param exists a help screen will be returned.
*
* @var boolean
*/
public $help = false;

/**
* A Request object.
Expand Down Expand Up @@ -100,16 +107,14 @@ protected function _init() {
* @todo Implement proper exception catching/throwing.
* @todo Implement filters.
*/
public function __invoke($action, $passed = array(), $options = array()) {
public function __invoke($action, $args = array(), $options = array()) {
try {
$result = $this->invokeMethod($action, $passed);

$this->response->status = 1;
$result = $this->invokeMethod($action, $args);
if (is_int($result)) {
$this->response->status = $result;
} elseif ($result || $result === null) {
$this->response->status = 0;
} else {
$this->response->status = 1;
}
} catch (Exception $e) {
$this->response->status = 1;
Expand Down Expand Up @@ -287,7 +292,12 @@ public function stop($status = 0, $message = null) {
*
* @return boolean
*/
public function help() {
protected function _help() {

var_dump(Inspector::info(get_class($this)));

$methods = array_keys(Inspector::methods($class, 'extents'));
var_dump($methods);
$parent = new ReflectionClass("\lithium\console\Command");
$class = new ReflectionClass(get_class($this));
$template = $class->newInstance();
Expand All @@ -301,7 +311,7 @@ public function help() {
$type = isset($comment['tags']['var']) ? strtok($comment['tags']['var'], ' ') : null;

$name = str_replace('_', '-', Inflector::underscore($property->getName()));
$usage = $type == 'boolean' ? "--{$name}" : "--{$name}=" . strtoupper($name);
$usage = $type == 'boolean' ? "-{$name}" : "--{$name}=" . strtoupper($name);

$property = compact('name', 'description', 'type', 'usage');
}
Expand Down Expand Up @@ -336,7 +346,7 @@ public function help() {
$this->nl();
}
}
if (!$this->request->params['command']) {
if (!$this->request->command) {
$this->nl();
$this->out('COMMANDS');
$commands = Libraries::locate('command', null, array('recursive' => false));
Expand Down
10 changes: 7 additions & 3 deletions libraries/lithium/console/Dispatcher.php
Expand Up @@ -116,7 +116,7 @@ protected static function _callable($request, $params, $options) {

if (!$name) {
$name = $class = '\lithium\console\Command';
$request->params['action'] = 'help';
$request->params['action'] = '_help';
}
if ($class[0] !== '\\') {
$name = Inflector::camelize($class);
Expand Down Expand Up @@ -148,8 +148,12 @@ protected static function _call($callable, $request, $params) {
array_unshift($request->params['args'], $request->params['action']);
$request->params['action'] = 'run';
}
if (!method_exists($callable, $request->params['action'])) {
$request->params['action'] = 'help';
$isHelp = (
!empty($request->params['help']) || !empty($request->params['h'])
|| !method_exists($callable, $request->params['action'])
);
if ($isHelp) {
$request->params['action'] = '_help';
}
return $callable($request->params['action'], $request->params['args']);
}
Expand Down
14 changes: 14 additions & 0 deletions libraries/lithium/console/Request.php
Expand Up @@ -83,6 +83,20 @@ protected function _init() {
}
parent::_init();
}

/**
* Allows request parameters to be accessed as object properties, i.e. `$this->request->action`
* instead of `$this->request->params['action']`.
*
* @param string $name The property name/parameter key to return.
* @return mixed Returns the value of `$params[$name]` if it is set, otherwise returns null.
* @see lithium\action\Request::$params
*/
public function __get($name) {
if (isset($this->params[$name])) {
return $this->params[$name];
}
}

/**
* Get environment variables.
Expand Down
14 changes: 12 additions & 2 deletions libraries/lithium/tests/cases/console/CommandTest.php
Expand Up @@ -10,6 +10,7 @@

use \lithium\console\Request;
use \lithium\tests\mocks\console\MockCommand;
use \lithium\tests\mocks\console\command\MockCommandHelp;

class CommandTest extends \lithium\test\Unit {

Expand Down Expand Up @@ -149,7 +150,7 @@ public function testColumns() {

public function testHelp() {
$command = new MockCommand(array('request' => $this->request));
$return = $command->help();
$return = $command->__invoke('_help');

$this->assertTrue($return);

Expand All @@ -159,12 +160,21 @@ public function testHelp() {
$this->assertPattern("/{$expected}/", $result);

$command = new MockCommand(array('request' => $this->request));
$command->help();
$return = $command->__invoke('_help');

$expected = "COMMANDS\n(\s{4}[a-z0-9\_]+\n)";
$result = $command->response->output;
$this->assertPattern("/{$expected}/m", $result);
}

public function testAdvancedHelp() {
$this->request->params['command'] = 'mock_command_help';
$command = new MockCommandHelp(array('request' => $this->request));
$return = $command->__invoke('_help');

var_dump($command->response->output);
}

public function testIn() {
$command = new MockCommand(array('request' => $this->request));
fwrite($command->request->input, 'nada mucho');
Expand Down
72 changes: 72 additions & 0 deletions libraries/lithium/tests/mocks/console/command/MockCommandHelp.php
@@ -0,0 +1,72 @@
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2009, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace lithium\tests\mocks\console\command;

/**
* This is a mock class for testing help
*
*/
class MockCommandHelp extends \lithium\console\Command {

/**
* This is a long param.
*
* @var string
*/
public $long = null;

/**
* This is a short param.
*
* @var boolean
*/
public $s = true;

/**
* Don't show this.
*
* @var array
*/
protected $_classes = array(
'response' => '\lithium\tests\mocks\console\MockResponse'
);

/**
* This is the run command so don't show it.
*
* @return boolean
*/
public function run() {
return true;
}

/**
* This is a task with required args.
*
* @param string $arg1
* @param string $arg2
* @return boolean
*/
public function taskWithRequiredArgs($arg1, $arg2) {
return true;
}

/**
* This is a task with optional args.
*
* @param string $arg1
* @param string $arg2
* @return boolean
*/
public function taskWithOptionalArgs($arg1 = null, $arg2 = nill) {
return true;
}
}

?>

0 comments on commit 2764bb0

Please sign in to comment.