PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/configure.php

https://bitbucket.org/enangyusup/vbulletin-cakephp
PHP | 1316 lines | 762 code | 113 blank | 441 comment | 165 complexity | 2ffbf40b63743a985a0c608f146407d3 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/42/The-Configuration-Class
  26. */
  27. class Configure extends Object {
  28. /**
  29. * Current debug level.
  30. *
  31. * @link http://book.cakephp.org/view/44/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/412/write
  72. * @param array $config Name of var to write
  73. * @param mixed $value Value to set for var
  74. * @return void
  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. }
  129. /**
  130. * Used to read information stored in the Configure instance.
  131. *
  132. * Usage:
  133. * {{{
  134. * Configure::read('Name'); will return all values for Name
  135. * Configure::read('Name.key'); will return only the value of Configure::Name[key]
  136. * }}}
  137. *
  138. * @link http://book.cakephp.org/view/413/read
  139. * @param string $var Variable to obtain. Use '.' to access array elements.
  140. * @return string value of Configure::$var
  141. * @access public
  142. */
  143. function read($var = 'debug') {
  144. $_this =& Configure::getInstance();
  145. if ($var === 'debug') {
  146. return $_this->debug;
  147. }
  148. if (strpos($var, '.') !== false) {
  149. $names = explode('.', $var, 3);
  150. $var = $names[0];
  151. }
  152. if (!isset($_this->{$var})) {
  153. return null;
  154. }
  155. if (!isset($names[1])) {
  156. return $_this->{$var};
  157. }
  158. switch (count($names)) {
  159. case 2:
  160. if (isset($_this->{$var}[$names[1]])) {
  161. return $_this->{$var}[$names[1]];
  162. }
  163. break;
  164. case 3:
  165. if (isset($_this->{$var}[$names[1]][$names[2]])) {
  166. return $_this->{$var}[$names[1]][$names[2]];
  167. }
  168. if (!isset($_this->{$var}[$names[1]])) {
  169. return null;
  170. }
  171. return Set::classicExtract($_this->{$var}[$names[1]], $names[2]);
  172. break;
  173. }
  174. return null;
  175. }
  176. /**
  177. * Used to delete a variable from the Configure instance.
  178. *
  179. * Usage:
  180. * {{{
  181. * Configure::delete('Name'); will delete the entire Configure::Name
  182. * Configure::delete('Name.key'); will delete only the Configure::Name[key]
  183. * }}}
  184. *
  185. * @link http://book.cakephp.org/view/414/delete
  186. * @param string $var the var to be deleted
  187. * @return void
  188. * @access public
  189. */
  190. function delete($var = null) {
  191. $_this =& Configure::getInstance();
  192. if (strpos($var, '.') === false) {
  193. unset($_this->{$var});
  194. return;
  195. }
  196. $names = explode('.', $var, 2);
  197. $_this->{$names[0]} = Set::remove($_this->{$names[0]}, $names[1]);
  198. }
  199. /**
  200. * Loads a file from app/config/configure_file.php.
  201. * Config file variables should be formated like:
  202. * `$config['name'] = 'value';`
  203. * These will be used to create dynamic Configure vars. load() is also used to
  204. * load stored config files created with Configure::store()
  205. *
  206. * - To load config files from app/config use `Configure::load('configure_file');`.
  207. * - To load config files from a plugin `Configure::load('plugin.configure_file');`.
  208. *
  209. * @link http://book.cakephp.org/view/415/load
  210. * @param string $fileName name of file to load, extension must be .php and only the name
  211. * should be used, not the extenstion
  212. * @return mixed false if file not found, void if load successful
  213. * @access public
  214. */
  215. function load($fileName) {
  216. $found = $plugin = $pluginPath = false;
  217. list($plugin, $fileName) = pluginSplit($fileName);
  218. if ($plugin) {
  219. $pluginPath = App::pluginPath($plugin);
  220. }
  221. $pos = strpos($fileName, '..');
  222. if ($pos === false) {
  223. if ($pluginPath && file_exists($pluginPath . 'config' . DS . $fileName . '.php')) {
  224. include($pluginPath . 'config' . DS . $fileName . '.php');
  225. $found = true;
  226. } elseif (file_exists(CONFIGS . $fileName . '.php')) {
  227. include(CONFIGS . $fileName . '.php');
  228. $found = true;
  229. } elseif (file_exists(CACHE . 'persistent' . DS . $fileName . '.php')) {
  230. include(CACHE . 'persistent' . DS . $fileName . '.php');
  231. $found = true;
  232. } else {
  233. foreach (App::core('cake') as $key => $path) {
  234. if (file_exists($path . DS . 'config' . DS . $fileName . '.php')) {
  235. include($path . DS . 'config' . DS . $fileName . '.php');
  236. $found = true;
  237. break;
  238. }
  239. }
  240. }
  241. }
  242. if (!$found) {
  243. return false;
  244. }
  245. if (!isset($config)) {
  246. trigger_error(sprintf(__('Configure::load() - no variable $config found in %s.php', true), $fileName), E_USER_WARNING);
  247. return false;
  248. }
  249. return Configure::write($config);
  250. }
  251. /**
  252. * Used to determine the current version of CakePHP.
  253. *
  254. * Usage `Configure::version();`
  255. *
  256. * @link http://book.cakephp.org/view/416/version
  257. * @return string Current version of CakePHP
  258. * @access public
  259. */
  260. function version() {
  261. $_this =& Configure::getInstance();
  262. if (!isset($_this->Cake['version'])) {
  263. require(CORE_PATH . 'cake' . DS . 'config' . DS . 'config.php');
  264. $_this->write($config);
  265. }
  266. return $_this->Cake['version'];
  267. }
  268. /**
  269. * Used to write a config file to disk.
  270. *
  271. * {{{
  272. * Configure::store('Model', 'class_paths', array('Users' => array(
  273. * 'path' => 'users', 'plugin' => true
  274. * )));
  275. * }}}
  276. *
  277. * @param string $type Type of config file to write, ex: Models, Controllers, Helpers, Components
  278. * @param string $name file name.
  279. * @param array $data array of values to store.
  280. * @return void
  281. * @access public
  282. */
  283. function store($type, $name, $data = array()) {
  284. $write = true;
  285. $content = '';
  286. foreach ($data as $key => $value) {
  287. $content .= "\$config['$type']['$key'] = " . var_export($value, true) . ";\n";
  288. }
  289. if (is_null($type)) {
  290. $write = false;
  291. }
  292. Configure::__writeConfig($content, $name, $write);
  293. }
  294. /**
  295. * Creates a cached version of a configuration file.
  296. * Appends values passed from Configure::store() to the cached file
  297. *
  298. * @param string $content Content to write on file
  299. * @param string $name Name to use for cache file
  300. * @param boolean $write true if content should be written, false otherwise
  301. * @return void
  302. * @access private
  303. */
  304. function __writeConfig($content, $name, $write = true) {
  305. $file = CACHE . 'persistent' . DS . $name . '.php';
  306. if (Configure::read() > 0) {
  307. $expires = "+10 seconds";
  308. } else {
  309. $expires = "+999 days";
  310. }
  311. $cache = cache('persistent' . DS . $name . '.php', null, $expires);
  312. if ($cache === null) {
  313. cache('persistent' . DS . $name . '.php', "<?php\n\$config = array();\n", $expires);
  314. }
  315. if ($write === true) {
  316. if (!class_exists('File')) {
  317. require LIBS . 'file.php';
  318. }
  319. $fileClass = new File($file);
  320. if ($fileClass->writable()) {
  321. $fileClass->append($content);
  322. }
  323. }
  324. }
  325. /**
  326. * @deprecated
  327. * @see App::objects()
  328. */
  329. function listObjects($type, $path = null, $cache = true) {
  330. return App::objects($type, $path, $cache);
  331. }
  332. /**
  333. * @deprecated
  334. * @see App::core()
  335. */
  336. function corePaths($type = null) {
  337. return App::core($type);
  338. }
  339. /**
  340. * @deprecated
  341. * @see App::build()
  342. */
  343. function buildPaths($paths) {
  344. return App::build($paths);
  345. }
  346. /**
  347. * Loads app/config/bootstrap.php.
  348. * If the alternative paths are set in this file
  349. * they will be added to the paths vars.
  350. *
  351. * @param boolean $boot Load application bootstrap (if true)
  352. * @return void
  353. * @access private
  354. */
  355. function __loadBootstrap($boot) {
  356. $libPaths = $modelPaths = $behaviorPaths = $controllerPaths = $componentPaths = $viewPaths = $helperPaths = $pluginPaths = $vendorPaths = $localePaths = $shellPaths = null;
  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. if (!include(CONFIGS . 'bootstrap.php')) {
  394. 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);
  395. }
  396. if (App::path('controllers') == array()) {
  397. App::build(array(
  398. 'models' => $modelPaths, 'views' => $viewPaths, 'controllers' => $controllerPaths,
  399. 'helpers' => $helperPaths, 'components' => $componentPaths, 'behaviors' => $behaviorPaths,
  400. 'plugins' => $pluginPaths, 'vendors' => $vendorPaths, 'locales' => $localePaths,
  401. 'shells' => $shellPaths, 'libs' => $libPaths
  402. ));
  403. }
  404. }
  405. }
  406. }
  407. /**
  408. * Class/file loader and path management.
  409. *
  410. * @link http://book.cakephp.org/view/499/The-App-Class
  411. * @since CakePHP(tm) v 1.2.0.6001
  412. * @package cake
  413. * @subpackage cake.cake.libs
  414. */
  415. class App extends Object {
  416. /**
  417. * List of object types and their properties
  418. *
  419. * @var array
  420. * @access public
  421. */
  422. var $types = array(
  423. 'class' => array('suffix' => '.php', 'extends' => null, 'core' => true),
  424. 'file' => array('suffix' => '.php', 'extends' => null, 'core' => true),
  425. 'model' => array('suffix' => '.php', 'extends' => 'AppModel', 'core' => false),
  426. 'behavior' => array('suffix' => '.php', 'extends' => 'ModelBehavior', 'core' => true),
  427. 'controller' => array('suffix' => '_controller.php', 'extends' => 'AppController', 'core' => true),
  428. 'component' => array('suffix' => '.php', 'extends' => null, 'core' => true),
  429. 'lib' => array('suffix' => '.php', 'extends' => null, 'core' => true),
  430. 'view' => array('suffix' => '.php', 'extends' => null, 'core' => true),
  431. 'helper' => array('suffix' => '.php', 'extends' => 'AppHelper', 'core' => true),
  432. 'vendor' => array('suffix' => '', 'extends' => null, 'core' => true),
  433. 'shell' => array('suffix' => '.php', 'extends' => 'Shell', 'core' => true),
  434. 'plugin' => array('suffix' => '', 'extends' => null, 'core' => true)
  435. );
  436. /**
  437. * List of additional path(s) where model files reside.
  438. *
  439. * @var array
  440. * @access public
  441. */
  442. var $models = array();
  443. /**
  444. * List of additional path(s) where behavior files reside.
  445. *
  446. * @var array
  447. * @access public
  448. */
  449. var $behaviors = array();
  450. /**
  451. * List of additional path(s) where controller files reside.
  452. *
  453. * @var array
  454. * @access public
  455. */
  456. var $controllers = array();
  457. /**
  458. * List of additional path(s) where component files reside.
  459. *
  460. * @var array
  461. * @access public
  462. */
  463. var $components = array();
  464. /**
  465. * List of additional path(s) where datasource files reside.
  466. *
  467. * @var array
  468. * @access public
  469. */
  470. var $datasources = array();
  471. /**
  472. * List of additional path(s) where libs files reside.
  473. *
  474. * @var array
  475. * @access public
  476. */
  477. var $libs = array();
  478. /**
  479. * List of additional path(s) where view files reside.
  480. *
  481. * @var array
  482. * @access public
  483. */
  484. var $views = array();
  485. /**
  486. * List of additional path(s) where helper files reside.
  487. *
  488. * @var array
  489. * @access public
  490. */
  491. var $helpers = array();
  492. /**
  493. * List of additional path(s) where plugins reside.
  494. *
  495. * @var array
  496. * @access public
  497. */
  498. var $plugins = array();
  499. /**
  500. * List of additional path(s) where vendor packages reside.
  501. *
  502. * @var array
  503. * @access public
  504. */
  505. var $vendors = array();
  506. /**
  507. * List of additional path(s) where locale files reside.
  508. *
  509. * @var array
  510. * @access public
  511. */
  512. var $locales = array();
  513. /**
  514. * List of additional path(s) where console shell files reside.
  515. *
  516. * @var array
  517. * @access public
  518. */
  519. var $shells = array();
  520. /**
  521. * Paths to search for files.
  522. *
  523. * @var array
  524. * @access public
  525. */
  526. var $search = array();
  527. /**
  528. * Whether or not to return the file that is loaded.
  529. *
  530. * @var boolean
  531. * @access public
  532. */
  533. var $return = false;
  534. /**
  535. * Determines if $__maps and $__paths cache should be written.
  536. *
  537. * @var boolean
  538. * @access private
  539. */
  540. var $__cache = false;
  541. /**
  542. * Holds key/value pairs of $type => file path.
  543. *
  544. * @var array
  545. * @access private
  546. */
  547. var $__map = array();
  548. /**
  549. * Holds paths for deep searching of files.
  550. *
  551. * @var array
  552. * @access private
  553. */
  554. var $__paths = array();
  555. /**
  556. * Holds loaded files.
  557. *
  558. * @var array
  559. * @access private
  560. */
  561. var $__loaded = array();
  562. /**
  563. * Holds and key => value array of object types.
  564. *
  565. * @var array
  566. * @access private
  567. */
  568. var $__objects = array();
  569. /**
  570. * Used to read information stored path
  571. *
  572. * Usage:
  573. *
  574. * `App::path('models'); will return all paths for models`
  575. *
  576. * @param string $type type of path
  577. * @return string array
  578. * @access public
  579. */
  580. function path($type) {
  581. $_this =& App::getInstance();
  582. if (!isset($_this->{$type})) {
  583. return array();
  584. }
  585. return $_this->{$type};
  586. }
  587. /**
  588. * Build path references. Merges the supplied $paths
  589. * with the base paths and the default core paths.
  590. *
  591. * @param array $paths paths defines in config/bootstrap.php
  592. * @param boolean $reset true will set paths, false merges paths [default] false
  593. * @return void
  594. * @access public
  595. */
  596. function build($paths = array(), $reset = false) {
  597. $_this =& App::getInstance();
  598. $defaults = array(
  599. 'models' => array(MODELS),
  600. 'behaviors' => array(BEHAVIORS),
  601. 'datasources' => array(MODELS . 'datasources'),
  602. 'controllers' => array(CONTROLLERS),
  603. 'components' => array(COMPONENTS),
  604. 'libs' => array(APPLIBS),
  605. 'views' => array(VIEWS),
  606. 'helpers' => array(HELPERS),
  607. 'locales' => array(APP . 'locale' . DS),
  608. 'shells' => array(APP . 'vendors' . DS . 'shells' . DS, VENDORS . 'shells' . DS),
  609. 'vendors' => array(APP . 'vendors' . DS, VENDORS),
  610. 'plugins' => array(APP . 'plugins' . DS)
  611. );
  612. if ($reset == true) {
  613. foreach ($paths as $type => $new) {
  614. $_this->{$type} = (array)$new;
  615. }
  616. return $paths;
  617. }
  618. $core = $_this->core();
  619. $app = array('models' => true, 'controllers' => true, 'helpers' => true);
  620. foreach ($defaults as $type => $default) {
  621. $merge = array();
  622. if (isset($app[$type])) {
  623. $merge = array(APP);
  624. }
  625. if (isset($core[$type])) {
  626. $merge = array_merge($merge, (array)$core[$type]);
  627. }
  628. $_this->{$type} = $default;
  629. if (!empty($paths[$type])) {
  630. $path = array_flip(array_flip(array_merge(
  631. $_this->{$type}, (array)$paths[$type], $merge
  632. )));
  633. $_this->{$type} = array_values($path);
  634. } else {
  635. $path = array_flip(array_flip(array_merge($_this->{$type}, $merge)));
  636. $_this->{$type} = array_values($path);
  637. }
  638. }
  639. }
  640. /**
  641. * Get the path that a plugin is on. Searches through the defined plugin paths.
  642. *
  643. * @param string $plugin CamelCased plugin name to find the path of.
  644. * @return string full path to the plugin.
  645. */
  646. function pluginPath($plugin) {
  647. $_this =& App::getInstance();
  648. $pluginDir = Inflector::underscore($plugin);
  649. for ($i = 0, $length = count($_this->plugins); $i < $length; $i++) {
  650. if (is_dir($_this->plugins[$i] . $pluginDir)) {
  651. return $_this->plugins[$i] . $pluginDir . DS ;
  652. }
  653. }
  654. return $_this->plugins[0] . $pluginDir . DS;
  655. }
  656. /**
  657. * Returns a key/value list of all paths where core libs are found.
  658. * Passing $type only returns the values for a given value of $key.
  659. *
  660. * @param string $type valid values are: 'model', 'behavior', 'controller', 'component',
  661. * 'view', 'helper', 'datasource', 'libs', and 'cake'
  662. * @return array numeric keyed array of core lib paths
  663. * @access public
  664. */
  665. function core($type = null) {
  666. static $paths = false;
  667. if ($paths === false) {
  668. $paths = Cache::read('core_paths', '_cake_core_');
  669. }
  670. if (!$paths) {
  671. $paths = array();
  672. $openBasedir = ini_get('open_basedir');
  673. if ($openBasedir) {
  674. $all = explode(PATH_SEPARATOR, $openBasedir);
  675. $all = array_flip(array_flip(array_merge(array(CAKE_CORE_INCLUDE_PATH), $all)));
  676. } else {
  677. $all = explode(PATH_SEPARATOR, ini_get('include_path'));
  678. $all = array_flip(array_flip((array_merge(array(CAKE_CORE_INCLUDE_PATH), $all))));
  679. }
  680. foreach ($all as $path) {
  681. if ($path !== DS) {
  682. $path = rtrim($path, DS);
  683. }
  684. if (empty($path) || $path === '.') {
  685. continue;
  686. }
  687. $cake = $path . DS . 'cake' . DS;
  688. $libs = $cake . 'libs' . DS;
  689. if (is_dir($libs)) {
  690. $paths['cake'][] = $cake;
  691. $paths['libs'][] = $libs;
  692. $paths['models'][] = $libs . 'model' . DS;
  693. $paths['datasources'][] = $libs . 'model' . DS . 'datasources' . DS;
  694. $paths['behaviors'][] = $libs . 'model' . DS . 'behaviors' . DS;
  695. $paths['controllers'][] = $libs . 'controller' . DS;
  696. $paths['components'][] = $libs . 'controller' . DS . 'components' . DS;
  697. $paths['views'][] = $libs . 'view' . DS;
  698. $paths['helpers'][] = $libs . 'view' . DS . 'helpers' . DS;
  699. $paths['plugins'][] = $path . DS . 'plugins' . DS;
  700. $paths['vendors'][] = $path . DS . 'vendors' . DS;
  701. $paths['shells'][] = $cake . 'console' . DS . 'libs' . DS;
  702. break;
  703. }
  704. }
  705. Cache::write('core_paths', array_filter($paths), '_cake_core_');
  706. }
  707. if ($type && isset($paths[$type])) {
  708. return $paths[$type];
  709. }
  710. return $paths;
  711. }
  712. /**
  713. * Returns an index of objects of the given type, with the physical path to each object.
  714. *
  715. * @param string $type Type of object, i.e. 'model', 'controller', 'helper', or 'plugin'
  716. * @param mixed $path Optional Scan only the path given. If null, paths for the chosen
  717. * type will be used.
  718. * @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
  719. * @return mixed Either false on incorrect / miss. Or an array of found objects.
  720. * @access public
  721. */
  722. function objects($type, $path = null, $cache = true) {
  723. $objects = array();
  724. $extension = false;
  725. $name = $type;
  726. if ($type === 'file' && !$path) {
  727. return false;
  728. } elseif ($type === 'file') {
  729. $extension = true;
  730. $name = $type . str_replace(DS, '', $path);
  731. }
  732. $_this =& App::getInstance();
  733. if (empty($_this->__objects) && $cache === true) {
  734. $_this->__objects = Cache::read('object_map', '_cake_core_');
  735. }
  736. if (!isset($_this->__objects[$name]) || $cache !== true) {
  737. $types = $_this->types;
  738. if (!isset($types[$type])) {
  739. return false;
  740. }
  741. $objects = array();
  742. if (empty($path)) {
  743. $path = $_this->{"{$type}s"};
  744. if (isset($types[$type]['core']) && $types[$type]['core'] === false) {
  745. array_pop($path);
  746. }
  747. }
  748. $items = array();
  749. foreach ((array)$path as $dir) {
  750. if ($dir != APP) {
  751. $items = $_this->__list($dir, $types[$type]['suffix'], $extension);
  752. $objects = array_merge($items, array_diff($objects, $items));
  753. }
  754. }
  755. if ($type !== 'file') {
  756. foreach ($objects as $key => $value) {
  757. $objects[$key] = Inflector::camelize($value);
  758. }
  759. }
  760. if ($cache === true) {
  761. $_this->__cache = true;
  762. }
  763. $_this->__objects[$name] = $objects;
  764. }
  765. return $_this->__objects[$name];
  766. }
  767. /**
  768. * Finds classes based on $name or specific file(s) to search.
  769. *
  770. * @link http://book.cakephp.org/view/529/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->__cache = 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->__cache = 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. require($file);
  963. $this->__loaded[$file] = true;
  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. * Object destructor.
  1180. *
  1181. * Writes cache file if changes have been made to the $__map or $__paths
  1182. *
  1183. * @return void
  1184. * @access private
  1185. */
  1186. function __destruct() {
  1187. if ($this->__cache) {
  1188. $core = App::core('cake');
  1189. unset($this->__paths[rtrim($core[0], DS)]);
  1190. Cache::write('dir_map', array_filter($this->__paths), '_cake_core_');
  1191. Cache::write('file_map', array_filter($this->__map), '_cake_core_');
  1192. Cache::write('object_map', $this->__objects, '_cake_core_');
  1193. }
  1194. }
  1195. }
  1196. ?>