Linux multi-monitor fullscreen support
[ryzomcore.git] / web / public_php / webtt / cake / libs / configure.php
blobe3e8e7835cebdbeaeedc0f899b2ec58359a4e8c4
1 <?php
2 /**
3 * App and Configure classes
5 * PHP versions 4 and 5
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
15 * @package cake
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)
21 /**
22 * Configuration class (singleton). Used for managing runtime configuration information.
24 * @package cake
25 * @subpackage cake.cake.libs
26 * @link http://book.cakephp.org/view/924/The-Configuration-Class
28 class Configure extends Object {
30 /**
31 * Current debug level.
33 * @link http://book.cakephp.org/view/931/CakePHP-Core-Configuration-Variables
34 * @var integer
35 * @access public
37 var $debug = 0;
39 /**
40 * Returns a singleton instance of the Configure class.
42 * @return Configure instance
43 * @access public
45 function &getInstance($boot = true) {
46 static $instance = array();
47 if (!$instance) {
48 if (!class_exists('Set')) {
49 require LIBS . 'set.php';
51 $instance[0] =& new Configure();
52 $instance[0]->__loadBootstrap($boot);
54 return $instance[0];
57 /**
58 * Used to store a dynamic variable in the Configure instance.
60 * Usage:
61 * {{{
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]'
67 * );
69 * Configure::write(array(
70 * 'One.key1' => 'value of the Configure::One[key1]',
71 * 'One.key2' => 'value of the Configure::One[key2]'
72 * ));
73 * }}}
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
79 * @access public
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;
91 } else {
92 $names = explode('.', $name, 4);
93 switch (count($names)) {
94 case 2:
95 $_this->{$names[0]}[$names[1]] = $value;
96 break;
97 case 3:
98 $_this->{$names[0]}[$names[1]][$names[2]] = $value;
99 break;
100 case 4:
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);
106 break;
111 if (isset($config['debug']) || isset($config['log'])) {
112 $reporting = 0;
113 if ($_this->debug) {
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;
131 } else {
132 $reporting = E_ALL & ~E_DEPRECATED;
135 error_reporting($reporting);
137 return true;
141 * Used to read information stored in the Configure instance.
143 * Usage:
144 * {{{
145 * Configure::read('Name'); will return all values for Name
146 * Configure::read('Name.key'); will return only the value of Configure::Name[key]
147 * }}}
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
152 * @access public
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);
163 $var = $names[0];
165 if (!isset($_this->{$var})) {
166 return null;
168 if (!isset($names[1])) {
169 return $_this->{$var};
171 switch (count($names)) {
172 case 2:
173 if (isset($_this->{$var}[$names[1]])) {
174 return $_this->{$var}[$names[1]];
176 break;
177 case 3:
178 if (isset($_this->{$var}[$names[1]][$names[2]])) {
179 return $_this->{$var}[$names[1]][$names[2]];
181 if (!isset($_this->{$var}[$names[1]])) {
182 return null;
184 return Set::classicExtract($_this->{$var}[$names[1]], $names[2]);
185 break;
187 return null;
191 * Used to delete a variable from the Configure instance.
193 * Usage:
194 * {{{
195 * Configure::delete('Name'); will delete the entire Configure::Name
196 * Configure::delete('Name.key'); will delete only the Configure::Name[key]
197 * }}}
199 * @link http://book.cakephp.org/view/928/delete
200 * @param string $var the var to be deleted
201 * @return void
202 * @access public
204 function delete($var = null) {
205 $_this =& Configure::getInstance();
207 if (strpos($var, '.') === false) {
208 unset($_this->{$var});
209 return;
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
230 * @access public
232 function load($fileName) {
233 $found = $plugin = $pluginPath = false;
234 list($plugin, $fileName) = pluginSplit($fileName);
235 if ($plugin) {
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');
243 $found = true;
244 } elseif (file_exists(CONFIGS . $fileName . '.php')) {
245 include(CONFIGS . $fileName . '.php');
246 $found = true;
247 } elseif (file_exists(CACHE . 'persistent' . DS . $fileName . '.php')) {
248 include(CACHE . 'persistent' . DS . $fileName . '.php');
249 $found = true;
250 } else {
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');
254 $found = true;
255 break;
261 if (!$found) {
262 return false;
265 if (!isset($config)) {
266 trigger_error(sprintf(__('Configure::load() - no variable $config found in %s.php', true), $fileName), E_USER_WARNING);
267 return false;
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
279 * @access public
281 function version() {
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.
294 * {{{
295 * Configure::store('Model', 'class_paths', array('Users' => array(
296 * 'path' => 'users', 'plugin' => true
297 * )));
298 * }}}
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.
303 * @return void
304 * @access public
306 function store($type, $name, $data = array()) {
307 $write = true;
308 $content = '';
310 foreach ($data as $key => $value) {
311 $content .= "\$config['$type']['$key'] = " . var_export($value, true) . ";\n";
313 if (is_null($type)) {
314 $write = false;
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
326 * @return void
327 * @access private
329 function __writeConfig($content, $name, $write = true) {
330 $file = CACHE . 'persistent' . DS . $name . '.php';
332 if (Configure::read() > 0) {
333 $expires = "+10 seconds";
334 } else {
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);
356 * @deprecated
357 * @see App::objects()
359 function listObjects($type, $path = null, $cache = true) {
360 return App::objects($type, $path, $cache);
364 * @deprecated
365 * @see App::core()
367 function corePaths($type = null) {
368 return App::core($type);
372 * @deprecated
373 * @see App::build()
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)
385 * @return void
386 * @access private
388 function __loadBootstrap($boot) {
389 if ($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']);
407 } else {
408 $prefix = $cache['settings']['prefix'];
411 if (Configure::read() >= 1) {
412 $duration = '+10 seconds';
413 } else {
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
421 )));
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
428 )));
430 Cache::config('default');
432 App::build();
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
445 * @package cake
446 * @subpackage cake.cake.libs
448 class App extends Object {
451 * List of object types and their properties
453 * @var array
454 * @access public
456 var $types = array(
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.
474 * @var array
475 * @access public
477 var $models = array();
480 * List of additional path(s) where behavior files reside.
482 * @var array
483 * @access public
485 var $behaviors = array();
488 * List of additional path(s) where controller files reside.
490 * @var array
491 * @access public
493 var $controllers = array();
496 * List of additional path(s) where component files reside.
498 * @var array
499 * @access public
501 var $components = array();
504 * List of additional path(s) where datasource files reside.
506 * @var array
507 * @access public
509 var $datasources = array();
512 * List of additional path(s) where libs files reside.
514 * @var array
515 * @access public
517 var $libs = array();
519 * List of additional path(s) where view files reside.
521 * @var array
522 * @access public
524 var $views = array();
527 * List of additional path(s) where helper files reside.
529 * @var array
530 * @access public
532 var $helpers = array();
535 * List of additional path(s) where plugins reside.
537 * @var array
538 * @access public
540 var $plugins = array();
543 * List of additional path(s) where vendor packages reside.
545 * @var array
546 * @access public
548 var $vendors = array();
551 * List of additional path(s) where locale files reside.
553 * @var array
554 * @access public
556 var $locales = array();
559 * List of additional path(s) where console shell files reside.
561 * @var array
562 * @access public
564 var $shells = array();
567 * Paths to search for files.
569 * @var array
570 * @access public
572 var $search = array();
575 * Whether or not to return the file that is loaded.
577 * @var boolean
578 * @access public
580 var $return = false;
583 * Holds key/value pairs of $type => file path.
585 * @var array
586 * @access private
588 var $__map = array();
591 * Holds paths for deep searching of files.
593 * @var array
594 * @access private
596 var $__paths = array();
599 * Holds loaded files.
601 * @var array
602 * @access private
604 var $__loaded = array();
607 * Holds and key => value array of object types.
609 * @var array
610 * @access private
612 var $__objects = array();
615 * Used to read information stored path
617 * Usage:
619 * `App::path('models'); will return all paths for models`
621 * @param string $type type of path
622 * @return string array
623 * @access public
625 function path($type) {
626 $_this =& App::getInstance();
627 if (!isset($_this->{$type})) {
628 return array();
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
639 * @return void
640 * @access public
642 function build($paths = array(), $reset = false) {
643 $_this =& App::getInstance();
644 $defaults = array(
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;
663 return $paths;
666 $core = $_this->core();
667 $app = array('models' => true, 'controllers' => true, 'helpers' => true);
669 foreach ($defaults as $type => $default) {
670 $merge = array();
672 if (isset($app[$type])) {
673 $merge = array(APP);
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
686 )));
687 $_this->{$type} = array_values($path);
688 } else {
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
736 * @access public
738 function core($type = null) {
739 static $paths = false;
740 if ($paths === false) {
741 $paths = Cache::read('core_paths', '_cake_core_');
743 if (!$paths) {
744 $paths = array();
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];
767 return $paths;
771 * Returns an array of objects of the given type.
773 * Example usage:
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
779 * type will be used.
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.
782 * @access public
784 function objects($type, $path = null, $cache = true) {
785 $objects = array();
786 $extension = false;
787 $name = $type;
789 if ($type === 'file' && !$path) {
790 return false;
791 } elseif ($type === 'file') {
792 $extension = true;
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])) {
805 return false;
807 $objects = array();
809 if (empty($path)) {
810 $path = $_this->{"{$type}s"};
811 if (isset($types[$type]['core']) && $types[$type]['core'] === false) {
812 array_pop($path);
815 $items = array();
817 foreach ((array)$path as $dir) {
818 if ($dir != APP) {
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
856 * @access public
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) {
870 $name = $type;
871 $type = 'Core';
872 } elseif ($name === null) {
873 $type = 'File';
876 if (is_array($name)) {
877 foreach ($name as $class) {
878 $tempType = $type;
879 $plugin = null;
881 if (strpos($class, '.') !== false) {
882 $value = explode('.', $class);
883 $count = count($value);
885 if ($count > 2) {
886 $tempType = $value[0];
887 $plugin = $value[1] . '.';
888 $class = $value[2];
889 } elseif ($count === 2 && ($type === 'Core' || $type === 'File')) {
890 $tempType = $value[0];
891 $class = $value[1];
892 } else {
893 $plugin = $value[0] . '.';
894 $class = $value[1];
898 if (!App::import($tempType, $plugin . $class, $parent)) {
899 return false;
902 return true;
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;
912 if (isset($ext)) {
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);
924 return true;
925 } else {
926 $_this->__remove($name . $ext['class'], $type, $plugin);
927 $_this->__resetCache(true);
930 if (!empty($search)) {
931 $_this->search = $search;
932 } elseif ($plugin) {
933 $_this->search = $_this->__paths('plugin');
934 } else {
935 $_this->search = $_this->__paths($type);
937 $find = $file;
939 if ($find === null) {
940 $find = Inflector::underscore($name . $ext['suffix']).'.php';
942 if ($plugin) {
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)) {
951 $directory = false;
952 } else {
953 $file = $find;
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);
965 return true;
967 return false;
969 return true;
973 * Returns a single instance of App.
975 * @return object
976 * @access public
978 function &getInstance() {
979 static $instance = array();
980 if (!$instance) {
981 $instance[0] =& new App();
982 $instance[0]->__map = (array)Cache::read('file_map', '_cake_core_');
984 return $instance[0];
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
993 * @access private
995 function __find($file, $recursive = true) {
996 static $appPath = false;
998 if (empty($this->search)) {
999 return null;
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) {
1015 $recursive = false;
1017 if ($recursive === false) {
1018 if ($this->__load($path . DS . $file)) {
1019 return $path . DS;
1021 continue;
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');
1030 sort($directories);
1031 $this->__paths[$path] = $directories;
1034 foreach ($this->__paths[$path] as $directory) {
1035 if ($this->__load($directory . DS . $file)) {
1036 return $directory . DS;
1040 return null;
1044 * Attempts to load $file.
1046 * @param string $file full path to file including file name
1047 * @return boolean
1048 * @access private
1050 function __load($file) {
1051 if (empty($file)) {
1052 return false;
1054 if (!$this->return && isset($this->__loaded[$file])) {
1055 return true;
1057 if (file_exists($file)) {
1058 if (!$this->return) {
1059 require($file);
1060 $this->__loaded[$file] = true;
1062 return true;
1064 return false;
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
1074 * @return void
1075 * @access private
1077 function __map($file, $name, $type, $plugin) {
1078 if ($plugin) {
1079 $this->__map['Plugin'][$plugin][$type][$name] = $file;
1080 } else {
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
1092 * @access private
1094 function __mapped($name, $type, $plugin) {
1095 if ($plugin) {
1096 if (isset($this->__map['Plugin'][$plugin][$type]) && isset($this->__map['Plugin'][$plugin][$type][$name])) {
1097 return $this->__map['Plugin'][$plugin][$type][$name];
1099 return false;
1102 if (isset($this->__map[$type]) && isset($this->__map[$type][$name])) {
1103 return $this->__map[$type][$name];
1105 return false;
1109 * Used to overload objects as needed.
1111 * @param string $type Model or Helper
1112 * @param string $name Class name to overload
1113 * @access private
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
1128 * @return array
1129 * @access private
1131 function __settings($type, $plugin, $parent) {
1132 if (!$parent) {
1133 return array('class' => null, 'suffix' => null, 'path' => null);
1136 if ($plugin) {
1137 $pluginPath = Inflector::underscore($plugin);
1139 $path = null;
1140 $load = strtolower($type);
1142 switch ($load) {
1143 case 'model':
1144 if (!class_exists('Model')) {
1145 require LIBS . 'model' . DS . 'model.php';
1147 if (!class_exists('AppModel')) {
1148 App::import($type, 'AppModel', false);
1150 if ($plugin) {
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);
1157 break;
1158 case 'behavior':
1159 if ($plugin) {
1160 $path = $pluginPath . DS . 'models' . DS . 'behaviors' . DS;
1162 return array('class' => $type, 'suffix' => null, 'path' => $path);
1163 break;
1164 case 'datasource':
1165 if ($plugin) {
1166 $path = $pluginPath . DS . 'models' . DS . 'datasources' . DS;
1168 return array('class' => $type, 'suffix' => null, 'path' => $path);
1169 case 'controller':
1170 App::import($type, 'AppController', false);
1171 if ($plugin) {
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);
1176 break;
1177 case 'component':
1178 if ($plugin) {
1179 $path = $pluginPath . DS . 'controllers' . DS . 'components' . DS;
1181 return array('class' => $type, 'suffix' => null, 'path' => $path);
1182 break;
1183 case 'lib':
1184 if ($plugin) {
1185 $path = $pluginPath . DS . 'libs' . DS;
1187 return array('class' => null, 'suffix' => null, 'path' => $path);
1188 break;
1189 case 'view':
1190 if ($plugin) {
1191 $path = $pluginPath . DS . 'views' . DS;
1193 return array('class' => $type, 'suffix' => null, 'path' => $path);
1194 break;
1195 case 'helper':
1196 if (!class_exists('AppHelper')) {
1197 App::import($type, 'AppHelper', false);
1199 if ($plugin) {
1200 $path = $pluginPath . DS . 'views' . DS . 'helpers' . DS;
1202 return array('class' => $type, 'suffix' => null, 'path' => $path);
1203 break;
1204 case 'vendor':
1205 if ($plugin) {
1206 $path = $pluginPath . DS . 'vendors' . DS;
1208 return array('class' => null, 'suffix' => null, 'path' => $path);
1209 break;
1210 default:
1211 $type = $suffix = $path = null;
1212 break;
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
1222 * @access private
1224 function __paths($type) {
1225 $type = strtolower($type);
1226 $paths = array();
1228 if ($type === 'core') {
1229 return App::core('libs');
1231 if (isset($this->{$type . 's'})) {
1232 return $this->{$type . 's'};
1234 return $paths;
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
1243 * @return void
1244 * @access private
1246 function __remove($name, $type, $plugin) {
1247 if ($plugin) {
1248 unset($this->__map['Plugin'][$plugin][$type][$name]);
1249 } else {
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
1260 * @access private
1262 function __list($path, $suffix = false, $extension = false) {
1263 if (!class_exists('Folder')) {
1264 require LIBS . 'folder.php';
1266 $items = array();
1267 $Folder =& new Folder($path);
1268 $contents = $Folder->read(false, true);
1270 if (is_array($contents)) {
1271 if (!$suffix) {
1272 return $contents[0];
1273 } else {
1274 foreach ($contents[1] as $item) {
1275 if (substr($item, - strlen($suffix)) === $suffix) {
1276 if ($extension) {
1277 $items[] = $item;
1278 } else {
1279 $items[] = substr($item, 0, strlen($item) - strlen($suffix));
1285 return $items;
1289 * Determines if $__maps, $__objects and $__paths cache should be reset.
1291 * @param boolean $reset
1292 * @return boolean
1293 * @access private
1295 function __resetCache($reset = null) {
1296 static $cache = array();
1297 if (!$cache && $reset === true) {
1298 $cache = true;
1300 return $cache;
1304 * Object destructor.
1306 * Writes cache file if changes have been made to the $__map or $__paths
1308 * @return void
1309 * @access private
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_');