Skip to content

Commit

Permalink
Rename Repository to Model.
Browse files Browse the repository at this point in the history
Update the RepositoryAwareTrait and repository related methods to use
'model' instead. Repositories are an implementation of the model in an
application. Our users will want access to their models. Repositories
are an implementation detail in providing a conceptual model.
  • Loading branch information
markstory committed Jan 16, 2014
1 parent 6321ee2 commit 5920074
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
10 changes: 5 additions & 5 deletions src/Console/Shell.php
Expand Up @@ -27,7 +27,7 @@
use Cake\Utility\File;
use Cake\Utility\Inflector;
use Cake\Utility\MergeVariablesTrait;
use Cake\Utility\RepositoryAwareTrait;
use Cake\Utility\ModelAwareTrait;
use Cake\Utility\String;

/**
Expand All @@ -37,7 +37,7 @@
class Shell extends Object {

use MergeVariablesTrait;
use RepositoryAwareTrait;
use ModelAwareTrait;

/**
* Output constant making verbose shells.
Expand Down Expand Up @@ -168,7 +168,7 @@ public function __construct($stdout = null, $stderr = null, $stdin = null) {
$this->name = str_replace(['Shell', 'Task'], '', $class);
}
$this->_setModelClass($this->name);
$this->repositoryFactory('Table', ['Cake\ORM\TableRegistry', 'get']);
$this->modelFactory('Table', ['Cake\ORM\TableRegistry', 'get']);
$this->Tasks = new TaskRegistry($this);

$this->stdout = $stdout ? $stdout : new ConsoleOutput('php://stdout');
Expand Down Expand Up @@ -223,7 +223,7 @@ protected function _welcome() {
}

/**
* Lazy loads models using the repository() method if it matches modelClass
* Lazy loads models using the loadModel() method if it matches modelClass
*
* @param string $name
* @return void
Expand All @@ -234,7 +234,7 @@ public function __isset($name) {
if (!$plugin) {
$plugin = $this->plugin ? $this->plugin . '.' : null;
}
return $this->repository($plugin . $this->modelClass);
return $this->loadModel($plugin . $this->modelClass);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Controller/Controller.php
Expand Up @@ -29,7 +29,7 @@
use Cake\Routing\Router;
use Cake\Utility\Inflector;
use Cake\Utility\MergeVariablesTrait;
use Cake\Utility\RepositoryAwareTrait;
use Cake\Utility\ModelAwareTrait;
use Cake\Utility\ViewVarsTrait;
use Cake\View\View;

Expand Down Expand Up @@ -79,7 +79,7 @@
class Controller extends Object implements EventListener {

use MergeVariablesTrait;
use RepositoryAwareTrait;
use ModelAwareTrait;
use RequestActionTrait;
use ViewVarsTrait;

Expand Down Expand Up @@ -313,7 +313,7 @@ public function __construct($request = null, $response = null) {
}

$this->_setModelClass($this->name);
$this->repositoryFactory('Table', ['Cake\ORM\TableRegistry', 'get']);
$this->modelFactory('Table', ['Cake\ORM\TableRegistry', 'get']);

$childMethods = get_class_methods($this);
$parentMethods = get_class_methods('Cake\Controller\Controller');
Expand Down Expand Up @@ -341,7 +341,7 @@ public function __get($name) {
if (!$plugin) {
$plugin = $this->plugin ? $this->plugin . '.' : null;
}
$this->repository($plugin . $this->modelClass);
$this->loadModel($plugin . $this->modelClass);
return $this->{$this->modelClass};
}
return false;
Expand Down
Expand Up @@ -21,10 +21,10 @@
* Provides functionality for loading table classes
* and other repositories onto properties of the host object.
*
* Example users of this trait are Cake\Controller\Controller and
* Example users of this trait are Cake\Controller\Controller and
* Cake\Console\Shell.
*/
trait RepositoryAwareTrait {
trait ModelAwareTrait {

/**
* This object's primary model class name, the Inflector::pluralized()'ed version of
Expand All @@ -37,11 +37,11 @@ trait RepositoryAwareTrait {
public $modelClass;

/**
* A list of repository factory functions.
* A list of model factory functions.
*
* @var array
*/
protected $_repositoryFactories = [];
protected $_modelFactories = [];

/**
* Set the modelClass and modelKey properties based on conventions.
Expand Down Expand Up @@ -73,7 +73,7 @@ protected function _setModelClass($name) {
* @throws Cake\Error\MissingModelException if the model class cannot be found.
* @throws Cake\Error\Exception When using a type that has not been registered.
*/
public function repository($modelClass = null, $type = 'Table') {
public function loadModel($modelClass = null, $type = 'Table') {
if ($modelClass === null) {
$modelClass = $this->modelClass;
}
Expand All @@ -84,13 +84,13 @@ public function repository($modelClass = null, $type = 'Table') {

list($plugin, $modelClass) = pluginSplit($modelClass, true);

if (!isset($this->_repositoryFactories[$type])) {
if (!isset($this->_modelFactories[$type])) {
throw new Error\Exception(sprintf(
'Unknown repository type "%s". Make sure you register a type before trying to use it.',
$type
));
}
$factory = $this->_repositoryFactories[$type];
$factory = $this->_modelFactories[$type];
$this->{$modelClass} = $factory($plugin . $modelClass);
if (!$this->{$modelClass}) {
throw new Error\MissingModelException($modelClass);
Expand All @@ -105,8 +105,8 @@ public function repository($modelClass = null, $type = 'Table') {
* @param callable $factory The factory function used to create instances.
* @return void
*/
public function repositoryFactory($type, callable $factory) {
$this->_repositoryFactories[$type] = $factory;
public function modelFactory($type, callable $factory) {
$this->_modelFactories[$type] = $factory;
}

}
6 changes: 3 additions & 3 deletions tests/TestCase/Console/ShellTest.php
Expand Up @@ -182,11 +182,11 @@ public function testInitialize() {
}

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

$Shell = new MergeShell();
Expand All @@ -197,7 +197,7 @@ public function testRepository() {
$this->assertEquals('Articles', $Shell->modelClass);

Plugin::load('TestPlugin');
$this->Shell->repository('TestPlugin.TestPluginComments');
$this->Shell->loadModel('TestPlugin.TestPluginComments');
$this->assertTrue(isset($this->Shell->TestPluginComments));
$this->assertInstanceOf(
'TestPlugin\Model\Repository\TestPluginCommentsTable',
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -248,19 +248,19 @@ public function testRepositoryAutoload() {
}

/**
* testRepository method
* testLoadModel method
*
* @return void
*/
public function testRepository() {
public function testLoadModel() {
Configure::write('App.namespace', 'TestApp');
$request = new Request('controller_posts/index');
$response = $this->getMock('Cake\Network\Response');
$Controller = new Controller($request, $response);

$this->assertFalse(isset($Controller->Articles));

$result = $Controller->repository('Articles');
$result = $Controller->loadModel('Articles');
$this->assertTrue($result);
$this->assertInstanceOf(
'TestApp\Model\Repository\ArticlesTable',
Expand All @@ -282,7 +282,7 @@ public function testLoadModelInPlugins() {

$this->assertFalse(isset($Controller->TestPluginComments));

$result = $Controller->repository('TestPlugin.TestPluginComments');
$result = $Controller->loadModel('TestPlugin.TestPluginComments');
$this->assertTrue($result);
$this->assertInstanceOf(
'TestPlugin\Model\Repository\TestPluginCommentsTable',
Expand Down
Expand Up @@ -14,14 +14,14 @@
namespace Cake\Test\TestCase\Utility;

use Cake\TestSuite\TestCase;
use Cake\Utility\RepositoryAwareTrait;
use Cake\Utility\ModelAwareTrait;

/**
* Testing stub.
*/
class Stub {

use RepositoryAwareTrait;
use ModelAwareTrait;

public function setProps($name) {
$this->_setModelClass($name);
Expand All @@ -30,9 +30,9 @@ public function setProps($name) {
}

/**
* RepositoryAwareTrait test case
* ModelAwareTrait test case
*/
class RepositoryAwareTraitTest extends TestCase {
class ModelAwareTraitTest extends TestCase {

/**
* Test set modelClass
Expand All @@ -48,38 +48,38 @@ public function testSetModelClass() {
}

/**
* test repository()
* test loadModel()
*
* @return void
*/
public function testRepository() {
public function testLoadModel() {
$stub = new Stub();
$stub->setProps('Articles');
$stub->repositoryFactory('Table', ['\Cake\ORM\TableRegistry', 'get']);
$stub->modelFactory('Table', ['\Cake\ORM\TableRegistry', 'get']);

$this->assertTrue($stub->repository());
$this->assertTrue($stub->loadModel());
$this->assertInstanceOf('Cake\ORM\Table', $stub->Articles);

$this->assertTrue($stub->repository('Comments'));
$this->assertTrue($stub->loadModel('Comments'));
$this->assertInstanceOf('Cake\ORM\Table', $stub->Comments);
}

/**
* test alternate repository factories.
* test alternate model factories.
*
* @return void
*/
public function testRepositoryFactory() {
public function testModelFactory() {
$stub = new Stub();
$stub->setProps('Articles');

$stub->repositoryFactory('Test', function($name) {
$stub->modelFactory('Test', function($name) {
$mock = new \StdClass();
$mock->name = $name;
return $mock;
});

$result = $stub->repository('Magic', 'Test');
$result = $stub->loadModel('Magic', 'Test');
$this->assertTrue($result);
$this->assertInstanceOf('\StdClass', $stub->Magic);
$this->assertEquals('Magic', $stub->Magic->name);
Expand Down

0 comments on commit 5920074

Please sign in to comment.