PageRenderTime 72ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/HelloWorld/cake/libs/configure.php

https://bitbucket.org/mauget/helloworld
PHP | 1320 lines | 754 code | 113 blank | 453 comment | 163 complexity | e117d5d4c4559d075a6edbc701441ca8 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. * Holds key/value pairs of $type => file path.
  530. *
  531. * @var array
  532. * @access private
  533. */
  534. var $__map = array();
  535. /**
  536. * Holds paths for deep searching of files.
  537. *
  538. * @var array
  539. * @access private
  540. */
  541. var $__paths = array();
  542. /**
  543. * Holds loaded files.
  544. *
  545. * @var array
  546. * @access private
  547. */
  548. var $__loaded = array();
  549. /**
  550. * Holds and key => value array of object types.
  551. *
  552. * @var array
  553. * @access private
  554. */
  555. var $__objects = array();
  556. /**
  557. * Used to read information stored path
  558. *
  559. * Usage:
  560. *
  561. * `App::path('models'); will return all paths for models`
  562. *
  563. * @param string $type type of path
  564. * @return string array
  565. * @access public
  566. */
  567. function path($type) {
  568. $_this =& App::getInstance();
  569. if (!isset($_this->{$type})) {
  570. return array();
  571. }
  572. return $_this->{$type};
  573. }
  574. /**
  575. * Build path references. Merges the supplied $paths
  576. * with the base paths and the default core paths.
  577. *
  578. * @param array $paths paths defines in config/bootstrap.php
  579. * @param boolean $reset true will set paths, false merges paths [default] false
  580. * @return void
  581. * @access public
  582. */
  583. function build($paths = array(), $reset = false) {
  584. $_this =& App::getInstance();
  585. $defaults = array(
  586. 'models' => array(MODELS),
  587. 'behaviors' => array(BEHAVIORS),
  588. 'datasources' => array(MODELS . 'datasources'),
  589. 'controllers' => array(CONTROLLERS),
  590. 'components' => array(COMPONENTS),
  591. 'libs' => array(APPLIBS),
  592. 'views' => array(VIEWS),
  593. 'helpers' => array(HELPERS),
  594. 'locales' => array(APP . 'locale' . DS),
  595. 'shells' => array(APP . 'vendors' . DS . 'shells' . DS, VENDORS . 'shells' . DS),
  596. 'vendors' => array(APP . 'vendors' . DS, VENDORS),
  597. 'plugins' => array(APP . 'plugins' . DS)
  598. );
  599. if ($reset == true) {
  600. foreach ($paths as $type => $new) {
  601. $_this->{$type} = (array)$new;
  602. }
  603. return $paths;
  604. }
  605. $core = $_this->core();
  606. $app = array('models' => true, 'controllers' => true, 'helpers' => true);
  607. foreach ($defaults as $type => $default) {
  608. $merge = array();
  609. if (isset($app[$type])) {
  610. $merge = array(APP);
  611. }
  612. if (isset($core[$type])) {
  613. $merge = array_merge($merge, (array)$core[$type]);
  614. }
  615. if (empty($_this->{$type}) || empty($paths)) {
  616. $_this->{$type} = $default;
  617. }
  618. if (!empty($paths[$type])) {
  619. $path = array_flip(array_flip(array_merge(
  620. (array)$paths[$type], $_this->{$type}, $merge
  621. )));
  622. $_this->{$type} = array_values($path);
  623. } else {
  624. $path = array_flip(array_flip(array_merge($_this->{$type}, $merge)));
  625. $_this->{$type} = array_values($path);
  626. }
  627. }
  628. }
  629. /**
  630. * Get the path that a plugin is on. Searches through the defined plugin paths.
  631. *
  632. * @param string $plugin CamelCased/lower_cased plugin name to find the path of.
  633. * @return string full path to the plugin.
  634. */
  635. function pluginPath($plugin) {
  636. $_this =& App::getInstance();
  637. $pluginDir = Inflector::underscore($plugin);
  638. for ($i = 0, $length = count($_this->plugins); $i < $length; $i++) {
  639. if (is_dir($_this->plugins[$i] . $pluginDir)) {
  640. return $_this->plugins[$i] . $pluginDir . DS ;
  641. }
  642. }
  643. return $_this->plugins[0] . $pluginDir . DS;
  644. }
  645. /**
  646. * Find the path that a theme is on. Search through the defined theme paths.
  647. *
  648. * @param string $theme lower_cased theme name to find the path of.
  649. * @return string full path to the theme.
  650. */
  651. function themePath($theme) {
  652. $_this =& App::getInstance();
  653. $themeDir = 'themed' . DS . Inflector::underscore($theme);
  654. for ($i = 0, $length = count($_this->views); $i < $length; $i++) {
  655. if (is_dir($_this->views[$i] . $themeDir)) {
  656. return $_this->views[$i] . $themeDir . DS ;
  657. }
  658. }
  659. return $_this->views[0] . $themeDir . DS;
  660. }
  661. /**
  662. * Returns a key/value list of all paths where core libs are found.
  663. * Passing $type only returns the values for a given value of $key.
  664. *
  665. * @param string $type valid values are: 'model', 'behavior', 'controller', 'component',
  666. * 'view', 'helper', 'datasource', 'libs', and 'cake'
  667. * @return array numeric keyed array of core lib paths
  668. * @access public
  669. */
  670. function core($type = null) {
  671. static $paths = false;
  672. if ($paths === false) {
  673. $paths = Cache::read('core_paths', '_cake_core_');
  674. }
  675. if (!$paths) {
  676. $paths = array();
  677. $libs = dirname(__FILE__) . DS;
  678. $cake = dirname($libs) . DS;
  679. $path = dirname($cake) . DS;
  680. $paths['cake'][] = $cake;
  681. $paths['libs'][] = $libs;
  682. $paths['models'][] = $libs . 'model' . DS;
  683. $paths['datasources'][] = $libs . 'model' . DS . 'datasources' . DS;
  684. $paths['behaviors'][] = $libs . 'model' . DS . 'behaviors' . DS;
  685. $paths['controllers'][] = $libs . 'controller' . DS;
  686. $paths['components'][] = $libs . 'controller' . DS . 'components' . DS;
  687. $paths['views'][] = $libs . 'view' . DS;
  688. $paths['helpers'][] = $libs . 'view' . DS . 'helpers' . DS;
  689. $paths['plugins'][] = $path . 'plugins' . DS;
  690. $paths['vendors'][] = $path . 'vendors' . DS;
  691. $paths['shells'][] = $cake . 'console' . DS . 'libs' . DS;
  692. Cache::write('core_paths', array_filter($paths), '_cake_core_');
  693. }
  694. if ($type && isset($paths[$type])) {
  695. return $paths[$type];
  696. }
  697. return $paths;
  698. }
  699. /**
  700. * Returns an array of objects of the given type.
  701. *
  702. * Example usage:
  703. *
  704. * `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
  705. *
  706. * @param string $type Type of object, i.e. 'model', 'controller', 'helper', or 'plugin'
  707. * @param mixed $path Optional Scan only the path given. If null, paths for the chosen
  708. * type will be used.
  709. * @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
  710. * @return mixed Either false on incorrect / miss. Or an array of found objects.
  711. * @access public
  712. */
  713. function objects($type, $path = null, $cache = true) {
  714. $objects = array();
  715. $extension = false;
  716. $name = $type;
  717. if ($type === 'file' && !$path) {
  718. return false;
  719. } elseif ($type === 'file') {
  720. $extension = true;
  721. $name = $type . str_replace(DS, '', $path);
  722. }
  723. $_this =& App::getInstance();
  724. if (empty($_this->__objects) && $cache === true) {
  725. $_this->__objects = Cache::read('object_map', '_cake_core_');
  726. }
  727. if (!isset($_this->__objects[$name]) || $cache !== true) {
  728. $types = $_this->types;
  729. if (!isset($types[$type])) {
  730. return false;
  731. }
  732. $objects = array();
  733. if (empty($path)) {
  734. $path = $_this->{"{$type}s"};
  735. if (isset($types[$type]['core']) && $types[$type]['core'] === false) {
  736. array_pop($path);
  737. }
  738. }
  739. $items = array();
  740. foreach ((array)$path as $dir) {
  741. if ($dir != APP) {
  742. $items = $_this->__list($dir, $types[$type]['suffix'], $extension);
  743. $objects = array_merge($items, array_diff($objects, $items));
  744. }
  745. }
  746. if ($type !== 'file') {
  747. foreach ($objects as $key => $value) {
  748. $objects[$key] = Inflector::camelize($value);
  749. }
  750. }
  751. if ($cache === true) {
  752. $_this->__resetCache(true);
  753. }
  754. $_this->__objects[$name] = $objects;
  755. }
  756. return $_this->__objects[$name];
  757. }
  758. /**
  759. * Finds classes based on $name or specific file(s) to search. Calling App::import() will
  760. * not construct any classes contained in the files. It will only find and require() the file.
  761. *
  762. * @link http://book.cakephp.org/view/934/Using-App-import
  763. * @param mixed $type The type of Class if passed as a string, or all params can be passed as
  764. * an single array to $type,
  765. * @param string $name Name of the Class or a unique name for the file
  766. * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
  767. * array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
  768. * $ext allows setting the extension of the file name
  769. * based on Inflector::underscore($name) . ".$ext";
  770. * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
  771. * @param string $file full name of the file to search for including extension
  772. * @param boolean $return, return the loaded file, the file must have a return
  773. * statement in it to work: return $variable;
  774. * @return boolean true if Class is already in memory or if file is found and loaded, false if not
  775. * @access public
  776. */
  777. function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
  778. $plugin = $directory = null;
  779. if (is_array($type)) {
  780. extract($type, EXTR_OVERWRITE);
  781. }
  782. if (is_array($parent)) {
  783. extract($parent, EXTR_OVERWRITE);
  784. }
  785. if ($name === null && $file === null) {
  786. $name = $type;
  787. $type = 'Core';
  788. } elseif ($name === null) {
  789. $type = 'File';
  790. }
  791. if (is_array($name)) {
  792. foreach ($name as $class) {
  793. $tempType = $type;
  794. $plugin = null;
  795. if (strpos($class, '.') !== false) {
  796. $value = explode('.', $class);
  797. $count = count($value);
  798. if ($count > 2) {
  799. $tempType = $value[0];
  800. $plugin = $value[1] . '.';
  801. $class = $value[2];
  802. } elseif ($count === 2 && ($type === 'Core' || $type === 'File')) {
  803. $tempType = $value[0];
  804. $class = $value[1];
  805. } else {
  806. $plugin = $value[0] . '.';
  807. $class = $value[1];
  808. }
  809. }
  810. if (!App::import($tempType, $plugin . $class, $parent)) {
  811. return false;
  812. }
  813. }
  814. return true;
  815. }
  816. if ($name != null && strpos($name, '.') !== false) {
  817. list($plugin, $name) = explode('.', $name);
  818. $plugin = Inflector::camelize($plugin);
  819. }
  820. $_this =& App::getInstance();
  821. $_this->return = $return;
  822. if (isset($ext)) {
  823. $file = Inflector::underscore($name) . ".{$ext}";
  824. }
  825. $ext = $_this->__settings($type, $plugin, $parent);
  826. if ($name != null && !class_exists($name . $ext['class'])) {
  827. if ($load = $_this->__mapped($name . $ext['class'], $type, $plugin)) {
  828. if ($_this->__load($load)) {
  829. $_this->__overload($type, $name . $ext['class'], $parent);
  830. if ($_this->return) {
  831. return include($load);
  832. }
  833. return true;
  834. } else {
  835. $_this->__remove($name . $ext['class'], $type, $plugin);
  836. $_this->__resetCache(true);
  837. }
  838. }
  839. if (!empty($search)) {
  840. $_this->search = $search;
  841. } elseif ($plugin) {
  842. $_this->search = $_this->__paths('plugin');
  843. } else {
  844. $_this->search = $_this->__paths($type);
  845. }
  846. $find = $file;
  847. if ($find === null) {
  848. $find = Inflector::underscore($name . $ext['suffix']).'.php';
  849. if ($plugin) {
  850. $paths = $_this->search;
  851. foreach ($paths as $key => $value) {
  852. $_this->search[$key] = $value . $ext['path'];
  853. }
  854. }
  855. }
  856. if (strtolower($type) !== 'vendor' && empty($search) && $_this->__load($file)) {
  857. $directory = false;
  858. } else {
  859. $file = $find;
  860. $directory = $_this->__find($find, true);
  861. }
  862. if ($directory !== null) {
  863. $_this->__resetCache(true);
  864. $_this->__map($directory . $file, $name . $ext['class'], $type, $plugin);
  865. $_this->__overload($type, $name . $ext['class'], $parent);
  866. if ($_this->return) {
  867. return include($directory . $file);
  868. }
  869. return true;
  870. }
  871. return false;
  872. }
  873. return true;
  874. }
  875. /**
  876. * Returns a single instance of App.
  877. *
  878. * @return object
  879. * @access public
  880. */
  881. function &getInstance() {
  882. static $instance = array();
  883. if (!$instance) {
  884. $instance[0] =& new App();
  885. $instance[0]->__map = (array)Cache::read('file_map', '_cake_core_');
  886. }
  887. return $instance[0];
  888. }
  889. /**
  890. * Locates the $file in $__paths, searches recursively.
  891. *
  892. * @param string $file full file name
  893. * @param boolean $recursive search $__paths recursively
  894. * @return mixed boolean on fail, $file directory path on success
  895. * @access private
  896. */
  897. function __find($file, $recursive = true) {
  898. static $appPath = false;
  899. if (empty($this->search)) {
  900. return null;
  901. } elseif (is_string($this->search)) {
  902. $this->search = array($this->search);
  903. }
  904. if (empty($this->__paths)) {
  905. $this->__paths = Cache::read('dir_map', '_cake_core_');
  906. }
  907. foreach ($this->search as $path) {
  908. if ($appPath === false) {
  909. $appPath = rtrim(APP, DS);
  910. }
  911. $path = rtrim($path, DS);
  912. if ($path === $appPath) {
  913. $recursive = false;
  914. }
  915. if ($recursive === false) {
  916. if ($this->__load($path . DS . $file)) {
  917. return $path . DS;
  918. }
  919. continue;
  920. }
  921. if (!isset($this->__paths[$path])) {
  922. if (!class_exists('Folder')) {
  923. require LIBS . 'folder.php';
  924. }
  925. $Folder =& new Folder();
  926. $directories = $Folder->tree($path, array('.svn', '.git', 'CVS', 'tests', 'templates'), 'dir');
  927. sort($directories);
  928. $this->__paths[$path] = $directories;
  929. }
  930. foreach ($this->__paths[$path] as $directory) {
  931. if ($this->__load($directory . DS . $file)) {
  932. return $directory . DS;
  933. }
  934. }
  935. }
  936. return null;
  937. }
  938. /**
  939. * Attempts to load $file.
  940. *
  941. * @param string $file full path to file including file name
  942. * @return boolean
  943. * @access private
  944. */
  945. function __load($file) {
  946. if (empty($file)) {
  947. return false;
  948. }
  949. if (!$this->return && isset($this->__loaded[$file])) {
  950. return true;
  951. }
  952. if (file_exists($file)) {
  953. if (!$this->return) {
  954. require($file);
  955. $this->__loaded[$file] = true;
  956. }
  957. return true;
  958. }
  959. return false;
  960. }
  961. /**
  962. * Maps the $name to the $file.
  963. *
  964. * @param string $file full path to file
  965. * @param string $name unique name for this map
  966. * @param string $type type object being mapped
  967. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  968. * @return void
  969. * @access private
  970. */
  971. function __map($file, $name, $type, $plugin) {
  972. if ($plugin) {
  973. $this->__map['Plugin'][$plugin][$type][$name] = $file;
  974. } else {
  975. $this->__map[$type][$name] = $file;
  976. }
  977. }
  978. /**
  979. * Returns a file's complete path.
  980. *
  981. * @param string $name unique name
  982. * @param string $type type object
  983. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  984. * @return mixed, file path if found, false otherwise
  985. * @access private
  986. */
  987. function __mapped($name, $type, $plugin) {
  988. if ($plugin) {
  989. if (isset($this->__map['Plugin'][$plugin][$type]) && isset($this->__map['Plugin'][$plugin][$type][$name])) {
  990. return $this->__map['Plugin'][$plugin][$type][$name];
  991. }
  992. return false;
  993. }
  994. if (isset($this->__map[$type]) && isset($this->__map[$type][$name])) {
  995. return $this->__map[$type][$name];
  996. }
  997. return false;
  998. }
  999. /**
  1000. * Used to overload objects as needed.
  1001. *
  1002. * @param string $type Model or Helper
  1003. * @param string $name Class name to overload
  1004. * @access private
  1005. */
  1006. function __overload($type, $name, $parent) {
  1007. if (($type === 'Model' || $type === 'Helper') && $parent !== false) {
  1008. Overloadable::overload($name);
  1009. }
  1010. }
  1011. /**
  1012. * Loads parent classes based on $type.
  1013. * Returns a prefix or suffix needed for loading files.
  1014. *
  1015. * @param string $type type of object
  1016. * @param string $plugin camelized name of plugin
  1017. * @param boolean $parent false will not attempt to load parent
  1018. * @return array
  1019. * @access private
  1020. */
  1021. function __settings($type, $plugin, $parent) {
  1022. if (!$parent) {
  1023. return array('class' => null, 'suffix' => null, 'path' => null);
  1024. }
  1025. if ($plugin) {
  1026. $pluginPath = Inflector::underscore($plugin);
  1027. }
  1028. $path = null;
  1029. $load = strtolower($type);
  1030. switch ($load) {
  1031. case 'model':
  1032. if (!class_exists('Model')) {
  1033. require LIBS . 'model' . DS . 'model.php';
  1034. }
  1035. if (!class_exists('AppModel')) {
  1036. App::import($type, 'AppModel', false);
  1037. }
  1038. if ($plugin) {
  1039. if (!class_exists($plugin . 'AppModel')) {
  1040. App::import($type, $plugin . '.' . $plugin . 'AppModel', false, array(), $pluginPath . DS . $pluginPath . '_app_model.php');
  1041. }
  1042. $path = $pluginPath . DS . 'models' . DS;
  1043. }
  1044. return array('class' => null, 'suffix' => null, 'path' => $path);
  1045. break;
  1046. case 'behavior':
  1047. if ($plugin) {
  1048. $path = $pluginPath . DS . 'models' . DS . 'behaviors' . DS;
  1049. }
  1050. return array('class' => $type, 'suffix' => null, 'path' => $path);
  1051. break;
  1052. case 'datasource':
  1053. if ($plugin) {
  1054. $path = $pluginPath . DS . 'models' . DS . 'datasources' . DS;
  1055. }
  1056. return array('class' => $type, 'suffix' => null, 'path' => $path);
  1057. case 'controller':
  1058. App::import($type, 'AppController', false);
  1059. if ($plugin) {
  1060. App::import($type, $plugin . '.' . $plugin . 'AppController', false, array(), $pluginPath . DS . $pluginPath . '_app_controller.php');
  1061. $path = $pluginPath . DS . 'controllers' . DS;
  1062. }
  1063. return array('class' => $type, 'suffix' => $type, 'path' => $path);
  1064. break;
  1065. case 'component':
  1066. if ($plugin) {
  1067. $path = $pluginPath . DS . 'controllers' . DS . 'components' . DS;
  1068. }
  1069. return array('class' => $type, 'suffix' => null, 'path' => $path);
  1070. break;
  1071. case 'lib':
  1072. if ($plugin) {
  1073. $path = $pluginPath . DS . 'libs' . DS;
  1074. }
  1075. return array('class' => null, 'suffix' => null, 'path' => $path);
  1076. break;
  1077. case 'view':
  1078. if ($plugin) {
  1079. $path = $pluginPath . DS . 'views' . DS;
  1080. }
  1081. return array('class' => $type, 'suffix' => null, 'path' => $path);
  1082. break;
  1083. case 'helper':
  1084. if (!class_exists('AppHelper')) {
  1085. App::import($type, 'AppHelper', false);
  1086. }
  1087. if ($plugin) {
  1088. $path = $pluginPath . DS . 'views' . DS . 'helpers' . DS;
  1089. }
  1090. return array('class' => $type, 'suffix' => null, 'path' => $path);
  1091. break;
  1092. case 'vendor':
  1093. if ($plugin) {
  1094. $path = $pluginPath . DS . 'vendors' . DS;
  1095. }
  1096. return array('class' => null, 'suffix' => null, 'path' => $path);
  1097. break;
  1098. default:
  1099. $type = $suffix = $path = null;
  1100. break;
  1101. }
  1102. return array('class' => null, 'suffix' => null, 'path' => null);
  1103. }
  1104. /**
  1105. * Returns default search paths.
  1106. *
  1107. * @param string $type type of object to be searched
  1108. * @return array list of paths
  1109. * @access private
  1110. */
  1111. function __paths($type) {
  1112. $type = strtolower($type);
  1113. $paths = array();
  1114. if ($type === 'core') {
  1115. return App::core('libs');
  1116. }
  1117. if (isset($this->{$type . 's'})) {
  1118. return $this->{$type . 's'};
  1119. }
  1120. return $paths;
  1121. }
  1122. /**
  1123. * Removes file location from map if the file has been deleted.
  1124. *
  1125. * @param string $name name of object
  1126. * @param string $type type of object
  1127. * @param string $plugin camelized name of plugin
  1128. * @return void
  1129. * @access private
  1130. */
  1131. function __remove($name, $type, $plugin) {
  1132. if ($plugin) {
  1133. unset($this->__map['Plugin'][$plugin][$type][$name]);
  1134. } else {
  1135. unset($this->__map[$type][$name]);
  1136. }
  1137. }
  1138. /**
  1139. * Returns an array of filenames of PHP files in the given directory.
  1140. *
  1141. * @param string $path Path to scan for files
  1142. * @param string $suffix if false, return only directories. if string, match and return files
  1143. * @return array List of directories or files in directory
  1144. * @access private
  1145. */
  1146. function __list($path, $suffix = false, $extension = false) {
  1147. if (!class_exists('Folder')) {
  1148. require LIBS . 'folder.php';
  1149. }
  1150. $items = array();
  1151. $Folder =& new Folder($path);
  1152. $contents = $Folder->read(false, true);
  1153. if (is_array($contents)) {
  1154. if (!$suffix) {
  1155. return $contents[0];
  1156. } else {
  1157. foreach ($contents[1] as $item) {
  1158. if (substr($item, - strlen($suffix)) === $suffix) {
  1159. if ($extension) {
  1160. $items[] = $item;
  1161. } else {
  1162. $items[] = substr($item, 0, strlen($item) - strlen($suffix));
  1163. }
  1164. }
  1165. }
  1166. }
  1167. }
  1168. return $items;
  1169. }
  1170. /**
  1171. * Determines if $__maps, $__objects and $__paths cache should be reset.
  1172. *
  1173. * @param boolean $reset
  1174. * @return boolean
  1175. * @access private
  1176. */
  1177. function __resetCache($reset = null) {
  1178. static $cache = array();
  1179. if (!$cache && $reset === true) {
  1180. $cache = true;
  1181. }
  1182. return $cache;
  1183. }
  1184. /**
  1185. * Object destructor.
  1186. *
  1187. * Writes cache file if changes have been made to the $__map or $__paths
  1188. *
  1189. * @return void
  1190. * @access private
  1191. */
  1192. function __destruct() {
  1193. if ($this->__resetCache() === true) {
  1194. $core = App::core('cake');
  1195. unset($this->__paths[rtrim($core[0], DS)]);
  1196. Cache::write('dir_map', array_filter($this->__paths), '_cake_core_');
  1197. Cache::write('file_map', array_filter($this->__map), '_cake_core_');
  1198. Cache::write('object_map', $this->__objects, '_cake_core_');
  1199. }
  1200. }
  1201. }