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

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 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
    • Network
      • Email
      • Http
    • Routing
      • 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 mixed $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 mixed $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: 
536:         $parts = explode('.', self::$_classMap[$className], 2);
537:         list($plugin, $package) = count($parts) > 1 ? $parts : array(null, current($parts));
538: 
539:         if ($file = self::_mapped($className, $plugin)) {
540:             return include $file;
541:         }
542:         $paths = self::path($package, $plugin);
543: 
544:         if (empty($plugin)) {
545:             $appLibs = empty(self::$_packages['Lib']) ? APPLIBS : current(self::$_packages['Lib']);
546:             $paths[] = $appLibs . $package . DS;
547:             $paths[] = APP . $package . DS;
548:             $paths[] = CAKE . $package . DS;
549:         } else {
550:             $pluginPath = self::pluginPath($plugin);
551:             $paths[] = $pluginPath . 'Lib' . DS . $package . DS;
552:             $paths[] = $pluginPath . $package . DS;
553:         }
554:         foreach ($paths as $path) {
555:             $file = $path . $className . '.php';
556:             if (file_exists($file)) {
557:                 self::_map($file, $className, $plugin);
558:                 return include $file;
559:             }
560:         }
561: 
562:         return false;
563:     }
564: 
565: /**
566:  * Returns the package name where a class was defined to be located at
567:  *
568:  * @param string $className name of the class to obtain the package name from
569:  * @return string package name or null if not declared
570:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::location
571:  */
572:     public static function location($className) {
573:         if (!empty(self::$_classMap[$className])) {
574:             return self::$_classMap[$className];
575:         }
576:         return null;
577:     }
578: 
579: /**
580:  * Finds classes based on $name or specific file(s) to search.  Calling App::import() will
581:  * not construct any classes contained in the files. It will only find and require() the file.
582:  *
583:  * @link          http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#including-files-with-app-import
584:  * @param mixed $type The type of Class if passed as a string, or all params can be passed as
585:  *                    an single array to $type,
586:  * @param string $name Name of the Class or a unique name for the file
587:  * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
588:  *              array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
589:  *              $ext allows setting the extension of the file name
590:  *              based on Inflector::underscore($name) . ".$ext";
591:  * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
592:  * @param string $file full name of the file to search for including extension
593:  * @param boolean $return Return the loaded file, the file must have a return
594:  *                         statement in it to work: return $variable;
595:  * @return boolean true if Class is already in memory or if file is found and loaded, false if not
596:  */
597:     public static function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
598:         $ext = null;
599: 
600:         if (is_array($type)) {
601:             extract($type, EXTR_OVERWRITE);
602:         }
603: 
604:         if (is_array($parent)) {
605:             extract($parent, EXTR_OVERWRITE);
606:         }
607: 
608:         if ($name == null && $file == null) {
609:             return false;
610:         }
611: 
612:         if (is_array($name)) {
613:             foreach ($name as $class) {
614:                 if (!App::import(compact('type', 'parent', 'search', 'file', 'return') + array('name' => $class))) {
615:                     return false;
616:                 }
617:             }
618:             return true;
619:         }
620: 
621:         $originalType = strtolower($type);
622:         $specialPackage = in_array($originalType, array('file', 'vendor'));
623:         if (!$specialPackage && isset(self::$legacy[$originalType . 's'])) {
624:             $type = self::$legacy[$originalType . 's'];
625:         }
626:         list($plugin, $name) = pluginSplit($name);
627:         if (!empty($plugin)) {
628:             if (!CakePlugin::loaded($plugin)) {
629:                 return false;
630:             }
631:         }
632: 
633:         if (!$specialPackage) {
634:             return self::_loadClass($name, $plugin, $type, $originalType, $parent);
635:         }
636: 
637:         if ($originalType == 'file' && !empty($file)) {
638:             return self::_loadFile($name, $plugin, $search, $file, $return);
639:         }
640: 
641:         if ($originalType == 'vendor') {
642:             return self::_loadVendor($name, $plugin, $file, $ext);
643:         }
644: 
645:         return false;
646:     }
647: 
648: /**
649:  * Helper function to include classes
650:  * This is a compatibility wrapper around using App::uses() and automatic class loading
651:  *
652:  * @param string $name unique name of the file for identifying it inside the application
653:  * @param string $plugin camel cased plugin name if any
654:  * @param string $type name of the packed where the class is located
655:  * @param string $originalType type name as supplied initially by the user
656:  * @param boolean $parent whether to load the class parent or not
657:  * @return boolean true indicating the successful load and existence of the class
658:  */
659:     protected static function _loadClass($name, $plugin, $type, $originalType, $parent) {
660:         if ($type == 'Console/Command' && $name == 'Shell') {
661:             $type = 'Console';
662:         } elseif (isset(self::$types[$originalType]['suffix'])) {
663:             $suffix = self::$types[$originalType]['suffix'];
664:             $name .= ($suffix == $name) ? '' : $suffix;
665:         }
666:         if ($parent && isset(self::$types[$originalType]['extends'])) {
667:             $extends = self::$types[$originalType]['extends'];
668:             $extendType = $type;
669:             if (strpos($extends, '/') !== false) {
670:                 $parts = explode('/', $extends);
671:                 $extends = array_pop($parts);
672:                 $extendType = implode('/', $parts);
673:             }
674:             App::uses($extends, $extendType);
675:             if ($plugin && in_array($originalType, array('controller', 'model'))) {
676:                 App::uses($plugin . $extends, $plugin . '.' . $type);
677:             }
678:         }
679:         if ($plugin) {
680:             $plugin .= '.';
681:         }
682:         $name = Inflector::camelize($name);
683:         App::uses($name, $plugin . $type);
684:         return class_exists($name);
685:     }
686: 
687: /**
688:  * Helper function to include single files
689:  *
690:  * @param string $name unique name of the file for identifying it inside the application
691:  * @param string $plugin camel cased plugin name if any
692:  * @param array $search list of paths to search the file into
693:  * @param string $file filename if known, the $name param will be used otherwise
694:  * @param boolean $return whether this function should return the contents of the file after being parsed by php or just a success notice
695:  * @return mixed if $return contents of the file after php parses it, boolean indicating success otherwise
696:  */
697:     protected static function _loadFile($name, $plugin, $search, $file, $return) {
698:         $mapped = self::_mapped($name, $plugin);
699:         if ($mapped) {
700:             $file = $mapped;
701:         } elseif (!empty($search)) {
702:             foreach ($search as $path) {
703:                 $found = false;
704:                 if (file_exists($path . $file)) {
705:                     $file = $path . $file;
706:                     $found = true;
707:                     break;
708:                 }
709:                 if (empty($found)) {
710:                     $file = false;
711:                 }
712:             }
713:         }
714:         if (!empty($file) && file_exists($file)) {
715:             self::_map($file, $name, $plugin);
716:             $returnValue = include $file;
717:             if ($return) {
718:                 return $returnValue;
719:             }
720:             return (bool)$returnValue;
721:         }
722:         return false;
723:     }
724: 
725: /**
726:  * Helper function to load files from vendors folders
727:  *
728:  * @param string $name unique name of the file for identifying it inside the application
729:  * @param string $plugin camel cased plugin name if any
730:  * @param string $file file name if known
731:  * @param string $ext file extension if known
732:  * @return boolean true if the file was loaded successfully, false otherwise
733:  */
734:     protected static function _loadVendor($name, $plugin, $file, $ext) {
735:         if ($mapped = self::_mapped($name, $plugin)) {
736:             return (bool)include_once $mapped;
737:         }
738:         $fileTries = array();
739:         $paths = ($plugin) ? App::path('vendors', $plugin) : App::path('vendors');
740:         if (empty($ext)) {
741:             $ext = 'php';
742:         }
743:         if (empty($file)) {
744:             $fileTries[] = $name . '.' . $ext;
745:             $fileTries[] = Inflector::underscore($name) . '.' . $ext;
746:         } else {
747:             $fileTries[] = $file;
748:         }
749: 
750:         foreach ($fileTries as $file) {
751:             foreach ($paths as $path) {
752:                 if (file_exists($path . $file)) {
753:                     self::_map($path . $file, $name, $plugin);
754:                     return (bool)include $path . $file;
755:                 }
756:             }
757:         }
758:         return false;
759:     }
760: 
761: /**
762:  * Initializes the cache for App, registers a shutdown function.
763:  *
764:  * @return void
765:  */
766:     public static function init() {
767:         self::$_map += (array)Cache::read('file_map', '_cake_core_');
768:         self::$_objects += (array)Cache::read('object_map', '_cake_core_');
769:         register_shutdown_function(array('App', 'shutdown'));
770:     }
771: 
772: /**
773:  * Maps the $name to the $file.
774:  *
775:  * @param string $file full path to file
776:  * @param string $name unique name for this map
777:  * @param string $plugin camelized if object is from a plugin, the name of the plugin
778:  * @return void
779:  */
780:     protected static function _map($file, $name, $plugin = null) {
781:         $key = $name;
782:         if ($plugin) {
783:             $key = 'plugin.' . $name;
784:         }
785:         if ($plugin && empty(self::$_map[$name])) {
786:             self::$_map[$key] = $file;
787:         }
788:         if (!$plugin && empty(self::$_map['plugin.' . $name])) {
789:             self::$_map[$key] = $file;
790:         }
791:         if (!self::$bootstrapping) {
792:             self::$_cacheChange = true;
793:         }
794:     }
795: 
796: /**
797:  * Returns a file's complete path.
798:  *
799:  * @param string $name unique name
800:  * @param string $plugin camelized if object is from a plugin, the name of the plugin
801:  * @return mixed file path if found, false otherwise
802:  */
803:     protected static function _mapped($name, $plugin = null) {
804:         $key = $name;
805:         if ($plugin) {
806:             $key = 'plugin.' . $name;
807:         }
808:         return isset(self::$_map[$key]) ? self::$_map[$key] : false;
809:     }
810: 
811: /**
812:  * Sets then returns the templates for each customizable package path
813:  *
814:  * @return array templates for each customizable package path
815:  */
816:     protected static function _packageFormat() {
817:         if (empty(self::$_packageFormat)) {
818:             self::$_packageFormat = array(
819:                 'Model' => array(
820:                     '%s' . 'Model' . DS
821:                 ),
822:                 'Model/Behavior' => array(
823:                     '%s' . 'Model' . DS . 'Behavior' . DS
824:                 ),
825:                 'Model/Datasource' => array(
826:                     '%s' . 'Model' . DS . 'Datasource' . DS
827:                 ),
828:                 'Model/Datasource/Database' => array(
829:                     '%s' . 'Model' . DS . 'Datasource' . DS . 'Database' . DS
830:                 ),
831:                 'Model/Datasource/Session' => array(
832:                     '%s' . 'Model' . DS . 'Datasource' . DS . 'Session' . DS
833:                 ),
834:                 'Controller' => array(
835:                     '%s' . 'Controller' . DS
836:                 ),
837:                 'Controller/Component' => array(
838:                     '%s' . 'Controller' . DS . 'Component' . DS
839:                 ),
840:                 'Controller/Component/Auth' => array(
841:                     '%s' . 'Controller' . DS . 'Component' . DS . 'Auth' . DS
842:                 ),
843:                 'Controller/Component/Acl' => array(
844:                     '%s' . 'Controller' . DS . 'Component' . DS . 'Acl' . DS
845:                 ),
846:                 'View' => array(
847:                     '%s' . 'View' . DS
848:                 ),
849:                 'View/Helper' => array(
850:                     '%s' . 'View' . DS . 'Helper' . DS
851:                 ),
852:                 'Console' => array(
853:                     '%s' . 'Console' . DS
854:                 ),
855:                 'Console/Command' => array(
856:                     '%s' . 'Console' . DS . 'Command' . DS
857:                 ),
858:                 'Console/Command/Task' => array(
859:                     '%s' . 'Console' . DS . 'Command' . DS . 'Task' . DS
860:                 ),
861:                 'Lib' => array(
862:                     '%s' . 'Lib' . DS
863:                 ),
864:                 'Locale' => array(
865:                     '%s' . 'Locale' . DS
866:                 ),
867:                 'Vendor' => array(
868:                     '%s' . 'Vendor' . DS,
869:                     dirname(dirname(CAKE)) . DS . 'vendors' . DS,
870:                 ),
871:                 'Plugin' => array(
872:                     APP . 'Plugin' . DS,
873:                     dirname(dirname(CAKE)) . DS . 'plugins' . DS
874:                 )
875:             );
876:         }
877: 
878:         return self::$_packageFormat;
879:     }
880: 
881: /**
882:  * Object destructor.
883:  *
884:  * Writes cache file if changes have been made to the $_map
885:  *
886:  * @return void
887:  */
888:     public static function shutdown() {
889:         if (self::$_cacheChange) {
890:             Cache::write('file_map', array_filter(self::$_map), '_cake_core_');
891:         }
892:         if (self::$_objectCacheChange) {
893:             Cache::write('object_map', self::$_objects, '_cake_core_');
894:         }
895:     }
896: 
897: }
898: 
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