1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\ORM;
16:
17: use Cake\ORM\Locator\LocatorInterface;
18:
19: /**
20: * Provides a registry/factory for Table objects.
21: *
22: * This registry allows you to centralize the configuration for tables
23: * their connections and other meta-data.
24: *
25: * ### Configuring instances
26: *
27: * You may need to configure your table objects, using TableRegistry you can
28: * centralize configuration. Any configuration set before instances are created
29: * will be used when creating instances. If you modify configuration after
30: * an instance is made, the instances *will not* be updated.
31: *
32: * ```
33: * TableRegistry::config('Users', ['table' => 'my_users']);
34: * ```
35: *
36: * Configuration data is stored *per alias* if you use the same table with
37: * multiple aliases you will need to set configuration multiple times.
38: *
39: * ### Getting instances
40: *
41: * You can fetch instances out of the registry using get(). One instance is stored
42: * per alias. Once an alias is populated the same instance will always be returned.
43: * This is used to make the ORM use less memory and help make cyclic references easier
44: * to solve.
45: *
46: * ```
47: * $table = TableRegistry::get('Users', $config);
48: * ```
49: */
50: class TableRegistry
51: {
52:
53: /**
54: * LocatorInterface implementation instance.
55: *
56: * @var \Cake\ORM\Locator\LocatorInterface
57: */
58: protected static $_locator;
59:
60: /**
61: * Default LocatorInterface implementation class.
62: *
63: * @var string
64: */
65: protected static $_defaultLocatorClass = 'Cake\ORM\Locator\TableLocator';
66:
67: /**
68: * Sets and returns a singleton instance of LocatorInterface implementation.
69: *
70: * @param \Cake\ORM\Locator\LocatorInterface|null $locator Instance of a locator to use.
71: * @return \Cake\ORM\Locator\LocatorInterface
72: * @deprecated 3.5.0 Use getTableLocator()/setTableLocator() instead.
73: */
74: public static function locator(LocatorInterface $locator = null)
75: {
76: deprecationWarning(
77: 'TableRegistry::locator() is deprecated. ' .
78: 'Use setTableLocator()/getTableLocator() instead.'
79: );
80: if ($locator) {
81: static::setTableLocator($locator);
82: }
83:
84: return static::getTableLocator();
85: }
86:
87: /**
88: * Returns a singleton instance of LocatorInterface implementation.
89: *
90: * @return \Cake\ORM\Locator\LocatorInterface
91: */
92: public static function getTableLocator()
93: {
94: if (!static::$_locator) {
95: static::$_locator = new static::$_defaultLocatorClass();
96: }
97:
98: return static::$_locator;
99: }
100:
101: /**
102: * Sets singleton instance of LocatorInterface implementation.
103: *
104: * @param \Cake\ORM\Locator\LocatorInterface $tableLocator Instance of a locator to use.
105: * @return void
106: */
107: public static function setTableLocator(LocatorInterface $tableLocator)
108: {
109: static::$_locator = $tableLocator;
110: }
111:
112: /**
113: * Stores a list of options to be used when instantiating an object
114: * with a matching alias.
115: *
116: * @param string|null $alias Name of the alias
117: * @param array|null $options list of options for the alias
118: * @return array The config data.
119: * @deprecated 3.6.0 Use \Cake\ORM\Locator\TableLocator::getConfig()/setConfig() instead.
120: */
121: public static function config($alias = null, $options = null)
122: {
123: deprecationWarning(
124: 'TableRegistry::config() is deprecated. ' .
125: 'Use \Cake\ORM\Locator\TableLocator::getConfig()/setConfig() instead.'
126: );
127:
128: return static::getTableLocator()->config($alias, $options);
129: }
130:
131: /**
132: * Get a table instance from the registry.
133: *
134: * See options specification in {@link TableLocator::get()}.
135: *
136: * @param string $alias The alias name you want to get.
137: * @param array $options The options you want to build the table with.
138: * @return \Cake\ORM\Table
139: * @deprecated 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead.
140: */
141: public static function get($alias, array $options = [])
142: {
143: return static::getTableLocator()->get($alias, $options);
144: }
145:
146: /**
147: * Check to see if an instance exists in the registry.
148: *
149: * @param string $alias The alias to check for.
150: * @return bool
151: * @deprecated 3.6.0 Use \Cake\ORM\Locator\TableLocator::exists() instead.
152: */
153: public static function exists($alias)
154: {
155: return static::getTableLocator()->exists($alias);
156: }
157:
158: /**
159: * Set an instance.
160: *
161: * @param string $alias The alias to set.
162: * @param \Cake\ORM\Table $object The table to set.
163: * @return \Cake\ORM\Table
164: * @deprecated 3.6.0 Use \Cake\ORM\Locator\TableLocator::set() instead.
165: */
166: public static function set($alias, Table $object)
167: {
168: return static::getTableLocator()->set($alias, $object);
169: }
170:
171: /**
172: * Removes an instance from the registry.
173: *
174: * @param string $alias The alias to remove.
175: * @return void
176: * @deprecated 3.6.0 Use \Cake\ORM\Locator\TableLocator::remove() instead.
177: */
178: public static function remove($alias)
179: {
180: static::getTableLocator()->remove($alias);
181: }
182:
183: /**
184: * Clears the registry of configuration and instances.
185: *
186: * @return void
187: * @deprecated 3.6.0 Use \Cake\ORM\Locator\TableLocator::clear() instead.
188: */
189: public static function clear()
190: {
191: static::getTableLocator()->clear();
192: }
193:
194: /**
195: * Proxy for static calls on a locator.
196: *
197: * @param string $name Method name.
198: * @param array $arguments Method arguments.
199: * @return mixed
200: */
201: public static function __callStatic($name, $arguments)
202: {
203: deprecationWarning(
204: 'TableRegistry::' . $name . '() is deprecated. ' .
205: 'Use \Cake\ORM\Locator\TableLocator::' . $name . '() instead.'
206: );
207:
208: return static::getTableLocator()->$name(...$arguments);
209: }
210: }
211: