Skip to content

Commit

Permalink
Make RepositoryAwareTrait not coupled to the ORM.
Browse files Browse the repository at this point in the history
Add a default type (the ORM loader) and allow userland repository types.
The plan is to allow users + plugins to define new repository types that
can be fetched from their own factory methods. Register the
TableRegistry so things 'just work'
  • Loading branch information
markstory committed Dec 7, 2013
1 parent cb52f9d commit 71e7892
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cake/Console/Shell.php
Expand Up @@ -168,6 +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->Tasks = new TaskRegistry($this);

$this->stdout = $stdout ? $stdout : new ConsoleOutput('php://stdout');
Expand Down
1 change: 1 addition & 0 deletions Cake/Controller/Controller.php
Expand Up @@ -322,6 +322,7 @@ public function __construct($request = null, $response = null) {
}

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

$childMethods = get_class_methods($this);
$parentMethods = get_class_methods('Cake\Controller\Controller');
Expand Down
88 changes: 88 additions & 0 deletions Cake/Test/TestCase/Utility/RepositoryAwareTraitTest.php
@@ -0,0 +1,88 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Utility;

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

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

use RepositoryAwareTrait;

public function setProps($name) {
$this->_setModelClass($name);
}

}

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

/**
* Test set modelClass
*
* @return void
*/
public function testSetModelClass() {
$stub = new Stub();
$this->assertNull($stub->modelClass);

$stub->setProps('StubArticles');
$this->assertEquals('StubArticles', $stub->modelClass);
}

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

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

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

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

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

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

}
37 changes: 24 additions & 13 deletions Cake/Utility/RepositoryAwareTrait.php
Expand Up @@ -14,7 +14,7 @@
*/
namespace Cake\Utility;

use Cake\ORM\TableRegistry;
use Cake\Error;
use Cake\Utility\Inflector;

/**
Expand All @@ -37,13 +37,11 @@ trait RepositoryAwareTrait {
public $modelClass;

/**
* This objects's repository key name, an underscored version of the objects's $modelClass property.
* A list of repository factory functions.
*
* Example: For an object named 'ArticleComments', the modelKey would be 'article_comment'
*
* @var string
* @var array
*/
public $modelKey;
protected $_repositoryFactories = [];

/**
* Set the modelClass and modelKey properties based on conventions.
Expand All @@ -55,10 +53,7 @@ trait RepositoryAwareTrait {
*/
protected function _setModelClass($name) {
if (empty($this->modelClass)) {
$this->modelClass = Inflector::singularize($this->name);
}
if (empty($this->modelKey)) {
$this->modelKey = Inflector::underscore($this->modelClass);
$this->modelClass = Inflector::pluralize($name);
}
}
/**
Expand Down Expand Up @@ -87,14 +82,30 @@ public function repository($modelClass = null, $type = 'Table') {

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

if ($type === 'Table') {
$this->{$modelClass} = TableRegistry::get($plugin . $modelClass);
if (!isset($this->_repositoryFactories[$type])) {
throw new Error\Exception(__d(
'cake_dev',
'Unknown repository type "%s". Make sure you register a type before trying to use it.',
$type
));
}
// TODO add other providers
$factory = $this->_repositoryFactories[$type];
$this->{$modelClass} = $factory($plugin . $modelClass);
if (!$this->{$modelClass}) {
throw new Error\MissingModelException($modelClass);
}
return true;
}

/**
* Register a callable to generate repositories of a given type.
*
* @param string $type The name of the repository type the factory function is for.
* @param callable $factory The factory function used to create instances.
* @return void
*/
public function repositoryFactory($type, callable $factory) {
$this->_repositoryFactories[$type] = $factory;
}

}

0 comments on commit 71e7892

Please sign in to comment.