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

/trunk/cake/cake/libs/configure.php

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