From d142c8d17e840c098cafca55fdfcfb7c8bf9ea86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Tue, 26 Sep 2017 15:33:09 +0200 Subject: [PATCH] Revert TableLocator singleton. --- src/Datasource/FactoryLocator.php | 4 +-- src/ORM/Locator/LocatorAwareTrait.php | 4 ++- src/ORM/Locator/TableLocator.php | 32 ------------------- src/ORM/TableRegistry.php | 17 +++++++--- src/Template/Element/auto_table_warning.ctp | 4 +-- .../TestCase/ORM/Locator/TableLocatorTest.php | 28 ---------------- 6 files changed, 19 insertions(+), 70 deletions(-) diff --git a/src/Datasource/FactoryLocator.php b/src/Datasource/FactoryLocator.php index 7c47878736f..3b4f7e9460d 100644 --- a/src/Datasource/FactoryLocator.php +++ b/src/Datasource/FactoryLocator.php @@ -14,7 +14,7 @@ */ namespace Cake\Datasource; -use Cake\ORM\Locator\TableLocator; +use Cake\ORM\TableRegistry; use InvalidArgumentException; class FactoryLocator @@ -59,7 +59,7 @@ public static function drop($type) public static function get($type) { if (!isset(static::$_modelFactories['Table'])) { - static::$_modelFactories['Table'] = [TableLocator::getInstance(), 'get']; + static::$_modelFactories['Table'] = [TableRegistry::getTableLocator(), 'get']; } if (!isset(static::$_modelFactories[$type])) { diff --git a/src/ORM/Locator/LocatorAwareTrait.php b/src/ORM/Locator/LocatorAwareTrait.php index da75990c1e0..b857518cc47 100644 --- a/src/ORM/Locator/LocatorAwareTrait.php +++ b/src/ORM/Locator/LocatorAwareTrait.php @@ -14,6 +14,8 @@ */ namespace Cake\ORM\Locator; +use Cake\ORM\TableRegistry; + /** * Contains method for setting and accessing LocatorInterface instance */ @@ -65,7 +67,7 @@ public function setTableLocator(LocatorInterface $tableLocator) public function getTableLocator() { if (!$this->_tableLocator) { - $this->_tableLocator = TableLocator::getInstance(); + $this->_tableLocator = TableRegistry::getTableLocator(); } return $this->_tableLocator; diff --git a/src/ORM/Locator/TableLocator.php b/src/ORM/Locator/TableLocator.php index a6574542472..36ab5cdaaa7 100644 --- a/src/ORM/Locator/TableLocator.php +++ b/src/ORM/Locator/TableLocator.php @@ -55,13 +55,6 @@ class TableLocator implements LocatorInterface */ protected $_options = []; - /** - * LocatorInterface implementation instance. - * - * @var \Cake\ORM\Locator\LocatorInterface - */ - protected static $_instance; - /** * Stores a list of options to be used when instantiating an object * with a matching alias. @@ -306,29 +299,4 @@ public function remove($alias) $this->_fallbacked[$alias] ); } - - /** - * Returns a singleton instance of LocatorInterface implementation. - * - * @return \Cake\ORM\Locator\LocatorInterface - */ - public static function getInstance() - { - if (!static::$_instance) { - static::$_instance = new static(); - } - - return static::$_instance; - } - - /** - * Sets singleton instance of LocatorInterface implementation. - * - * @param \Cake\ORM\Locator\LocatorInterface|null $tableLocator Instance of a locator to use. - * @return void - */ - public static function setInstance(LocatorInterface $tableLocator) - { - static::$_instance = $tableLocator; - } } diff --git a/src/ORM/TableRegistry.php b/src/ORM/TableRegistry.php index 66352751d08..bba3ad9e7b8 100644 --- a/src/ORM/TableRegistry.php +++ b/src/ORM/TableRegistry.php @@ -15,7 +15,6 @@ namespace Cake\ORM; use Cake\ORM\Locator\LocatorInterface; -use Cake\ORM\Locator\TableLocator; /** * Provides a registry/factory for Table objects. @@ -47,8 +46,6 @@ * ``` * $table = TableRegistry::get('Users', $config); * ``` - * - * @deprecated 3.6.0 Use \Cake\ORM\Locator\TableLocator instead. */ class TableRegistry { @@ -90,7 +87,11 @@ public static function locator(LocatorInterface $locator = null) */ public static function getTableLocator() { - return TableLocator::getInstance(); + if (!static::$_locator) { + static::$_locator = new static::$_defaultLocatorClass(); + } + + return static::$_locator; } /** @@ -101,7 +102,7 @@ public static function getTableLocator() */ public static function setTableLocator(LocatorInterface $tableLocator) { - TableLocator::setInstance($tableLocator); + static::$_locator = $tableLocator; } /** @@ -111,6 +112,7 @@ public static function setTableLocator(LocatorInterface $tableLocator) * @param string|null $alias Name of the alias * @param array|null $options list of options for the alias * @return array The config data. + * @deprecated 3.6.0 Use \Cake\ORM\Locator\TableLocator::config() instead. */ public static function config($alias = null, $options = null) { @@ -125,6 +127,7 @@ public static function config($alias = null, $options = null) * @param string $alias The alias name you want to get. * @param array $options The options you want to build the table with. * @return \Cake\ORM\Table + * @deprecated 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead. */ public static function get($alias, array $options = []) { @@ -136,6 +139,7 @@ public static function get($alias, array $options = []) * * @param string $alias The alias to check for. * @return bool + * @deprecated 3.6.0 Use \Cake\ORM\Locator\TableLocator::exists() instead. */ public static function exists($alias) { @@ -148,6 +152,7 @@ public static function exists($alias) * @param string $alias The alias to set. * @param \Cake\ORM\Table $object The table to set. * @return \Cake\ORM\Table + * @deprecated 3.6.0 Use \Cake\ORM\Locator\TableLocator::set() instead. */ public static function set($alias, Table $object) { @@ -159,6 +164,7 @@ public static function set($alias, Table $object) * * @param string $alias The alias to remove. * @return void + * @deprecated 3.6.0 Use \Cake\ORM\Locator\TableLocator::remove() instead. */ public static function remove($alias) { @@ -169,6 +175,7 @@ public static function remove($alias) * Clears the registry of configuration and instances. * * @return void + * @deprecated 3.6.0 Use \Cake\ORM\Locator\TableLocator::clear() instead. */ public static function clear() { diff --git a/src/Template/Element/auto_table_warning.ctp b/src/Template/Element/auto_table_warning.ctp index 755d390e983..16628f16cc0 100644 --- a/src/Template/Element/auto_table_warning.ctp +++ b/src/Template/Element/auto_table_warning.ctp @@ -12,8 +12,8 @@ * @since 3.0.0 * @license https://opensource.org/licenses/mit-license.php MIT License */ -use Cake\ORM\Locator\TableLocator; -$autoTables = TableLocator::getInstance()->genericInstances(); +use Cake\ORM\TableRegistry; +$autoTables = TableRegistry::getTableLocator()->genericInstances(); if (!$autoTables) { return; } diff --git a/tests/TestCase/ORM/Locator/TableLocatorTest.php b/tests/TestCase/ORM/Locator/TableLocatorTest.php index 874ffc95d57..f8f6bdef831 100644 --- a/tests/TestCase/ORM/Locator/TableLocatorTest.php +++ b/tests/TestCase/ORM/Locator/TableLocatorTest.php @@ -16,7 +16,6 @@ use Cake\Core\Plugin; use Cake\Datasource\ConnectionManager; -use Cake\ORM\Locator\LocatorInterface; use Cake\ORM\Locator\TableLocator; use Cake\ORM\Table; use Cake\TestSuite\TestCase; @@ -574,31 +573,4 @@ public function testRemovePlugin() $this->assertSame($plugin, $plugin3, 'Should be the same TestPluginTwo.Comments object'); } - - /** - * Test testSetInstance() method. - * - * @return void - */ - public function testSetInstance() - { - $old = TableLocator::getInstance(); - - $locator = $this->createMock(LocatorInterface::class); - TableLocator::setInstance($locator); - - $this->assertSame($locator, TableLocator::getInstance()); - - TableLocator::setInstance($old); - } - - /** - * Test testSetInstance() method. - * - * @return void - */ - public function testGetInstance() - { - $this->assertInstanceOf(LocatorInterface::class, TableLocator::getInstance()); - } }