Skip to content

Commit

Permalink
Revert TableLocator singleton.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Sep 26, 2017
1 parent 8e93d6c commit d142c8d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/Datasource/FactoryLocator.php
Expand Up @@ -14,7 +14,7 @@
*/
namespace Cake\Datasource;

use Cake\ORM\Locator\TableLocator;
use Cake\ORM\TableRegistry;
use InvalidArgumentException;

class FactoryLocator
Expand Down Expand Up @@ -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])) {
Expand Down
4 changes: 3 additions & 1 deletion src/ORM/Locator/LocatorAwareTrait.php
Expand Up @@ -14,6 +14,8 @@
*/
namespace Cake\ORM\Locator;

use Cake\ORM\TableRegistry;

/**
* Contains method for setting and accessing LocatorInterface instance
*/
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 0 additions & 32 deletions src/ORM/Locator/TableLocator.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
17 changes: 12 additions & 5 deletions src/ORM/TableRegistry.php
Expand Up @@ -15,7 +15,6 @@
namespace Cake\ORM;

use Cake\ORM\Locator\LocatorInterface;
use Cake\ORM\Locator\TableLocator;

/**
* Provides a registry/factory for Table objects.
Expand Down Expand Up @@ -47,8 +46,6 @@
* ```
* $table = TableRegistry::get('Users', $config);
* ```
*
* @deprecated 3.6.0 Use \Cake\ORM\Locator\TableLocator instead.
*/
class TableRegistry
{
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -101,7 +102,7 @@ public static function getTableLocator()
*/
public static function setTableLocator(LocatorInterface $tableLocator)
{
TableLocator::setInstance($tableLocator);
static::$_locator = $tableLocator;
}

/**
Expand All @@ -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)
{
Expand All @@ -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 = [])
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Template/Element/auto_table_warning.ctp
Expand Up @@ -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;
}
Expand Down
28 changes: 0 additions & 28 deletions tests/TestCase/ORM/Locator/TableLocatorTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit d142c8d

Please sign in to comment.