cake/libs/configure.php

1 <?php
2 /**
3 * App and Configure classes
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8 * Copyright 2005-2011, 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-2011, 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)
19 */
20  
21 /**
22 * Configuration class (singleton). Used for managing runtime configuration information.
23 *
24 * @package cake
25 * @subpackage cake.cake.libs
26 * @link http://book.cakephp.org/view/924/The-Configuration-Class
27 */
28 class Configure extends Object {
29  
30 /**
31 * Current debug level.
32 *
33 * @link http://book.cakephp.org/view/931/CakePHP-Core-Configuration-Variables
34 * @var integer
35 * @access public
36 */
37 var $debug = 0;
38  
39 /**
40 * Returns a singleton instance of the Configure class.
41 *
42 * @return Configure instance
43 * @access public
44 */
45 function &getInstance($boot = true) {
46 static $instance = array();
47 if (!$instance) {
48 if (!class_exists('Set')) {
49 require LIBS . 'set.php';
50 }
51 $instance[0] =& new Configure();
52 $instance[0]->__loadBootstrap($boot);
53 }
54 return $instance[0];
55 }
56  
57 /**
58 * Used to store a dynamic variable in the Configure instance.
59 *
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 * );
68 *
69 * Configure::write(array(
70 * 'One.key1' => 'value of the Configure::One[key1]',
71 * 'One.key2' => 'value of the Configure::One[key2]'
72 * ));
73 * }}}
74 *
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
80 */
81 function write($config, $value = null) {
82 $_this =& Configure::getInstance();
83  
84 if (!is_array($config)) {
85 $config = array($config => $value);
86 }
87  
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();
104 }
105 $_this->{$names[0]} = Set::insert($_this->{$names[0]}, $names[1], $value);
106 break;
107 }
108 }
109 }
110  
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';
116 }
117 $reporting = E_ALL & ~E_DEPRECATED;
118 if (function_exists('ini_set')) {
119 ini_set('display_errors', 1);
120 }
121 } elseif (function_exists('ini_set')) {
122 ini_set('display_errors', 0);
123 }
124  
125 if (isset($_this->log) && $_this->log) {
126 if (is_integer($_this->log) && !$_this->debug) {
127 $reporting = $_this->log;
128 } else {
129 $reporting = E_ALL & ~E_DEPRECATED;
130 }
131 error_reporting($reporting);
132 if (!class_exists('CakeLog')) {
133 require LIBS . 'cake_log.php';
134 }
135 }
136 error_reporting($reporting);
137 }
138 return true;
139 }
140  
141 /**
142 * Used to read information stored in the Configure instance.
143 *
144 * Usage:
145 * {{{
146 * Configure::read('Name'); will return all values for Name
147 * Configure::read('Name.key'); will return only the value of Configure::Name[key]
148 * }}}
149 *
150 * @link http://book.cakephp.org/view/927/read
151 * @param string $var Variable to obtain. Use '.' to access array elements.
152 * @return string value of Configure::$var
153 * @access public
154 */
155 function read($var = 'debug') {
156 $_this =& Configure::getInstance();
157  
158 if ($var === 'debug') {
159 return $_this->debug;
160 }
161  
162 if (strpos($var, '.') !== false) {
163 $names = explode('.', $var, 3);
164 $var = $names[0];
165 }
166 if (!isset($_this->{$var})) {
167 return null;
168 }
169 if (!isset($names[1])) {
170 return $_this->{$var};
171 }
172 switch (count($names)) {
173 case 2:
174 if (isset($_this->{$var}[$names[1]])) {
175 return $_this->{$var}[$names[1]];
176 }
177 break;
178 case 3:
179 if (isset($_this->{$var}[$names[1]][$names[2]])) {
180 return $_this->{$var}[$names[1]][$names[2]];
181 }
182 if (!isset($_this->{$var}[$names[1]])) {
183 return null;
184 }
185 return Set::classicExtract($_this->{$var}[$names[1]], $names[2]);
186 break;
187 }
188 return null;
189 }
190  
191 /**
192 * Used to delete a variable from the Configure instance.
193 *
194 * Usage:
195 * {{{
196 * Configure::delete('Name'); will delete the entire Configure::Name
197 * Configure::delete('Name.key'); will delete only the Configure::Name[key]
198 * }}}
199 *
200 * @link http://book.cakephp.org/view/928/delete
201 * @param string $var the var to be deleted
202 * @return void
203 * @access public
204 */
205 function delete($var = null) {
206 $_this =& Configure::getInstance();
207  
208 if (strpos($var, '.') === false) {
209 unset($_this->{$var});
210 return;
211 }
212  
213 $names = explode('.', $var, 2);
214 $_this->{$names[0]} = Set::remove($_this->{$names[0]}, $names[1]);
215 }
216  
217 /**
218 * Loads a file from app/config/configure_file.php.
219 * Config file variables should be formated like:
220 * `$config['name'] = 'value';`
221 * These will be used to create dynamic Configure vars. load() is also used to
222 * load stored config files created with Configure::store()
223 *
224 * - To load config files from app/config use `Configure::load('configure_file');`.
225 * - To load config files from a plugin `Configure::load('plugin.configure_file');`.
226 *
227 * @link http://book.cakephp.org/view/929/load
228 * @param string $fileName name of file to load, extension must be .php and only the name
229 * should be used, not the extenstion
230 * @return mixed false if file not found, void if load successful
231 * @access public
232 */
233 function load($fileName) {
234 $found = $plugin = $pluginPath = false;
235 list($plugin, $fileName) = pluginSplit($fileName);
236 if ($plugin) {
237 $pluginPath = App::pluginPath($plugin);
238 }
239 $pos = strpos($fileName, '..');
240  
241 if ($pos === false) {
242 if ($pluginPath && file_exists($pluginPath . 'config' . DS . $fileName . '.php')) {
243 include($pluginPath . 'config' . DS . $fileName . '.php');
244 $found = true;
245 } elseif (file_exists(CONFIGS . $fileName . '.php')) {
246 include(CONFIGS . $fileName . '.php');
247 $found = true;
248 } elseif (file_exists(CACHE . 'persistent' . DS . $fileName . '.php')) {
249 include(CACHE . 'persistent' . DS . $fileName . '.php');
250 $found = true;
251 } else {
252 foreach (App::core('cake') as $key => $path) {
253 if (file_exists($path . DS . 'config' . DS . $fileName . '.php')) {
254 include($path . DS . 'config' . DS . $fileName . '.php');
255 $found = true;
256 break;
257 }
258 }
259 }
260 }
261  
262 if (!$found) {
263 return false;
264 }
265  
266 if (!isset($config)) {
267 trigger_error(sprintf(__('Configure::load() - no variable $config found in %s.php', true), $fileName), E_USER_WARNING);
268 return false;
269 }
270 return Configure::write($config);
271 }
272  
273 /**
274 * Used to determine the current version of CakePHP.
275 *
276 * Usage `Configure::version();`
277 *
278 * @link http://book.cakephp.org/view/930/version
279 * @return string Current version of CakePHP
280 * @access public
281 */
282 function version() {
283 $_this =& Configure::getInstance();
284  
285 if (!isset($_this->Cake['version'])) {
286 require(CORE_PATH . 'cake' . DS . 'config' . DS . 'config.php');
287 $_this->write($config);
288 }
289 return $_this->Cake['version'];
290 }
291  
292 /**
293 * Used to write a config file to disk.
294 *
295 * {{{
296 * Configure::store('Model', 'class_paths', array('Users' => array(
297 * 'path' => 'users', 'plugin' => true
298 * )));
299 * }}}
300 *
301 * @param string $type Type of config file to write, ex: Models, Controllers, Helpers, Components
302 * @param string $name file name.
303 * @param array $data array of values to store.
304 * @return void
305 * @access public
306 */
307 function store($type, $name, $data = array()) {
308 $write = true;
309 $content = '';
310  
311 foreach ($data as $key => $value) {
312 $content .= "\$config['$type']['$key'] = " . var_export($value, true) . ";\n";
313 }
314 if (is_null($type)) {
315 $write = false;
316 }
317 Configure::__writeConfig($content, $name, $write);
318 }
319  
320 /**
321 * Creates a cached version of a configuration file.
322 * Appends values passed from Configure::store() to the cached file
323 *
324 * @param string $content Content to write on file
325 * @param string $name Name to use for cache file
326 * @param boolean $write true if content should be written, false otherwise
327 * @return void
328 * @access private
329 */
330 function __writeConfig($content, $name, $write = true) {
331 $file = CACHE . 'persistent' . DS . $name . '.php';
332  
333 if (Configure::read() > 0) {
334 $expires = "+10 seconds";
335 } else {
336 $expires = "+999 days";
337 }
338 $cache = cache('persistent' . DS . $name . '.php', null, $expires);
339  
340 if ($cache === null) {
341 cache('persistent' . DS . $name . '.php', "<?php\n\$config = array();\n", $expires);
342 }
343  
344 if ($write === true) {
345 if (!class_exists('File')) {
346 require LIBS . 'file.php';
347 }
348 $fileClass = new File($file);
349  
350 if ($fileClass->writable()) {
351 $fileClass->append($content);
352 }
353 }
354 }
355  
356 /**
357 * @deprecated
358 * @see App::objects()
359 */
360 function listObjects($type, $path = null, $cache = true) {
361 return App::objects($type, $path, $cache);
362 }
363  
364 /**
365 * @deprecated
366 * @see App::core()
367 */
368 function corePaths($type = null) {
369 return App::core($type);
370 }
371  
372 /**
373 * @deprecated
374 * @see App::build()
375 */
376 function buildPaths($paths) {
377 return App::build($paths);
378 }
379  
380 /**
381 * Loads app/config/bootstrap.php.
382 * If the alternative paths are set in this file
383 * they will be added to the paths vars.
384 *
385 * @param boolean $boot Load application bootstrap (if true)
386 * @return void
387 * @access private
388 */
389 function __loadBootstrap($boot) {
390 if ($boot) {
391 Configure::write('App', array('base' => false, 'baseUrl' => false, 'dir' => APP_DIR, 'webroot' => WEBROOT_DIR, 'www_root' => WWW_ROOT));
392  
393 if (!include(CONFIGS . 'core.php')) {
394 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);
395 }
396  
397 if (Configure::read('Cache.disable') !== true) {
398 $cache = Cache::config('default');
399  
400 if (empty($cache['settings'])) {
401 trigger_error(__('Cache not configured properly. Please check Cache::config(); in APP/config/core.php', true), E_USER_WARNING);
402 $cache = Cache::config('default', array('engine' => 'File'));
403 }
404 $path = $prefix = $duration = null;
405  
406 if (!empty($cache['settings']['path'])) {
407 $path = realpath($cache['settings']['path']);
408 } else {
409 $prefix = $cache['settings']['prefix'];
410 }
411  
412 if (Configure::read() >= 1) {
413 $duration = '+10 seconds';
414 } else {
415 $duration = '+999 days';
416 }
417  
418 if (Cache::config('_cake_core_') === false) {
419 Cache::config('_cake_core_', array_merge((array)$cache['settings'], array(
420 'prefix' => $prefix . 'cake_core_', 'path' => $path . DS . 'persistent' . DS,
421 'serialize' => true, 'duration' => $duration
422 )));
423 }
424  
425 if (Cache::config('_cake_model_') === false) {
426 Cache::config('_cake_model_', array_merge((array)$cache['settings'], array(
427 'prefix' => $prefix . 'cake_model_', 'path' => $path . DS . 'models' . DS,
428 'serialize' => true, 'duration' => $duration
429 )));
430 }
431 Cache::config('default');
432 }
433 App::build();
434 if (!include(CONFIGS . 'bootstrap.php')) {
435 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);
436 }
437 }
438 }
439 }
440  
441 /**
442 * Class/file loader and path management.
443 *
444 * @link http://book.cakephp.org/view/933/The-App-Class
445 * @since CakePHP(tm) v 1.2.0.6001
446 * @package cake
447 * @subpackage cake.cake.libs
448 */
449 class App extends Object {
450  
451 /**
452 * List of object types and their properties
453 *
454 * @var array
455 * @access public
456 */
457 var $types = array(
458 'class' => array('suffix' => '.php', 'extends' => null, 'core' => true),
459 'file' => array('suffix' => '.php', 'extends' => null, 'core' => true),
460 'model' => array('suffix' => '.php', 'extends' => 'AppModel', 'core' => false),
461 'behavior' => array('suffix' => '.php', 'extends' => 'ModelBehavior', 'core' => true),
462 'controller' => array('suffix' => '_controller.php', 'extends' => 'AppController', 'core' => true),
463 'component' => array('suffix' => '.php', 'extends' => null, 'core' => true),
464 'lib' => array('suffix' => '.php', 'extends' => null, 'core' => true),
465 'view' => array('suffix' => '.php', 'extends' => null, 'core' => true),
466 'helper' => array('suffix' => '.php', 'extends' => 'AppHelper', 'core' => true),
467 'vendor' => array('suffix' => '', 'extends' => null, 'core' => true),
468 'shell' => array('suffix' => '.php', 'extends' => 'Shell', 'core' => true),
469 'plugin' => array('suffix' => '', 'extends' => null, 'core' => true)
470 );
471  
472 /**
473 * List of additional path(s) where model files reside.
474 *
475 * @var array
476 * @access public
477 */
478 var $models = array();
479  
480 /**
481 * List of additional path(s) where behavior files reside.
482 *
483 * @var array
484 * @access public
485 */
486 var $behaviors = array();
487  
488 /**
489 * List of additional path(s) where controller files reside.
490 *
491 * @var array
492 * @access public
493 */
494 var $controllers = array();
495  
496 /**
497 * List of additional path(s) where component files reside.
498 *
499 * @var array
500 * @access public
501 */
502 var $components = array();
503  
504 /**
505 * List of additional path(s) where datasource files reside.
506 *
507 * @var array
508 * @access public
509 */
510 var $datasources = array();
511  
512 /**
513 * List of additional path(s) where libs files reside.
514 *
515 * @var array
516 * @access public
517 */
518 var $libs = array();
519 /**
520 * List of additional path(s) where view files reside.
521 *
522 * @var array
523 * @access public
524 */
525 var $views = array();
526  
527 /**
528 * List of additional path(s) where helper files reside.
529 *
530 * @var array
531 * @access public
532 */
533 var $helpers = array();
534  
535 /**
536 * List of additional path(s) where plugins reside.
537 *
538 * @var array
539 * @access public
540 */
541 var $plugins = array();
542  
543 /**
544 * List of additional path(s) where vendor packages reside.
545 *
546 * @var array
547 * @access public
548 */
549 var $vendors = array();
550  
551 /**
552 * List of additional path(s) where locale files reside.
553 *
554 * @var array
555 * @access public
556 */
557 var $locales = array();
558  
559 /**
560 * List of additional path(s) where console shell files reside.
561 *
562 * @var array
563 * @access public
564 */
565 var $shells = array();
566  
567 /**
568 * Paths to search for files.
569 *
570 * @var array
571 * @access public
572 */
573 var $search = array();
574  
575 /**
576 * Whether or not to return the file that is loaded.
577 *
578 * @var boolean
579 * @access public
580 */
581 var $return = false;
582  
583 /**
584 * Holds key/value pairs of $type => file path.
585 *
586 * @var array
587 * @access private
588 */
589 var $__map = array();
590  
591 /**
592 * Holds paths for deep searching of files.
593 *
594 * @var array
595 * @access private
596 */
597 var $__paths = array();
598  
599 /**
600 * Holds loaded files.
601 *
602 * @var array
603 * @access private
604 */
605 var $__loaded = array();
606  
607 /**
608 * Holds and key => value array of object types.
609 *
610 * @var array
611 * @access private
612 */
613 var $__objects = array();
614  
615 /**
616 * Used to read information stored path
617 *
618 * Usage:
619 *
620 * `App::path('models'); will return all paths for models`
621 *
622 * @param string $type type of path
623 * @return string array
624 * @access public
625 */
626 function path($type) {
627 $_this =& App::getInstance();
628 if (!isset($_this->{$type})) {
629 return array();
630 }
631 return $_this->{$type};
632 }
633  
634 /**
635 * Build path references. Merges the supplied $paths
636 * with the base paths and the default core paths.
637 *
638 * @param array $paths paths defines in config/bootstrap.php
639 * @param boolean $reset true will set paths, false merges paths [default] false
640 * @return void
641 * @access public
642 */
643 function build($paths = array(), $reset = false) {
644 $_this =& App::getInstance();
645 $defaults = array(
646 'models' => array(MODELS),
647 'behaviors' => array(BEHAVIORS),
648 'datasources' => array(MODELS . 'datasources'),
649 'controllers' => array(CONTROLLERS),
650 'components' => array(COMPONENTS),
651 'libs' => array(APPLIBS),
652 'views' => array(VIEWS),
653 'helpers' => array(HELPERS),
654 'locales' => array(APP . 'locale' . DS),
655 'shells' => array(APP . 'vendors' . DS . 'shells' . DS, VENDORS . 'shells' . DS),
656 'vendors' => array(APP . 'vendors' . DS, VENDORS),
657 'plugins' => array(APP . 'plugins' . DS)
658 );
659  
660 if ($reset == true) {
661 foreach ($paths as $type => $new) {
662 $_this->{$type} = (array)$new;
663 }
664 return $paths;
665 }
666  
667 $core = $_this->core();
668 $app = array('models' => true, 'controllers' => true, 'helpers' => true);
669  
670 foreach ($defaults as $type => $default) {
671 $merge = array();
672  
673 if (isset($app[$type])) {
674 $merge = array(APP);
675 }
676 if (isset($core[$type])) {
677 $merge = array_merge($merge, (array)$core[$type]);
678 }
679  
680 if (empty($_this->{$type}) || empty($paths)) {
681 $_this->{$type} = $default;
682 }
683  
684 if (!empty($paths[$type])) {
685 $path = array_flip(array_flip(array_merge(
686 (array)$paths[$type], $_this->{$type}, $merge
687 )));
688 $_this->{$type} = array_values($path);
689 } else {
690 $path = array_flip(array_flip(array_merge($_this->{$type}, $merge)));
691 $_this->{$type} = array_values($path);
692 }
693 }
694 }
695  
696 /**
697 * Get the path that a plugin is on. Searches through the defined plugin paths.
698 *
699 * @param string $plugin CamelCased/lower_cased plugin name to find the path of.
700 * @return string full path to the plugin.
701 */
702 function pluginPath($plugin) {
703 $_this =& App::getInstance();
704 $pluginDir = Inflector::underscore($plugin);
705 for ($i = 0, $length = count($_this->plugins); $i < $length; $i++) {
706 if (is_dir($_this->plugins[$i] . $pluginDir)) {
707 return $_this->plugins[$i] . $pluginDir . DS ;
708 }
709 }
710 return $_this->plugins[0] . $pluginDir . DS;
711 }
712  
713 /**
714 * Find the path that a theme is on. Search through the defined theme paths.
715 *
716 * @param string $theme lower_cased theme name to find the path of.
717 * @return string full path to the theme.
718 */
719 function themePath($theme) {
720 $_this =& App::getInstance();
721 $themeDir = 'themed' . DS . Inflector::underscore($theme);
722 for ($i = 0, $length = count($_this->views); $i < $length; $i++) {
723 if (is_dir($_this->views[$i] . $themeDir)) {
724 return $_this->views[$i] . $themeDir . DS ;
725 }
726 }
727 return $_this->views[0] . $themeDir . DS;
728 }
729  
730 /**
731 * Returns a key/value list of all paths where core libs are found.
732 * Passing $type only returns the values for a given value of $key.
733 *
734 * @param string $type valid values are: 'model', 'behavior', 'controller', 'component',
735 * 'view', 'helper', 'datasource', 'libs', and 'cake'
736 * @return array numeric keyed array of core lib paths
737 * @access public
738 */
739 function core($type = null) {
740 static $paths = false;
741 if ($paths === false) {
742 $paths = Cache::read('core_paths', '_cake_core_');
743 }
744 if (!$paths) {
745 $paths = array();
746 $libs = dirname(__FILE__) . DS;
747 $cake = dirname($libs) . DS;
748 $path = dirname($cake) . DS;
749  
750 $paths['cake'][] = $cake;
751 $paths['libs'][] = $libs;
752 $paths['models'][] = $libs . 'model' . DS;
753 $paths['datasources'][] = $libs . 'model' . DS . 'datasources' . DS;
754 $paths['behaviors'][] = $libs . 'model' . DS . 'behaviors' . DS;
755 $paths['controllers'][] = $libs . 'controller' . DS;
756 $paths['components'][] = $libs . 'controller' . DS . 'components' . DS;
757 $paths['views'][] = $libs . 'view' . DS;
758 $paths['helpers'][] = $libs . 'view' . DS . 'helpers' . DS;
759 $paths['plugins'][] = $path . 'plugins' . DS;
760 $paths['vendors'][] = $path . 'vendors' . DS;
761 $paths['shells'][] = $cake . 'console' . DS . 'libs' . DS;
762  
763 Cache::write('core_paths', array_filter($paths), '_cake_core_');
764 }
765 if ($type && isset($paths[$type])) {
766 return $paths[$type];
767 }
768 return $paths;
769 }
770  
771 /**
772 * Returns an array of objects of the given type.
773 *
774 * Example usage:
775 *
776 * `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
777 *
778 * @param string $type Type of object, i.e. 'model', 'controller', 'helper', or 'plugin'
779 * @param mixed $path Optional Scan only the path given. If null, paths for the chosen
780 * type will be used.
781 * @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
782 * @return mixed Either false on incorrect / miss. Or an array of found objects.
783 * @access public
784 */
785 function objects($type, $path = null, $cache = true) {
786 $objects = array();
787 $extension = false;
788 $name = $type;
789  
790 if ($type === 'file' && !$path) {
791 return false;
792 } elseif ($type === 'file') {
793 $extension = true;
794 $name = $type . str_replace(DS, '', $path);
795 }
796 $_this =& App::getInstance();
797  
798 if (empty($_this->__objects) && $cache === true) {
799 $_this->__objects = Cache::read('object_map', '_cake_core_');
800 }
801  
802 if (!isset($_this->__objects[$name]) || $cache !== true) {
803 $types = $_this->types;
804  
805 if (!isset($types[$type])) {
806 return false;
807 }
808 $objects = array();
809  
810 if (empty($path)) {
811 $path = $_this->{"{$type}s"};
812 if (isset($types[$type]['core']) && $types[$type]['core'] === false) {
813 array_pop($path);
814 }
815 }
816 $items = array();
817  
818 foreach ((array)$path as $dir) {
819 if ($dir != APP) {
820 $items = $_this->__list($dir, $types[$type]['suffix'], $extension);
821 $objects = array_merge($items, array_diff($objects, $items));
822 }
823 }
824  
825 if ($type !== 'file') {
826 foreach ($objects as $key => $value) {
827 $objects[$key] = Inflector::camelize($value);
828 }
829 }
830  
831 if ($cache === true) {
832 $_this->__resetCache(true);
833 }
834 $_this->__objects[$name] = $objects;
835 }
836  
837 return $_this->__objects[$name];
838 }
839  
840 /**
841 * Finds classes based on $name or specific file(s) to search. Calling App::import() will
842 * not construct any classes contained in the files. It will only find and require() the file.
843 *
844 * @link http://book.cakephp.org/view/934/Using-App-import
845 * @param mixed $type The type of Class if passed as a string, or all params can be passed as
846 * an single array to $type,
847 * @param string $name Name of the Class or a unique name for the file
848 * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
849 * array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
850 * $ext allows setting the extension of the file name
851 * based on Inflector::underscore($name) . ".$ext";
852 * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
853 * @param string $file full name of the file to search for including extension
854 * @param boolean $return, return the loaded file, the file must have a return
855 * statement in it to work: return $variable;
856 * @return boolean true if Class is already in memory or if file is found and loaded, false if not
857 * @access public
858 */
859 function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
860 $plugin = $directory = null;
861  
862 if (is_array($type)) {
863 extract($type, EXTR_OVERWRITE);
864 }
865  
866 if (is_array($parent)) {
867 extract($parent, EXTR_OVERWRITE);
868 }
869  
870 if ($name === null && $file === null) {
871 $name = $type;
872 $type = 'Core';
873 } elseif ($name === null) {
874 $type = 'File';
875 }
876  
877 if (is_array($name)) {
878 foreach ($name as $class) {
879 $tempType = $type;
880 $plugin = null;
881  
882 if (strpos($class, '.') !== false) {
883 $value = explode('.', $class);
884 $count = count($value);
885  
886 if ($count > 2) {
887 $tempType = $value[0];
888 $plugin = $value[1] . '.';
889 $class = $value[2];
890 } elseif ($count === 2 && ($type === 'Core' || $type === 'File')) {
891 $tempType = $value[0];
892 $class = $value[1];
893 } else {
894 $plugin = $value[0] . '.';
895 $class = $value[1];
896 }
897 }
898  
899 if (!App::import($tempType, $plugin . $class, $parent)) {
900 return false;
901 }
902 }
903 return true;
904 }
905  
906 if ($name != null && strpos($name, '.') !== false) {
907 list($plugin, $name) = explode('.', $name);
908 $plugin = Inflector::camelize($plugin);
909 }
910 $_this =& App::getInstance();
911 $_this->return = $return;
912  
913 if (isset($ext)) {
914 $file = Inflector::underscore($name) . ".{$ext}";
915 }
916 $ext = $_this->__settings($type, $plugin, $parent);
917 if ($name != null && !class_exists($name . $ext['class'])) {
918 if ($load = $_this->__mapped($name . $ext['class'], $type, $plugin)) {
919 if ($_this->__load($load)) {
920 $_this->__overload($type, $name . $ext['class'], $parent);
921  
922 if ($_this->return) {
923 return include($load);
924 }
925 return true;
926 } else {
927 $_this->__remove($name . $ext['class'], $type, $plugin);
928 $_this->__resetCache(true);
929 }
930 }
931 if (!empty($search)) {
932 $_this->search = $search;
933 } elseif ($plugin) {
934 $_this->search = $_this->__paths('plugin');
935 } else {
936 $_this->search = $_this->__paths($type);
937 }
938 $find = $file;
939  
940 if ($find === null) {
941 $find = Inflector::underscore($name . $ext['suffix']).'.php';
942  
943 if ($plugin) {
944 $paths = $_this->search;
945 foreach ($paths as $key => $value) {
946 $_this->search[$key] = $value . $ext['path'];
947 }
948 }
949 }
950  
951 if (strtolower($type) !== 'vendor' && empty($search) && $_this->__load($file)) {
952 $directory = false;
953 } else {
954 $file = $find;
955 $directory = $_this->__find($find, true);
956 }
957  
958 if ($directory !== null) {
959 $_this->__resetCache(true);
960 $_this->__map($directory . $file, $name . $ext['class'], $type, $plugin);
961 $_this->__overload($type, $name . $ext['class'], $parent);
962  
963 if ($_this->return) {
964 return include($directory . $file);
965 }
966 return true;
967 }
968 return false;
969 }
970 return true;
971 }
972  
973 /**
974 * Returns a single instance of App.
975 *
976 * @return object
977 * @access public
978 */
979 function &getInstance() {
980 static $instance = array();
981 if (!$instance) {
982 $instance[0] =& new App();
983 $instance[0]->__map = (array)Cache::read('file_map', '_cake_core_');
984 }
985 return $instance[0];
986 }
987  
988 /**
989 * Locates the $file in $__paths, searches recursively.
990 *
991 * @param string $file full file name
992 * @param boolean $recursive search $__paths recursively
993 * @return mixed boolean on fail, $file directory path on success
994 * @access private
995 */
996 function __find($file, $recursive = true) {
997 static $appPath = false;
998  
999 if (empty($this->search)) {
1000 return null;
1001 } elseif (is_string($this->search)) {
1002 $this->search = array($this->search);
1003 }
1004  
1005 if (empty($this->__paths)) {
1006 $this->__paths = Cache::read('dir_map', '_cake_core_');
1007 }
1008  
1009 foreach ($this->search as $path) {
1010 if ($appPath === false) {
1011 $appPath = rtrim(APP, DS);
1012 }
1013 $path = rtrim($path, DS);
1014  
1015 if ($path === $appPath) {
1016 $recursive = false;
1017 }
1018 if ($recursive === false) {
1019 if ($this->__load($path . DS . $file)) {
1020 return $path . DS;
1021 }
1022 continue;
1023 }
1024  
1025 if (!isset($this->__paths[$path])) {
1026 if (!class_exists('Folder')) {
1027 require LIBS . 'folder.php';
1028 }
1029 $Folder =& new Folder();
1030 $directories = $Folder->tree($path, array('.svn', '.git', 'CVS', 'tests', 'templates'), 'dir');
1031 sort($directories);
1032 $this->__paths[$path] = $directories;
1033 }
1034  
1035 foreach ($this->__paths[$path] as $directory) {
1036 if ($this->__load($directory . DS . $file)) {
1037 return $directory . DS;
1038 }
1039 }
1040 }
1041 return null;
1042 }
1043  
1044 /**
1045 * Attempts to load $file.
1046 *
1047 * @param string $file full path to file including file name
1048 * @return boolean
1049 * @access private
1050 */
1051 function __load($file) {
1052 if (empty($file)) {
1053 return false;
1054 }
1055 if (!$this->return && isset($this->__loaded[$file])) {
1056 return true;
1057 }
1058 if (file_exists($file)) {
1059 if (!$this->return) {
1060 require($file);
1061 $this->__loaded[$file] = true;
1062 }
1063 return true;
1064 }
1065 return false;
1066 }
1067  
1068 /**
1069 * Maps the $name to the $file.
1070 *
1071 * @param string $file full path to file
1072 * @param string $name unique name for this map
1073 * @param string $type type object being mapped
1074 * @param string $plugin camelized if object is from a plugin, the name of the plugin
1075 * @return void
1076 * @access private
1077 */
1078 function __map($file, $name, $type, $plugin) {
1079 if ($plugin) {
1080 $this->__map['Plugin'][$plugin][$type][$name] = $file;
1081 } else {
1082 $this->__map[$type][$name] = $file;
1083 }
1084 }
1085  
1086 /**
1087 * Returns a file's complete path.
1088 *
1089 * @param string $name unique name
1090 * @param string $type type object
1091 * @param string $plugin camelized if object is from a plugin, the name of the plugin
1092 * @return mixed, file path if found, false otherwise
1093 * @access private
1094 */
1095 function __mapped($name, $type, $plugin) {
1096 if ($plugin) {
1097 if (isset($this->__map['Plugin'][$plugin][$type]) && isset($this->__map['Plugin'][$plugin][$type][$name])) {
1098 return $this->__map['Plugin'][$plugin][$type][$name];
1099 }
1100 return false;
1101 }
1102  
1103 if (isset($this->__map[$type]) && isset($this->__map[$type][$name])) {
1104 return $this->__map[$type][$name];
1105 }
1106 return false;
1107 }
1108  
1109 /**
1110 * Used to overload objects as needed.
1111 *
1112 * @param string $type Model or Helper
1113 * @param string $name Class name to overload
1114 * @access private
1115 */
1116 function __overload($type, $name, $parent) {
1117 if (($type === 'Model' || $type === 'Helper') && $parent !== false) {
1118 Overloadable::overload($name);
1119 }
1120 }
1121  
1122 /**
1123 * Loads parent classes based on $type.
1124 * Returns a prefix or suffix needed for loading files.
1125 *
1126 * @param string $type type of object
1127 * @param string $plugin camelized name of plugin
1128 * @param boolean $parent false will not attempt to load parent
1129 * @return array
1130 * @access private
1131 */
1132 function __settings($type, $plugin, $parent) {
1133 if (!$parent) {
1134 return array('class' => null, 'suffix' => null, 'path' => null);
1135 }
1136  
1137 if ($plugin) {
1138 $pluginPath = Inflector::underscore($plugin);
1139 }
1140 $path = null;
1141 $load = strtolower($type);
1142  
1143 switch ($load) {
1144 case 'model':
1145 if (!class_exists('Model')) {
1146 require LIBS . 'model' . DS . 'model.php';
1147 }
1148 if (!class_exists('AppModel')) {
1149 App::import($type, 'AppModel', false);
1150 }
1151 if ($plugin) {
1152 if (!class_exists($plugin . 'AppModel')) {
1153 App::import($type, $plugin . '.' . $plugin . 'AppModel', false, array(), $pluginPath . DS . $pluginPath . '_app_model.php');
1154 }
1155 $path = $pluginPath . DS . 'models' . DS;
1156 }
1157 return array('class' => null, 'suffix' => null, 'path' => $path);
1158 break;
1159 case 'behavior':
1160 if ($plugin) {
1161 $path = $pluginPath . DS . 'models' . DS . 'behaviors' . DS;
1162 }
1163 return array('class' => $type, 'suffix' => null, 'path' => $path);
1164 break;
1165 case 'datasource':
1166 if ($plugin) {
1167 $path = $pluginPath . DS . 'models' . DS . 'datasources' . DS;
1168 }
1169 return array('class' => $type, 'suffix' => null, 'path' => $path);
1170 case 'controller':
1171 App::import($type, 'AppController', false);
1172 if ($plugin) {
1173 App::import($type, $plugin . '.' . $plugin . 'AppController', false, array(), $pluginPath . DS . $pluginPath . '_app_controller.php');
1174 $path = $pluginPath . DS . 'controllers' . DS;
1175 }
1176 return array('class' => $type, 'suffix' => $type, 'path' => $path);
1177 break;
1178 case 'component':
1179 if ($plugin) {
1180 $path = $pluginPath . DS . 'controllers' . DS . 'components' . DS;
1181 }
1182 return array('class' => $type, 'suffix' => null, 'path' => $path);
1183 break;
1184 case 'lib':
1185 if ($plugin) {
1186 $path = $pluginPath . DS . 'libs' . DS;
1187 }
1188 return array('class' => null, 'suffix' => null, 'path' => $path);
1189 break;
1190 case 'view':
1191 if ($plugin) {
1192 $path = $pluginPath . DS . 'views' . DS;
1193 }
1194 return array('class' => $type, 'suffix' => null, 'path' => $path);
1195 break;
1196 case 'helper':
1197 if (!class_exists('AppHelper')) {
1198 App::import($type, 'AppHelper', false);
1199 }
1200 if ($plugin) {
1201 $path = $pluginPath . DS . 'views' . DS . 'helpers' . DS;
1202 }
1203 return array('class' => $type, 'suffix' => null, 'path' => $path);
1204 break;
1205 case 'vendor':
1206 if ($plugin) {
1207 $path = $pluginPath . DS . 'vendors' . DS;
1208 }
1209 return array('class' => null, 'suffix' => null, 'path' => $path);
1210 break;
1211 default:
1212 $type = $suffix = $path = null;
1213 break;
1214 }
1215 return array('class' => null, 'suffix' => null, 'path' => null);
1216 }
1217  
1218 /**
1219 * Returns default search paths.
1220 *
1221 * @param string $type type of object to be searched
1222 * @return array list of paths
1223 * @access private
1224 */
1225 function __paths($type) {
1226 $type = strtolower($type);
1227 $paths = array();
1228  
1229 if ($type === 'core') {
1230 return App::core('libs');
1231 }
1232 if (isset($this->{$type . 's'})) {
1233 return $this->{$type . 's'};
1234 }
1235 return $paths;
1236 }
1237  
1238 /**
1239 * Removes file location from map if the file has been deleted.
1240 *
1241 * @param string $name name of object
1242 * @param string $type type of object
1243 * @param string $plugin camelized name of plugin
1244 * @return void
1245 * @access private
1246 */
1247 function __remove($name, $type, $plugin) {
1248 if ($plugin) {
1249 unset($this->__map['Plugin'][$plugin][$type][$name]);
1250 } else {
1251 unset($this->__map[$type][$name]);
1252 }
1253 }
1254  
1255 /**
1256 * Returns an array of filenames of PHP files in the given directory.
1257 *
1258 * @param string $path Path to scan for files
1259 * @param string $suffix if false, return only directories. if string, match and return files
1260 * @return array List of directories or files in directory
1261 * @access private
1262 */
1263 function __list($path, $suffix = false, $extension = false) {
1264 if (!class_exists('Folder')) {
1265 require LIBS . 'folder.php';
1266 }
1267 $items = array();
1268 $Folder =& new Folder($path);
1269 $contents = $Folder->read(false, true);
1270  
1271 if (is_array($contents)) {
1272 if (!$suffix) {
1273 return $contents[0];
1274 } else {
1275 foreach ($contents[1] as $item) {
1276 if (substr($item, - strlen($suffix)) === $suffix) {
1277 if ($extension) {
1278 $items[] = $item;
1279 } else {
1280 $items[] = substr($item, 0, strlen($item) - strlen($suffix));
1281 }
1282 }
1283 }
1284 }
1285 }
1286 return $items;
1287 }
1288
1289 /**
1290 * Determines if $__maps, $__objects and $__paths cache should be reset.
1291 *
1292 * @param boolean $reset
1293 * @return boolean
1294 * @access private
1295 */
1296 function __resetCache($reset = null) {
1297 static $cache = array();
1298 if (!$cache && $reset === true) {
1299 $cache = true;
1300 }
1301 return $cache;
1302 }
1303  
1304 /**
1305 * Object destructor.
1306 *
1307 * Writes cache file if changes have been made to the $__map or $__paths
1308 *
1309 * @return void
1310 * @access private
1311 */
1312 function __destruct() {
1313 if ($this->__resetCache() === true) {
1314 $core = App::core('cake');
1315 unset($this->__paths[rtrim($core[0], DS)]);
1316 Cache::write('dir_map', array_filter($this->__paths), '_cake_core_');
1317 Cache::write('file_map', array_filter($this->__map), '_cake_core_');
1318 Cache::write('object_map', $this->__objects, '_cake_core_');
1319 }
1320 }
1321 }
1322  
1323