PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/olink/src/cake/libs/configure.php

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