CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.4 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.4
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • App
  • CakePlugin
  • Configure
  • Object

Interfaces

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

Generated using CakePHP API Docs