PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/configure.php

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