PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/cake/libs/configure.php

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