PageRenderTime 64ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/configure.php

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