Skip to content

Commit

Permalink
Removing run method from console\Command to make subclassing more…
Browse files Browse the repository at this point in the history
… flexible. Now a subclass run method can accept params.

Also moved the environment message to Help.
  • Loading branch information
gwoo committed Nov 10, 2011
1 parent 423c6aa commit 67d7554
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
50 changes: 17 additions & 33 deletions console/Command.php
Expand Up @@ -9,11 +9,18 @@
namespace lithium\console;

use Exception;
use lithium\core\Environment;
use lithium\console\command\Help;

/**
* The base class to inherit when writing console scripts in Lithium.
* All Commands to be run from the Lithium console must extend this class.
* The `run` method is automatically called if it exists. Otherwise, if a method does not exist
* the `Help` command will be run.
*
* {{{
* $ li3 example
* $ li3 example --format=json
* }}}
*
*/
class Command extends \lithium\core\Object {

Expand Down Expand Up @@ -103,11 +110,6 @@ protected function _init() {
*/
public function __invoke($action, $args = array(), $options = array()) {
try {
if (!$this->request->env) {
$message = 'Lithium console started in the ' . Environment::get() .' environment.';
$message .= ' Use the --env=environment key to alter this.';
$this->out($message);
}
$this->response->status = 1;
$result = $this->invokeMethod($action, $args);

Expand All @@ -123,24 +125,20 @@ public function __invoke($action, $args = array(), $options = array()) {
}

/**
* This is the main method any subclass is recommended to implement. This
* method is called when the command is invoked without any arguments as
* shown in the examples below. When not overridden help for the
* subclassing command is generated by default.
*
* {{{
* $ li3 example
* $ li3 example --format=json
* }}}
*
* The invoked Help command will take over request and response objects of
* the originally invoked command. Thus the response of the Help command
* becomes the response of the original one.
*
* @return boolean
*/
public function run() {
return $this->_help();
protected function _help() {
$help = new Help(array(
'request' => $this->request,
'response' => $this->response,
'classes' => $this->_classes
));
return $help->run(get_class($this));
}

/**
Expand Down Expand Up @@ -261,11 +259,11 @@ public function header($text, $line = 80) {
* formatting. Passing something like `$this->columns($output, array('style' => 'red)`
* would print the table in red.
*
* @see \lithium\console\Response::styles()
* @param array $rows The rows to print, with each column as an array element.
* @param array $options Optional params:
* - separator : Different column separator, defaults to `\t`
* - style : the style name to wrap around the columns output
* @see \lithium\console\Response::styles()
* @return void
*/
public function columns($rows, $options = array()) {
Expand Down Expand Up @@ -334,20 +332,6 @@ public function stop($status = 0, $message = null) {
exit($status);
}

/**
* Invokes `Help` command.
*
* @return boolean
*/
protected function _help() {
$help = new Help(array(
'request' => $this->request,
'response' => $this->response,
'classes' => $this->_classes
));
return $help->run(get_class($this));
}

/**
* Handles the response that is sent to the stream.
*
Expand Down
5 changes: 5 additions & 0 deletions console/command/Help.php
Expand Up @@ -9,6 +9,7 @@
namespace lithium\console\command;

use lithium\core\Libraries;
use lithium\core\Environment;
use lithium\util\Inflector;
use lithium\analysis\Inspector;
use lithium\analysis\Docblock;
Expand All @@ -26,6 +27,10 @@ class Help extends \lithium\console\Command {
* @return void
*/
public function run($command = null) {
$message = 'Lithium console started in the ' . Environment::get() . ' environment.';
$message .= ' Use the --env=environment key to alter this.';
$this->out($message);

if (!$command) {
$this->_renderCommands();
return true;
Expand Down

2 comments on commit 67d7554

@daschl
Copy link
Contributor

@daschl daschl commented on 67d7554 Nov 10, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't try it out, but does this mean that the --env gets passed correctly downstream now?

@L-P
Copy link
Contributor

@L-P L-P commented on 67d7554 Nov 10, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried and it did not fix #188.

Please sign in to comment.