/
TableLocator.php
310 lines (277 loc) · 9.94 KB
/
TableLocator.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\ORM\Locator;
use Cake\Core\App;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\AssociationCollection;
use Cake\ORM\Table;
use Cake\Utility\Inflector;
use RuntimeException;
/**
* Provides a default registry/factory for Table objects.
*/
class TableLocator implements LocatorInterface
{
/**
* Configuration for aliases.
*
* @var array
*/
protected $_config = [];
/**
* Instances that belong to the registry.
*
* @var \Cake\ORM\Table[]
*/
protected $_instances = [];
/**
* Contains a list of Table objects that were created out of the
* built-in Table class. The list is indexed by table alias
*
* @var \Cake\ORM\Table[]
*/
protected $_fallbacked = [];
/**
* Contains a list of options that were passed to get() method.
*
* @var array
*/
protected $_options = [];
/**
* Stores a list of options to be used when instantiating an object
* with a matching alias.
*
* @param string|array $alias Name of the alias or array to completely overwrite current config.
* @param array|null $options list of options for the alias
* @return $this
* @throws \RuntimeException When you attempt to configure an existing table instance.
*/
public function setConfig($alias, $options = null)
{
if (!is_string($alias)) {
$this->_config = $alias;
return $this;
}
if (isset($this->_instances[$alias])) {
throw new RuntimeException(sprintf(
'You cannot configure "%s", it has already been constructed.',
$alias
));
}
$this->_config[$alias] = $options;
return $this;
}
/**
* Returns configuration for an alias or the full configuration array for all aliases.
*
* @param string|null $alias Alias to get config for, null for complete config.
* @return array The config data.
*/
public function getConfig($alias = null)
{
if ($alias === null) {
return $this->_config;
}
return isset($this->_config[$alias]) ? $this->_config[$alias] : [];
}
/**
* Stores a list of options to be used when instantiating an object
* with a matching alias.
*
* The options that can be stored are those that are recognized by `get()`
* If second argument is omitted, it will return the current settings
* for $alias.
*
* If no arguments are passed it will return the full configuration array for
* all aliases
*
* @deprecated 3.4.0 Use setConfig()/getConfig() instead.
* @param string|array|null $alias Name of the alias
* @param array|null $options list of options for the alias
* @return array The config data.
* @throws \RuntimeException When you attempt to configure an existing table instance.
*/
public function config($alias = null, $options = null)
{
deprecationWarning(
'TableLocator::config() is deprecated. ' .
'Use getConfig()/setConfig() instead.'
);
if ($alias !== null) {
if (is_string($alias) && $options === null) {
return $this->getConfig($alias);
}
$this->setConfig($alias, $options);
}
return $this->getConfig($alias);
}
/**
* Get a table instance from the registry.
*
* Tables are only created once until the registry is flushed.
* This means that aliases must be unique across your application.
* This is important because table associations are resolved at runtime
* and cyclic references need to be handled correctly.
*
* The options that can be passed are the same as in Cake\ORM\Table::__construct(), but the
* `className` key is also recognized.
*
* ### Options
*
* - `className` Define the specific class name to use. If undefined, CakePHP will generate the
* class name based on the alias. For example 'Users' would result in
* `App\Model\Table\UsersTable` being used. If this class does not exist,
* then the default `Cake\ORM\Table` class will be used. By setting the `className`
* option you can define the specific class to use. The className option supports
* plugin short class references {@link Cake\Core\App::shortName()}.
* - `table` Define the table name to use. If undefined, this option will default to the underscored
* version of the alias name.
* - `connection` Inject the specific connection object to use. If this option and `connectionName` are undefined,
* The table class' `defaultConnectionName()` method will be invoked to fetch the connection name.
* - `connectionName` Define the connection name to use. The named connection will be fetched from
* Cake\Datasource\ConnectionManager.
*
* *Note* If your `$alias` uses plugin syntax only the name part will be used as
* key in the registry. This means that if two plugins, or a plugin and app provide
* the same alias, the registry will only store the first instance.
*
* @param string $alias The alias name you want to get.
* @param array $options The options you want to build the table with.
* If a table has already been loaded the options will be ignored.
* @return \Cake\ORM\Table
* @throws \RuntimeException When you try to configure an alias that already exists.
*/
public function get($alias, array $options = [])
{
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
));
}
return $this->_instances[$alias];
}
$this->_options[$alias] = $options;
list(, $classAlias) = pluginSplit($alias);
$options = ['alias' => $classAlias] + $options;
if (isset($this->_config[$alias])) {
$options += $this->_config[$alias];
}
$className = $this->_getClassName($alias, $options);
if ($className) {
$options['className'] = $className;
} else {
if (empty($options['className'])) {
$options['className'] = Inflector::camelize($alias);
}
if (!isset($options['table']) && strpos($options['className'], '\\') === false) {
list(, $table) = pluginSplit($options['className']);
$options['table'] = Inflector::underscore($table);
}
$options['className'] = 'Cake\ORM\Table';
}
if (empty($options['connection'])) {
if (!empty($options['connectionName'])) {
$connectionName = $options['connectionName'];
} else {
/* @var \Cake\ORM\Table $className */
$className = $options['className'];
$connectionName = $className::defaultConnectionName();
}
$options['connection'] = ConnectionManager::get($connectionName);
}
if (empty($options['associations'])) {
$associations = new AssociationCollection($this);
$options['associations'] = $associations;
}
$options['registryAlias'] = $alias;
$this->_instances[$alias] = $this->_create($options);
if ($options['className'] === 'Cake\ORM\Table') {
$this->_fallbacked[$alias] = $this->_instances[$alias];
}
return $this->_instances[$alias];
}
/**
* Gets the table class name.
*
* @param string $alias The alias name you want to get.
* @param array $options Table options array.
* @return string|false
*/
protected function _getClassName($alias, array $options = [])
{
if (empty($options['className'])) {
$options['className'] = Inflector::camelize($alias);
}
return App::className($options['className'], 'Model/Table', 'Table');
}
/**
* Wrapper for creating table instances
*
* @param array $options The alias to check for.
* @return \Cake\ORM\Table
*/
protected function _create(array $options)
{
return new $options['className']($options);
}
/**
* {@inheritDoc}
*/
public function exists($alias)
{
return isset($this->_instances[$alias]);
}
/**
* {@inheritDoc}
*/
public function set($alias, Table $object)
{
return $this->_instances[$alias] = $object;
}
/**
* {@inheritDoc}
*/
public function clear()
{
$this->_instances = [];
$this->_config = [];
$this->_fallbacked = [];
}
/**
* Returns the list of tables that were created by this registry that could
* not be instantiated from a specific subclass. This method is useful for
* debugging common mistakes when setting up associations or created new table
* classes.
*
* @return \Cake\ORM\Table[]
*/
public function genericInstances()
{
return $this->_fallbacked;
}
/**
* {@inheritDoc}
*/
public function remove($alias)
{
unset(
$this->_instances[$alias],
$this->_config[$alias],
$this->_fallbacked[$alias]
);
}
}