Browse Source

Move LocatorInterface accessor/mutator to LocatorAwareTrait and use it in Association.

Robert 11 years ago
parent
commit
f617e9675c

+ 4 - 29
src/ORM/Association.php

@@ -18,7 +18,7 @@ use Cake\Core\ConventionsTrait;
 use Cake\Database\Expression\IdentifierExpression;
 use Cake\Datasource\EntityInterface;
 use Cake\Datasource\ResultSetDecorator;
-use Cake\ORM\Locator\LocatorInterface;
+use Cake\ORM\Locator\LocatorAwareTrait;
 use Cake\ORM\Query;
 use Cake\ORM\Table;
 use Cake\ORM\TableRegistry;
@@ -35,6 +35,7 @@ abstract class Association
 {
 
     use ConventionsTrait;
+    use LocatorAwareTrait;
 
     /**
      * Strategy name to use joins for fetching associated records
@@ -146,13 +147,6 @@ abstract class Association
     protected $_targetTable;
 
     /**
-     * Table locator instance
-     *
-     * @var \Cake\ORM\Locator\LocatorInterface
-     */
-    protected $_locator;
-
-    /**
      * The type of join to be used when adding the association to a query
      *
      * @var string
@@ -224,10 +218,6 @@ abstract class Association
         list(, $name) = pluginSplit($alias);
         $this->_name = $name;
 
-        if (!$this->_locator) {
-            $this->_locator =& TableRegistry::locator();
-        }
-
         $this->_options($options);
 
         if (!empty($options['strategy'])) {
@@ -305,10 +295,10 @@ abstract class Association
         }
 
         $config = [];
-        if (!$this->_locator->exists($registryAlias)) {
+        if (!$this->locator()->exists($registryAlias)) {
             $config = ['className' => $this->_className];
         }
-        $this->_targetTable = $this->_locator->get($registryAlias, $config);
+        $this->_targetTable = $this->locator()->get($registryAlias, $config);
 
         return $this->_targetTable;
     }
@@ -450,21 +440,6 @@ abstract class Association
     }
 
     /**
-     * Sets the table locator for this association.
-     * If no parameters are passed, it will return the currently used locator.
-     *
-     * @param LocatorInterface|null $locator LocatorInterface instance.
-     * @return LocatorInterface
-     */
-    public function locator(LocatorInterface $locator = null)
-    {
-        if ($locator !== null) {
-            $this->_locator = $locator;
-        }
-        return $this->_locator;
-    }
-
-    /**
      * Override this function to initialize any concrete association class, it will
      * get passed the original list of options used in the constructor
      *

+ 3 - 3
src/ORM/Association/BelongsToMany.php

@@ -176,15 +176,15 @@ class BelongsToMany extends Association
                 $tableAlias = Inflector::camelize($tableName);
 
                 $config = [];
-                if (!$this->_locator->exists($tableAlias)) {
+                if (!$this->locator()->exists($tableAlias)) {
                     $config = ['table' => $tableName];
                 }
-                $table = $this->_locator->get($tableAlias, $config);
+                $table = $this->locator()->get($tableAlias, $config);
             }
         }
 
         if (is_string($table)) {
-            $table = $this->_locator->get($table);
+            $table = $this->locator()->get($table);
         }
         $junctionAlias = $table->alias();
 

+ 50 - 0
src/ORM/Locator/LocatorAwareTrait.php

@@ -0,0 +1,50 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.1.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\ORM\Locator;
+
+use Cake\ORM\Locator\LocatorInterface;
+use Cake\ORM\TableRegistry;
+
+/**
+ * Contains method for setting and accessing LocatorInterface instance
+ */
+trait LocatorAwareTrait
+{
+
+    /**
+     * Table locator instance
+     *
+     * @var \Cake\ORM\Locator\LocatorInterface
+     */
+    protected $_locator;
+
+    /**
+     * Sets the table locator.
+     * If no parameters are passed, it will return the currently used locator.
+     *
+     * @param LocatorInterface|null $locator LocatorInterface instance.
+     * @return LocatorInterface
+     */
+    public function locator(LocatorInterface $locator = null)
+    {
+        if ($locator !== null) {
+            $this->_locator = $locator;
+        }
+        if (!$this->_locator) {
+            $this->_locator = TableRegistry::locator();
+        }
+        return $this->_locator;
+    }
+}

+ 0 - 15
tests/TestCase/ORM/AssociationTest.php

@@ -328,21 +328,6 @@ class AssociationTest extends TestCase
     }
 
     /**
-     * Tests locator method
-     *
-     * @return void
-     */
-    public function testLocator()
-    {
-        $locator = $this->association->locator();
-        $this->assertSame(TableRegistry::locator(), $locator);
-
-        $newLocator = $this->getMock('Cake\ORM\Locator\LocatorInterface');
-        $associationLocator = $this->association->locator($newLocator);
-        $this->assertSame($newLocator, $associationLocator);
-    }
-    
-    /**
      * Tests that `locator` is a valid option for the association constructor
      *
      * @return void

+ 53 - 0
tests/TestCase/ORM/Locator/LocatorAwareTraitTest.php

@@ -0,0 +1,53 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.1.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+
+namespace Cake\Test\TestCase\ORM\Locator;
+
+use Cake\ORM\TableRegistry;
+use Cake\TestSuite\TestCase;
+
+/**
+ * LocatorAwareTrait test case
+ *
+ */
+class LocatorAwareTraitTest extends TestCase
+{
+
+    /**
+     * setup
+     *
+     * @return void
+     */
+    public function setUp()
+    {
+        parent::setUp();
+
+        $this->subject = $this->getObjectForTrait('Cake\ORM\Locator\LocatorAwareTrait');
+    }
+
+    /**
+     * Tests locator method
+     *
+     * @return void
+     */
+    public function testLocator()
+    {
+        $locator = $this->subject->locator();
+        $this->assertSame(TableRegistry::locator(), $locator);
+
+        $newLocator = $this->getMock('Cake\ORM\Locator\LocatorInterface');
+        $subjectLocator = $this->subject->locator($newLocator);
+        $this->assertSame($newLocator, $subjectLocator);
+    }
+}