PageRenderTime 84ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/configure.php

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