| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?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.0.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\ORM;
- use Cake\ORM\Locator\LocatorInterface;
- /**
- * 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);
- * ```
- *
- */
- class TableRegistry
- {
- /**
- * Singleton for static calls.
- *
- * @var \Cake\ORM\Locator\LocatorInterface
- */
- protected static $_instance;
- /**
- * Default LocatorInterface implementation class.
- *
- * @var string
- */
- protected static $_defaultLocatorClass = 'Cake\ORM\Locator\TableLocator';
- /**
- * Sets and returns singleton instance of a LocatorInterface.
- *
- * @param \Cake\ORM\Locator\LocatorInterface $instance Instance of registry to set.
- * @return \Cake\ORM\Locator\LocatorInterface
- */
- public static function instance(LocatorInterface $instance = null)
- {
- if ($instance) {
- static::$_instance = $instance;
- }
- if (!static::$_instance) {
- static::$_instance = new static::$_defaultLocatorClass;
- }
- return static::$_instance;
- }
- /**
- * 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 static function config($alias = null, $options = null)
- {
- return static::instance()->config($alias, $options);
- }
- /**
- * 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 static function get($alias, array $options = [])
- {
- return static::instance()->get($alias, $options);
- }
- /**
- * Check to see if an instance exists in the registry.
- *
- * @param string $alias The alias to check for.
- * @return bool
- */
- public static function exists($alias)
- {
- return static::instance()->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 static function set($alias, Table $object)
- {
- return static::instance()->set($alias, $object);
- }
- /**
- * Removes an instance from the registry.
- *
- * @param string $alias The alias to remove.
- * @return void
- */
- public static function remove($alias)
- {
- static::instance()->remove($alias);
- }
- /**
- * Clears the registry of configuration and instances.
- *
- * @return void
- */
- public static function clear()
- {
- static::instance()->clear();
- }
- /**
- * Proxy for static calls on a singleton.
- *
- * @param string $name Method name.
- * @param array $arguments Method arguments.
- * @return mixed
- */
- public static function __callStatic($name, $arguments)
- {
- return call_user_func_array([static::instance(), $name], $arguments);
- }
- }
|