PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/configure.php

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