PageRenderTime 80ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/configure.php

https://bitbucket.org/webpolis/liiv
PHP | 1194 lines | 720 code | 68 blank | 406 comment | 167 complexity | 5492c05a880ee96a172dc2a098e17350 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-2008, 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-2008, 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 (count($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']";
  441. if (is_array($value)) {
  442. $content .= " = array(";
  443. foreach ($value as $key1 => $value2) {
  444. $value2 = addslashes($value2);
  445. $content .= "'$key1' => '$value2', ";
  446. }
  447. $content .= ");\n";
  448. } else {
  449. $value = addslashes($value);
  450. $content .= " = '$value';\n";
  451. }
  452. }
  453. if (is_null($type)) {
  454. $write = false;
  455. }
  456. Configure::__writeConfig($content, $name, $write);
  457. }
  458. /**
  459. * Returns a key/value list of all paths where core libs are found.
  460. * Passing $type only returns the values for a given value of $key.
  461. *
  462. * @param string $type valid values are: 'model', 'behavior', 'controller', 'component',
  463. * 'view', 'helper', 'datasource', 'libs', and 'cake'
  464. * @return array numeric keyed array of core lib paths
  465. * @access public
  466. */
  467. function corePaths($type = null) {
  468. $paths = Cache::read('core_paths', '_cake_core_');
  469. if (!$paths) {
  470. $paths = array();
  471. $openBasedir = ini_get('open_basedir');
  472. if ($openBasedir) {
  473. $all = explode(PATH_SEPARATOR, $openBasedir);
  474. $all = array_flip(array_flip((array_merge(array(CAKE_CORE_INCLUDE_PATH), $all))));
  475. } else {
  476. $all = explode(PATH_SEPARATOR, ini_get('include_path'));
  477. $all = array_flip(array_flip((array_merge(array(CAKE_CORE_INCLUDE_PATH), $all))));
  478. }
  479. foreach ($all as $path) {
  480. if ($path !== DS) {
  481. $path = rtrim($path, DS);
  482. }
  483. if (empty($path) || $path === '.') {
  484. continue;
  485. }
  486. $cake = $path . DS . 'cake' . DS;
  487. $libs = $cake . 'libs' . DS;
  488. if (is_dir($libs)) {
  489. $paths['libs'][] = $libs;
  490. $paths['model'][] = $libs . 'model' . DS;
  491. $paths['behavior'][] = $libs . 'model' . DS . 'behaviors' . DS;
  492. $paths['controller'][] = $libs . 'controller' . DS;
  493. $paths['component'][] = $libs . 'controller' . DS . 'components' . DS;
  494. $paths['view'][] = $libs . 'view' . DS;
  495. $paths['helper'][] = $libs . 'view' . DS . 'helpers' . DS;
  496. $paths['cake'][] = $cake;
  497. $paths['vendor'][] = $path . DS . 'vendors' . DS;
  498. $paths['shell'][] = $cake . 'console' . DS . 'libs' . DS;
  499. break;
  500. }
  501. }
  502. Cache::write('core_paths', array_filter($paths), '_cake_core_');
  503. }
  504. if ($type && isset($paths[$type])) {
  505. return $paths[$type];
  506. }
  507. return $paths;
  508. }
  509. /**
  510. * Creates a cached version of a configuration file.
  511. * Appends values passed from Configure::store() to the cached file
  512. *
  513. * @param string $content Content to write on file
  514. * @param string $name Name to use for cache file
  515. * @param boolean $write true if content should be written, false otherwise
  516. * @return void
  517. * @access private
  518. */
  519. function __writeConfig($content, $name, $write = true) {
  520. $file = CACHE . 'persistent' . DS . $name . '.php';
  521. if (Configure::read() > 0) {
  522. $expires = "+10 seconds";
  523. } else {
  524. $expires = "+999 days";
  525. }
  526. $cache = cache('persistent' . DS . $name . '.php', null, $expires);
  527. if ($cache === null) {
  528. cache('persistent' . DS . $name . '.php', "<?php\n\$config = array();\n", $expires);
  529. }
  530. if ($write === true) {
  531. if (!class_exists('File')) {
  532. require LIBS . 'file.php';
  533. }
  534. $fileClass = new File($file);
  535. if ($fileClass->writable()) {
  536. $fileClass->append($content);
  537. }
  538. }
  539. }
  540. /**
  541. * Checks $name for dot notation to create dynamic Configure::$var as an array when needed.
  542. *
  543. * @param mixed $name Name to split
  544. * @return array Name separated in items through dot notation
  545. * @access private
  546. */
  547. function __configVarNames($name) {
  548. if (is_string($name)) {
  549. if (strpos($name, ".")) {
  550. return explode(".", $name);
  551. }
  552. return array($name);
  553. }
  554. return $name;
  555. }
  556. /**
  557. * Build path references. Merges the supplied $paths
  558. * with the base paths and the default core paths.
  559. *
  560. * @param array $paths paths defines in config/bootstrap.php
  561. * @return void
  562. * @access public
  563. */
  564. function buildPaths($paths) {
  565. $_this =& Configure::getInstance();
  566. $core = $_this->corePaths();
  567. $basePaths = array(
  568. 'model' => array(MODELS),
  569. 'behavior' => array(BEHAVIORS),
  570. 'controller' => array(CONTROLLERS),
  571. 'component' => array(COMPONENTS),
  572. 'view' => array(VIEWS),
  573. 'helper' => array(HELPERS),
  574. 'plugin' => array(APP . 'plugins' . DS),
  575. 'vendor' => array(APP . 'vendors' . DS, VENDORS),
  576. 'locale' => array(APP . 'locale' . DS),
  577. 'shell' => array(),
  578. 'datasource' => array(MODELS . 'datasources')
  579. );
  580. foreach ($basePaths as $type => $default) {
  581. $pathsVar = $type . 'Paths';
  582. $merge = array();
  583. if (isset($core[$type])) {
  584. $merge = $core[$type];
  585. }
  586. if ($type === 'model' || $type === 'controller' || $type === 'helper') {
  587. $merge = array_merge(array(APP), $merge);
  588. }
  589. if (!is_array($default)) {
  590. $default = array($default);
  591. }
  592. $_this->{$pathsVar} = $default;
  593. if (isset($paths[$pathsVar]) && !empty($paths[$pathsVar])) {
  594. $path = array_flip(array_flip((array_merge(
  595. $_this->{$pathsVar}, (array)$paths[$pathsVar], $merge
  596. ))));
  597. $_this->{$pathsVar} = array_values($path);
  598. } else {
  599. $path = array_flip(array_flip((array_merge($_this->{$pathsVar}, $merge))));
  600. $_this->{$pathsVar} = array_values($path);
  601. }
  602. }
  603. }
  604. /**
  605. * Loads app/config/bootstrap.php.
  606. * If the alternative paths are set in this file
  607. * they will be added to the paths vars.
  608. *
  609. * @param boolean $boot Load application bootstrap (if true)
  610. * @return void
  611. * @access private
  612. */
  613. function __loadBootstrap($boot) {
  614. $modelPaths = $behaviorPaths = $controllerPaths = $componentPaths = $viewPaths = $helperPaths = $pluginPaths = $vendorPaths = $localePaths = $shellPaths = null;
  615. if ($boot) {
  616. Configure::write('App', array('base' => false, 'baseUrl' => false, 'dir' => APP_DIR, 'webroot' => WEBROOT_DIR));
  617. if (!include(CONFIGS . 'core.php')) {
  618. 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);
  619. }
  620. if (!include(CONFIGS . 'bootstrap.php')) {
  621. 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);
  622. }
  623. if (Configure::read('Cache.disable') !== true) {
  624. $cache = Cache::config('default');
  625. if (empty($cache['settings'])) {
  626. trigger_error('Cache not configured properly. Please check Cache::config(); in APP/config/core.php', E_USER_WARNING);
  627. $cache = Cache::config('default', array('engine' => 'File'));
  628. }
  629. $path = $prefix = $duration = null;
  630. if (!empty($cache['settings']['path'])) {
  631. $path = realpath($cache['settings']['path']);
  632. } else {
  633. $prefix = $cache['settings']['prefix'];
  634. }
  635. if (Configure::read() >= 1) {
  636. $duration = '+10 seconds';
  637. } else {
  638. $duration = '+999 days';
  639. }
  640. if (Cache::config('_cake_core_') === false) {
  641. Cache::config('_cake_core_', array_merge($cache['settings'], array(
  642. 'prefix' => $prefix . 'cake_core_', 'path' => $path . DS . 'persistent' . DS,
  643. 'serialize' => true, 'duration' => $duration
  644. )));
  645. }
  646. if (Cache::config('_cake_model_') === false) {
  647. Cache::config('_cake_model_', array_merge($cache['settings'], array(
  648. 'prefix' => $prefix . 'cake_model_', 'path' => $path . DS . 'models' . DS,
  649. 'serialize' => true, 'duration' => $duration
  650. )));
  651. }
  652. Cache::config('default');
  653. }
  654. Configure::buildPaths(compact(
  655. 'modelPaths', 'viewPaths', 'controllerPaths', 'helperPaths', 'componentPaths',
  656. 'behaviorPaths', 'pluginPaths', 'vendorPaths', 'localePaths', 'shellPaths'
  657. ));
  658. }
  659. }
  660. /**
  661. * Caches the object map when the instance of the Configure class is destroyed
  662. *
  663. * @access public
  664. */
  665. function __destruct() {
  666. if ($this->__cache) {
  667. Cache::write('object_map', array_filter($this->__objects), '_cake_core_');
  668. }
  669. }
  670. }
  671. /**
  672. * Class and file loader.
  673. *
  674. * @link http://book.cakephp.org/view/499/The-App-Class
  675. * @since CakePHP(tm) v 1.2.0.6001
  676. * @package cake
  677. * @subpackage cake.cake.libs
  678. */
  679. class App extends Object {
  680. /**
  681. * Paths to search for files.
  682. *
  683. * @var array
  684. * @access public
  685. */
  686. var $search = array();
  687. /**
  688. * Whether or not to return the file that is loaded.
  689. *
  690. * @var boolean
  691. * @access public
  692. */
  693. var $return = false;
  694. /**
  695. * Determines if $__maps and $__paths cache should be written.
  696. *
  697. * @var boolean
  698. * @access private
  699. */
  700. var $__cache = false;
  701. /**
  702. * Holds key/value pairs of $type => file path.
  703. *
  704. * @var array
  705. * @access private
  706. */
  707. var $__map = array();
  708. /**
  709. * Holds paths for deep searching of files.
  710. *
  711. * @var array
  712. * @access private
  713. */
  714. var $__paths = array();
  715. /**
  716. * Holds loaded files.
  717. *
  718. * @var array
  719. * @access private
  720. */
  721. var $__loaded = array();
  722. /**
  723. * Finds classes based on $name or specific file(s) to search.
  724. *
  725. * @link http://book.cakephp.org/view/529/Using-App-import
  726. * @param mixed $type The type of Class if passed as a string, or all params can be passed as
  727. * an single array to $type,
  728. * @param string $name Name of the Class or a unique name for the file
  729. * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
  730. * array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
  731. * $ext allows setting the extension of the file name
  732. * based on Inflector::underscore($name) . ".$ext";
  733. * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
  734. * @param string $file full name of the file to search for including extension
  735. * @param boolean $return, return the loaded file, the file must have a return
  736. * statement in it to work: return $variable;
  737. * @return boolean true if Class is already in memory or if file is found and loaded, false if not
  738. * @access public
  739. */
  740. function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
  741. $plugin = $directory = null;
  742. if (is_array($type)) {
  743. extract($type, EXTR_OVERWRITE);
  744. }
  745. if (is_array($parent)) {
  746. extract($parent, EXTR_OVERWRITE);
  747. }
  748. if ($name === null && $file === null) {
  749. $name = $type;
  750. $type = 'Core';
  751. } elseif ($name === null) {
  752. $type = 'File';
  753. }
  754. if (is_array($name)) {
  755. foreach ($name as $class) {
  756. $tempType = $type;
  757. $plugin = null;
  758. if (strpos($class, '.') !== false) {
  759. $value = explode('.', $class);
  760. $count = count($value);
  761. if ($count > 2) {
  762. $tempType = $value[0];
  763. $plugin = $value[1] . '.';
  764. $class = $value[2];
  765. } elseif ($count === 2 && ($type === 'Core' || $type === 'File')) {
  766. $tempType = $value[0];
  767. $class = $value[1];
  768. } else {
  769. $plugin = $value[0] . '.';
  770. $class = $value[1];
  771. }
  772. }
  773. if (!App::import($tempType, $plugin . $class)) {
  774. return false;
  775. }
  776. }
  777. return true;
  778. }
  779. if ($name != null && strpos($name, '.') !== false) {
  780. list($plugin, $name) = explode('.', $name);
  781. }
  782. $_this =& App::getInstance();
  783. $_this->return = $return;
  784. if (isset($ext)) {
  785. $file = Inflector::underscore($name) . ".$ext";
  786. }
  787. $ext = $_this->__settings($type, $plugin, $parent);
  788. if ($name != null && !class_exists($name . $ext['class'])) {
  789. if ($load = $_this->__mapped($name . $ext['class'], $type, $plugin)) {
  790. if ($_this->__load($load)) {
  791. $_this->__overload($type, $name . $ext['class']);
  792. if ($_this->return) {
  793. $value = include $load;
  794. return $value;
  795. }
  796. return true;
  797. } else {
  798. $_this->__remove($name . $ext['class'], $type, $plugin);
  799. $_this->__cache = true;
  800. }
  801. }
  802. if (!empty($search)) {
  803. $_this->search = $search;
  804. } elseif ($plugin) {
  805. $_this->search = $_this->__paths('plugin');
  806. } else {
  807. $_this->search = $_this->__paths($type);
  808. }
  809. $find = $file;
  810. if ($find === null) {
  811. $find = Inflector::underscore($name . $ext['suffix']).'.php';
  812. if ($plugin) {
  813. $paths = $_this->search;
  814. foreach ($paths as $key => $value) {
  815. $_this->search[$key] = $value . $ext['path'];
  816. }
  817. $plugin = Inflector::camelize($plugin);
  818. }
  819. }
  820. if (strtolower($type) !== 'vendor' && empty($search) && $_this->__load($file)) {
  821. $directory = false;
  822. } else {
  823. $file = $find;
  824. $directory = $_this->__find($find, true);
  825. }
  826. if ($directory !== null) {
  827. $_this->__cache = true;
  828. $_this->__map($directory . $file, $name . $ext['class'], $type, $plugin);
  829. $_this->__overload($type, $name . $ext['class']);
  830. if ($_this->return) {
  831. $value = include $directory . $file;
  832. return $value;
  833. }
  834. return true;
  835. }
  836. return false;
  837. }
  838. return true;
  839. }
  840. /**
  841. * Returns a single instance of App.
  842. *
  843. * @return object
  844. * @access public
  845. */
  846. function &getInstance() {
  847. static $instance = array();
  848. if (!$instance) {
  849. $instance[0] =& new App();
  850. $instance[0]->__map = Cache::read('file_map', '_cake_core_');
  851. }
  852. return $instance[0];
  853. }
  854. /**
  855. * Locates the $file in $__paths, searches recursively.
  856. *
  857. * @param string $file full file name
  858. * @param boolean $recursive search $__paths recursively
  859. * @return mixed boolean on fail, $file directory path on success
  860. * @access private
  861. */
  862. function __find($file, $recursive = true) {
  863. if (empty($this->search)) {
  864. return null;
  865. } elseif (is_string($this->search)) {
  866. $this->search = array($this->search);
  867. }
  868. if (empty($this->__paths)) {
  869. $this->__paths = Cache::read('dir_map', '_cake_core_');
  870. }
  871. foreach ($this->search as $path) {
  872. $path = rtrim($path, DS);
  873. if ($path === rtrim(APP, DS)) {
  874. $recursive = false;
  875. }
  876. if ($recursive === false) {
  877. if ($this->__load($path . DS . $file)) {
  878. return $path . DS;
  879. }
  880. continue;
  881. }
  882. if (!isset($this->__paths[$path])) {
  883. if (!class_exists('Folder')) {
  884. require LIBS . 'folder.php';
  885. }
  886. $Folder =& new Folder();
  887. $directories = $Folder->tree($path, false, 'dir');
  888. $this->__paths[$path] = $directories;
  889. }
  890. foreach ($this->__paths[$path] as $directory) {
  891. if ($this->__load($directory . DS . $file)) {
  892. return $directory . DS;
  893. }
  894. }
  895. }
  896. return null;
  897. }
  898. /**
  899. * Attempts to load $file.
  900. *
  901. * @param string $file full path to file including file name
  902. * @return boolean
  903. * @access private
  904. */
  905. function __load($file) {
  906. if (empty($file)) {
  907. return false;
  908. }
  909. if (!$this->return && isset($this->__loaded[$file])) {
  910. return true;
  911. }
  912. if (file_exists($file)) {
  913. if (!$this->return) {
  914. require($file);
  915. $this->__loaded[$file] = true;
  916. }
  917. return true;
  918. }
  919. return false;
  920. }
  921. /**
  922. * Maps the $name to the $file.
  923. *
  924. * @param string $file full path to file
  925. * @param string $name unique name for this map
  926. * @param string $type type object being mapped
  927. * @param string $plugin if object is from a plugin, the name of the plugin
  928. * @access private
  929. */
  930. function __map($file, $name, $type, $plugin) {
  931. if ($plugin) {
  932. $plugin = Inflector::camelize($plugin);
  933. $this->__map['Plugin'][$plugin][$type][$name] = $file;
  934. } else {
  935. $this->__map[$type][$name] = $file;
  936. }
  937. }
  938. /**
  939. * Returns a file's complete path.
  940. *
  941. * @param string $name unique name
  942. * @param string $type type object
  943. * @param string $plugin if object is from a plugin, the name of the plugin
  944. * @return mixed, file path if found, false otherwise
  945. * @access private
  946. */
  947. function __mapped($name, $type, $plugin) {
  948. if ($plugin) {
  949. $plugin = Inflector::camelize($plugin);
  950. if (isset($this->__map['Plugin'][$plugin][$type]) && isset($this->__map['Plugin'][$plugin][$type][$name])) {
  951. return $this->__map['Plugin'][$plugin][$type][$name];
  952. }
  953. return false;
  954. }
  955. if (isset($this->__map[$type]) && isset($this->__map[$type][$name])) {
  956. return $this->__map[$type][$name];
  957. }
  958. return false;
  959. }
  960. /**
  961. * Used to overload objects as needed.
  962. *
  963. * @param string $type Model or Helper
  964. * @param string $name Class name to overload
  965. * @access private
  966. */
  967. function __overload($type, $name) {
  968. if (($type === 'Model' || $type === 'Helper') && strtolower($name) != 'schema') {
  969. Overloadable::overload($name);
  970. }
  971. }
  972. /**
  973. * Loads parent classes based on $type.
  974. * Returns a prefix or suffix needed for loading files.
  975. *
  976. * @param string $type type of object
  977. * @param string $plugin name of plugin
  978. * @param boolean $parent false will not attempt to load parent
  979. * @return array
  980. * @access private
  981. */
  982. function __settings($type, $plugin, $parent) {
  983. if (!$parent) {
  984. return null;
  985. }
  986. if ($plugin) {
  987. $plugin = Inflector::underscore($plugin);
  988. $name = Inflector::camelize($plugin);
  989. }
  990. $path = null;
  991. $load = strtolower($type);
  992. switch ($load) {
  993. case 'model':
  994. if (!class_exists('Model')) {
  995. App::import('Core', 'Model', false, Configure::corePaths('model'));
  996. }
  997. if (!class_exists('AppModel')) {
  998. App::import($type, 'AppModel', false, Configure::read('modelPaths'));
  999. }
  1000. if ($plugin) {
  1001. if (!class_exists($name . 'AppModel')) {
  1002. App::import($type, $plugin . '.' . $name . 'AppModel', false, array(), $plugin . DS . $plugin . '_app_model.php');
  1003. }
  1004. $path = $plugin . DS . 'models' . DS;
  1005. }
  1006. return array('class' => null, 'suffix' => null, 'path' => $path);
  1007. break;
  1008. case 'behavior':
  1009. if ($plugin) {
  1010. $path = $plugin . DS . 'models' . DS . 'behaviors' . DS;
  1011. }
  1012. return array('class' => $type, 'suffix' => null, 'path' => $path);
  1013. break;
  1014. case 'controller':
  1015. App::import($type, 'AppController', false);
  1016. if ($plugin) {
  1017. App::import($type, $plugin . '.' . $name . 'AppController', false, array(), $plugin . DS . $plugin . '_app_controller.php');
  1018. $path = $plugin . DS . 'controllers' . DS;
  1019. }
  1020. return array('class' => $type, 'suffix' => $type, 'path' => $path);
  1021. break;
  1022. case 'component':
  1023. if ($plugin) {
  1024. $path = $plugin . DS . 'controllers' . DS . 'components' . DS;
  1025. }
  1026. return array('class' => $type, 'suffix' => null, 'path' => $path);
  1027. break;
  1028. case 'view':
  1029. if ($plugin) {
  1030. $path = $plugin . DS . 'views' . DS;
  1031. }
  1032. return array('class' => $type, 'suffix' => null, 'path' => $path);
  1033. break;
  1034. case 'helper':
  1035. if (!class_exists('AppHelper')) {
  1036. App::import($type, 'AppHelper', false);
  1037. }
  1038. if ($plugin) {
  1039. $path = $plugin . DS . 'views' . DS . 'helpers' . DS;
  1040. }
  1041. return array('class' => $type, 'suffix' => null, 'path' => $path);
  1042. break;
  1043. case 'vendor':
  1044. if ($plugin) {
  1045. $path = $plugin . DS . 'vendors' . DS;
  1046. }
  1047. return array('class' => null, 'suffix' => null, 'path' => $path);
  1048. break;
  1049. default:
  1050. $type = $suffix = $path = null;
  1051. break;
  1052. }
  1053. return array('class' => null, 'suffix' => null, 'path' => null);
  1054. }
  1055. /**
  1056. * Returns default search paths.
  1057. *
  1058. * @param string $type type of object to be searched
  1059. * @return array list of paths
  1060. * @access private
  1061. */
  1062. function __paths($type) {
  1063. $type = strtolower($type);
  1064. if ($type === 'core') {
  1065. $path = Configure::corePaths();
  1066. $paths = array();
  1067. foreach ($path as $key => $value) {
  1068. $count = count($key);
  1069. for ($i = 0; $i < $count; $i++) {
  1070. $paths[] = $path[$key][$i];
  1071. }
  1072. }
  1073. return $paths;
  1074. }
  1075. if ($paths = Configure::read($type . 'Paths')) {
  1076. return $paths;
  1077. }
  1078. switch ($type) {
  1079. case 'plugin':
  1080. return array(APP . 'plugins' . DS);
  1081. case 'vendor':
  1082. return array(APP . 'vendors' . DS, VENDORS, APP . 'plugins' . DS);
  1083. case 'controller':
  1084. return array(APP . 'controllers' . DS, APP);
  1085. case 'model':
  1086. return array(APP . 'models' . DS, APP);
  1087. case 'view':
  1088. return array(APP . 'views' . DS);
  1089. }
  1090. }
  1091. /**
  1092. * Removes file location from map if the file has been deleted.
  1093. *
  1094. * @param string $name name of object
  1095. * @param string $type type of object
  1096. * @param string $plugin name of plugin
  1097. * @return void
  1098. * @access private
  1099. */
  1100. function __remove($name, $type, $plugin) {
  1101. if ($plugin) {
  1102. $plugin = Inflector::camelize($plugin);
  1103. unset($this->__map['Plugin'][$plugin][$type][$name]);
  1104. } else {
  1105. unset($this->__map[$type][$name]);
  1106. }
  1107. }
  1108. /**
  1109. * Object destructor.
  1110. *
  1111. * Writes cache file if changes have been made to the $__map or $__paths
  1112. *
  1113. * @return void
  1114. * @access private
  1115. */
  1116. function __destruct() {
  1117. if ($this->__cache) {
  1118. $core = Configure::corePaths('cake');
  1119. unset($this->__paths[rtrim($core[0], DS)]);
  1120. Cache::write('dir_map', array_filter($this->__paths), '_cake_core_');
  1121. Cache::write('file_map', array_filter($this->__map), '_cake_core_');
  1122. }
  1123. }
  1124. }
  1125. ?>