Browse Source

Add the ability to set the default model type

Marlin Cremers 10 years ago
parent
commit
2fd7ac5013

+ 40 - 7
src/Datasource/ModelAwareTrait.php

@@ -16,6 +16,7 @@ namespace Cake\Datasource;
 
 use Cake\Datasource\Exception\MissingModelException;
 use InvalidArgumentException;
+use UnexpectedValueException;
 
 /**
  * Provides functionality for loading table classes
@@ -47,6 +48,13 @@ trait ModelAwareTrait
     protected $_modelFactories = [];
 
     /**
+     * The model type to use.
+     *
+     * @var string
+     */
+    protected $_modelType = 'Table';
+
+    /**
      * Set the modelClass and modelKey properties based on conventions.
      *
      * If the properties are already set they will not be overwritten
@@ -71,17 +79,24 @@ trait ModelAwareTrait
      * 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);
 
@@ -89,16 +104,16 @@ trait ModelAwareTrait
             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};
     }
@@ -114,4 +129,22 @@ trait ModelAwareTrait
     {
         $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 - 0
tests/TestCase/Datasource/ModelAwareTraitTest.php

@@ -60,6 +60,7 @@ class ModelAwareTraitTest extends TestCase
         $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);
@@ -83,6 +84,7 @@ class ModelAwareTraitTest extends TestCase
         $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);
@@ -116,6 +118,29 @@ class ModelAwareTraitTest extends TestCase
     }
 
     /**
+     * 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
      *
      * @return void