PageRenderTime 29ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/configure.php

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