TableRegistry.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\ORM;
  16. use Cake\ORM\Locator\LocatorInterface;
  17. /**
  18. * Provides a registry/factory for Table objects.
  19. *
  20. * This registry allows you to centralize the configuration for tables
  21. * their connections and other meta-data.
  22. *
  23. * ### Configuring instances
  24. *
  25. * You may need to configure your table objects, using TableRegistry you can
  26. * centralize configuration. Any configuration set before instances are created
  27. * will be used when creating instances. If you modify configuration after
  28. * an instance is made, the instances *will not* be updated.
  29. *
  30. * ```
  31. * TableRegistry::config('Users', ['table' => 'my_users']);
  32. * ```
  33. *
  34. * Configuration data is stored *per alias* if you use the same table with
  35. * multiple aliases you will need to set configuration multiple times.
  36. *
  37. * ### Getting instances
  38. *
  39. * You can fetch instances out of the registry using get(). One instance is stored
  40. * per alias. Once an alias is populated the same instance will always be returned.
  41. * This is used to make the ORM use less memory and help make cyclic references easier
  42. * to solve.
  43. *
  44. * ```
  45. * $table = TableRegistry::get('Users', $config);
  46. * ```
  47. *
  48. */
  49. class TableRegistry
  50. {
  51. /**
  52. * Singleton for static calls.
  53. *
  54. * @var \Cake\ORM\Locator\LocatorInterface
  55. */
  56. protected static $_instance;
  57. /**
  58. * Default LocatorInterface implementation class.
  59. *
  60. * @var string
  61. */
  62. protected static $_defaultLocatorClass = 'Cake\ORM\Locator\TableLocator';
  63. /**
  64. * Sets and returns singleton instance of a LocatorInterface.
  65. *
  66. * @param \Cake\ORM\Locator\LocatorInterface $instance Instance of registry to set.
  67. * @return \Cake\ORM\Locator\LocatorInterface
  68. */
  69. public static function instance(LocatorInterface $instance = null)
  70. {
  71. if ($instance) {
  72. static::$_instance = $instance;
  73. }
  74. if (!static::$_instance) {
  75. static::$_instance = new static::$_defaultLocatorClass;
  76. }
  77. return static::$_instance;
  78. }
  79. /**
  80. * Stores a list of options to be used when instantiating an object
  81. * with a matching alias.
  82. *
  83. * @param string|null $alias Name of the alias
  84. * @param array|null $options list of options for the alias
  85. * @return array The config data.
  86. */
  87. public static function config($alias = null, $options = null)
  88. {
  89. return static::instance()->config($alias, $options);
  90. }
  91. /**
  92. * Get a table instance from the registry.
  93. *
  94. * @param string $alias The alias name you want to get.
  95. * @param array $options The options you want to build the table with.
  96. * @return \Cake\ORM\Table
  97. */
  98. public static function get($alias, array $options = [])
  99. {
  100. return static::instance()->get($alias, $options);
  101. }
  102. /**
  103. * Check to see if an instance exists in the registry.
  104. *
  105. * @param string $alias The alias to check for.
  106. * @return bool
  107. */
  108. public static function exists($alias)
  109. {
  110. return static::instance()->exists($alias);
  111. }
  112. /**
  113. * Set an instance.
  114. *
  115. * @param string $alias The alias to set.
  116. * @param \Cake\ORM\Table $object The table to set.
  117. * @return \Cake\ORM\Table
  118. */
  119. public static function set($alias, Table $object)
  120. {
  121. return static::instance()->set($alias, $object);
  122. }
  123. /**
  124. * Removes an instance from the registry.
  125. *
  126. * @param string $alias The alias to remove.
  127. * @return void
  128. */
  129. public static function remove($alias)
  130. {
  131. static::instance()->remove($alias);
  132. }
  133. /**
  134. * Clears the registry of configuration and instances.
  135. *
  136. * @return void
  137. */
  138. public static function clear()
  139. {
  140. static::instance()->clear();
  141. }
  142. /**
  143. * Proxy for static calls on a singleton.
  144. *
  145. * @param string $name Method name.
  146. * @param array $arguments Method arguments.
  147. * @return mixed
  148. */
  149. public static function __callStatic($name, $arguments)
  150. {
  151. return call_user_func_array([static::instance(), $name], $arguments);
  152. }
  153. }