/
App.php
694 lines (627 loc) · 18.8 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
<?php
/**
* App class
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Core
* @since CakePHP(tm) v 1.2.0.6001
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Core;
use Cake\Cache\Cache;
use Cake\Error\ErrorHandler;
use Cake\Utility\Inflector;
/**
* App is responsible for path management, class location and class loading.
*
* ### Adding paths
*
* You can add paths to the search indexes App uses to find classes using `App::build()`. Adding
* additional controller paths for example would alter where CakePHP looks for controllers.
* This allows you to split your application up across the filesystem.
*
* ### Packages
*
* CakePHP is organized around the idea of packages, each class belongs to a package or folder where other
* classes reside. You can configure each package location in your application using `App::build('APackage/SubPackage', $paths)`
* to inform the framework where should each class be loaded. Almost every class in the CakePHP framework can be swapped
* by your own compatible implementation. If you wish to use you own class instead of the classes the framework provides,
* just add the class to your libs folder mocking the directory location of where CakePHP expects to find it.
*
* For instance if you'd like to use your own HttpSocket class, put it under
*
* App/Network/Http/HttpSocket.php
*
* ### 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 see 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.
*
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html
* @package Cake.Core
*/
class App {
/**
* Append paths
*
* @constant APPEND
*/
const APPEND = 'append';
/**
* Prepend paths
*
* @constant PREPEND
*/
const PREPEND = 'prepend';
/**
* Register package
*
* @constant REGISTER
*/
const REGISTER = 'register';
/**
* Reset paths instead of merging
*
* @constant RESET
*/
const RESET = true;
/**
* Paths to search for files.
*
* @var array
*/
public static $search = array();
/**
* Whether or not to return the file that is loaded.
*
* @var boolean
*/
public static $return = false;
/**
* Holds key/value pairs of $type => file path.
*
* @var array
*/
protected static $_map = array();
/**
* Holds and key => value array of object types.
*
* @var array
*/
protected static $_objects = array();
/**
* Holds the location of each class
*
* @var array
*/
protected static $_classMap = array();
/**
* Holds the possible paths for each package name
*
* @var array
*/
protected static $_packages = array();
/**
* Holds the templates for each customizable package path in the application
*
* @var array
*/
protected static $_packageFormat = array();
/**
* Indicates whether the class cache should be stored again because of an addition to it
*
* @var boolean
*/
protected static $_cacheChange = false;
/**
* 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);
$checkCore = true;
if ($plugin) {
$base = Plugin::getNamespace($plugin);
$checkCore = false;
} else {
$base = Configure::read('App.namespace');
}
$base = rtrim($base, '\\');
$fullname = '\\' . str_replace('/', '\\', $type) . '\\' . $name . $suffix;
if (class_exists($base . $fullname)) {
return $base . $fullname;
}
if ($checkCore) {
if (class_exists('Cake' . $fullname)) {
return 'Cake' . $fullname;
}
}
return false;
}
/**
* Used to read information stored path
*
* Usage:
*
* `App::path('Model'); will return all paths for models`
*
* `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 (!empty($plugin)) {
$path = array();
$pluginPath = static::pluginPath($plugin);
$packageFormat = static::_packageFormat();
if (!empty($packageFormat[$type])) {
foreach ($packageFormat[$type] as $f) {
$path[] = sprintf($f, $pluginPath);
}
}
return $path;
}
if (!isset(static::$_packages[$type])) {
return array();
}
return static::$_packages[$type];
}
/**
* Get all the currently loaded paths from App. Useful for inspecting
* or storing all paths App knows about. For a paths to a specific package
* use App::path()
*
* @return array An array of packages and their associated paths.
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::paths
*/
public static function paths() {
return static::$_packages;
}
/**
* Sets up each package location on the file system. You can configure multiple search paths
* for each package, those will be used to look for files one folder at a time in the specified order
* All paths should be terminated with a Directory separator
*
* Usage:
*
* `App::build(array('Model' => array('/a/full/path/to/models/'))); will setup a new search path for the Model package`
*
* `App::build(array('Model' => array('/path/to/models/')), App::RESET); will setup the path as the only valid path for searching models`
*
* `App::build(array('View/Helper' => array('/path/to/helpers/', '/another/path/'))); will setup multiple search paths for helpers`
*
* `App::build(array('Service' => array('%s' . 'Service/')), App::REGISTER); will register new package 'Service'`
*
* If reset is set to true, all loaded plugins will be forgotten and they will be needed to be loaded again.
*
* @param array $paths associative array with package names as keys and a list of directories for new search paths
* @param boolean|string $mode App::RESET will set paths, App::APPEND with append paths, App::PREPEND will prepend paths (default)
* App::REGISTER will register new packages and their paths, %s in path will be replaced by APP path
* @return void
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::build
*/
public static function build($paths = array(), $mode = self::PREPEND) {
if ($mode === static::RESET) {
foreach ($paths as $type => $new) {
static::$_packages[$type] = (array)$new;
static::objects($type, null, false);
}
return;
}
if (empty($paths)) {
static::$_packageFormat = null;
}
$packageFormat = static::_packageFormat();
if ($mode === static::REGISTER) {
foreach ($paths as $package => $formats) {
if (empty($packageFormat[$package])) {
$packageFormat[$package] = $formats;
} else {
$formats = array_merge($packageFormat[$package], $formats);
$packageFormat[$package] = array_values(array_unique($formats));
}
}
static::$_packageFormat = $packageFormat;
}
$defaults = array();
foreach ($packageFormat as $package => $format) {
foreach ($format as $f) {
$defaults[$package][] = sprintf($f, APP);
}
}
if (empty($paths)) {
static::$_packages = $defaults;
return;
}
if ($mode === static::REGISTER) {
$paths = array();
}
foreach ($defaults as $type => $default) {
if (!empty(static::$_packages[$type])) {
$path = static::$_packages[$type];
} else {
$path = $default;
}
if (!empty($paths[$type])) {
$newPath = (array)$paths[$type];
if ($mode === static::PREPEND) {
$path = array_merge($newPath, $path);
} else {
$path = array_merge($path, $newPath);
}
$path = array_values(array_unique($path));
}
static::$_packages[$type] = $path;
}
}
/**
* 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/' . Inflector::camelize($theme);
foreach (static::$_packages['View'] as $path) {
if (is_dir($path . $themeDir)) {
return $path . $themeDir . DS;
}
}
return static::$_packages['View'][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 array(CAKE . str_replace('/', DS, $type) . DS);
}
/**
* Returns an array of objects of the given type.
*
* Example usage:
*
* `App::objects('Plugin');` returns `array('DebugKit', 'Blog', 'User');`
*
* `App::objects('Controller');` returns `array('PagesController', 'BlogController');`
*
* You can also search only within a plugin's objects by using the plugin dot
* syntax.
*
* `App::objects('MyPlugin.Model');` returns `array('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) {
$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);
}
if (empty(static::$_objects) && $cache === true) {
static::$_objects = (array)Cache::read('object_map', '_cake_core_');
}
$cacheLocation = empty($plugin) ? 'app' : $plugin;
if ($cache !== true || !isset(static::$_objects[$cacheLocation][$name])) {
$objects = array();
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];
}
/**
* Method to handle the class loading manually, ie. Vendor classes.
*
* @param string $className the name of the class to load
* @return boolean
*/
public static function load($className) {
if (!isset(static::$_classMap[$className])) {
return false;
}
if (empty(static::$_map)) {
static::$_map = (array)Cache::read('file_map', '_cake_core_');
}
$parts = explode('.', static::$_classMap[$className], 2);
list($plugin, $package) = count($parts) > 1 ? $parts : array(null, current($parts));
if ($file = static::_mapped($className, $plugin)) {
return include $file;
}
$paths = static::path($package, $plugin);
if (empty($plugin)) {
$appLibs = empty(static::$_packages['Lib']) ? APPLIBS : current(static::$_packages['Lib']);
$paths[] = $appLibs . $package . DS;
$paths[] = APP . $package . DS;
$paths[] = CAKE . $package . DS;
} else {
$pluginPath = static::pluginPath($plugin);
$paths[] = $pluginPath . 'Lib/' . $package . DS;
$paths[] = $pluginPath . $package . DS;
}
$normalizedClassName = str_replace('\\', DS, $className);
foreach ($paths as $path) {
$file = $path . $normalizedClassName . '.php';
if (file_exists($file)) {
static::_map($file, $className, $plugin);
return include $file;
}
}
return false;
}
/**
* Returns the package name where a class was defined to be located at
*
* @param string $className name of the class to obtain the package name from
* @return string package name or null if not declared
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::location
*/
public static function location($className) {
if (!empty(static::$_classMap[$className])) {
return static::$_classMap[$className];
}
return null;
}
/**
* Initializes the App, registers a shutdown function.
*
* @return void
*/
public static function init() {
register_shutdown_function(array(get_called_class(), 'shutdown'));
}
/**
* Maps the $name to the $file.
*
* @param string $file full path to file
* @param string $name unique name for this map
* @param string $plugin camelized if object is from a plugin, the name of the plugin
* @return void
*/
protected static function _map($file, $name, $plugin = null) {
$key = $name;
if ($plugin) {
$key = 'plugin.' . $name;
}
if ($plugin && empty(static::$_map[$name])) {
static::$_map[$key] = $file;
}
if (!$plugin && empty(static::$_map['plugin.' . $name])) {
static::$_map[$key] = $file;
}
}
/**
* Returns a file's complete path.
*
* @param string $name unique name
* @param string $plugin camelized if object is from a plugin, the name of the plugin
* @return mixed file path if found, false otherwise
*/
protected static function _mapped($name, $plugin = null) {
$key = $name;
if ($plugin) {
$key = 'plugin.' . $name;
}
return isset(static::$_map[$key]) ? static::$_map[$key] : false;
}
/**
* Sets then returns the templates for each customizable package path
*
* @return array templates for each customizable package path
*/
protected static function _packageFormat() {
if (empty(static::$_packageFormat)) {
static::$_packageFormat = array(
'Model' => array(
'%s' . 'Model/'
),
'Model/Behavior' => array(
'%s' . 'Model/Behavior/'
),
'Model/Datasource' => array(
'%s' . 'Model/Datasource/'
),
'Model/Datasource/Database' => array(
'%s' . 'Model/Datasource/Database/'
),
'Model/Datasource/Session' => array(
'%s' . 'Model/Datasource/Session/'
),
'Controller' => array(
'%s' . 'Controller/'
),
'Controller/Component' => array(
'%s' . 'Controller/Component/'
),
'Controller/Component/Auth' => array(
'%s' . 'Controller/Component/Auth/'
),
'Controller/Component/Acl' => array(
'%s' . 'Controller/Component/Acl/'
),
'View' => array(
'%s' . 'View/'
),
'View/Helper' => array(
'%s' . 'View/Helper/'
),
'Console' => array(
'%s' . 'Console/'
),
'Console/Command' => array(
'%s' . 'Console/Command/'
),
'Console/Command/Task' => array(
'%s' . 'Console/Command/Task/'
),
'Lib' => array(
'%s' . 'Lib/'
),
'Locale' => array(
'%s' . 'Locale/'
),
'Vendor' => array(
'%s' . 'Vendor/',
dirname(dirname(CAKE)) . DS . 'vendors/',
),
'Plugin' => array(
APP . 'Plugin/',
dirname(dirname(CAKE)) . DS . 'plugins/'
)
);
}
return static::$_packageFormat;
}
/**
* 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::$_cacheChange) {
Cache::write('file_map', array_filter(static::$_map), '_cake_core_');
}
if (static::$_objectCacheChange) {
Cache::write('object_map', static::$_objects, '_cake_core_');
}
static::_checkFatalError();
}
/**
* Check if a fatal error happened and trigger the configured handler if configured
*
* @return void
*/
protected static function _checkFatalError() {
$lastError = error_get_last();
if (!is_array($lastError)) {
return;
}
list(, $log) = ErrorHandler::mapErrorCode($lastError['type']);
if ($log !== LOG_ERR) {
return;
}
if (PHP_SAPI === 'cli') {
$errorHandler = Configure::read('Error.consoleHandler');
} else {
$errorHandler = Configure::read('Error.handler');
}
if (!is_callable($errorHandler)) {
return;
}
call_user_func($errorHandler, $lastError['type'], $lastError['message'], $lastError['file'], $lastError['line'], array());
}
}