/
TableRegistry.php
190 lines (174 loc) · 5.35 KB
/
TableRegistry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?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
{
/**
* LocatorInterface implementation instance.
*
* @var \Cake\ORM\Locator\LocatorInterface
*/
protected static $_locator;
/**
* Default LocatorInterface implementation class.
*
* @var string
*/
protected static $_defaultLocatorClass = 'Cake\ORM\Locator\TableLocator';
/**
* Sets and returns a singleton instance of LocatorInterface implementation.
*
* @param \Cake\ORM\Locator\LocatorInterface|null $locator Instance of a locator to use.
* @return \Cake\ORM\Locator\LocatorInterface
* @deprecated 3.5.0 Use getter/setter instead.
*/
public static function locator(LocatorInterface $locator = null)
{
if ($locator) {
static::setTableLocator($locator);
}
return static::getTableLocator();
}
/**
* Returns a singleton instance of LocatorInterface implementation.
*
* @return \Cake\ORM\Locator\LocatorInterface
*/
public static function getTableLocator()
{
if (!static::$_locator) {
static::$_locator = new static::$_defaultLocatorClass;
}
return static::$_locator;
}
/**
* Sets singleton instance of LocatorInterface implementation.
*
* @param \Cake\ORM\Locator\LocatorInterface|null $locator Instance of a locator to use.
* @return void
*/
public static function setTableLocator(LocatorInterface $locator)
{
static::$_locator = $locator;
}
/**
* 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::getTableLocator()->config($alias, $options);
}
/**
* Get a table instance from the registry.
*
* See options specification in {@link TableLocator::get()}.
*
* @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::getTableLocator()->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::getTableLocator()->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::getTableLocator()->set($alias, $object);
}
/**
* Removes an instance from the registry.
*
* @param string $alias The alias to remove.
* @return void
*/
public static function remove($alias)
{
static::getTableLocator()->remove($alias);
}
/**
* Clears the registry of configuration and instances.
*
* @return void
*/
public static function clear()
{
static::getTableLocator()->clear();
}
/**
* Proxy for static calls on a locator.
*
* @param string $name Method name.
* @param array $arguments Method arguments.
* @return mixed
*/
public static function __callStatic($name, $arguments)
{
return static::getTableLocator()->$name(...$arguments);
}
}