Browse Source

Table registries must implement RegistryInterface

Robert 11 years ago
parent
commit
b5395705b4
3 changed files with 107 additions and 62 deletions
  1. 15 54
      src/ORM/Registry.php
  2. 77 0
      src/ORM/Registry/RegistryInterface.php
  3. 15 8
      src/ORM/TableRegistry.php

+ 15 - 54
src/ORM/Registry.php

@@ -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
 {
 
     /**
@@ -114,7 +87,7 @@ class Registry
         }
         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;
@@ -159,7 +132,7 @@ class Registry
         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];
@@ -197,7 +170,7 @@ class Registry
         if ($options['className'] === 'Cake\ORM\Table') {
             $this->_fallbacked[$alias] = $this->_instances[$alias];
         }
-        
+
         return $this->_instances[$alias];
     }
 
@@ -213,10 +186,7 @@ class Registry
     }
 
     /**
-     * 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)
     {
@@ -224,11 +194,7 @@ class Registry
     }
 
     /**
-     * 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)
     {
@@ -236,9 +202,7 @@ class Registry
     }
 
     /**
-     * Clears the registry of configuration and instances.
-     *
-     * @return void
+     * @inheritDoc
      */
     public function clear()
     {
@@ -261,16 +225,13 @@ class Registry
     }
 
     /**
-     * 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]
         );
     }

+ 77 - 0
src/ORM/Registry/RegistryInterface.php

@@ -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);
+}

+ 15 - 8
src/ORM/TableRegistry.php

@@ -16,7 +16,7 @@
 
 namespace Cake\ORM;
 
-use Cake\ORM\Registry;
+use Cake\ORM\Registry\RegistryInterface;
 
 /**
  * Provides a registry/factory for Table objects.
@@ -56,24 +56,31 @@ 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;
@@ -81,7 +88,7 @@ class TableRegistry
 
     /**
      * Proxy for static calls on a singleton.
-     * 
+     *
      * @param string $name
      * @param array $arguments
      * @return mixed