Skip to content

Commit

Permalink
Table registries must implement RegistryInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Apr 9, 2015
1 parent 400105d commit b539570
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 62 deletions.
69 changes: 15 additions & 54 deletions src/ORM/Registry.php → src/ORM/Registry/DefaultRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,23 @@
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @since 3.1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

namespace Cake\ORM;
namespace Cake\ORM\Registry;

use Cake\Core\App;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\Registry\RegistryInterface;
use Cake\ORM\Table;
use Cake\Utility\Inflector;
use RuntimeException;

/**
* Provides a registry/factory for Table objects.
*
* This registry allows you to centralize the configuration for tables
* their connections and other meta-data.
*
* ### Configuring instances
*
* You may need to configure your table objects, using TableRegistry you can
* centralize configuration. Any configuration set before instances are created
* will be used when creating instances. If you modify configuration after
* an instance is made, the instances *will not* be updated.
*
* ```
* TableRegistry::config('Users', ['table' => 'my_users']);
* ```
*
* Configuration data is stored *per alias* if you use the same table with
* multiple aliases you will need to set configuration multiple times.
*
* ### Getting instances
*
* You can fetch instances out of the registry using get(). One instance is stored
* per alias. Once an alias is populated the same instance will always be returned.
* This is used to make the ORM use less memory and help make cyclic references easier
* to solve.
*
* ```
* $table = TableRegistry::get('Users', $config);
* ```
*
* Provides a default registry/factory for Table objects.
*/
class Registry
class DefaultRegistry implements RegistryInterface
{

/**
Expand Down Expand Up @@ -114,7 +87,7 @@ public function config($alias = null, $options = null)
}
if (isset($this->_instances[$alias])) {
throw new RuntimeException(sprintf(
'You cannot configure "%s", it has already been constructed.', $alias
'You cannot configure "%s", it has already been constructed.', $alias
));
}
return $this->_config[$alias] = $options;
Expand Down Expand Up @@ -159,7 +132,7 @@ public function get($alias, array $options = [])
if (isset($this->_instances[$alias])) {
if (!empty($options) && $this->_options[$alias] !== $options) {
throw new RuntimeException(sprintf(
'You cannot configure "%s", it already exists in the registry.', $alias
'You cannot configure "%s", it already exists in the registry.', $alias
));
}
return $this->_instances[$alias];
Expand Down Expand Up @@ -197,7 +170,7 @@ public function get($alias, array $options = [])
if ($options['className'] === 'Cake\ORM\Table') {
$this->_fallbacked[$alias] = $this->_instances[$alias];
}

return $this->_instances[$alias];
}

Expand All @@ -213,32 +186,23 @@ protected function _create(array $options)
}

/**
* Check to see if an instance exists in the registry.
*
* @param string $alias The alias to check for.
* @return bool
* @inheritDoc
*/
public function exists($alias)
{
return isset($this->_instances[$alias]);
}

/**
* Set an instance.
*
* @param string $alias The alias to set.
* @param \Cake\ORM\Table $object The table to set.
* @return \Cake\ORM\Table
* @inheritDoc
*/
public function set($alias, Table $object)
{
return $this->_instances[$alias] = $object;
}

/**
* Clears the registry of configuration and instances.
*
* @return void
* @inheritDoc
*/
public function clear()
{
Expand All @@ -261,16 +225,13 @@ public function genericInstances()
}

/**
* Removes an instance from the registry.
*
* @param string $alias The alias to remove.
* @return void
* @inheritDoc
*/
public function remove($alias)
{
unset(
$this->_instances[$alias],
$this->_config[$alias],
$this->_instances[$alias],
$this->_config[$alias],
$this->_fallbacked[$alias]
);
}
Expand Down
77 changes: 77 additions & 0 deletions src/ORM/Registry/RegistryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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\Registry;

use Cake\ORM\Table;

/**
* Registries for Table objects should implement this interface.
*/
interface RegistryInterface
{

/**
* Stores a list of options to be used when instantiating an object
* with a matching alias.
*
* @param string|null $alias Name of the alias
* @param array|null $options list of options for the alias
* @return array The config data.
*/
public function config($alias = null, $options = null);

/**
* Get a table instance from the registry.
*
* @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
*/
public function get($alias, array $options = []);

/**
* Check to see if an instance exists in the registry.
*
* @param string $alias The alias to check for.
* @return bool
*/
public function exists($alias);

/**
* Set an instance.
*
* @param string $alias The alias to set.
* @param \Cake\ORM\Table $object The table to set.
* @return \Cake\ORM\Table
*/
public function set($alias, Table $object);

/**
* Clears the registry of configuration and instances.
*
* @return void
*/
public function clear();

/**
* Removes an instance from the registry.
*
* @param string $alias The alias to remove.
* @return void
*/
public function remove($alias);
}
23 changes: 15 additions & 8 deletions src/ORM/TableRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Cake\ORM;

use Cake\ORM\Registry;
use Cake\ORM\Registry\RegistryInterface;

/**
* Provides a registry/factory for Table objects.
Expand Down Expand Up @@ -56,32 +56,39 @@ class TableRegistry
/**
* Singleton for static calls.
*
* @var Registry
* @var \Cake\ORM\Registry\RegistryInterface
*/
protected static $_instance;

/**
* Default RegistryInterface implementation class.
*
* @var string
*/
protected static $_defaultRegistryClass = 'Cake\ORM\Registry\DefaultRegistry';

/**
* Sets and returns singleton instance of Registry.
*
* @param \Cake\ORM\Registry $instance
* @return \Cake\ORM\Registry
*
* @param \Cake\ORM\Registry\RegistryInterface $instance
* @return \Cake\ORM\Registry\RegistryInterface
*/
public static function instance(Registry $instance = null)
public static function instance(RegistryInterface $instance = null)
{
if ($instance) {
static::$_instance = $instance;
}

if (!static::$_instance) {
static::$_instance = new Registry;
static::$_instance = new static::$_defaultRegistryClass;
}

return static::$_instance;
}

/**
* Proxy for static calls on a singleton.
*
*
* @param string $name
* @param array $arguments
* @return mixed
Expand Down

0 comments on commit b539570

Please sign in to comment.