Skip to content

Commit

Permalink
Merge branch '0.4-build' into 0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
gwoo committed Dec 30, 2009
2 parents 83449e3 + 646739c commit 4f83a60
Show file tree
Hide file tree
Showing 23 changed files with 808 additions and 92 deletions.
21 changes: 21 additions & 0 deletions app/webroot/img/empty
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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\cases\console\command\build;

use \lithium\console\command\build\Test;

class TestTest extends \lithium\test\Unit {

public function testRun() {


}

}
?>
8 changes: 6 additions & 2 deletions libraries/lithium/console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ public function __construct($config = array()) {
*/
protected function _init() {
parent::_init();

$this->request = $this->_config['request'];
$this->response = new $this->_classes['response']($this->_config['response']);

if ($this->request) {
foreach ((array) $this->request->params['named'] as $key => $param) {
if (!empty($this->request->params)) {
$params = (array) array_diff_key(
$this->request->params, array('command' => null, 'action' => null, 'args' => null)
);
foreach ($params as $key => $param) {
$this->{$key} = $param;
}
}
Expand Down
4 changes: 2 additions & 2 deletions libraries/lithium/console/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ protected static function _call($callable, $request, $params) {
$request = $params['request'];

if (!method_exists($callable, $request->params['action'])) {
array_unshift($request->params['passed'], $request->params['action']);
array_unshift($request->params['args'], $request->params['action']);
$request->params['action'] = 'run';
}
if (!method_exists($callable, $request->params['action'])) {
$request->params['action'] = 'help';
}
return $callable($request->params['action'], $request->params['passed']);
return $callable($request->params['action'], $request->params['args']);
}
throw new UnexpectedValueException("{$callable} not callable");
});
Expand Down
25 changes: 21 additions & 4 deletions libraries/lithium/console/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ class Request extends \lithium\core\Object {
* @see lithium\console\Router
*/
public $params = array(
'command' => null,
'action' => 'run',
'passed' => array(),
'named' => array()
'command' => null, 'action' => 'run', 'args' => array()
);

/**
Expand All @@ -57,6 +54,11 @@ class Request extends \lithium\core\Object {
*/
protected $_autoConfig = array('env' => 'merge');

/**
* Class Constructor
*
* @param array $config
*/
public function __construct($config = array()) {
$defaults = array('args' => array(), 'input' => null);
$config += $defaults;
Expand Down Expand Up @@ -98,6 +100,21 @@ public function env($key = null) {
return null;
}

/**
* Moves params up a level. Sets command to action, action to passed[0], and so on.
*
* @param integer $num how many times to shift
* @return self
*/
public function shift($num = 1) {
for ($i = $num; $i > 1; $i--) {
$this->shift(--$i);
}
$this->params['command'] = $this->params['action'];
$this->params['action'] = array_shift($this->params['args']);
return $this;
}

/**
* Reads a line from input.
*
Expand Down
24 changes: 9 additions & 15 deletions libraries/lithium/console/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,37 @@ class Router extends \lithium\core\Object {
* Parse incoming request from console
*
* @param object $request \lithium\console\Request
* @return array command, passed, named
* @return array $params
*
**/
public static function parse($request = null) {
$params = array(
'command' => null, 'action' => 'run',
'passed' => array(), 'named' => array()
'command' => null, 'action' => 'run', 'args' => array()
);

if (!empty($request->params)) {
$params = $request->params + $params;
}

if (!empty($request->args)) {
$args = $request->args;
if (!isset($request->params['command'])) {
if (empty($params['command'])) {
$params['command'] = array_shift($args);
}

while ($arg = array_shift($args)) {

if (preg_match('/^-(?P<key>[a-zA-Z0-9]+)$/', $arg, $match)) {
$arg = array_shift($args);
$params['named'][$match['key']] = $arg;
$params[$match['key']] = true;
continue;
}
if (preg_match('/^--(?P<key>[a-z0-9-]+)(?:=(?P<val>.+))?$/', $arg, $match)) {
$params['named'][$match['key']] = !isset($match['val']) ? true : $match['val'];
$params[$match['key']] = !isset($match['val']) ? true : $match['val'];
continue;
}
$params['passed'][] = $arg;
$params['args'][] = $arg;
}
}

if (!empty($params['passed'][0])) {
$params['action'] = $params['passed'][0];
unset($params['passed'][0]);
$params['passed'] = array_values($params['passed']);
if (!empty($params['args'])) {
$params['action'] = array_shift($params['args']);
}
return $params;
}
Expand Down
137 changes: 137 additions & 0 deletions libraries/lithium/console/command/Build.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?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\console\command;

use \lithium\core\Libraries;
use \lithium\util\String;

/**
* The `build` command allows you to rapidly develop your models, views, controllers, and tests
* by generating the minimum code necessary to test and run your application.
*
*/
class Build extends \lithium\console\Command {

/**
* Controls the interactive nature of the command
* When true, the command will ask questions and expect answers to generate the result
* When false, the command will do it's best to determine the result to generate
*
* @var boolean
*/
public $i = false;

/**
* Name of library to use
*
*/
public $library = 'app';

/**
* The template to use to generate the file
*
*/
public $template = null;

/**
* Class Constrcutor
*
* @param string $config
*/
public function __construct($config = array()) {
$this->template = strtolower(join('', array_slice(explode("\\", get_class($this)), -1)));
parent::__construct($config);
}

/**
* Run the build command. Takes `$command` and delegates to `$command::$method`
*
* @param string $command
* @param string $method
* @return void
*/
public function run($command = null, $method = 'run') {
if (!$command) {
return $this->interactive();
}
$class = Libraries::locate('command.build', $command);
$command = new $class(array('request' => $this->request->shift(2)));

if (!method_exists($command, $method)) {
array_unshift($command->request->params['args'], $method);
$method = 'run';
}
return $command->invokeMethod($method, $command->request->params['args']);
}

/**
* Ask questions and use answers to build
*
* @return void
*/
public function interactive() {

}

/**
* Get the namespace
*
* @param string $name
* @return string
*/
protected function _namespace($name) {
$nameToSpace = array(
'model' => 'models', 'view' => 'views', 'controller' => 'controllers',
'command' => 'extensions.command', 'adapter' => 'extensions.adapter',
'helper' => 'extensions.helper'
);
if (isset($nameToSpace[$name])) {
$name = $nameToSpace[$name];
}
return str_replace('.', '\\', $name);
}

/**
* Save a template with the current params. Writes file to `Build::$path`
*
* @param string $template
* @param string $params
* @return boolean
*/
protected function _save($template, $params = array()) {
$file = Libraries::locate('command.build.template', $template, array(
'filter' => false, 'type' => 'file', 'suffix' => '.txt.php',
));
if (!$file || is_array($file)) {
return false;
}

$contents = file_get_contents($file);
$result = String::insert($contents, $params);
$library = Libraries::get($this->library);

if (!empty($library['path'])) {
$path = dirname($library['path']) . str_replace('\\', '/',
"\\{$params['namespace']}\\{$params['class']}"
);
$file = str_replace('//', '/', "{$path}.php");
$directory = dirname($file);

if (!is_dir($directory)) {
if (!mkdir($directory, 0755, true)) {
return false;
}
}
return file_put_contents($file, "<?php\n\n{$result}\n\n?>");
}
return false;
}
}

?>
14 changes: 0 additions & 14 deletions libraries/lithium/console/command/Generate.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace lithium\console\command\generate;
namespace lithium\console\command\build;

class Controller extends \lithium\console\Command {}
class Controller extends \lithium\console\command\Build {}

?>
Loading

0 comments on commit 4f83a60

Please sign in to comment.