Skip to content

Commit

Permalink
Add the ability to set the default model type
Browse files Browse the repository at this point in the history
  • Loading branch information
Marlinc committed Oct 28, 2015
1 parent 9f90a04 commit 2fd7ac5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
47 changes: 40 additions & 7 deletions src/Datasource/ModelAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Cake\Datasource\Exception\MissingModelException;
use InvalidArgumentException;
use UnexpectedValueException;

/**
* Provides functionality for loading table classes
Expand Down Expand Up @@ -46,6 +47,13 @@ trait ModelAwareTrait
*/
protected $_modelFactories = [];

/**
* The model type to use.
*
* @var string
*/
protected $_modelType = 'Table';

/**
* Set the modelClass and modelKey properties based on conventions.
*
Expand All @@ -71,34 +79,41 @@ protected function _setModelClass($name)
* be thrown.
*
* @param string|null $modelClass Name of model class to load. Defaults to $this->modelClass
* @param string $type The type of repository to load. Defaults to 'Table' which
* delegates to Cake\ORM\TableRegistry.
* @param string|null $modelType The type of repository to load. Defaults to the modelType() value.
* @return object The model instance created.
* @throws \Cake\Datasource\Exception\MissingModelException If the model class cannot be found.
* @throws \InvalidArgumentException When using a type that has not been registered.
* @throws \UnexpectedValueException If no model type has been defined
*/
public function loadModel($modelClass = null, $type = 'Table')
public function loadModel($modelClass = null, $modelType = null)
{
if ($modelClass === null) {
$modelClass = $this->modelClass;
}
if ($modelType === null) {
$modelType = $this->modelType();

if ($modelType === null) {
throw new UnexpectedValueException('No model type has been defined');
}
}

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

if (isset($this->{$alias})) {
return $this->{$alias};
}

if (!isset($this->_modelFactories[$type])) {
if (!isset($this->_modelFactories[$modelType])) {
throw new InvalidArgumentException(sprintf(
'Unknown repository type "%s". Make sure you register a type before trying to use it.',
$type
$modelType
));
}
$factory = $this->_modelFactories[$type];
$factory = $this->_modelFactories[$modelType];
$this->{$alias} = $factory($modelClass);
if (!$this->{$alias}) {
throw new MissingModelException([$modelClass, $type]);
throw new MissingModelException([$modelClass, $modelType]);
}
return $this->{$alias};
}
Expand All @@ -114,4 +129,22 @@ public function modelFactory($type, callable $factory)
{
$this->_modelFactories[$type] = $factory;
}

/**
* Set or get the model type to be used by this class
*
* @param string|null $modelType The model type or null to retrieve the current
*
* @return string|$this
*/
public function modelType($modelType = null)
{
if ($modelType === null) {
return $this->_modelType;
}

$this->_modelType = $modelType;

return $this;
}
}
25 changes: 25 additions & 0 deletions tests/TestCase/Datasource/ModelAwareTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function testLoadModel()
$stub = new Stub();
$stub->setProps('Articles');
$stub->modelFactory('Table', ['\Cake\ORM\TableRegistry', 'get']);
$stub->modelType('Table');

$result = $stub->loadModel();
$this->assertInstanceOf('Cake\ORM\Table', $result);
Expand All @@ -83,6 +84,7 @@ public function testLoadModelPlugin()
$stub = new Stub();
$stub->setProps('Articles');
$stub->modelFactory('Table', ['\Cake\ORM\TableRegistry', 'get']);
$stub->modelType('Table');

$result = $stub->loadModel('TestPlugin.Comments');
$this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $result);
Expand Down Expand Up @@ -115,6 +117,29 @@ public function testModelFactory()
$this->assertEquals('Magic', $stub->Magic->name);
}

/**
* test alternate default model type.
*
* @return void
*/
public function testModelType()
{
$stub = new Stub();
$stub->setProps('Articles');

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

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

/**
* test MissingModelException being thrown
*
Expand Down

0 comments on commit 2fd7ac5

Please sign in to comment.