Skip to content

Commit

Permalink
Update Shell to use repository trait.
Browse files Browse the repository at this point in the history
Replace some duplicate code with the RepositoryAwareTrait. This
lets Shells and Controllers share the same behavior around handling
repositories which is nice.
  • Loading branch information
markstory committed Dec 6, 2013
1 parent 8a8ac0f commit 5a58c8c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 98 deletions.
88 changes: 11 additions & 77 deletions Cake/Console/Shell.php
Expand Up @@ -27,6 +27,7 @@
use Cake\Utility\File;
use Cake\Utility\Inflector;
use Cake\Utility\MergeVariablesTrait;
use Cake\Utility\RepositoryAwareTrait;
use Cake\Utility\String;

/**
Expand All @@ -36,6 +37,8 @@
class Shell extends Object {

use MergeVariablesTrait;
use RepositoryAwareTrait;

/**
* Output constant making verbose shells.
*/
Expand Down Expand Up @@ -116,21 +119,6 @@ class Shell extends Object {
*/
public $taskNames = [];

/**
* Contains models to load and instantiate
*
* @var array
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::$uses
*/
public $uses = [];

/**
* This shell's primary model class name, the first model in the $uses property
*
* @var string
*/
public $modelClass = null;

/**
* Task Collection for the command, used to create Tasks.
*
Expand Down Expand Up @@ -179,6 +167,7 @@ public function __construct($stdout = null, $stderr = null, $stdin = null) {
list(, $class) = namespaceSplit(get_class($this));
$this->name = str_replace(['Shell', 'Task'], '', $class);
}
$this->_setModelClass($this->name);
$this->Tasks = new TaskRegistry($this);

$this->stdout = $stdout ? $stdout : new ConsoleOutput('php://stdout');
Expand All @@ -187,7 +176,7 @@ public function __construct($stdout = null, $stderr = null, $stdin = null) {

$this->_useLogger();
$this->_mergeVars(
['tasks', 'uses'],
['tasks'],
['associative' => ['tasks']]
);
}
Expand All @@ -201,7 +190,6 @@ public function __construct($stdout = null, $stderr = null, $stdin = null) {
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::initialize
*/
public function initialize() {
$this->_loadModels();
$this->loadTasks();
}

Expand Down Expand Up @@ -234,75 +222,21 @@ protected function _welcome() {
}

/**
* If $uses is an array load each of the models in the array
*
* @return boolean
*/
protected function _loadModels() {
if (is_array($this->uses)) {
list(, $this->modelClass) = pluginSplit(current($this->uses));
foreach ($this->uses as $modelClass) {
$this->loadModel($modelClass);
}
}
return true;
}

/**
* Lazy loads models using the loadModel() method if declared in $uses
* Lazy loads models using the repository() method if it matches modelClass
*
* @param string $name
* @return void
*/
public function __isset($name) {
if (is_array($this->uses)) {
foreach ($this->uses as $modelClass) {
list(, $class) = pluginSplit($modelClass);
if ($name === $class) {
return $this->loadModel($modelClass);
}
list(, $class) = namespaceSplit($modelClass);
if ($name === $class) {
return $this->loadModel($class);
}
if ($name === $this->modelClass) {
list($plugin, $class) = pluginSplit($name, true);
if (!$plugin) {
$plugin = $this->plugin ? $this->plugin . '.' : null;
}
return $this->repository($plugin . $this->modelClass);
}
}

/**
* Loads and instantiates models required by this shell.
*
* @param string $modelClass Name of model class to load
* @param mixed $id Initial ID the instanced model class should have
* @return mixed true when single model found and instance created, error returned if model not found.
* @throws Cake\Error\MissingModelException if the model class cannot be found.
*/
public function loadModel($modelClass = null, $id = null) {
if ($modelClass === null) {
$modelClass = $this->modelClass;
}

$this->uses = ($this->uses) ? (array)$this->uses : [];
if (!in_array($modelClass, $this->uses)) {
$this->uses[] = $modelClass;
}

list($plugin, $modelClass) = pluginSplit($modelClass, true);
if (!isset($this->modelClass)) {
$this->modelClass = $modelClass;
}

$this->{$modelClass} = ClassRegistry::init([
'class' => $plugin . $modelClass,
'alias' => $modelClass,
'id' => $id
]);
if (!$this->{$modelClass}) {
throw new Error\MissingModelException($modelClass);
}
return true;
}

/**
* Loads tasks defined in public $tasks
*
Expand Down
42 changes: 21 additions & 21 deletions Cake/Test/TestCase/Console/ShellTest.php
Expand Up @@ -34,7 +34,7 @@ class MergeShell extends Shell {

public $tasks = array('DbConfig', 'Fixture');

public $uses = array('Comment');
public $modelClass = 'Articles';

}

Expand Down Expand Up @@ -170,39 +170,39 @@ public function testInitialize() {

Plugin::load('TestPlugin');
$this->Shell->tasks = array('DbConfig' => array('one', 'two'));
$this->Shell->uses = array('TestPlugin.TestPluginPost');
$this->Shell->plugin = 'TestPlugin';
$this->Shell->modelClass = 'TestPluginComments';
$this->Shell->initialize();

$this->assertTrue(isset($this->Shell->TestPluginPost));
$this->assertInstanceOf('TestPlugin\Model\TestPluginPost', $this->Shell->TestPluginPost);
$this->assertEquals('TestPluginPost', $this->Shell->modelClass);
Plugin::unload('TestPlugin');

$this->Shell->uses = array('TestApp\Model\Comment');
$this->Shell->initialize();
$this->assertTrue(isset($this->Shell->Comment));
$this->assertInstanceOf('TestApp\Model\Comment', $this->Shell->Comment);
$this->assertEquals('TestApp\Model\Comment', $this->Shell->modelClass);
$this->assertTrue(isset($this->Shell->TestPluginComments));
$this->assertInstanceOf(
'TestPlugin\Model\Repository\TestPluginCommentsTable',
$this->Shell->TestPluginComments
);
}

/**
* testLoadModel method
* test repository method
*
* @return void
*/
public function testLoadModel() {
public function testRepository() {
Configure::write('App.namespace', 'TestApp');

$Shell = new MergeShell();
$this->assertEquals('Comment', $Shell->Comment->alias);
$this->assertInstanceOf('TestApp\Model\Comment', $Shell->Comment);
$this->assertEquals('Comment', $Shell->modelClass);
$this->assertInstanceOf(
'TestApp\Model\Repository\ArticlesTable',
$Shell->Articles
);
$this->assertEquals('Articles', $Shell->modelClass);

Plugin::load('TestPlugin');
$this->Shell->loadModel('TestPlugin.TestPluginPost');
$this->assertTrue(isset($this->Shell->TestPluginPost));
$this->assertInstanceOf('TestPlugin\Model\TestPluginPost', $this->Shell->TestPluginPost);
$this->assertEquals('TestPluginPost', $this->Shell->modelClass);
$this->Shell->repository('TestPlugin.TestPluginComments');
$this->assertTrue(isset($this->Shell->TestPluginComments));
$this->assertInstanceOf(
'TestPlugin\Model\Repository\TestPluginCommentsTable',
$this->Shell->TestPluginComments
);
}

/**
Expand Down

0 comments on commit 5a58c8c

Please sign in to comment.