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
* Determines if $__maps and $__paths cache should be written.
588
var $__cache = false;
591
* Holds key/value pairs of $type => file path.
596
var $__map = array();
599
* Holds paths for deep searching of files.
604
var $__paths = array();
607
* Holds loaded files.
612
var $__loaded = array();
615
* Holds and key => value array of object types.
620
var $__objects = array();
623
* Used to read information stored path
627
* `App::path('models'); will return all paths for models`
629
* @param string $type type of path
630
* @return string array
633
function path($type) {
634
$_this =& App::getInstance();
635
if (!isset($_this->{$type})) {
638
return $_this->{$type};
642
* Build path references. Merges the supplied $paths
643
* with the base paths and the default core paths.
645
* @param array $paths paths defines in config/bootstrap.php
646
* @param boolean $reset true will set paths, false merges paths [default] false
650
function build($paths = array(), $reset = false) {
651
$_this =& App::getInstance();
653
'models' => array(MODELS),
654
'behaviors' => array(BEHAVIORS),
655
'datasources' => array(MODELS . 'datasources'),
656
'controllers' => array(CONTROLLERS),
657
'components' => array(COMPONENTS),
658
'libs' => array(APPLIBS),
659
'views' => array(VIEWS),
660
'helpers' => array(HELPERS),
661
'locales' => array(APP . 'locale' . DS),
662
'shells' => array(APP . 'vendors' . DS . 'shells' . DS, VENDORS . 'shells' . DS),
663
'vendors' => array(APP . 'vendors' . DS, VENDORS),
664
'plugins' => array(APP . 'plugins' . DS)
667
if ($reset == true) {
668
foreach ($paths as $type => $new) {
669
$_this->{$type} = (array)$new;
674
$core = $_this->core();
675
$app = array('models' => true, 'controllers' => true, 'helpers' => true);
677
foreach ($defaults as $type => $default) {
680
if (isset($app[$type])) {
683
if (isset($core[$type])) {
684
$merge = array_merge($merge, (array)$core[$type]);
687
if (empty($_this->{$type}) || empty($paths)) {
688
$_this->{$type} = $default;
691
if (!empty($paths[$type])) {
692
$path = array_flip(array_flip(array_merge(
693
(array)$paths[$type], $_this->{$type}, $merge
695
$_this->{$type} = array_values($path);
697
$path = array_flip(array_flip(array_merge($_this->{$type}, $merge)));
698
$_this->{$type} = array_values($path);
704
* Get the path that a plugin is on. Searches through the defined plugin paths.
706
* @param string $plugin CamelCased/lower_cased plugin name to find the path of.
707
* @return string full path to the plugin.
709
function pluginPath($plugin) {
710
$_this =& App::getInstance();
711
$pluginDir = Inflector::underscore($plugin);
712
for ($i = 0, $length = count($_this->plugins); $i < $length; $i++) {
713
if (is_dir($_this->plugins[$i] . $pluginDir)) {
714
return $_this->plugins[$i] . $pluginDir . DS ;
717
return $_this->plugins[0] . $pluginDir . DS;
721
* Find the path that a theme is on. Search through the defined theme paths.
723
* @param string $theme lower_cased theme name to find the path of.
724
* @return string full path to the theme.
726
function themePath($theme) {
727
$_this =& App::getInstance();
728
$themeDir = 'themed' . DS . Inflector::underscore($theme);
729
for ($i = 0, $length = count($_this->views); $i < $length; $i++) {
730
if (is_dir($_this->views[$i] . $themeDir)) {
731
return $_this->views[$i] . $themeDir . DS ;
734
return $_this->views[0] . $themeDir . DS;
738
* Returns a key/value list of all paths where core libs are found.
739
* Passing $type only returns the values for a given value of $key.
741
* @param string $type valid values are: 'model', 'behavior', 'controller', 'component',
742
* 'view', 'helper', 'datasource', 'libs', and 'cake'
743
* @return array numeric keyed array of core lib paths
746
function core($type = null) {
747
static $paths = false;
748
if ($paths === false) {
749
$paths = Cache::read('core_paths', '_cake_core_');
753
$libs = dirname(__FILE__) . DS;
754
$cake = dirname($libs) . DS;
755
$path = dirname($cake) . DS;
757
$paths['cake'][] = $cake;
758
$paths['libs'][] = $libs;
759
$paths['models'][] = $libs . 'model' . DS;
760
$paths['datasources'][] = $libs . 'model' . DS . 'datasources' . DS;
761
$paths['behaviors'][] = $libs . 'model' . DS . 'behaviors' . DS;
762
$paths['controllers'][] = $libs . 'controller' . DS;
763
$paths['components'][] = $libs . 'controller' . DS . 'components' . DS;
764
$paths['views'][] = $libs . 'view' . DS;
765
$paths['helpers'][] = $libs . 'view' . DS . 'helpers' . DS;
766
$paths['plugins'][] = $path . 'plugins' . DS;
767
$paths['vendors'][] = $path . 'vendors' . DS;
768
$paths['shells'][] = $cake . 'console' . DS . 'libs' . DS;
770
Cache::write('core_paths', array_filter($paths), '_cake_core_');
772
if ($type && isset($paths[$type])) {
773
return $paths[$type];
779
* Returns an array of objects of the given type.
783
* `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
785
* @param string $type Type of object, i.e. 'model', 'controller', 'helper', or 'plugin'
786
* @param mixed $path Optional Scan only the path given. If null, paths for the chosen
788
* @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
789
* @return mixed Either false on incorrect / miss. Or an array of found objects.
792
function objects($type, $path = null, $cache = true) {
797
if ($type === 'file' && !$path) {
799
} elseif ($type === 'file') {
801
$name = $type . str_replace(DS, '', $path);
803
$_this =& App::getInstance();
805
if (empty($_this->__objects) && $cache === true) {
806
$_this->__objects = Cache::read('object_map', '_cake_core_');
809
if (!isset($_this->__objects[$name]) || $cache !== true) {
810
$types = $_this->types;
812
if (!isset($types[$type])) {
818
$path = $_this->{"{$type}s"};
819
if (isset($types[$type]['core']) && $types[$type]['core'] === false) {
825
foreach ((array)$path as $dir) {
827
$items = $_this->__list($dir, $types[$type]['suffix'], $extension);
828
$objects = array_merge($items, array_diff($objects, $items));
832
if ($type !== 'file') {
833
foreach ($objects as $key => $value) {
834
$objects[$key] = Inflector::camelize($value);
838
if ($cache === true) {
839
$_this->__cache = true;
841
$_this->__objects[$name] = $objects;
844
return $_this->__objects[$name];
848
* Finds classes based on $name or specific file(s) to search. Calling App::import() will
849
* not construct any classes contained in the files. It will only find and require() the file.
851
* @link http://book.cakephp.org/view/934/Using-App-import
852
* @param mixed $type The type of Class if passed as a string, or all params can be passed as
853
* an single array to $type,
854
* @param string $name Name of the Class or a unique name for the file
855
* @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
856
* array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
857
* $ext allows setting the extension of the file name
858
* based on Inflector::underscore($name) . ".$ext";
859
* @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
860
* @param string $file full name of the file to search for including extension
861
* @param boolean $return, return the loaded file, the file must have a return
862
* statement in it to work: return $variable;
863
* @return boolean true if Class is already in memory or if file is found and loaded, false if not
866
function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
867
$plugin = $directory = null;
869
if (is_array($type)) {
870
extract($type, EXTR_OVERWRITE);
873
if (is_array($parent)) {
874
extract($parent, EXTR_OVERWRITE);
877
if ($name === null && $file === null) {
880
} elseif ($name === null) {
884
if (is_array($name)) {
885
foreach ($name as $class) {
889
if (strpos($class, '.') !== false) {
890
$value = explode('.', $class);
891
$count = count($value);
894
$tempType = $value[0];
895
$plugin = $value[1] . '.';
897
} elseif ($count === 2 && ($type === 'Core' || $type === 'File')) {
898
$tempType = $value[0];
901
$plugin = $value[0] . '.';
906
if (!App::import($tempType, $plugin . $class, $parent)) {
913
if ($name != null && strpos($name, '.') !== false) {
914
list($plugin, $name) = explode('.', $name);
915
$plugin = Inflector::camelize($plugin);
917
$_this =& App::getInstance();
918
$_this->return = $return;
921
$file = Inflector::underscore($name) . ".{$ext}";
923
$ext = $_this->__settings($type, $plugin, $parent);
924
if ($name != null && !class_exists($name . $ext['class'])) {
925
if ($load = $_this->__mapped($name . $ext['class'], $type, $plugin)) {
926
if ($_this->__load($load)) {
927
$_this->__overload($type, $name . $ext['class'], $parent);
929
if ($_this->return) {
930
return include($load);
934
$_this->__remove($name . $ext['class'], $type, $plugin);
935
$_this->__cache = true;
938
if (!empty($search)) {
939
$_this->search = $search;
941
$_this->search = $_this->__paths('plugin');
943
$_this->search = $_this->__paths($type);
947
if ($find === null) {
948
$find = Inflector::underscore($name . $ext['suffix']).'.php';
951
$paths = $_this->search;
952
foreach ($paths as $key => $value) {
953
$_this->search[$key] = $value . $ext['path'];
958
if (strtolower($type) !== 'vendor' && empty($search) && $_this->__load($file)) {
962
$directory = $_this->__find($find, true);
965
if ($directory !== null) {
966
$_this->__cache = true;
967
$_this->__map($directory . $file, $name . $ext['class'], $type, $plugin);
968
$_this->__overload($type, $name . $ext['class'], $parent);
970
if ($_this->return) {
971
return include($directory . $file);
981
* Returns a single instance of App.
986
function &getInstance() {
987
static $instance = array();
989
$instance[0] =& new App();
990
$instance[0]->__map = (array)Cache::read('file_map', '_cake_core_');
996
* Locates the $file in $__paths, searches recursively.
998
* @param string $file full file name
999
* @param boolean $recursive search $__paths recursively
1000
* @return mixed boolean on fail, $file directory path on success
1003
function __find($file, $recursive = true) {
1004
static $appPath = false;
1006
if (empty($this->search)) {
1008
} elseif (is_string($this->search)) {
1009
$this->search = array($this->search);
1012
if (empty($this->__paths)) {
1013
$this->__paths = Cache::read('dir_map', '_cake_core_');
1016
foreach ($this->search as $path) {
1017
if ($appPath === false) {
1018
$appPath = rtrim(APP, DS);
1020
$path = rtrim($path, DS);
1022
if ($path === $appPath) {
1025
if ($recursive === false) {
1026
if ($this->__load($path . DS . $file)) {
1032
if (!isset($this->__paths[$path])) {
1033
if (!class_exists('Folder')) {
1034
require LIBS . 'folder.php';
1036
$Folder =& new Folder();
1037
$directories = $Folder->tree($path, array('.svn', '.git', 'CVS', 'tests', 'templates'), 'dir');
1039
$this->__paths[$path] = $directories;
1042
foreach ($this->__paths[$path] as $directory) {
1043
if ($this->__load($directory . DS . $file)) {
1044
return $directory . DS;
1052
* Attempts to load $file.
1054
* @param string $file full path to file including file name
1058
function __load($file) {
1062
if (!$this->return && isset($this->__loaded[$file])) {
1065
if (file_exists($file)) {
1066
if (!$this->return) {
1068
$this->__loaded[$file] = true;
1076
* Maps the $name to the $file.
1078
* @param string $file full path to file
1079
* @param string $name unique name for this map
1080
* @param string $type type object being mapped
1081
* @param string $plugin camelized if object is from a plugin, the name of the plugin
1085
function __map($file, $name, $type, $plugin) {
1087
$this->__map['Plugin'][$plugin][$type][$name] = $file;
1089
$this->__map[$type][$name] = $file;
1094
* Returns a file's complete path.
1096
* @param string $name unique name
1097
* @param string $type type object
1098
* @param string $plugin camelized if object is from a plugin, the name of the plugin
1099
* @return mixed, file path if found, false otherwise
1102
function __mapped($name, $type, $plugin) {
1104
if (isset($this->__map['Plugin'][$plugin][$type]) && isset($this->__map['Plugin'][$plugin][$type][$name])) {
1105
return $this->__map['Plugin'][$plugin][$type][$name];
1110
if (isset($this->__map[$type]) && isset($this->__map[$type][$name])) {
1111
return $this->__map[$type][$name];
1117
* Used to overload objects as needed.
1119
* @param string $type Model or Helper
1120
* @param string $name Class name to overload
1123
function __overload($type, $name, $parent) {
1124
if (($type === 'Model' || $type === 'Helper') && $parent !== false) {
1125
Overloadable::overload($name);
1130
* Loads parent classes based on $type.
1131
* Returns a prefix or suffix needed for loading files.
1133
* @param string $type type of object
1134
* @param string $plugin camelized name of plugin
1135
* @param boolean $parent false will not attempt to load parent
1139
function __settings($type, $plugin, $parent) {
1141
return array('class' => null, 'suffix' => null, 'path' => null);
1145
$pluginPath = Inflector::underscore($plugin);
1148
$load = strtolower($type);
1152
if (!class_exists('Model')) {
1153
require LIBS . 'model' . DS . 'model.php';
1155
if (!class_exists('AppModel')) {
1156
App::import($type, 'AppModel', false);
1159
if (!class_exists($plugin . 'AppModel')) {
1160
App::import($type, $plugin . '.' . $plugin . 'AppModel', false, array(), $pluginPath . DS . $pluginPath . '_app_model.php');
1162
$path = $pluginPath . DS . 'models' . DS;
1164
return array('class' => null, 'suffix' => null, 'path' => $path);
1168
$path = $pluginPath . DS . 'models' . DS . 'behaviors' . DS;
1170
return array('class' => $type, 'suffix' => null, 'path' => $path);
1174
$path = $pluginPath . DS . 'models' . DS . 'datasources' . DS;
1176
return array('class' => $type, 'suffix' => null, 'path' => $path);
1178
App::import($type, 'AppController', false);
1180
App::import($type, $plugin . '.' . $plugin . 'AppController', false, array(), $pluginPath . DS . $pluginPath . '_app_controller.php');
1181
$path = $pluginPath . DS . 'controllers' . DS;
1183
return array('class' => $type, 'suffix' => $type, 'path' => $path);
1187
$path = $pluginPath . DS . 'controllers' . DS . 'components' . DS;
1189
return array('class' => $type, 'suffix' => null, 'path' => $path);
1193
$path = $pluginPath . DS . 'libs' . DS;
1195
return array('class' => null, 'suffix' => null, 'path' => $path);
1199
$path = $pluginPath . DS . 'views' . DS;
1201
return array('class' => $type, 'suffix' => null, 'path' => $path);
1204
if (!class_exists('AppHelper')) {
1205
App::import($type, 'AppHelper', false);
1208
$path = $pluginPath . DS . 'views' . DS . 'helpers' . DS;
1210
return array('class' => $type, 'suffix' => null, 'path' => $path);
1214
$path = $pluginPath . DS . 'vendors' . DS;
1216
return array('class' => null, 'suffix' => null, 'path' => $path);
1219
$type = $suffix = $path = null;
1222
return array('class' => null, 'suffix' => null, 'path' => null);
1226
* Returns default search paths.
1228
* @param string $type type of object to be searched
1229
* @return array list of paths
1232
function __paths($type) {
1233
$type = strtolower($type);
1236
if ($type === 'core') {
1237
return App::core('libs');
1239
if (isset($this->{$type . 's'})) {
1240
return $this->{$type . 's'};
1246
* Removes file location from map if the file has been deleted.
1248
* @param string $name name of object
1249
* @param string $type type of object
1250
* @param string $plugin camelized name of plugin
1254
function __remove($name, $type, $plugin) {
1256
unset($this->__map['Plugin'][$plugin][$type][$name]);
1258
unset($this->__map[$type][$name]);
1263
* Returns an array of filenames of PHP files in the given directory.
1265
* @param string $path Path to scan for files
1266
* @param string $suffix if false, return only directories. if string, match and return files
1267
* @return array List of directories or files in directory
1270
function __list($path, $suffix = false, $extension = false) {
1271
if (!class_exists('Folder')) {
1272
require LIBS . 'folder.php';
1275
$Folder =& new Folder($path);
1276
$contents = $Folder->read(false, true);
1278
if (is_array($contents)) {
1280
return $contents[0];
1282
foreach ($contents[1] as $item) {
1283
if (substr($item, - strlen($suffix)) === $suffix) {
1287
$items[] = substr($item, 0, strlen($item) - strlen($suffix));
1297
* Object destructor.
1299
* Writes cache file if changes have been made to the $__map or $__paths
1304
function __destruct() {
1305
if ($this->__cache) {
1306
$core = App::core('cake');
1307
unset($this->__paths[rtrim($core[0], DS)]);
1308
Cache::write('dir_map', array_filter($this->__paths), '_cake_core_');
1309
Cache::write('file_map', array_filter($this->__map), '_cake_core_');
1310
Cache::write('object_map', $this->__objects, '_cake_core_');