-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
App.php
321 lines (293 loc) · 9.09 KB
/
App.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
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
* PHP 5
*
* 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 CakePHP(tm) v 1.2.0.6001
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Core;
use Cake\Cache\Cache;
use Cake\Error\ErrorHandler;
use Cake\Utility\Inflector;
/**
* App is responsible for resource location, and path management.
*
* ### Adding paths
*
* Additional paths for Views and Plugins are configured with Configure now. See App/Config/app.php for an
* example. The `App.paths.plugins` and `App.paths.views` variables are used to configure paths for plugins
* and views respectively. All class based resources should be mapped using your application's autoloader.
*
* ### Inspecting loaded paths
*
* You can inspect the currently loaded paths using `App::path('Controller')` for example to see loaded
* controller paths.
*
* It is also possible to inspect paths for plugin classes, for instance, to get
* the path to a plugin's helpers you would call `App::path('View/Helper', 'MyPlugin')`
*
* ### Locating plugins and themes
*
* Plugins and Themes can be located with App as well. Using App::pluginPath('DebugKit') for example, will
* give you the full path to the DebugKit plugin. App::themePath('purple'), would give the full path to the
* `purple` theme.
*
* ### Inspecting known objects
*
* You can find out which objects App knows about using App::objects('Controller') for example to find
* which application controllers App knows about. This method will not find objects in sub-namespaces
* by default.
*
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html
*/
class App {
/**
* Holds and key => value array of object types.
*
* @var array
*/
protected static $_objects = [];
/**
* Indicates whether the object cache should be stored again because of an addition to it
*
* @var boolean
*/
protected static $_objectCacheChange = false;
/**
* Return the classname namespaced. This method check if the class is defined on the
* application/plugin, otherwise try to load from the CakePHP core
*
* @param string $class Classname
* @param strign $type Type of class
* @param string $suffix Classname suffix
* @return boolean|string False if the class is not found or namespaced classname
*/
public static function classname($class, $type = '', $suffix = '') {
if (strpos($class, '\\') !== false) {
return $class;
}
list($plugin, $name) = pluginSplit($class);
if ($plugin) {
$base = Plugin::getNamespace($plugin);
} else {
$base = Configure::read('App.namespace');
}
$base = rtrim($base, '\\');
$fullname = '\\' . str_replace('/', '\\', $type . '\\' . $name) . $suffix;
if (static::_classExistsInBase($fullname, $base)) {
return $base . $fullname;
}
if ($plugin) {
return false;
}
if (static::_classExistsInBase($fullname, 'Cake')) {
return 'Cake' . $fullname;
}
return false;
}
/**
* _classExistsInBase
*
* Test isolation wrapper
*
* @param string $name
* @param string $namespace
* @return bool
*/
protected static function _classExistsInBase($name, $namespace) {
return class_exists($namespace . $name);
}
/**
* Used to read information stored path
*
* Usage:
*
* `App::path('Plugin');`
*
* Will return the configured paths for plugins. This is a simpler way to access
* the `App.paths.plugins` configure variable.
*
* `App::path('Model/Datasource', 'MyPlugin');`
*
* Will return the path for datasources under the 'MyPlugin' plugin.
*
* @param string $type type of path
* @param string $plugin name of plugin
* @return array
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::path
*/
public static function path($type, $plugin = null) {
if ($type === 'Plugin') {
return (array)Configure::read('App.paths.plugins');
}
if (empty($plugin) && $type === 'View') {
return (array)Configure::read('App.paths.views');
}
if (!empty($plugin)) {
return [static::pluginPath($plugin) . $type . DS];
}
return [APP . $type . DS];
}
/**
* Gets the path that a plugin is on. Searches through the defined plugin paths.
*
* Usage:
*
* `App::pluginPath('MyPlugin');`
*
* Will return the full path to 'MyPlugin' plugin
*
* @param string $plugin CamelCased/lower_cased plugin name to find the path of.
* @return string full path to the plugin.
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::pluginPath
*/
public static function pluginPath($plugin) {
return Plugin::path($plugin);
}
/**
* Finds the path that a theme is on. Searches through the defined theme paths.
*
* Usage:
*
* `App::themePath('MyTheme');` will return the full path to the 'MyTheme' theme.
*
* @param string $theme theme name to find the path of.
* @return string full path to the theme.
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::themePath
*/
public static function themePath($theme) {
$themeDir = 'Themed' . DS . Inflector::camelize($theme);
$paths = static::path('View');
foreach ($paths as $path) {
if (is_dir($path . $themeDir)) {
return $path . $themeDir . DS;
}
}
return $paths[0] . $themeDir . DS;
}
/**
* Returns the full path to a package inside the CakePHP core
*
* Usage:
*
* `App::core('Cache/Engine');`
*
* Will return the full path to the cache engines package.
*
* @param string $type
* @return array full path to package
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::core
*/
public static function core($type) {
return [CAKE . str_replace('/', DS, $type) . DS];
}
/**
* Returns an array of objects of the given type.
*
* Example usage:
*
* `App::objects('Plugin');` returns `['DebugKit', 'Blog', 'User'];`
*
* `App::objects('Controller');` returns `['PagesController', 'BlogController'];`
*
* You can also search only within a plugin's objects by using the plugin dot
* syntax.
*
* `App::objects('MyPlugin.Model');` returns `['MyPluginPost', 'MyPluginComment'];`
*
* When scanning directories, files and directories beginning with `.` will be excluded as these
* are commonly used by version control systems.
*
* @param string $type Type of object, i.e. 'Model', 'Controller', 'View/Helper', 'file', 'class' or 'Plugin'
* @param string|array $path Optional Scan only the path given. If null, paths for the chosen type will be used.
* @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
* @return mixed Either false on incorrect / miss. Or an array of found objects.
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::objects
*/
public static function objects($type, $path = null, $cache = true) {
if (empty(static::$_objects) && $cache === true) {
static::$_objects = (array)Cache::read('object_map', '_cake_core_');
}
$extension = '/\.php$/';
$includeDirectories = false;
$name = $type;
if ($type === 'Plugin') {
$extension = '/.*/';
$includeDirectories = true;
}
list($plugin, $type) = pluginSplit($type);
if ($type === 'file' && !$path) {
return false;
} elseif ($type === 'file') {
$extension = '/\.php$/';
$name = $type . str_replace(DS, '', $path);
}
$cacheLocation = empty($plugin) ? 'app' : $plugin;
if ($cache !== true || !isset(static::$_objects[$cacheLocation][$name])) {
$objects = [];
if (empty($path)) {
$path = static::path($type, $plugin);
}
foreach ((array)$path as $dir) {
if ($dir != APP && is_dir($dir)) {
$files = new \RegexIterator(new \DirectoryIterator($dir), $extension);
foreach ($files as $file) {
$fileName = basename($file);
if (!$file->isDot() && $fileName[0] !== '.') {
$isDir = $file->isDir();
if ($isDir && $includeDirectories) {
$objects[] = $fileName;
} elseif (!$includeDirectories && !$isDir) {
$objects[] = substr($fileName, 0, -4);
}
}
}
}
}
if ($type !== 'file') {
foreach ($objects as $key => $value) {
$objects[$key] = Inflector::camelize($value);
}
}
sort($objects);
if ($plugin) {
return $objects;
}
static::$_objects[$cacheLocation][$name] = $objects;
if ($cache) {
static::$_objectCacheChange = true;
}
}
return static::$_objects[$cacheLocation][$name];
}
/**
* Initializes the App, registers a shutdown function.
*
* @return void
*/
public static function init() {
register_shutdown_function([get_called_class(), 'shutdown']);
}
/**
* Object destructor.
*
* Writes cache file if changes have been made to the $_map. Also, check if a fatal
* error happened and call the handler.
*
* @return void
*/
public static function shutdown() {
if (static::$_objectCacheChange) {
Cache::write('object_map', static::$_objects, '_cake_core_');
}
}
}