3 * App and Configure classes
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10 * Licensed under The MIT License
11 * Redistributions of files must retain the above copyright notice.
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://cakephp.org CakePHP(tm) Project
16 * @subpackage cake.cake.libs
17 * @since CakePHP(tm) v 1.0.0.2363
18 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
22 * Configuration class (singleton). Used for managing runtime configuration information.
25 * @subpackage cake.cake.libs
26 * @link http://book.cakephp.org/view/924/The-Configuration-Class
28 class Configure
extends Object {
31 * Current debug level.
33 * @link http://book.cakephp.org/view/931/CakePHP-Core-Configuration-Variables
40 * Returns a singleton instance of the Configure class.
42 * @return Configure instance
45 function &getInstance($boot = true) {
46 static $instance = array();
48 if (!class_exists('Set')) {
49 require LIBS
. 'set.php';
51 $instance[0] =& new Configure();
52 $instance[0]->__loadBootstrap($boot);
58 * Used to store a dynamic variable in the Configure instance.
62 * Configure::write('One.key1', 'value of the Configure::One[key1]');
63 * Configure::write(array('One.key1' => 'value of the Configure::One[key1]'));
64 * Configure::write('One', array(
65 * 'key1' => 'value of the Configure::One[key1]',
66 * 'key2' => 'value of the Configure::One[key2]'
69 * Configure::write(array(
70 * 'One.key1' => 'value of the Configure::One[key1]',
71 * 'One.key2' => 'value of the Configure::One[key2]'
75 * @link http://book.cakephp.org/view/926/write
76 * @param array $config Name of var to write
77 * @param mixed $value Value to set for var
78 * @return boolean True if write was successful
81 function write($config, $value = null) {
82 $_this =& Configure
::getInstance();
84 if (!is_array($config)) {
85 $config = array($config => $value);
88 foreach ($config as $name => $value) {
89 if (strpos($name, '.') === false) {
90 $_this->{$name} = $value;
92 $names = explode('.', $name, 4);
93 switch (count($names)) {
95 $_this->{$names[0]}[$names[1]] = $value;
98 $_this->{$names[0]}[$names[1]][$names[2]] = $value;
101 $names = explode('.', $name, 2);
102 if (!isset($_this->{$names[0]})) {
103 $_this->{$names[0]} = array();
105 $_this->{$names[0]} = Set
::insert($_this->{$names[0]}, $names[1], $value);
111 if (isset($config['debug']) ||
isset($config['log'])) {
114 if (!class_exists('Debugger')) {
115 require LIBS
. 'debugger.php';
117 $reporting = E_ALL
& ~E_DEPRECATED
;
118 if (function_exists('ini_set')) {
119 ini_set('display_errors', 1);
121 } elseif (function_exists('ini_set')) {
122 ini_set('display_errors', 0);
125 if (isset($_this->log
) && $_this->log
) {
126 if (!class_exists('CakeLog')) {
127 require LIBS
. 'cake_log.php';
129 if (is_integer($_this->log
) && !$_this->debug
) {
130 $reporting = $_this->log
;
132 $reporting = E_ALL
& ~E_DEPRECATED
;
135 error_reporting($reporting);
141 * Used to read information stored in the Configure instance.
145 * Configure::read('Name'); will return all values for Name
146 * Configure::read('Name.key'); will return only the value of Configure::Name[key]
149 * @link http://book.cakephp.org/view/927/read
150 * @param string $var Variable to obtain. Use '.' to access array elements.
151 * @return string value of Configure::$var
154 function read($var = 'debug') {
155 $_this =& Configure
::getInstance();
157 if ($var === 'debug') {
158 return $_this->debug
;
161 if (strpos($var, '.') !== false) {
162 $names = explode('.', $var, 3);
165 if (!isset($_this->{$var})) {
168 if (!isset($names[1])) {
169 return $_this->{$var};
171 switch (count($names)) {
173 if (isset($_this->{$var}[$names[1]])) {
174 return $_this->{$var}[$names[1]];
178 if (isset($_this->{$var}[$names[1]][$names[2]])) {
179 return $_this->{$var}[$names[1]][$names[2]];
181 if (!isset($_this->{$var}[$names[1]])) {
184 return Set
::classicExtract($_this->{$var}[$names[1]], $names[2]);
191 * Used to delete a variable from the Configure instance.
195 * Configure::delete('Name'); will delete the entire Configure::Name
196 * Configure::delete('Name.key'); will delete only the Configure::Name[key]
199 * @link http://book.cakephp.org/view/928/delete
200 * @param string $var the var to be deleted
204 function delete($var = null) {
205 $_this =& Configure
::getInstance();
207 if (strpos($var, '.') === false) {
208 unset($_this->{$var});
212 $names = explode('.', $var, 2);
213 $_this->{$names[0]} = Set
::remove($_this->{$names[0]}, $names[1]);
217 * Loads a file from app/config/configure_file.php.
218 * Config file variables should be formated like:
219 * `$config['name'] = 'value';`
220 * These will be used to create dynamic Configure vars. load() is also used to
221 * load stored config files created with Configure::store()
223 * - To load config files from app/config use `Configure::load('configure_file');`.
224 * - To load config files from a plugin `Configure::load('plugin.configure_file');`.
226 * @link http://book.cakephp.org/view/929/load
227 * @param string $fileName name of file to load, extension must be .php and only the name
228 * should be used, not the extenstion
229 * @return mixed false if file not found, void if load successful
232 function load($fileName) {
233 $found = $plugin = $pluginPath = false;
234 list($plugin, $fileName) = pluginSplit($fileName);
236 $pluginPath = App
::pluginPath($plugin);
238 $pos = strpos($fileName, '..');
240 if ($pos === false) {
241 if ($pluginPath && file_exists($pluginPath . 'config' . DS
. $fileName . '.php')) {
242 include($pluginPath . 'config' . DS
. $fileName . '.php');
244 } elseif (file_exists(CONFIGS
. $fileName . '.php')) {
245 include(CONFIGS
. $fileName . '.php');
247 } elseif (file_exists(CACHE
. 'persistent' . DS
. $fileName . '.php')) {
248 include(CACHE
. 'persistent' . DS
. $fileName . '.php');
251 foreach (App
::core('cake') as $key => $path) {
252 if (file_exists($path . DS
. 'config' . DS
. $fileName . '.php')) {
253 include($path . DS
. 'config' . DS
. $fileName . '.php');
265 if (!isset($config)) {
266 trigger_error(sprintf(__('Configure::load() - no variable $config found in %s.php', true), $fileName), E_USER_WARNING
);
269 return Configure
::write($config);
273 * Used to determine the current version of CakePHP.
275 * Usage `Configure::version();`
277 * @link http://book.cakephp.org/view/930/version
278 * @return string Current version of CakePHP
282 $_this =& Configure
::getInstance();
284 if (!isset($_this->Cake
['version'])) {
285 require(CORE_PATH
. 'cake' . DS
. 'config' . DS
. 'config.php');
286 $_this->write($config);
288 return $_this->Cake
['version'];
292 * Used to write a config file to disk.
295 * Configure::store('Model', 'class_paths', array('Users' => array(
296 * 'path' => 'users', 'plugin' => true
300 * @param string $type Type of config file to write, ex: Models, Controllers, Helpers, Components
301 * @param string $name file name.
302 * @param array $data array of values to store.
306 function store($type, $name, $data = array()) {
310 foreach ($data as $key => $value) {
311 $content .= "\$config['$type']['$key'] = " . var_export($value, true) . ";\n";
313 if (is_null($type)) {
316 Configure
::__writeConfig($content, $name, $write);
320 * Creates a cached version of a configuration file.
321 * Appends values passed from Configure::store() to the cached file
323 * @param string $content Content to write on file
324 * @param string $name Name to use for cache file
325 * @param boolean $write true if content should be written, false otherwise
329 function __writeConfig($content, $name, $write = true) {
330 $file = CACHE
. 'persistent' . DS
. $name . '.php';
332 if (Configure
::read() > 0) {
333 $expires = "+10 seconds";
335 $expires = "+999 days";
337 $cache = cache('persistent' . DS
. $name . '.php', null, $expires);
339 if ($cache === null) {
340 cache('persistent' . DS
. $name . '.php', "<?php\n\$config = array();\n", $expires);
343 if ($write === true) {
344 if (!class_exists('File')) {
345 require LIBS
. 'file.php';
347 $fileClass = new File($file);
349 if ($fileClass->writable()) {
350 $fileClass->append($content);
357 * @see App::objects()
359 function listObjects($type, $path = null, $cache = true) {
360 return App
::objects($type, $path, $cache);
367 function corePaths($type = null) {
368 return App
::core($type);
375 function buildPaths($paths) {
376 return App
::build($paths);
380 * Loads app/config/bootstrap.php.
381 * If the alternative paths are set in this file
382 * they will be added to the paths vars.
384 * @param boolean $boot Load application bootstrap (if true)
388 function __loadBootstrap($boot) {
390 Configure
::write('App', array('base' => false, 'baseUrl' => false, 'dir' => APP_DIR
, 'webroot' => WEBROOT_DIR
, 'www_root' => WWW_ROOT
));
392 if (!include(CONFIGS
. 'core.php')) {
393 trigger_error(sprintf(__("Can't find application core file. Please create %score.php, and make sure it is readable by PHP.", true), CONFIGS
), E_USER_ERROR
);
396 if (Configure
::read('Cache.disable') !== true) {
397 $cache = Cache
::config('default');
399 if (empty($cache['settings'])) {
400 trigger_error(__('Cache not configured properly. Please check Cache::config(); in APP/config/core.php', true), E_USER_WARNING
);
401 $cache = Cache
::config('default', array('engine' => 'File'));
403 $path = $prefix = $duration = null;
405 if (!empty($cache['settings']['path'])) {
406 $path = realpath($cache['settings']['path']);
408 $prefix = $cache['settings']['prefix'];
411 if (Configure
::read() >= 1) {
412 $duration = '+10 seconds';
414 $duration = '+999 days';
417 if (Cache
::config('_cake_core_') === false) {
418 Cache
::config('_cake_core_', array_merge((array)$cache['settings'], array(
419 'prefix' => $prefix . 'cake_core_', 'path' => $path . DS
. 'persistent' . DS
,
420 'serialize' => true, 'duration' => $duration
424 if (Cache
::config('_cake_model_') === false) {
425 Cache
::config('_cake_model_', array_merge((array)$cache['settings'], array(
426 'prefix' => $prefix . 'cake_model_', 'path' => $path . DS
. 'models' . DS
,
427 'serialize' => true, 'duration' => $duration
430 Cache
::config('default');
433 if (!include(CONFIGS
. 'bootstrap.php')) {
434 trigger_error(sprintf(__("Can't find application bootstrap file. Please create %sbootstrap.php, and make sure it is readable by PHP.", true), CONFIGS
), E_USER_ERROR
);
441 * Class/file loader and path management.
443 * @link http://book.cakephp.org/view/933/The-App-Class
444 * @since CakePHP(tm) v 1.2.0.6001
446 * @subpackage cake.cake.libs
448 class App
extends Object {
451 * List of object types and their properties
457 'class' => array('suffix' => '.php', 'extends' => null, 'core' => true),
458 'file' => array('suffix' => '.php', 'extends' => null, 'core' => true),
459 'model' => array('suffix' => '.php', 'extends' => 'AppModel', 'core' => false),
460 'behavior' => array('suffix' => '.php', 'extends' => 'ModelBehavior', 'core' => true),
461 'controller' => array('suffix' => '_controller.php', 'extends' => 'AppController', 'core' => true),
462 'component' => array('suffix' => '.php', 'extends' => null, 'core' => true),
463 'lib' => array('suffix' => '.php', 'extends' => null, 'core' => true),
464 'view' => array('suffix' => '.php', 'extends' => null, 'core' => true),
465 'helper' => array('suffix' => '.php', 'extends' => 'AppHelper', 'core' => true),
466 'vendor' => array('suffix' => '', 'extends' => null, 'core' => true),
467 'shell' => array('suffix' => '.php', 'extends' => 'Shell', 'core' => true),
468 'plugin' => array('suffix' => '', 'extends' => null, 'core' => true)
472 * List of additional path(s) where model files reside.
477 var $models = array();
480 * List of additional path(s) where behavior files reside.
485 var $behaviors = array();
488 * List of additional path(s) where controller files reside.
493 var $controllers = array();
496 * List of additional path(s) where component files reside.
501 var $components = array();
504 * List of additional path(s) where datasource files reside.
509 var $datasources = array();
512 * List of additional path(s) where libs files reside.
519 * List of additional path(s) where view files reside.
524 var $views = array();
527 * List of additional path(s) where helper files reside.
532 var $helpers = array();
535 * List of additional path(s) where plugins reside.
540 var $plugins = array();
543 * List of additional path(s) where vendor packages reside.
548 var $vendors = array();
551 * List of additional path(s) where locale files reside.
556 var $locales = array();
559 * List of additional path(s) where console shell files reside.
564 var $shells = array();
567 * Paths to search for files.
572 var $search = array();
575 * Whether or not to return the file that is loaded.
583 * Holds key/value pairs of $type => file path.
588 var $__map = array();
591 * Holds paths for deep searching of files.
596 var $__paths = array();
599 * Holds loaded files.
604 var $__loaded = array();
607 * Holds and key => value array of object types.
612 var $__objects = array();
615 * Used to read information stored path
619 * `App::path('models'); will return all paths for models`
621 * @param string $type type of path
622 * @return string array
625 function path($type) {
626 $_this =& App
::getInstance();
627 if (!isset($_this->{$type})) {
630 return $_this->{$type};
634 * Build path references. Merges the supplied $paths
635 * with the base paths and the default core paths.
637 * @param array $paths paths defines in config/bootstrap.php
638 * @param boolean $reset true will set paths, false merges paths [default] false
642 function build($paths = array(), $reset = false) {
643 $_this =& App
::getInstance();
645 'models' => array(MODELS
),
646 'behaviors' => array(BEHAVIORS
),
647 'datasources' => array(MODELS
. 'datasources'),
648 'controllers' => array(CONTROLLERS
),
649 'components' => array(COMPONENTS
),
650 'libs' => array(APPLIBS
),
651 'views' => array(VIEWS
),
652 'helpers' => array(HELPERS
),
653 'locales' => array(APP
. 'locale' . DS
),
654 'shells' => array(APP
. 'vendors' . DS
. 'shells' . DS
, VENDORS
. 'shells' . DS
),
655 'vendors' => array(APP
. 'vendors' . DS
, VENDORS
),
656 'plugins' => array(APP
. 'plugins' . DS
)
659 if ($reset == true) {
660 foreach ($paths as $type => $new) {
661 $_this->{$type} = (array)$new;
666 $core = $_this->core();
667 $app = array('models' => true, 'controllers' => true, 'helpers' => true);
669 foreach ($defaults as $type => $default) {
672 if (isset($app[$type])) {
675 if (isset($core[$type])) {
676 $merge = array_merge($merge, (array)$core[$type]);
679 if (empty($_this->{$type}) ||
empty($paths)) {
680 $_this->{$type} = $default;
683 if (!empty($paths[$type])) {
684 $path = array_flip(array_flip(array_merge(
685 (array)$paths[$type], $_this->{$type}, $merge
687 $_this->{$type} = array_values($path);
689 $path = array_flip(array_flip(array_merge($_this->{$type}, $merge)));
690 $_this->{$type} = array_values($path);
696 * Get the path that a plugin is on. Searches through the defined plugin paths.
698 * @param string $plugin CamelCased/lower_cased plugin name to find the path of.
699 * @return string full path to the plugin.
701 function pluginPath($plugin) {
702 $_this =& App
::getInstance();
703 $pluginDir = Inflector
::underscore($plugin);
704 for ($i = 0, $length = count($_this->plugins
); $i < $length; $i++
) {
705 if (is_dir($_this->plugins
[$i] . $pluginDir)) {
706 return $_this->plugins
[$i] . $pluginDir . DS
;
709 return $_this->plugins
[0] . $pluginDir . DS
;
713 * Find the path that a theme is on. Search through the defined theme paths.
715 * @param string $theme lower_cased theme name to find the path of.
716 * @return string full path to the theme.
718 function themePath($theme) {
719 $_this =& App
::getInstance();
720 $themeDir = 'themed' . DS
. Inflector
::underscore($theme);
721 for ($i = 0, $length = count($_this->views
); $i < $length; $i++
) {
722 if (is_dir($_this->views
[$i] . $themeDir)) {
723 return $_this->views
[$i] . $themeDir . DS
;
726 return $_this->views
[0] . $themeDir . DS
;
730 * Returns a key/value list of all paths where core libs are found.
731 * Passing $type only returns the values for a given value of $key.
733 * @param string $type valid values are: 'model', 'behavior', 'controller', 'component',
734 * 'view', 'helper', 'datasource', 'libs', and 'cake'
735 * @return array numeric keyed array of core lib paths
738 function core($type = null) {
739 static $paths = false;
740 if ($paths === false) {
741 $paths = Cache
::read('core_paths', '_cake_core_');
745 $libs = dirname(__FILE__
) . DS
;
746 $cake = dirname($libs) . DS
;
747 $path = dirname($cake) . DS
;
749 $paths['cake'][] = $cake;
750 $paths['libs'][] = $libs;
751 $paths['models'][] = $libs . 'model' . DS
;
752 $paths['datasources'][] = $libs . 'model' . DS
. 'datasources' . DS
;
753 $paths['behaviors'][] = $libs . 'model' . DS
. 'behaviors' . DS
;
754 $paths['controllers'][] = $libs . 'controller' . DS
;
755 $paths['components'][] = $libs . 'controller' . DS
. 'components' . DS
;
756 $paths['views'][] = $libs . 'view' . DS
;
757 $paths['helpers'][] = $libs . 'view' . DS
. 'helpers' . DS
;
758 $paths['plugins'][] = $path . 'plugins' . DS
;
759 $paths['vendors'][] = $path . 'vendors' . DS
;
760 $paths['shells'][] = $cake . 'console' . DS
. 'libs' . DS
;
762 Cache
::write('core_paths', array_filter($paths), '_cake_core_');
764 if ($type && isset($paths[$type])) {
765 return $paths[$type];
771 * Returns an array of objects of the given type.
775 * `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
777 * @param string $type Type of object, i.e. 'model', 'controller', 'helper', or 'plugin'
778 * @param mixed $path Optional Scan only the path given. If null, paths for the chosen
780 * @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
781 * @return mixed Either false on incorrect / miss. Or an array of found objects.
784 function objects($type, $path = null, $cache = true) {
789 if ($type === 'file' && !$path) {
791 } elseif ($type === 'file') {
793 $name = $type . str_replace(DS
, '', $path);
795 $_this =& App
::getInstance();
797 if (empty($_this->__objects
) && $cache === true) {
798 $_this->__objects
= Cache
::read('object_map', '_cake_core_');
801 if (!isset($_this->__objects
[$name]) ||
$cache !== true) {
802 $types = $_this->types
;
804 if (!isset($types[$type])) {
810 $path = $_this->{"{$type}s"};
811 if (isset($types[$type]['core']) && $types[$type]['core'] === false) {
817 foreach ((array)$path as $dir) {
819 $items = $_this->__list($dir, $types[$type]['suffix'], $extension);
820 $objects = array_merge($items, array_diff($objects, $items));
824 if ($type !== 'file') {
825 foreach ($objects as $key => $value) {
826 $objects[$key] = Inflector
::camelize($value);
830 if ($cache === true) {
831 $_this->__resetCache(true);
833 $_this->__objects
[$name] = $objects;
836 return $_this->__objects
[$name];
840 * Finds classes based on $name or specific file(s) to search. Calling App::import() will
841 * not construct any classes contained in the files. It will only find and require() the file.
843 * @link http://book.cakephp.org/view/934/Using-App-import
844 * @param mixed $type The type of Class if passed as a string, or all params can be passed as
845 * an single array to $type,
846 * @param string $name Name of the Class or a unique name for the file
847 * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
848 * array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
849 * $ext allows setting the extension of the file name
850 * based on Inflector::underscore($name) . ".$ext";
851 * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
852 * @param string $file full name of the file to search for including extension
853 * @param boolean $return, return the loaded file, the file must have a return
854 * statement in it to work: return $variable;
855 * @return boolean true if Class is already in memory or if file is found and loaded, false if not
858 function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
859 $plugin = $directory = null;
861 if (is_array($type)) {
862 extract($type, EXTR_OVERWRITE
);
865 if (is_array($parent)) {
866 extract($parent, EXTR_OVERWRITE
);
869 if ($name === null && $file === null) {
872 } elseif ($name === null) {
876 if (is_array($name)) {
877 foreach ($name as $class) {
881 if (strpos($class, '.') !== false) {
882 $value = explode('.', $class);
883 $count = count($value);
886 $tempType = $value[0];
887 $plugin = $value[1] . '.';
889 } elseif ($count === 2 && ($type === 'Core' ||
$type === 'File')) {
890 $tempType = $value[0];
893 $plugin = $value[0] . '.';
898 if (!App
::import($tempType, $plugin . $class, $parent)) {
905 if ($name != null && strpos($name, '.') !== false) {
906 list($plugin, $name) = explode('.', $name);
907 $plugin = Inflector
::camelize($plugin);
909 $_this =& App
::getInstance();
910 $_this->return = $return;
913 $file = Inflector
::underscore($name) . ".{$ext}";
915 $ext = $_this->__settings($type, $plugin, $parent);
916 if ($name != null && !class_exists($name . $ext['class'])) {
917 if ($load = $_this->__mapped($name . $ext['class'], $type, $plugin)) {
918 if ($_this->__load($load)) {
919 $_this->__overload($type, $name . $ext['class'], $parent);
921 if ($_this->return) {
922 return include($load);
926 $_this->__remove($name . $ext['class'], $type, $plugin);
927 $_this->__resetCache(true);
930 if (!empty($search)) {
931 $_this->search
= $search;
933 $_this->search
= $_this->__paths('plugin');
935 $_this->search
= $_this->__paths($type);
939 if ($find === null) {
940 $find = Inflector
::underscore($name . $ext['suffix']).'.php';
943 $paths = $_this->search
;
944 foreach ($paths as $key => $value) {
945 $_this->search
[$key] = $value . $ext['path'];
950 if (strtolower($type) !== 'vendor' && empty($search) && $_this->__load($file)) {
954 $directory = $_this->__find($find, true);
957 if ($directory !== null) {
958 $_this->__resetCache(true);
959 $_this->__map($directory . $file, $name . $ext['class'], $type, $plugin);
960 $_this->__overload($type, $name . $ext['class'], $parent);
962 if ($_this->return) {
963 return include($directory . $file);
973 * Returns a single instance of App.
978 function &getInstance() {
979 static $instance = array();
981 $instance[0] =& new App();
982 $instance[0]->__map
= (array)Cache
::read('file_map', '_cake_core_');
988 * Locates the $file in $__paths, searches recursively.
990 * @param string $file full file name
991 * @param boolean $recursive search $__paths recursively
992 * @return mixed boolean on fail, $file directory path on success
995 function __find($file, $recursive = true) {
996 static $appPath = false;
998 if (empty($this->search
)) {
1000 } elseif (is_string($this->search
)) {
1001 $this->search
= array($this->search
);
1004 if (empty($this->__paths
)) {
1005 $this->__paths
= Cache
::read('dir_map', '_cake_core_');
1008 foreach ($this->search
as $path) {
1009 if ($appPath === false) {
1010 $appPath = rtrim(APP
, DS
);
1012 $path = rtrim($path, DS
);
1014 if ($path === $appPath) {
1017 if ($recursive === false) {
1018 if ($this->__load($path . DS
. $file)) {
1024 if (!isset($this->__paths
[$path])) {
1025 if (!class_exists('Folder')) {
1026 require LIBS
. 'folder.php';
1028 $Folder =& new Folder();
1029 $directories = $Folder->tree($path, array('.svn', '.git', 'CVS', 'tests', 'templates'), 'dir');
1031 $this->__paths
[$path] = $directories;
1034 foreach ($this->__paths
[$path] as $directory) {
1035 if ($this->__load($directory . DS
. $file)) {
1036 return $directory . DS
;
1044 * Attempts to load $file.
1046 * @param string $file full path to file including file name
1050 function __load($file) {
1054 if (!$this->return && isset($this->__loaded
[$file])) {
1057 if (file_exists($file)) {
1058 if (!$this->return) {
1060 $this->__loaded
[$file] = true;
1068 * Maps the $name to the $file.
1070 * @param string $file full path to file
1071 * @param string $name unique name for this map
1072 * @param string $type type object being mapped
1073 * @param string $plugin camelized if object is from a plugin, the name of the plugin
1077 function __map($file, $name, $type, $plugin) {
1079 $this->__map
['Plugin'][$plugin][$type][$name] = $file;
1081 $this->__map
[$type][$name] = $file;
1086 * Returns a file's complete path.
1088 * @param string $name unique name
1089 * @param string $type type object
1090 * @param string $plugin camelized if object is from a plugin, the name of the plugin
1091 * @return mixed, file path if found, false otherwise
1094 function __mapped($name, $type, $plugin) {
1096 if (isset($this->__map
['Plugin'][$plugin][$type]) && isset($this->__map
['Plugin'][$plugin][$type][$name])) {
1097 return $this->__map
['Plugin'][$plugin][$type][$name];
1102 if (isset($this->__map
[$type]) && isset($this->__map
[$type][$name])) {
1103 return $this->__map
[$type][$name];
1109 * Used to overload objects as needed.
1111 * @param string $type Model or Helper
1112 * @param string $name Class name to overload
1115 function __overload($type, $name, $parent) {
1116 if (($type === 'Model' ||
$type === 'Helper') && $parent !== false) {
1117 Overloadable
::overload($name);
1122 * Loads parent classes based on $type.
1123 * Returns a prefix or suffix needed for loading files.
1125 * @param string $type type of object
1126 * @param string $plugin camelized name of plugin
1127 * @param boolean $parent false will not attempt to load parent
1131 function __settings($type, $plugin, $parent) {
1133 return array('class' => null, 'suffix' => null, 'path' => null);
1137 $pluginPath = Inflector
::underscore($plugin);
1140 $load = strtolower($type);
1144 if (!class_exists('Model')) {
1145 require LIBS
. 'model' . DS
. 'model.php';
1147 if (!class_exists('AppModel')) {
1148 App
::import($type, 'AppModel', false);
1151 if (!class_exists($plugin . 'AppModel')) {
1152 App
::import($type, $plugin . '.' . $plugin . 'AppModel', false, array(), $pluginPath . DS
. $pluginPath . '_app_model.php');
1154 $path = $pluginPath . DS
. 'models' . DS
;
1156 return array('class' => null, 'suffix' => null, 'path' => $path);
1160 $path = $pluginPath . DS
. 'models' . DS
. 'behaviors' . DS
;
1162 return array('class' => $type, 'suffix' => null, 'path' => $path);
1166 $path = $pluginPath . DS
. 'models' . DS
. 'datasources' . DS
;
1168 return array('class' => $type, 'suffix' => null, 'path' => $path);
1170 App
::import($type, 'AppController', false);
1172 App
::import($type, $plugin . '.' . $plugin . 'AppController', false, array(), $pluginPath . DS
. $pluginPath . '_app_controller.php');
1173 $path = $pluginPath . DS
. 'controllers' . DS
;
1175 return array('class' => $type, 'suffix' => $type, 'path' => $path);
1179 $path = $pluginPath . DS
. 'controllers' . DS
. 'components' . DS
;
1181 return array('class' => $type, 'suffix' => null, 'path' => $path);
1185 $path = $pluginPath . DS
. 'libs' . DS
;
1187 return array('class' => null, 'suffix' => null, 'path' => $path);
1191 $path = $pluginPath . DS
. 'views' . DS
;
1193 return array('class' => $type, 'suffix' => null, 'path' => $path);
1196 if (!class_exists('AppHelper')) {
1197 App
::import($type, 'AppHelper', false);
1200 $path = $pluginPath . DS
. 'views' . DS
. 'helpers' . DS
;
1202 return array('class' => $type, 'suffix' => null, 'path' => $path);
1206 $path = $pluginPath . DS
. 'vendors' . DS
;
1208 return array('class' => null, 'suffix' => null, 'path' => $path);
1211 $type = $suffix = $path = null;
1214 return array('class' => null, 'suffix' => null, 'path' => null);
1218 * Returns default search paths.
1220 * @param string $type type of object to be searched
1221 * @return array list of paths
1224 function __paths($type) {
1225 $type = strtolower($type);
1228 if ($type === 'core') {
1229 return App
::core('libs');
1231 if (isset($this->{$type . 's'})) {
1232 return $this->{$type . 's'};
1238 * Removes file location from map if the file has been deleted.
1240 * @param string $name name of object
1241 * @param string $type type of object
1242 * @param string $plugin camelized name of plugin
1246 function __remove($name, $type, $plugin) {
1248 unset($this->__map
['Plugin'][$plugin][$type][$name]);
1250 unset($this->__map
[$type][$name]);
1255 * Returns an array of filenames of PHP files in the given directory.
1257 * @param string $path Path to scan for files
1258 * @param string $suffix if false, return only directories. if string, match and return files
1259 * @return array List of directories or files in directory
1262 function __list($path, $suffix = false, $extension = false) {
1263 if (!class_exists('Folder')) {
1264 require LIBS
. 'folder.php';
1267 $Folder =& new Folder($path);
1268 $contents = $Folder->read(false, true);
1270 if (is_array($contents)) {
1272 return $contents[0];
1274 foreach ($contents[1] as $item) {
1275 if (substr($item, - strlen($suffix)) === $suffix) {
1279 $items[] = substr($item, 0, strlen($item) - strlen($suffix));
1289 * Determines if $__maps, $__objects and $__paths cache should be reset.
1291 * @param boolean $reset
1295 function __resetCache($reset = null) {
1296 static $cache = array();
1297 if (!$cache && $reset === true) {
1304 * Object destructor.
1306 * Writes cache file if changes have been made to the $__map or $__paths
1311 function __destruct() {
1312 if ($this->__resetCache() === true) {
1313 $core = App
::core('cake');
1314 unset($this->__paths
[rtrim($core[0], DS
)]);
1315 Cache
::write('dir_map', array_filter($this->__paths
), '_cake_core_');
1316 Cache
::write('file_map', array_filter($this->__map
), '_cake_core_');
1317 Cache
::write('object_map', $this->__objects
, '_cake_core_');