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