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.2 API

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