1: <?php
2: /**
3: * App class
4: *
5: * PHP 5
6: *
7: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9: *
10: * Licensed under The MIT License
11: * For full copyright and license information, please see the LICENSE.txt
12: * Redistributions of files must retain the above copyright notice.
13: *
14: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15: * @link http://cakephp.org CakePHP(tm) Project
16: * @package Cake.Core
17: * @since CakePHP(tm) v 1.2.0.6001
18: * @license http://www.opensource.org/licenses/mit-license.php MIT License
19: */
20:
21: App::uses('Inflector', 'Utility');
22:
23: /**
24: * App is responsible for path management, class location and class loading.
25: *
26: * ### Adding paths
27: *
28: * You can add paths to the search indexes App uses to find classes using `App::build()`. Adding
29: * additional controller paths for example would alter where CakePHP looks for controllers.
30: * This allows you to split your application up across the filesystem.
31: *
32: * ### Packages
33: *
34: * CakePHP is organized around the idea of packages, each class belongs to a package or folder where other
35: * classes reside. You can configure each package location in your application using `App::build('APackage/SubPackage', $paths)`
36: * to inform the framework where should each class be loaded. Almost every class in the CakePHP framework can be swapped
37: * by your own compatible implementation. If you wish to use you own class instead of the classes the framework provides,
38: * just add the class to your libs folder mocking the directory location of where CakePHP expects to find it.
39: *
40: * For instance if you'd like to use your own HttpSocket class, put it under
41: *
42: * app/Network/Http/HttpSocket.php
43: *
44: * ### Inspecting loaded paths
45: *
46: * You can inspect the currently loaded paths using `App::path('Controller')` for example to see loaded
47: * controller paths.
48: *
49: * It is also possible to inspect paths for plugin classes, for instance, to see a plugin's helpers you would call
50: * `App::path('View/Helper', 'MyPlugin')`
51: *
52: * ### Locating plugins and themes
53: *
54: * Plugins and Themes can be located with App as well. Using App::pluginPath('DebugKit') for example, will
55: * give you the full path to the DebugKit plugin. App::themePath('purple'), would give the full path to the
56: * `purple` theme.
57: *
58: * ### Inspecting known objects
59: *
60: * You can find out which objects App knows about using App::objects('Controller') for example to find
61: * which application controllers App knows about.
62: *
63: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html
64: * @package Cake.Core
65: */
66: class App {
67:
68: /**
69: * Append paths
70: *
71: * @constant APPEND
72: */
73: const APPEND = 'append';
74:
75: /**
76: * Prepend paths
77: *
78: * @constant PREPEND
79: */
80: const PREPEND = 'prepend';
81:
82: /**
83: * Register package
84: *
85: * @constant REGISTER
86: */
87: const REGISTER = 'register';
88:
89: /**
90: * Reset paths instead of merging
91: *
92: * @constant RESET
93: */
94: const RESET = true;
95:
96: /**
97: * List of object types and their properties
98: *
99: * @var array
100: */
101: public static $types = array(
102: 'class' => array('extends' => null, 'core' => true),
103: 'file' => array('extends' => null, 'core' => true),
104: 'model' => array('extends' => 'AppModel', 'core' => false),
105: 'behavior' => array('suffix' => 'Behavior', 'extends' => 'Model/ModelBehavior', 'core' => true),
106: 'controller' => array('suffix' => 'Controller', 'extends' => 'AppController', 'core' => true),
107: 'component' => array('suffix' => 'Component', 'extends' => null, 'core' => true),
108: 'lib' => array('extends' => null, 'core' => true),
109: 'view' => array('suffix' => 'View', 'extends' => null, 'core' => true),
110: 'helper' => array('suffix' => 'Helper', 'extends' => 'AppHelper', 'core' => true),
111: 'vendor' => array('extends' => null, 'core' => true),
112: 'shell' => array('suffix' => 'Shell', 'extends' => 'AppShell', 'core' => true),
113: 'plugin' => array('extends' => null, 'core' => true)
114: );
115:
116: /**
117: * Paths to search for files.
118: *
119: * @var array
120: */
121: public static $search = array();
122:
123: /**
124: * Whether or not to return the file that is loaded.
125: *
126: * @var boolean
127: */
128: public static $return = false;
129:
130: /**
131: * Holds key/value pairs of $type => file path.
132: *
133: * @var array
134: */
135: protected static $_map = array();
136:
137: /**
138: * Holds and key => value array of object types.
139: *
140: * @var array
141: */
142: protected static $_objects = array();
143:
144: /**
145: * Holds the location of each class
146: *
147: * @var array
148: */
149: protected static $_classMap = array();
150:
151: /**
152: * Holds the possible paths for each package name
153: *
154: * @var array
155: */
156: protected static $_packages = array();
157:
158: /**
159: * Holds the templates for each customizable package path in the application
160: *
161: * @var array
162: */
163: protected static $_packageFormat = array();
164:
165: /**
166: * Maps an old style CakePHP class type to the corresponding package
167: *
168: * @var array
169: */
170: public static $legacy = array(
171: 'models' => 'Model',
172: 'behaviors' => 'Model/Behavior',
173: 'datasources' => 'Model/Datasource',
174: 'controllers' => 'Controller',
175: 'components' => 'Controller/Component',
176: 'views' => 'View',
177: 'helpers' => 'View/Helper',
178: 'shells' => 'Console/Command',
179: 'libs' => 'Lib',
180: 'vendors' => 'Vendor',
181: 'plugins' => 'Plugin',
182: 'locales' => 'Locale'
183: );
184:
185: /**
186: * Indicates whether the class cache should be stored again because of an addition to it
187: *
188: * @var boolean
189: */
190: protected static $_cacheChange = false;
191:
192: /**
193: * Indicates whether the object cache should be stored again because of an addition to it
194: *
195: * @var boolean
196: */
197: protected static $_objectCacheChange = false;
198:
199: /**
200: * Indicates the the Application is in the bootstrapping process. Used to better cache
201: * loaded classes while the cache libraries have not been yet initialized
202: *
203: * @var boolean
204: */
205: public static $bootstrapping = false;
206:
207: /**
208: * Used to read information stored path
209: *
210: * Usage:
211: *
212: * `App::path('Model'); will return all paths for models`
213: *
214: * `App::path('Model/Datasource', 'MyPlugin'); will return the path for datasources under the 'MyPlugin' plugin`
215: *
216: * @param string $type type of path
217: * @param string $plugin name of plugin
218: * @return array
219: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::path
220: */
221: public static function path($type, $plugin = null) {
222: if (!empty(self::$legacy[$type])) {
223: $type = self::$legacy[$type];
224: }
225:
226: if (!empty($plugin)) {
227: $path = array();
228: $pluginPath = self::pluginPath($plugin);
229: $packageFormat = self::_packageFormat();
230: if (!empty($packageFormat[$type])) {
231: foreach ($packageFormat[$type] as $f) {
232: $path[] = sprintf($f, $pluginPath);
233: }
234: }
235: return $path;
236: }
237:
238: if (!isset(self::$_packages[$type])) {
239: return array();
240: }
241: return self::$_packages[$type];
242: }
243:
244: /**
245: * Get all the currently loaded paths from App. Useful for inspecting
246: * or storing all paths App knows about. For a paths to a specific package
247: * use App::path()
248: *
249: * @return array An array of packages and their associated paths.
250: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::paths
251: */
252: public static function paths() {
253: return self::$_packages;
254: }
255:
256: /**
257: * Sets up each package location on the file system. You can configure multiple search paths
258: * for each package, those will be used to look for files one folder at a time in the specified order
259: * All paths should be terminated with a Directory separator
260: *
261: * Usage:
262: *
263: * `App::build(array(Model' => array('/a/full/path/to/models/'))); will setup a new search path for the Model package`
264: *
265: * `App::build(array('Model' => array('/path/to/models/')), App::RESET); will setup the path as the only valid path for searching models`
266: *
267: * `App::build(array('View/Helper' => array('/path/to/helpers/', '/another/path/'))); will setup multiple search paths for helpers`
268: *
269: * `App::build(array('Service' => array('%s' . 'Service' . DS)), App::REGISTER); will register new package 'Service'`
270: *
271: * If reset is set to true, all loaded plugins will be forgotten and they will be needed to be loaded again.
272: *
273: * @param array $paths associative array with package names as keys and a list of directories for new search paths
274: * @param boolean|string $mode App::RESET will set paths, App::APPEND with append paths, App::PREPEND will prepend paths (default)
275: * App::REGISTER will register new packages and their paths, %s in path will be replaced by APP path
276: * @return void
277: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::build
278: */
279: public static function build($paths = array(), $mode = App::PREPEND) {
280: //Provides Backwards compatibility for old-style package names
281: $legacyPaths = array();
282: foreach ($paths as $type => $path) {
283: if (!empty(self::$legacy[$type])) {
284: $type = self::$legacy[$type];
285: }
286: $legacyPaths[$type] = $path;
287: }
288: $paths = $legacyPaths;
289:
290: if ($mode === App::RESET) {
291: foreach ($paths as $type => $new) {
292: self::$_packages[$type] = (array)$new;
293: self::objects($type, null, false);
294: }
295: return;
296: }
297:
298: if (empty($paths)) {
299: self::$_packageFormat = null;
300: }
301:
302: $packageFormat = self::_packageFormat();
303:
304: if ($mode === App::REGISTER) {
305: foreach ($paths as $package => $formats) {
306: if (empty($packageFormat[$package])) {
307: $packageFormat[$package] = $formats;
308: } else {
309: $formats = array_merge($packageFormat[$package], $formats);
310: $packageFormat[$package] = array_values(array_unique($formats));
311: }
312: }
313: self::$_packageFormat = $packageFormat;
314: }
315:
316: $defaults = array();
317: foreach ($packageFormat as $package => $format) {
318: foreach ($format as $f) {
319: $defaults[$package][] = sprintf($f, APP);
320: }
321: }
322:
323: if (empty($paths)) {
324: self::$_packages = $defaults;
325: return;
326: }
327:
328: if ($mode === App::REGISTER) {
329: $paths = array();
330: }
331:
332: foreach ($defaults as $type => $default) {
333: if (!empty(self::$_packages[$type])) {
334: $path = self::$_packages[$type];
335: } else {
336: $path = $default;
337: }
338:
339: if (!empty($paths[$type])) {
340: $newPath = (array)$paths[$type];
341:
342: if ($mode === App::PREPEND) {
343: $path = array_merge($newPath, $path);
344: } else {
345: $path = array_merge($path, $newPath);
346: }
347:
348: $path = array_values(array_unique($path));
349: }
350:
351: self::$_packages[$type] = $path;
352: }
353: }
354:
355: /**
356: * Gets the path that a plugin is on. Searches through the defined plugin paths.
357: *
358: * Usage:
359: *
360: * `App::pluginPath('MyPlugin'); will return the full path to 'MyPlugin' plugin'`
361: *
362: * @param string $plugin CamelCased/lower_cased plugin name to find the path of.
363: * @return string full path to the plugin.
364: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::pluginPath
365: */
366: public static function pluginPath($plugin) {
367: return CakePlugin::path($plugin);
368: }
369:
370: /**
371: * Finds the path that a theme is on. Searches through the defined theme paths.
372: *
373: * Usage:
374: *
375: * `App::themePath('MyTheme'); will return the full path to the 'MyTheme' theme`
376: *
377: * @param string $theme theme name to find the path of.
378: * @return string full path to the theme.
379: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::themePath
380: */
381: public static function themePath($theme) {
382: $themeDir = 'Themed' . DS . Inflector::camelize($theme);
383: foreach (self::$_packages['View'] as $path) {
384: if (is_dir($path . $themeDir)) {
385: return $path . $themeDir . DS;
386: }
387: }
388: return self::$_packages['View'][0] . $themeDir . DS;
389: }
390:
391: /**
392: * Returns the full path to a package inside the CakePHP core
393: *
394: * Usage:
395: *
396: * `App::core('Cache/Engine'); will return the full path to the cache engines package`
397: *
398: * @param string $type
399: * @return array full path to package
400: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::core
401: */
402: public static function core($type) {
403: return array(CAKE . str_replace('/', DS, $type) . DS);
404: }
405:
406: /**
407: * Returns an array of objects of the given type.
408: *
409: * Example usage:
410: *
411: * `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
412: *
413: * `App::objects('Controller');` returns `array('PagesController', 'BlogController');`
414: *
415: * You can also search only within a plugin's objects by using the plugin dot
416: * syntax.
417: *
418: * `App::objects('MyPlugin.Model');` returns `array('MyPluginPost', 'MyPluginComment');`
419: *
420: * When scanning directories, files and directories beginning with `.` will be excluded as these
421: * are commonly used by version control systems.
422: *
423: * @param string $type Type of object, i.e. 'Model', 'Controller', 'View/Helper', 'file', 'class' or 'plugin'
424: * @param string|array $path Optional Scan only the path given. If null, paths for the chosen type will be used.
425: * @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
426: * @return mixed Either false on incorrect / miss. Or an array of found objects.
427: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::objects
428: */
429: public static function objects($type, $path = null, $cache = true) {
430: if (empty(self::$_objects) && $cache === true) {
431: self::$_objects = (array)Cache::read('object_map', '_cake_core_');
432: }
433:
434: $extension = '/\.php$/';
435: $includeDirectories = false;
436: $name = $type;
437:
438: if ($type === 'plugin') {
439: $type = 'plugins';
440: }
441:
442: if ($type === 'plugins') {
443: $extension = '/.*/';
444: $includeDirectories = true;
445: }
446:
447: list($plugin, $type) = pluginSplit($type);
448:
449: if (isset(self::$legacy[$type . 's'])) {
450: $type = self::$legacy[$type . 's'];
451: }
452:
453: if ($type === 'file' && !$path) {
454: return false;
455: } elseif ($type === 'file') {
456: $extension = '/\.php$/';
457: $name = $type . str_replace(DS, '', $path);
458: }
459:
460: $cacheLocation = empty($plugin) ? 'app' : $plugin;
461:
462: if ($cache !== true || !isset(self::$_objects[$cacheLocation][$name])) {
463: $objects = array();
464:
465: if (empty($path)) {
466: $path = self::path($type, $plugin);
467: }
468:
469: foreach ((array)$path as $dir) {
470: if ($dir != APP && is_dir($dir)) {
471: $files = new RegexIterator(new DirectoryIterator($dir), $extension);
472: foreach ($files as $file) {
473: $fileName = basename($file);
474: if (!$file->isDot() && $fileName[0] !== '.') {
475: $isDir = $file->isDir();
476: if ($isDir && $includeDirectories) {
477: $objects[] = $fileName;
478: } elseif (!$includeDirectories && !$isDir) {
479: $objects[] = substr($fileName, 0, -4);
480: }
481: }
482: }
483: }
484: }
485:
486: if ($type !== 'file') {
487: foreach ($objects as $key => $value) {
488: $objects[$key] = Inflector::camelize($value);
489: }
490: }
491:
492: sort($objects);
493: if ($plugin) {
494: return $objects;
495: }
496:
497: self::$_objects[$cacheLocation][$name] = $objects;
498: if ($cache) {
499: self::$_objectCacheChange = true;
500: }
501: }
502:
503: return self::$_objects[$cacheLocation][$name];
504: }
505:
506: /**
507: * Declares a package for a class. This package location will be used
508: * by the automatic class loader if the class is tried to be used
509: *
510: * Usage:
511: *
512: * `App::uses('MyCustomController', 'Controller');` will setup the class to be found under Controller package
513: *
514: * `App::uses('MyHelper', 'MyPlugin.View/Helper');` will setup the helper class to be found in plugin's helper package
515: *
516: * @param string $className the name of the class to configure package for
517: * @param string $location the package name
518: * @return void
519: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::uses
520: */
521: public static function uses($className, $location) {
522: self::$_classMap[$className] = $location;
523: }
524:
525: /**
526: * Method to handle the automatic class loading. It will look for each class' package
527: * defined using App::uses() and with this information it will resolve the package name to a full path
528: * to load the class from. File name for each class should follow the class name. For instance,
529: * if a class is name `MyCustomClass` the file name should be `MyCustomClass.php`
530: *
531: * @param string $className the name of the class to load
532: * @return boolean
533: */
534: public static function load($className) {
535: if (!isset(self::$_classMap[$className])) {
536: return false;
537: }
538: if (strpos($className, '..') !== false) {
539: return false;
540: }
541:
542: $parts = explode('.', self::$_classMap[$className], 2);
543: list($plugin, $package) = count($parts) > 1 ? $parts : array(null, current($parts));
544:
545: $file = self::_mapped($className, $plugin);
546: if ($file) {
547: return include $file;
548: }
549: $paths = self::path($package, $plugin);
550:
551: if (empty($plugin)) {
552: $appLibs = empty(self::$_packages['Lib']) ? APPLIBS : current(self::$_packages['Lib']);
553: $paths[] = $appLibs . $package . DS;
554: $paths[] = APP . $package . DS;
555: $paths[] = CAKE . $package . DS;
556: } else {
557: $pluginPath = self::pluginPath($plugin);
558: $paths[] = $pluginPath . 'Lib' . DS . $package . DS;
559: $paths[] = $pluginPath . $package . DS;
560: }
561:
562: $normalizedClassName = str_replace('\\', DS, $className);
563: foreach ($paths as $path) {
564: $file = $path . $normalizedClassName . '.php';
565: if (file_exists($file)) {
566: self::_map($file, $className, $plugin);
567: return include $file;
568: }
569: }
570:
571: return false;
572: }
573:
574: /**
575: * Returns the package name where a class was defined to be located at
576: *
577: * @param string $className name of the class to obtain the package name from
578: * @return string package name or null if not declared
579: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::location
580: */
581: public static function location($className) {
582: if (!empty(self::$_classMap[$className])) {
583: return self::$_classMap[$className];
584: }
585: return null;
586: }
587:
588: /**
589: * Finds classes based on $name or specific file(s) to search. Calling App::import() will
590: * not construct any classes contained in the files. It will only find and require() the file.
591: *
592: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#including-files-with-app-import
593: * @param string|array $type The type of Class if passed as a string, or all params can be passed as
594: * an single array to $type,
595: * @param string $name Name of the Class or a unique name for the file
596: * @param boolean|array $parent boolean true if Class Parent should be searched, accepts key => value
597: * array('parent' => $parent, 'file' => $file, 'search' => $search, 'ext' => '$ext');
598: * $ext allows setting the extension of the file name
599: * based on Inflector::underscore($name) . ".$ext";
600: * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
601: * @param string $file full name of the file to search for including extension
602: * @param boolean $return Return the loaded file, the file must have a return
603: * statement in it to work: return $variable;
604: * @return boolean true if Class is already in memory or if file is found and loaded, false if not
605: */
606: public static function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
607: $ext = null;
608:
609: if (is_array($type)) {
610: extract($type, EXTR_OVERWRITE);
611: }
612:
613: if (is_array($parent)) {
614: extract($parent, EXTR_OVERWRITE);
615: }
616:
617: if (!$name && !$file) {
618: return false;
619: }
620:
621: if (is_array($name)) {
622: foreach ($name as $class) {
623: if (!App::import(compact('type', 'parent', 'search', 'file', 'return') + array('name' => $class))) {
624: return false;
625: }
626: }
627: return true;
628: }
629:
630: $originalType = strtolower($type);
631: $specialPackage = in_array($originalType, array('file', 'vendor'));
632: if (!$specialPackage && isset(self::$legacy[$originalType . 's'])) {
633: $type = self::$legacy[$originalType . 's'];
634: }
635: list($plugin, $name) = pluginSplit($name);
636: if (!empty($plugin)) {
637: if (!CakePlugin::loaded($plugin)) {
638: return false;
639: }
640: }
641:
642: if (!$specialPackage) {
643: return self::_loadClass($name, $plugin, $type, $originalType, $parent);
644: }
645:
646: if ($originalType === 'file' && !empty($file)) {
647: return self::_loadFile($name, $plugin, $search, $file, $return);
648: }
649:
650: if ($originalType === 'vendor') {
651: return self::_loadVendor($name, $plugin, $file, $ext);
652: }
653:
654: return false;
655: }
656:
657: /**
658: * Helper function to include classes
659: * This is a compatibility wrapper around using App::uses() and automatic class loading
660: *
661: * @param string $name unique name of the file for identifying it inside the application
662: * @param string $plugin camel cased plugin name if any
663: * @param string $type name of the packed where the class is located
664: * @param string $originalType type name as supplied initially by the user
665: * @param boolean $parent whether to load the class parent or not
666: * @return boolean true indicating the successful load and existence of the class
667: */
668: protected static function _loadClass($name, $plugin, $type, $originalType, $parent) {
669: if ($type === 'Console/Command' && $name === 'Shell') {
670: $type = 'Console';
671: } elseif (isset(self::$types[$originalType]['suffix'])) {
672: $suffix = self::$types[$originalType]['suffix'];
673: $name .= ($suffix == $name) ? '' : $suffix;
674: }
675: if ($parent && isset(self::$types[$originalType]['extends'])) {
676: $extends = self::$types[$originalType]['extends'];
677: $extendType = $type;
678: if (strpos($extends, '/') !== false) {
679: $parts = explode('/', $extends);
680: $extends = array_pop($parts);
681: $extendType = implode('/', $parts);
682: }
683: App::uses($extends, $extendType);
684: if ($plugin && in_array($originalType, array('controller', 'model'))) {
685: App::uses($plugin . $extends, $plugin . '.' . $type);
686: }
687: }
688: if ($plugin) {
689: $plugin .= '.';
690: }
691: $name = Inflector::camelize($name);
692: App::uses($name, $plugin . $type);
693: return class_exists($name);
694: }
695:
696: /**
697: * Helper function to include single files
698: *
699: * @param string $name unique name of the file for identifying it inside the application
700: * @param string $plugin camel cased plugin name if any
701: * @param array $search list of paths to search the file into
702: * @param string $file filename if known, the $name param will be used otherwise
703: * @param boolean $return whether this function should return the contents of the file after being parsed by php or just a success notice
704: * @return mixed if $return contents of the file after php parses it, boolean indicating success otherwise
705: */
706: protected static function _loadFile($name, $plugin, $search, $file, $return) {
707: $mapped = self::_mapped($name, $plugin);
708: if ($mapped) {
709: $file = $mapped;
710: } elseif (!empty($search)) {
711: foreach ($search as $path) {
712: $found = false;
713: if (file_exists($path . $file)) {
714: $file = $path . $file;
715: $found = true;
716: break;
717: }
718: if (empty($found)) {
719: $file = false;
720: }
721: }
722: }
723: if (!empty($file) && file_exists($file)) {
724: self::_map($file, $name, $plugin);
725: $returnValue = include $file;
726: if ($return) {
727: return $returnValue;
728: }
729: return (bool)$returnValue;
730: }
731: return false;
732: }
733:
734: /**
735: * Helper function to load files from vendors folders
736: *
737: * @param string $name unique name of the file for identifying it inside the application
738: * @param string $plugin camel cased plugin name if any
739: * @param string $file file name if known
740: * @param string $ext file extension if known
741: * @return boolean true if the file was loaded successfully, false otherwise
742: */
743: protected static function _loadVendor($name, $plugin, $file, $ext) {
744: if ($mapped = self::_mapped($name, $plugin)) {
745: return (bool)include_once $mapped;
746: }
747: $fileTries = array();
748: $paths = ($plugin) ? App::path('vendors', $plugin) : App::path('vendors');
749: if (empty($ext)) {
750: $ext = 'php';
751: }
752: if (empty($file)) {
753: $fileTries[] = $name . '.' . $ext;
754: $fileTries[] = Inflector::underscore($name) . '.' . $ext;
755: } else {
756: $fileTries[] = $file;
757: }
758:
759: foreach ($fileTries as $file) {
760: foreach ($paths as $path) {
761: if (file_exists($path . $file)) {
762: self::_map($path . $file, $name, $plugin);
763: return (bool)include $path . $file;
764: }
765: }
766: }
767: return false;
768: }
769:
770: /**
771: * Initializes the cache for App, registers a shutdown function.
772: *
773: * @return void
774: */
775: public static function init() {
776: self::$_map += (array)Cache::read('file_map', '_cake_core_');
777: register_shutdown_function(array('App', 'shutdown'));
778: }
779:
780: /**
781: * Maps the $name to the $file.
782: *
783: * @param string $file full path to file
784: * @param string $name unique name for this map
785: * @param string $plugin camelized if object is from a plugin, the name of the plugin
786: * @return void
787: */
788: protected static function _map($file, $name, $plugin = null) {
789: $key = $name;
790: if ($plugin) {
791: $key = 'plugin.' . $name;
792: }
793: if ($plugin && empty(self::$_map[$name])) {
794: self::$_map[$key] = $file;
795: }
796: if (!$plugin && empty(self::$_map['plugin.' . $name])) {
797: self::$_map[$key] = $file;
798: }
799: if (!self::$bootstrapping) {
800: self::$_cacheChange = true;
801: }
802: }
803:
804: /**
805: * Returns a file's complete path.
806: *
807: * @param string $name unique name
808: * @param string $plugin camelized if object is from a plugin, the name of the plugin
809: * @return mixed file path if found, false otherwise
810: */
811: protected static function _mapped($name, $plugin = null) {
812: $key = $name;
813: if ($plugin) {
814: $key = 'plugin.' . $name;
815: }
816: return isset(self::$_map[$key]) ? self::$_map[$key] : false;
817: }
818:
819: /**
820: * Sets then returns the templates for each customizable package path
821: *
822: * @return array templates for each customizable package path
823: */
824: protected static function _packageFormat() {
825: if (empty(self::$_packageFormat)) {
826: self::$_packageFormat = array(
827: 'Model' => array(
828: '%s' . 'Model' . DS
829: ),
830: 'Model/Behavior' => array(
831: '%s' . 'Model' . DS . 'Behavior' . DS
832: ),
833: 'Model/Datasource' => array(
834: '%s' . 'Model' . DS . 'Datasource' . DS
835: ),
836: 'Model/Datasource/Database' => array(
837: '%s' . 'Model' . DS . 'Datasource' . DS . 'Database' . DS
838: ),
839: 'Model/Datasource/Session' => array(
840: '%s' . 'Model' . DS . 'Datasource' . DS . 'Session' . DS
841: ),
842: 'Controller' => array(
843: '%s' . 'Controller' . DS
844: ),
845: 'Controller/Component' => array(
846: '%s' . 'Controller' . DS . 'Component' . DS
847: ),
848: 'Controller/Component/Auth' => array(
849: '%s' . 'Controller' . DS . 'Component' . DS . 'Auth' . DS
850: ),
851: 'Controller/Component/Acl' => array(
852: '%s' . 'Controller' . DS . 'Component' . DS . 'Acl' . DS
853: ),
854: 'View' => array(
855: '%s' . 'View' . DS
856: ),
857: 'View/Helper' => array(
858: '%s' . 'View' . DS . 'Helper' . DS
859: ),
860: 'Console' => array(
861: '%s' . 'Console' . DS
862: ),
863: 'Console/Command' => array(
864: '%s' . 'Console' . DS . 'Command' . DS
865: ),
866: 'Console/Command/Task' => array(
867: '%s' . 'Console' . DS . 'Command' . DS . 'Task' . DS
868: ),
869: 'Lib' => array(
870: '%s' . 'Lib' . DS
871: ),
872: 'Locale' => array(
873: '%s' . 'Locale' . DS
874: ),
875: 'Vendor' => array(
876: '%s' . 'Vendor' . DS,
877: dirname(dirname(CAKE)) . DS . 'vendors' . DS,
878: ),
879: 'Plugin' => array(
880: APP . 'Plugin' . DS,
881: dirname(dirname(CAKE)) . DS . 'plugins' . DS
882: )
883: );
884: }
885:
886: return self::$_packageFormat;
887: }
888:
889: /**
890: * Object destructor.
891: *
892: * Writes cache file if changes have been made to the $_map. Also, check if a fatal
893: * error happened and call the handler.
894: *
895: * @return void
896: */
897: public static function shutdown() {
898: if (self::$_cacheChange) {
899: Cache::write('file_map', array_filter(self::$_map), '_cake_core_');
900: }
901: if (self::$_objectCacheChange) {
902: Cache::write('object_map', self::$_objects, '_cake_core_');
903: }
904: self::_checkFatalError();
905: }
906:
907: /**
908: * Check if a fatal error happened and trigger the configured handler if configured
909: *
910: * @return void
911: */
912: protected static function _checkFatalError() {
913: $lastError = error_get_last();
914: if (!is_array($lastError)) {
915: return;
916: }
917:
918: list(, $log) = ErrorHandler::mapErrorCode($lastError['type']);
919: if ($log !== LOG_ERR) {
920: return;
921: }
922:
923: if (PHP_SAPI === 'cli') {
924: $errorHandler = Configure::read('Error.consoleHandler');
925: } else {
926: $errorHandler = Configure::read('Error.handler');
927: }
928: if (!is_callable($errorHandler)) {
929: return;
930: }
931: call_user_func($errorHandler, $lastError['type'], $lastError['message'], $lastError['file'], $lastError['line'], array());
932: }
933:
934: }
935: