PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/cake/libs/configure.php

http://skygames.googlecode.com/
PHP | 1171 lines | 714 code | 68 blank | 389 comment | 167 complexity | 3162ee174755e1e4fbf059e9fe3b4bd0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, CC-BY-SA-3.0
  1. <?php
  2. /* SVN FILE: $Id: configure.php 7847 2008-11-08 02:54:07Z renan.saddam $ */
  3. /**
  4. * Short description for file.
  5. *
  6. * Long description for filec
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  19. * @package cake
  20. * @subpackage cake.cake.libs
  21. * @since CakePHP(tm) v 1.0.0.2363
  22. * @version $Revision: 7847 $
  23. * @modifiedby $LastChangedBy: renan.saddam $
  24. * @lastmodified $Date: 2008-11-07 20:54:07 -0600 (Fri, 07 Nov 2008) $
  25. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  26. */
  27. /**
  28. * Configuration class (singleton). Used for managing runtime configuration information.
  29. *
  30. * @package cake
  31. * @subpackage cake.cake.libs
  32. * @link http://book.cakephp.org/view/42/The-Configuration-Class
  33. */
  34. class Configure extends Object {
  35. /**
  36. * List of additional path(s) where model files reside.
  37. *
  38. * @var array
  39. * @access public
  40. */
  41. var $modelPaths = array();
  42. /**
  43. * List of additional path(s) where behavior files reside.
  44. *
  45. * @var array
  46. * @access public
  47. */
  48. var $behaviorPaths = array();
  49. /**
  50. * List of additional path(s) where controller files reside.
  51. *
  52. * @var array
  53. * @access public
  54. */
  55. var $controllerPaths = array();
  56. /**
  57. * List of additional path(s) where component files reside.
  58. *
  59. * @var array
  60. * @access public
  61. */
  62. var $componentPaths = array();
  63. /**
  64. * List of additional path(s) where view files reside.
  65. *
  66. * @var array
  67. * @access public
  68. */
  69. var $viewPaths = array();
  70. /**
  71. * List of additional path(s) where helper files reside.
  72. *
  73. * @var array
  74. * @access public
  75. */
  76. var $helperPaths = array();
  77. /**
  78. * List of additional path(s) where plugins reside.
  79. *
  80. * @var array
  81. * @access public
  82. */
  83. var $pluginPaths = array();
  84. /**
  85. * List of additional path(s) where vendor packages reside.
  86. *
  87. * @var array
  88. * @access public
  89. */
  90. var $vendorPaths = array();
  91. /**
  92. * List of additional path(s) where locale files reside.
  93. *
  94. * @var array
  95. * @access public
  96. */
  97. var $localePaths = array();
  98. /**
  99. * List of additional path(s) where console shell files reside.
  100. *
  101. * @var array
  102. * @access public
  103. */
  104. var $shellPaths = array();
  105. /**
  106. * Current debug level.
  107. *
  108. * @link http://book.cakephp.org/view/44/CakePHP-Core-Configuration-Variables
  109. * @var integer
  110. * @access public
  111. */
  112. var $debug = null;
  113. /**
  114. * Determines if $__objects cache should be written.
  115. *
  116. * @var boolean
  117. * @access private
  118. */
  119. var $__cache = false;
  120. /**
  121. * Holds and key => value array of objects' types.
  122. *
  123. * @var array
  124. * @access private
  125. */
  126. var $__objects = array();
  127. /**
  128. * Returns a singleton instance of the Configure class.
  129. *
  130. * @return Configure instance
  131. * @access public
  132. */
  133. function &getInstance($boot = true) {
  134. static $instance = array();
  135. if (!$instance) {
  136. $instance[0] =& new Configure();
  137. $instance[0]->__loadBootstrap($boot);
  138. }
  139. return $instance[0];
  140. }
  141. /**
  142. * Returns an index of objects of the given type, with the physical path to each object.
  143. *
  144. * @param string $type Type of object, i.e. 'model', 'controller', 'helper', or 'plugin'
  145. * @param mixed $path Optional
  146. * @return Configure instance
  147. * @access public
  148. */
  149. function listObjects($type, $path = null, $cache = true) {
  150. $objects = array();
  151. $extension = false;
  152. $name = $type;
  153. if ($type === 'file' && !$path) {
  154. return false;
  155. } elseif ($type === 'file') {
  156. $extension = true;
  157. $name = $type . str_replace(DS, '', $path);
  158. }
  159. $_this =& Configure::getInstance();
  160. if (empty($_this->__objects) && $cache === true) {
  161. $_this->__objects = Cache::read('object_map', '_cake_core_');
  162. }
  163. if (empty($_this->__objects) || !isset($_this->__objects[$type]) || $cache !== true) {
  164. $types = array(
  165. 'model' => array('suffix' => '.php', 'base' => 'AppModel', 'core' => false),
  166. 'behavior' => array('suffix' => '.php', 'base' => 'ModelBehavior'),
  167. 'controller' => array('suffix' => '_controller.php', 'base' => 'AppController'),
  168. 'component' => array('suffix' => '.php', 'base' => null),
  169. 'view' => array('suffix' => '.php', 'base' => null),
  170. 'helper' => array('suffix' => '.php', 'base' => 'AppHelper'),
  171. 'plugin' => array('suffix' => '', 'base' => null),
  172. 'vendor' => array('suffix' => '', 'base' => null),
  173. 'class' => array('suffix' => '.php', 'base' => null),
  174. 'file' => array('suffix' => '.php', 'base' => null)
  175. );
  176. if (!isset($types[$type])) {
  177. return false;
  178. }
  179. $objects = array();
  180. if (empty($path)) {
  181. $path = $_this->{$type . 'Paths'};
  182. if (isset($types[$type]['core']) && $types[$type]['core'] === false) {
  183. array_pop($path);
  184. }
  185. }
  186. $items = array();
  187. foreach ((array)$path as $dir) {
  188. if ($type === 'file' || $type === 'class' || strpos($dir, $type) !== false) {
  189. $items = $_this->__list($dir, $types[$type]['suffix'], $extension);
  190. $objects = array_merge($items, array_diff($objects, $items));
  191. }
  192. }
  193. if ($type !== 'file') {
  194. foreach ($objects as $key => $value) {
  195. $objects[$key] = Inflector::camelize($value);
  196. }
  197. }
  198. if ($cache === true && !empty($objects)) {
  199. $_this->__objects[$name] = $objects;
  200. $_this->__cache = true;
  201. } else {
  202. return $objects;
  203. }
  204. }
  205. return $_this->__objects[$name];
  206. }
  207. /**
  208. * Returns an array of filenames of PHP files in the given directory.
  209. *
  210. * @param string $path Path to scan for files
  211. * @param string $suffix if false, return only directories. if string, match and return files
  212. * @return array List of directories or files in directory
  213. */
  214. function __list($path, $suffix = false, $extension = false) {
  215. if (!class_exists('Folder')) {
  216. require LIBS . 'folder.php';
  217. }
  218. $items = array();
  219. $Folder =& new Folder($path);
  220. $contents = $Folder->read(false, true);
  221. if (is_array($contents)) {
  222. if (!$suffix) {
  223. return $contents[0];
  224. } else {
  225. foreach ($contents[1] as $item) {
  226. if (substr($item, - strlen($suffix)) === $suffix) {
  227. if ($extension) {
  228. $items[] = $item;
  229. } else {
  230. $items[] = substr($item, 0, strlen($item) - strlen($suffix));
  231. }
  232. }
  233. }
  234. }
  235. }
  236. return $items;
  237. }
  238. /**
  239. * Used to store a dynamic variable in the Configure instance.
  240. *
  241. * Usage
  242. * Configure::write('One.key1', 'value of the Configure::One[key1]');
  243. * Configure::write(array('One.key1' => 'value of the Configure::One[key1]'));
  244. * Configure::write('One', array('key1'=>'value of the Configure::One[key1]', 'key2'=>'value of the Configure::One[key2]');
  245. * Configure::write(array('One.key1' => 'value of the Configure::One[key1]', 'One.key2' => 'value of the Configure::One[key2]'));
  246. *
  247. * @link http://book.cakephp.org/view/412/write
  248. * @param array $config Name of var to write
  249. * @param mixed $value Value to set for var
  250. * @return void
  251. * @access public
  252. */
  253. function write($config, $value = null) {
  254. $_this =& Configure::getInstance();
  255. if (!is_array($config)) {
  256. $config = array($config => $value);
  257. }
  258. foreach ($config as $names => $value) {
  259. $name = $_this->__configVarNames($names);
  260. switch (count($name)) {
  261. case 3:
  262. $_this->{$name[0]}[$name[1]][$name[2]] = $value;
  263. break;
  264. case 2:
  265. $_this->{$name[0]}[$name[1]] = $value;
  266. break;
  267. case 1:
  268. $_this->{$name[0]} = $value;
  269. break;
  270. }
  271. }
  272. if (isset($config['debug'])) {
  273. if ($_this->debug) {
  274. error_reporting(E_ALL);
  275. if (function_exists('ini_set')) {
  276. ini_set('display_errors', 1);
  277. }
  278. if (!class_exists('Debugger')) {
  279. require LIBS . 'debugger.php';
  280. }
  281. if (!class_exists('CakeLog')) {
  282. require LIBS . 'cake_log.php';
  283. }
  284. Configure::write('log', LOG_NOTICE);
  285. } else {
  286. error_reporting(0);
  287. Configure::write('log', LOG_NOTICE);
  288. }
  289. }
  290. }
  291. /**
  292. * Used to read information stored in the Configure instance.
  293. *
  294. * Usage
  295. * Configure::read('Name'); will return all values for Name
  296. * Configure::read('Name.key'); will return only the value of Configure::Name[key]
  297. *
  298. * @link http://book.cakephp.org/view/413/read
  299. * @param string $var Variable to obtain
  300. * @return string value of Configure::$var
  301. * @access public
  302. */
  303. function read($var = 'debug') {
  304. $_this =& Configure::getInstance();
  305. if ($var === 'debug') {
  306. if (!isset($_this->debug)) {
  307. if (defined('DEBUG')) {
  308. $_this->debug = DEBUG;
  309. } else {
  310. $_this->debug = 0;
  311. }
  312. }
  313. return $_this->debug;
  314. }
  315. $name = $_this->__configVarNames($var);
  316. switch (count($name)) {
  317. case 3:
  318. if (isset($_this->{$name[0]}[$name[1]][$name[2]])) {
  319. return $_this->{$name[0]}[$name[1]][$name[2]];
  320. }
  321. break;
  322. case 2:
  323. if (isset($_this->{$name[0]}[$name[1]])) {
  324. return $_this->{$name[0]}[$name[1]];
  325. }
  326. break;
  327. case 1:
  328. if (isset($_this->{$name[0]})) {
  329. return $_this->{$name[0]};
  330. }
  331. break;
  332. }
  333. return null;
  334. }
  335. /**
  336. * Used to delete a variable from the Configure instance.
  337. *
  338. * Usage:
  339. * Configure::delete('Name'); will delete the entire Configure::Name
  340. * Configure::delete('Name.key'); will delete only the Configure::Name[key]
  341. *
  342. * @link http://book.cakephp.org/view/414/delete
  343. * @param string $var the var to be deleted
  344. * @return void
  345. * @access public
  346. */
  347. function delete($var = null) {
  348. $_this =& Configure::getInstance();
  349. $name = $_this->__configVarNames($var);
  350. if (count($name) > 1) {
  351. unset($_this->{$name[0]}[$name[1]]);
  352. } else {
  353. unset($_this->{$name[0]});
  354. }
  355. }
  356. /**
  357. * Loads a file from app/config/configure_file.php.
  358. * Config file variables should be formated like:
  359. * $config['name'] = 'value';
  360. * These will be used to create dynamic Configure vars.
  361. *
  362. * Usage Configure::load('configure_file');
  363. *
  364. * @link http://book.cakephp.org/view/415/load
  365. * @param string $fileName name of file to load, extension must be .php and only the name should be used, not the extenstion
  366. * @return mixed false if file not found, void if load successful
  367. * @access public
  368. */
  369. function load($fileName) {
  370. $found = false;
  371. if (file_exists(CONFIGS . $fileName . '.php')) {
  372. include(CONFIGS . $fileName . '.php');
  373. $found = true;
  374. } elseif (file_exists(CACHE . 'persistent' . DS . $fileName . '.php')) {
  375. include(CACHE . 'persistent' . DS . $fileName . '.php');
  376. $found = true;
  377. } else {
  378. foreach (Configure::corePaths('cake') as $key => $path) {
  379. if (file_exists($path . DS . 'config' . DS . $fileName . '.php')) {
  380. include($path . DS . 'config' . DS . $fileName . '.php');
  381. $found = true;
  382. break;
  383. }
  384. }
  385. }
  386. if (!$found) {
  387. return false;
  388. }
  389. if (!isset($config)) {
  390. trigger_error(sprintf(__("Configure::load() - no variable \$config found in %s.php", true), $fileName), E_USER_WARNING);
  391. return false;
  392. }
  393. return Configure::write($config);
  394. }
  395. /**
  396. * Used to determine the current version of CakePHP.
  397. *
  398. * Usage Configure::version();
  399. *
  400. * @link http://book.cakephp.org/view/416/version
  401. * @return string Current version of CakePHP
  402. * @access public
  403. */
  404. function version() {
  405. $_this =& Configure::getInstance();
  406. if (!isset($_this->Cake['version'])) {
  407. require(CORE_PATH . 'cake' . DS . 'config' . DS . 'config.php');
  408. $_this->write($config);
  409. }
  410. return $_this->Cake['version'];
  411. }
  412. /**
  413. * Used to write a config file to disk.
  414. *
  415. * Configure::store('Model', 'class.paths', array('Users' => array('path' => 'users', 'plugin' => true)));
  416. *
  417. * @param string $type Type of config file to write, ex: Models, Controllers, Helpers, Components
  418. * @param string $name file name.
  419. * @param array $data array of values to store.
  420. * @return void
  421. * @access public
  422. */
  423. function store($type, $name, $data = array()) {
  424. $write = true;
  425. $content = '';
  426. foreach ($data as $key => $value) {
  427. $content .= "\$config['$type']['$key']";
  428. if (is_array($value)) {
  429. $content .= " = array(";
  430. foreach ($value as $key1 => $value2) {
  431. $value2 = addslashes($value2);
  432. $content .= "'$key1' => '$value2', ";
  433. }
  434. $content .= ");\n";
  435. } else {
  436. $value = addslashes($value);
  437. $content .= " = '$value';\n";
  438. }
  439. }
  440. if (is_null($type)) {
  441. $write = false;
  442. }
  443. Configure::__writeConfig($content, $name, $write);
  444. }
  445. /**
  446. * Returns a key/value list of all paths where core libs are found.
  447. * Passing $type only returns the values for a given value of $key.
  448. *
  449. * @param string $type valid values are: 'model', 'behavior', 'controller', 'component', 'view', 'helper', 'libs', and 'cake'
  450. * @return array numeric keyed array of core lib paths
  451. * @access public
  452. */
  453. function corePaths($type = null) {
  454. $paths = Cache::read('core_paths', '_cake_core_');
  455. if (!$paths) {
  456. $paths = array();
  457. $openBasedir = ini_get('open_basedir');
  458. if ($openBasedir) {
  459. $all = explode(PATH_SEPARATOR, $openBasedir);
  460. $all = array_flip(array_flip((array_merge(array(CAKE_CORE_INCLUDE_PATH), $all))));
  461. } else {
  462. $all = explode(PATH_SEPARATOR, ini_get('include_path'));
  463. $all = array_flip(array_flip((array_merge(array(CAKE_CORE_INCLUDE_PATH), $all))));
  464. }
  465. foreach ($all as $path) {
  466. if ($path !== DS) {
  467. $path = rtrim($path, DS);
  468. }
  469. if (empty($path) || $path === '.') {
  470. continue;
  471. }
  472. $cake = $path . DS . 'cake' . DS;
  473. $libs = $cake . 'libs' . DS;
  474. if (is_dir($libs)) {
  475. $paths['libs'][] = $libs;
  476. $paths['model'][] = $libs . 'model' . DS;
  477. $paths['behavior'][] = $libs . 'model' . DS . 'behaviors' . DS;
  478. $paths['controller'][] = $libs . 'controller' . DS;
  479. $paths['component'][] = $libs . 'controller' . DS . 'components' . DS;
  480. $paths['view'][] = $libs . 'view' . DS;
  481. $paths['helper'][] = $libs . 'view' . DS . 'helpers' . DS;
  482. $paths['cake'][] = $cake;
  483. $paths['class'][] = $cake;
  484. $paths['vendor'][] = $path . DS . 'vendors' . DS;
  485. $paths['shell'][] = $cake . 'console' . DS . 'libs' . DS;
  486. break;
  487. }
  488. }
  489. Cache::write('core_paths', array_filter($paths), '_cake_core_');
  490. }
  491. if ($type && isset($paths[$type])) {
  492. return $paths[$type];
  493. }
  494. return $paths;
  495. }
  496. /**
  497. * Creates a cached version of a configuration file.
  498. * Appends values passed from Configure::store() to the cached file
  499. *
  500. * @param string $content Content to write on file
  501. * @param string $name Name to use for cache file
  502. * @param boolean $write true if content should be written, false otherwise
  503. * @return void
  504. * @access private
  505. */
  506. function __writeConfig($content, $name, $write = true) {
  507. $file = CACHE . 'persistent' . DS . $name . '.php';
  508. if (Configure::read() > 0) {
  509. $expires = "+10 seconds";
  510. } else {
  511. $expires = "+999 days";
  512. }
  513. $cache = cache('persistent' . DS . $name . '.php', null, $expires);
  514. if ($cache === null) {
  515. cache('persistent' . DS . $name . '.php', "<?php\n\$config = array();\n", $expires);
  516. }
  517. if ($write === true) {
  518. if (!class_exists('File')) {
  519. require LIBS . 'file.php';
  520. }
  521. $fileClass = new File($file);
  522. if ($fileClass->writable()) {
  523. $fileClass->append($content);
  524. }
  525. }
  526. }
  527. /**
  528. * Checks $name for dot notation to create dynamic Configure::$var as an array when needed.
  529. *
  530. * @param mixed $name Name to split
  531. * @return array Name separated in items through dot notation
  532. * @access private
  533. */
  534. function __configVarNames($name) {
  535. if (is_string($name)) {
  536. if (strpos($name, ".")) {
  537. return explode(".", $name);
  538. }
  539. return array($name);
  540. }
  541. return $name;
  542. }
  543. /**
  544. * Build path references. Merges the supplied $paths
  545. * with the base paths and the default core paths.
  546. *
  547. * @param array $paths paths defines in config/bootstrap.php
  548. * @return void
  549. * @access public
  550. */
  551. function buildPaths($paths) {
  552. $_this =& Configure::getInstance();
  553. $core = $_this->corePaths();
  554. $basePaths = array(
  555. 'model' => array(MODELS),
  556. 'behavior' => array(BEHAVIORS),
  557. 'controller' => array(CONTROLLERS),
  558. 'component' => array(COMPONENTS),
  559. 'view' => array(VIEWS),
  560. 'helper' => array(HELPERS),
  561. 'plugin' => array(APP . 'plugins' . DS),
  562. 'vendor' => array(APP . 'vendors' . DS, VENDORS),
  563. 'locale' => array(APP . 'locale' . DS),
  564. 'shell' => array()
  565. );
  566. foreach ($basePaths as $type => $default) {
  567. $pathsVar = $type . 'Paths';
  568. $merge = array();
  569. if (isset($core[$type])) {
  570. $merge = $core[$type];
  571. }
  572. if ($type === 'model' || $type === 'controller' || $type === 'helper') {
  573. $merge = array_merge(array(APP), $merge);
  574. }
  575. if (!is_array($default)) {
  576. $default = array($default);
  577. }
  578. $_this->{$pathsVar} = $default;
  579. if (isset($paths[$pathsVar]) && !empty($paths[$pathsVar])) {
  580. $path = array_flip(array_flip((array_merge($_this->{$pathsVar}, (array)$paths[$pathsVar], $merge))));
  581. $_this->{$pathsVar} = array_values($path);
  582. } else {
  583. $path = array_flip(array_flip((array_merge($_this->{$pathsVar}, $merge))));
  584. $_this->{$pathsVar} = array_values($path);
  585. }
  586. }
  587. }
  588. /**
  589. * Loads app/config/bootstrap.php.
  590. * If the alternative paths are set in this file
  591. * they will be added to the paths vars.
  592. *
  593. * @param boolean $boot Load application bootstrap (if true)
  594. * @return void
  595. * @access private
  596. */
  597. function __loadBootstrap($boot) {
  598. $modelPaths = $behaviorPaths = $controllerPaths = $componentPaths = $viewPaths = $helperPaths = $pluginPaths = $vendorPaths = $localePaths = $shellPaths = null;
  599. if ($boot) {
  600. Configure::write('App', array('base' => false, 'baseUrl' => false, 'dir' => APP_DIR, 'webroot' => WEBROOT_DIR));
  601. if (!include(CONFIGS . 'core.php')) {
  602. 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);
  603. }
  604. if (!include(CONFIGS . 'bootstrap.php')) {
  605. 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);
  606. }
  607. if (Configure::read('Cache.disable') !== true) {
  608. $cache = Cache::config('default');
  609. if (empty($cache['settings'])) {
  610. trigger_error('Cache not configured properly. Please check Cache::config(); in APP/config/core.php', E_USER_WARNING);
  611. $cache = Cache::config('default', array('engine' => 'File'));
  612. }
  613. $path = $prefix = $duration = null;
  614. if (!empty($cache['settings']['path'])) {
  615. $path = realpath($cache['settings']['path']);
  616. } else {
  617. $prefix = $cache['settings']['prefix'];
  618. }
  619. if (Configure::read() >= 1) {
  620. $duration = '+10 seconds';
  621. } else {
  622. $duration = '+999 days';
  623. }
  624. if (Cache::config('_cake_core_') === false) {
  625. Cache::config('_cake_core_', array_merge($cache['settings'], array(
  626. 'prefix' => $prefix . 'cake_core_', 'path' => $path . DS . 'persistent' . DS,
  627. 'serialize' => true, 'duration' => $duration
  628. )));
  629. }
  630. if (Cache::config('_cake_model_') === false) {
  631. Cache::config('_cake_model_', array_merge($cache['settings'], array(
  632. 'prefix' => $prefix . 'cake_model_', 'path' => $path . DS . 'models' . DS,
  633. 'serialize' => true, 'duration' => $duration
  634. )));
  635. }
  636. Cache::config('default');
  637. }
  638. Configure::buildPaths(compact('modelPaths', 'viewPaths', 'controllerPaths', 'helperPaths', 'componentPaths', 'behaviorPaths', 'pluginPaths', 'vendorPaths', 'localePaths', 'shellPaths'));
  639. }
  640. }
  641. /**
  642. * Caches the object map when the instance of the Configure class is destroyed
  643. *
  644. * @access public
  645. */
  646. function __destruct() {
  647. if ($this->__cache) {
  648. Cache::write('object_map', array_filter($this->__objects), '_cake_core_');
  649. }
  650. }
  651. }
  652. /**
  653. * Class and file loader.
  654. *
  655. * @link http://book.cakephp.org/view/499/The-App-Class
  656. * @since CakePHP(tm) v 1.2.0.6001
  657. * @package cake
  658. * @subpackage cake.cake.libs
  659. */
  660. class App extends Object {
  661. /**
  662. * Paths to search for files.
  663. *
  664. * @var array
  665. * @access public
  666. */
  667. var $search = array();
  668. /**
  669. * Whether or not to return the file that is loaded.
  670. *
  671. * @var boolean
  672. * @access public
  673. */
  674. var $return = false;
  675. /**
  676. * Determines if $__maps and $__paths cache should be written.
  677. *
  678. * @var boolean
  679. * @access private
  680. */
  681. var $__cache = false;
  682. /**
  683. * Holds key/value pairs of $type => file path.
  684. *
  685. * @var array
  686. * @access private
  687. */
  688. var $__map = array();
  689. /**
  690. * Holds paths for deep searching of files.
  691. *
  692. * @var array
  693. * @access private
  694. */
  695. var $__paths = array();
  696. /**
  697. * Holds loaded files.
  698. *
  699. * @var array
  700. * @access private
  701. */
  702. var $__loaded = array();
  703. /**
  704. * Finds classes based on $name or specific file(s) to search.
  705. *
  706. * @link http://book.cakephp.org/view/529/Using-App-import
  707. * @param mixed $type The type of Class if passed as a string, or all params can be passed as an single array to $type,
  708. * @param string $name Name of the Class or a unique name for the file
  709. * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
  710. * $ext allows setting the extension of the file name based on Inflector::underscore($name) . ".$ext";
  711. * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
  712. * @param string $file full name of the file to search for including extension
  713. * @param boolean $return, return the loaded file, the file must have a return statement in it to work: return $variable;
  714. * @return boolean true if Class is already in memory or if file is found and loaded, false if not
  715. * @access public
  716. */
  717. function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
  718. $plugin = $directory = null;
  719. if (is_array($type)) {
  720. extract($type, EXTR_OVERWRITE);
  721. }
  722. if (is_array($parent)) {
  723. extract($parent, EXTR_OVERWRITE);
  724. }
  725. if ($name === null && $file === null) {
  726. $name = $type;
  727. $type = 'Core';
  728. } elseif ($name === null) {
  729. $type = 'File';
  730. }
  731. if (is_array($name)) {
  732. foreach ($name as $class) {
  733. $tempType = $type;
  734. $plugin = null;
  735. if (strpos($class, '.') !== false) {
  736. $value = explode('.', $class);
  737. $count = count($value);
  738. if ($count > 2) {
  739. $tempType = $value[0];
  740. $plugin = $value[1] . '.';
  741. $class = $value[2];
  742. } elseif ($count === 2 && ($type === 'Core' || $type === 'File')) {
  743. $tempType = $value[0];
  744. $class = $value[1];
  745. } else {
  746. $plugin = $value[0] . '.';
  747. $class = $value[1];
  748. }
  749. }
  750. if (!App::import($tempType, $plugin . $class)) {
  751. return false;
  752. }
  753. }
  754. return true;
  755. }
  756. if ($name != null && strpos($name, '.') !== false) {
  757. list($plugin, $name) = explode('.', $name);
  758. }
  759. $_this =& App::getInstance();
  760. $_this->return = $return;
  761. if (isset($ext)) {
  762. $file = Inflector::underscore($name) . ".$ext";
  763. }
  764. $ext = $_this->__settings($type, $plugin, $parent);
  765. if ($name != null && !class_exists($name . $ext['class'])) {
  766. if ($load = $_this->__mapped($name . $ext['class'], $type, $plugin)) {
  767. if ($_this->__load($load)) {
  768. $_this->__overload($type, $name . $ext['class']);
  769. if ($_this->return) {
  770. $value = include $load;
  771. return $value;
  772. }
  773. return true;
  774. } else {
  775. $_this->__remove($name . $ext['class'], $type, $plugin);
  776. $_this->__cache = true;
  777. }
  778. }
  779. if (!empty($search)) {
  780. $_this->search = $search;
  781. } elseif ($plugin) {
  782. $_this->search = $_this->__paths('plugin');
  783. } else {
  784. $_this->search = $_this->__paths($type);
  785. }
  786. $find = $file;
  787. if ($find === null) {
  788. $find = Inflector::underscore($name . $ext['suffix']).'.php';
  789. if ($plugin) {
  790. $paths = $_this->search;
  791. foreach ($paths as $key => $value) {
  792. $_this->search[$key] = $value . $ext['path'];
  793. }
  794. $plugin = Inflector::camelize($plugin);
  795. }
  796. }
  797. if (strtolower($type) !== 'vendor' && empty($search) && $_this->__load($file)) {
  798. $directory = false;
  799. } else {
  800. $file = $find;
  801. $directory = $_this->__find($find, true);
  802. }
  803. if ($directory !== null) {
  804. $_this->__cache = true;
  805. $_this->__map($directory . $file, $name . $ext['class'], $type, $plugin);
  806. $_this->__overload($type, $name . $ext['class']);
  807. if ($_this->return) {
  808. $value = include $directory . $file;
  809. return $value;
  810. }
  811. return true;
  812. }
  813. return false;
  814. }
  815. return true;
  816. }
  817. /**
  818. * Returns a single instance of App.
  819. *
  820. * @return object
  821. * @access public
  822. */
  823. function &getInstance() {
  824. static $instance = array();
  825. if (!$instance) {
  826. $instance[0] =& new App();
  827. $instance[0]->__map = Cache::read('file_map', '_cake_core_');
  828. }
  829. return $instance[0];
  830. }
  831. /**
  832. * Locates the $file in $__paths, searches recursively.
  833. *
  834. * @param string $file full file name
  835. * @param boolean $recursive search $__paths recursively
  836. * @return mixed boolean on fail, $file directory path on success
  837. * @access private
  838. */
  839. function __find($file, $recursive = true) {
  840. if (empty($this->search)) {
  841. return null;
  842. } elseif (is_string($this->search)) {
  843. $this->search = array($this->search);
  844. }
  845. if (empty($this->__paths)) {
  846. $this->__paths = Cache::read('dir_map', '_cake_core_');
  847. }
  848. foreach ($this->search as $path) {
  849. $path = rtrim($path, DS);
  850. if ($path === rtrim(APP, DS)) {
  851. $recursive = false;
  852. }
  853. if ($recursive === false) {
  854. if ($this->__load($path . DS . $file)) {
  855. return $path . DS;
  856. }
  857. continue;
  858. }
  859. if (!isset($this->__paths[$path])) {
  860. if (!class_exists('Folder')) {
  861. require LIBS . 'folder.php';
  862. }
  863. $Folder =& new Folder();
  864. $directories = $Folder->tree($path, false, 'dir');
  865. $this->__paths[$path] = $directories;
  866. }
  867. foreach ($this->__paths[$path] as $directory) {
  868. if ($this->__load($directory . DS . $file)) {
  869. return $directory . DS;
  870. }
  871. }
  872. }
  873. return null;
  874. }
  875. /**
  876. * Attempts to load $file.
  877. *
  878. * @param string $file full path to file including file name
  879. * @return boolean
  880. * @access private
  881. */
  882. function __load($file) {
  883. if (empty($file)) {
  884. return false;
  885. }
  886. if (!$this->return && isset($this->__loaded[$file])) {
  887. return true;
  888. }
  889. if (file_exists($file)) {
  890. if (!$this->return) {
  891. require($file);
  892. $this->__loaded[$file] = true;
  893. }
  894. return true;
  895. }
  896. return false;
  897. }
  898. /**
  899. * Maps the $name to the $file.
  900. *
  901. * @param string $file full path to file
  902. * @param string $name unique name for this map
  903. * @param string $type type object being mapped
  904. * @param string $plugin if object is from a plugin, the name of the plugin
  905. * @access private
  906. */
  907. function __map($file, $name, $type, $plugin) {
  908. if ($plugin) {
  909. $plugin = Inflector::camelize($plugin);
  910. $this->__map['Plugin'][$plugin][$type][$name] = $file;
  911. } else {
  912. $this->__map[$type][$name] = $file;
  913. }
  914. }
  915. /**
  916. * Returns a file's complete path.
  917. *
  918. * @param string $name unique name
  919. * @param string $type type object
  920. * @param string $plugin if object is from a plugin, the name of the plugin
  921. * @return mixed, file path if found, false otherwise
  922. * @access private
  923. */
  924. function __mapped($name, $type, $plugin) {
  925. if ($plugin) {
  926. $plugin = Inflector::camelize($plugin);
  927. if (isset($this->__map['Plugin'][$plugin][$type]) && isset($this->__map['Plugin'][$plugin][$type][$name])) {
  928. return $this->__map['Plugin'][$plugin][$type][$name];
  929. }
  930. return false;
  931. }
  932. if (isset($this->__map[$type]) && isset($this->__map[$type][$name])) {
  933. return $this->__map[$type][$name];
  934. }
  935. return false;
  936. }
  937. /**
  938. * Used to overload objects as needed.
  939. *
  940. * @param string $type Model or Helper
  941. * @param string $name Class name to overload
  942. * @access private
  943. */
  944. function __overload($type, $name) {
  945. if (($type === 'Model' || $type === 'Helper') && strtolower($name) != 'schema') {
  946. Overloadable::overload($name);
  947. }
  948. }
  949. /**
  950. * Loads parent classes based on $type.
  951. * Returns a prefix or suffix needed for loading files.
  952. *
  953. * @param string $type type of object
  954. * @param string $plugin name of plugin
  955. * @param boolean $parent false will not attempt to load parent
  956. * @return array
  957. * @access private
  958. */
  959. function __settings($type, $plugin, $parent) {
  960. if (!$parent) {
  961. return null;
  962. }
  963. if ($plugin) {
  964. $plugin = Inflector::underscore($plugin);
  965. $name = Inflector::camelize($plugin);
  966. }
  967. $path = null;
  968. $load = strtolower($type);
  969. switch ($load) {
  970. case 'model':
  971. if (!class_exists('Model')) {
  972. App::import('Core', 'Model', false, Configure::corePaths('model'));
  973. }
  974. if (!class_exists('AppModel')) {
  975. App::import($type, 'AppModel', false, Configure::read('modelPaths'));
  976. }
  977. if ($plugin) {
  978. if (!class_exists($name . 'AppModel')) {
  979. App::import($type, $plugin . '.' . $name . 'AppModel', false, array(), $plugin . DS . $plugin . '_app_model.php');
  980. }
  981. $path = $plugin . DS . 'models' . DS;
  982. }
  983. return array('class' => null, 'suffix' => null, 'path' => $path);
  984. break;
  985. case 'behavior':
  986. if ($plugin) {
  987. $path = $plugin . DS . 'models' . DS . 'behaviors' . DS;
  988. }
  989. return array('class' => $type, 'suffix' => null, 'path' => $path);
  990. break;
  991. case 'controller':
  992. App::import($type, 'AppController', false);
  993. if ($plugin) {
  994. App::import($type, $plugin . '.' . $name . 'AppController', false, array(), $plugin . DS . $plugin . '_app_controller.php');
  995. $path = $plugin . DS . 'controllers' . DS;
  996. }
  997. return array('class' => $type, 'suffix' => $type, 'path' => $path);
  998. break;
  999. case 'component':
  1000. if ($plugin) {
  1001. $path = $plugin . DS . 'controllers' . DS . 'components' . DS;
  1002. }
  1003. return array('class' => $type, 'suffix' => null, 'path' => $path);
  1004. break;
  1005. case 'view':
  1006. if ($plugin) {
  1007. $path = $plugin . DS . 'views' . DS;
  1008. }
  1009. return array('class' => $type, 'suffix' => null, 'path' => $path);
  1010. break;
  1011. case 'helper':
  1012. if (!class_exists('AppHelper')) {
  1013. App::import($type, 'AppHelper', false);
  1014. }
  1015. if ($plugin) {
  1016. $path = $plugin . DS . 'views' . DS . 'helpers' . DS;
  1017. }
  1018. return array('class' => $type, 'suffix' => null, 'path' => $path);
  1019. break;
  1020. case 'vendor':
  1021. if ($plugin) {
  1022. $path = $plugin . DS . 'vendors' . DS;
  1023. }
  1024. return array('class' => null, 'suffix' => null, 'path' => $path);
  1025. break;
  1026. default:
  1027. $type = $suffix = $path = null;
  1028. break;
  1029. }
  1030. return array('class' => null, 'suffix' => null, 'path' => null);
  1031. }
  1032. /**
  1033. * Returns default search paths.
  1034. *
  1035. * @param string $type type of object to be searched
  1036. * @return array list of paths
  1037. * @access private
  1038. */
  1039. function __paths($type) {
  1040. $type = strtolower($type);
  1041. if ($type === 'core') {
  1042. $path = Configure::corePaths();
  1043. $paths = array();
  1044. foreach ($path as $key => $value) {
  1045. $count = count($key);
  1046. for ($i = 0; $i < $count; $i++) {
  1047. $paths[] = $path[$key][$i];
  1048. }
  1049. }
  1050. return $paths;
  1051. }
  1052. if ($paths = Configure::read($type . 'Paths')) {
  1053. return $paths;
  1054. }
  1055. switch ($type) {
  1056. case 'plugin':
  1057. return array(APP . 'plugins' . DS);
  1058. case 'vendor':
  1059. return array(APP . 'vendors' . DS, VENDORS, APP . 'plugins' . DS);
  1060. case 'controller':
  1061. return array(APP . 'controllers' . DS, APP);
  1062. case 'model':
  1063. return array(APP . 'models' . DS, APP);
  1064. case 'view':
  1065. return array(APP . 'views' . DS);
  1066. }
  1067. }
  1068. /**
  1069. * Removes file location from map if the file has been deleted.
  1070. *
  1071. * @param string $name name of object
  1072. * @param string $type type of object
  1073. * @param string $plugin name of plugin
  1074. * @return void
  1075. * @access private
  1076. */
  1077. function __remove($name, $type, $plugin) {
  1078. if ($plugin) {
  1079. $plugin = Inflector::camelize($plugin);
  1080. unset($this->__map['Plugin'][$plugin][$type][$name]);
  1081. } else {
  1082. unset($this->__map[$type][$name]);
  1083. }
  1084. }
  1085. /**
  1086. * Object destructor.
  1087. *
  1088. * Writes cache file if changes have been made to the $__map or $__paths
  1089. *
  1090. * @return void
  1091. * @access private
  1092. */
  1093. function __destruct() {
  1094. if ($this->__cache) {
  1095. $core = Configure::corePaths('cake');
  1096. unset($this->__paths[rtrim($core[0], DS)]);
  1097. Cache::write('dir_map', array_filter($this->__paths), '_cake_core_');
  1098. Cache::write('file_map', array_filter($this->__map), '_cake_core_');
  1099. }
  1100. }
  1101. }
  1102. ?>