YiiBase.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. <?php
  2. /**
  3. * YiiBase class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. * @package system
  10. * @since 1.0
  11. */
  12. /**
  13. * Gets the application start timestamp.
  14. */
  15. defined('YII_BEGIN_TIME') or define('YII_BEGIN_TIME',microtime(true));
  16. /**
  17. * This constant defines whether the application should be in debug mode or not. Defaults to false.
  18. */
  19. defined('YII_DEBUG') or define('YII_DEBUG',false);
  20. /**
  21. * This constant defines how much call stack information (file name and line number) should be logged by Yii::trace().
  22. * Defaults to 0, meaning no backtrace information. If it is greater than 0,
  23. * at most that number of call stacks will be logged. Note, only user application call stacks are considered.
  24. */
  25. defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',0);
  26. /**
  27. * This constant defines whether exception handling should be enabled. Defaults to true.
  28. */
  29. defined('YII_ENABLE_EXCEPTION_HANDLER') or define('YII_ENABLE_EXCEPTION_HANDLER',true);
  30. /**
  31. * This constant defines whether error handling should be enabled. Defaults to true.
  32. */
  33. defined('YII_ENABLE_ERROR_HANDLER') or define('YII_ENABLE_ERROR_HANDLER',true);
  34. /**
  35. * Defines the Yii framework installation path.
  36. */
  37. defined('YII_PATH') or define('YII_PATH',dirname(__FILE__));
  38. /**
  39. * Defines the Zii library installation path.
  40. */
  41. defined('YII_ZII_PATH') or define('YII_ZII_PATH',YII_PATH.DIRECTORY_SEPARATOR.'zii');
  42. /**
  43. * YiiBase is a helper class serving common framework functionalities.
  44. *
  45. * Do not use YiiBase directly. Instead, use its child class {@link Yii} where
  46. * you can customize methods of YiiBase.
  47. *
  48. * @author Qiang Xue <qiang.xue@gmail.com>
  49. * @package system
  50. * @since 1.0
  51. */
  52. class YiiBase
  53. {
  54. /**
  55. * @var array class map used by the Yii autoloading mechanism.
  56. * The array keys are the class names and the array values are the corresponding class file paths.
  57. * @since 1.1.5
  58. */
  59. public static $classMap=array();
  60. /**
  61. * @var boolean whether to rely on PHP include path to autoload class files. Defaults to true.
  62. * You may set this to be false if your hosting environment doesn't allow changing the PHP
  63. * include path, or if you want to append additional autoloaders to the default Yii autoloader.
  64. * @since 1.1.8
  65. */
  66. public static $enableIncludePath=true;
  67. private static $_aliases=array('system'=>YII_PATH,'zii'=>YII_ZII_PATH); // alias => path
  68. private static $_imports=array(); // alias => class name or directory
  69. private static $_includePaths; // list of include paths
  70. private static $_app;
  71. private static $_logger;
  72. /**
  73. * @return string the version of Yii framework
  74. */
  75. public static function getVersion()
  76. {
  77. return '1.1.14';
  78. }
  79. /**
  80. * Creates a Web application instance.
  81. * @param mixed $config application configuration.
  82. * If a string, it is treated as the path of the file that contains the configuration;
  83. * If an array, it is the actual configuration information.
  84. * Please make sure you specify the {@link CApplication::basePath basePath} property in the configuration,
  85. * which should point to the directory containing all application logic, template and data.
  86. * If not, the directory will be defaulted to 'protected'.
  87. * @return CWebApplication
  88. */
  89. public static function createWebApplication($config=null)
  90. {
  91. return self::createApplication('CWebApplication',$config);
  92. }
  93. /**
  94. * Creates a console application instance.
  95. * @param mixed $config application configuration.
  96. * If a string, it is treated as the path of the file that contains the configuration;
  97. * If an array, it is the actual configuration information.
  98. * Please make sure you specify the {@link CApplication::basePath basePath} property in the configuration,
  99. * which should point to the directory containing all application logic, template and data.
  100. * If not, the directory will be defaulted to 'protected'.
  101. * @return CConsoleApplication
  102. */
  103. public static function createConsoleApplication($config=null)
  104. {
  105. return self::createApplication('CConsoleApplication',$config);
  106. }
  107. /**
  108. * Creates an application of the specified class.
  109. * @param string $class the application class name
  110. * @param mixed $config application configuration. This parameter will be passed as the parameter
  111. * to the constructor of the application class.
  112. * @return mixed the application instance
  113. */
  114. public static function createApplication($class,$config=null)
  115. {
  116. return new $class($config);
  117. }
  118. /**
  119. * Returns the application singleton or null if the singleton has not been created yet.
  120. * @return CApplication the application singleton, null if the singleton has not been created yet.
  121. */
  122. public static function app()
  123. {
  124. return self::$_app;
  125. }
  126. /**
  127. * Stores the application instance in the class static member.
  128. * This method helps implement a singleton pattern for CApplication.
  129. * Repeated invocation of this method or the CApplication constructor
  130. * will cause the throw of an exception.
  131. * To retrieve the application instance, use {@link app()}.
  132. * @param CApplication $app the application instance. If this is null, the existing
  133. * application singleton will be removed.
  134. * @throws CException if multiple application instances are registered.
  135. */
  136. public static function setApplication($app)
  137. {
  138. if(self::$_app===null || $app===null)
  139. self::$_app=$app;
  140. else
  141. throw new CException(Yii::t('yii','Yii application can only be created once.'));
  142. }
  143. /**
  144. * @return string the path of the framework
  145. */
  146. public static function getFrameworkPath()
  147. {
  148. return YII_PATH;
  149. }
  150. /**
  151. * Creates an object and initializes it based on the given configuration.
  152. *
  153. * The specified configuration can be either a string or an array.
  154. * If the former, the string is treated as the object type which can
  155. * be either the class name or {@link YiiBase::getPathOfAlias class path alias}.
  156. * If the latter, the 'class' element is treated as the object type,
  157. * and the rest of the name-value pairs in the array are used to initialize
  158. * the corresponding object properties.
  159. *
  160. * Any additional parameters passed to this method will be
  161. * passed to the constructor of the object being created.
  162. *
  163. * @param mixed $config the configuration. It can be either a string or an array.
  164. * @return mixed the created object
  165. * @throws CException if the configuration does not have a 'class' element.
  166. */
  167. public static function createComponent($config)
  168. {
  169. if(is_string($config))
  170. {
  171. $type=$config;
  172. $config=array();
  173. }
  174. elseif(isset($config['class']))
  175. {
  176. $type=$config['class'];
  177. unset($config['class']);
  178. }
  179. else
  180. throw new CException(Yii::t('yii','Object configuration must be an array containing a "class" element.'));
  181. if(!class_exists($type,false))
  182. $type=Yii::import($type,true);
  183. if(($n=func_num_args())>1)
  184. {
  185. $args=func_get_args();
  186. if($n===2)
  187. $object=new $type($args[1]);
  188. elseif($n===3)
  189. $object=new $type($args[1],$args[2]);
  190. elseif($n===4)
  191. $object=new $type($args[1],$args[2],$args[3]);
  192. else
  193. {
  194. unset($args[0]);
  195. $class=new ReflectionClass($type);
  196. // Note: ReflectionClass::newInstanceArgs() is available for PHP 5.1.3+
  197. // $object=$class->newInstanceArgs($args);
  198. $object=call_user_func_array(array($class,'newInstance'),$args);
  199. }
  200. }
  201. else
  202. $object=new $type;
  203. foreach($config as $key=>$value)
  204. $object->$key=$value;
  205. return $object;
  206. }
  207. /**
  208. * Imports a class or a directory.
  209. *
  210. * Importing a class is like including the corresponding class file.
  211. * The main difference is that importing a class is much lighter because it only
  212. * includes the class file when the class is referenced the first time.
  213. *
  214. * Importing a directory is equivalent to adding a directory into the PHP include path.
  215. * If multiple directories are imported, the directories imported later will take
  216. * precedence in class file searching (i.e., they are added to the front of the PHP include path).
  217. *
  218. * Path aliases are used to import a class or directory. For example,
  219. * <ul>
  220. * <li><code>application.components.GoogleMap</code>: import the <code>GoogleMap</code> class.</li>
  221. * <li><code>application.components.*</code>: import the <code>components</code> directory.</li>
  222. * </ul>
  223. *
  224. * The same path alias can be imported multiple times, but only the first time is effective.
  225. * Importing a directory does not import any of its subdirectories.
  226. *
  227. * Starting from version 1.1.5, this method can also be used to import a class in namespace format
  228. * (available for PHP 5.3 or above only). It is similar to importing a class in path alias format,
  229. * except that the dot separator is replaced by the backslash separator. For example, importing
  230. * <code>application\components\GoogleMap</code> is similar to importing <code>application.components.GoogleMap</code>.
  231. * The difference is that the former class is using qualified name, while the latter unqualified.
  232. *
  233. * Note, importing a class in namespace format requires that the namespace corresponds to
  234. * a valid path alias once backslash characters are replaced with dot characters.
  235. * For example, the namespace <code>application\components</code> must correspond to a valid
  236. * path alias <code>application.components</code>.
  237. *
  238. * @param string $alias path alias to be imported
  239. * @param boolean $forceInclude whether to include the class file immediately. If false, the class file
  240. * will be included only when the class is being used. This parameter is used only when
  241. * the path alias refers to a class.
  242. * @return string the class name or the directory that this alias refers to
  243. * @throws CException if the alias is invalid
  244. */
  245. public static function import($alias,$forceInclude=false)
  246. {
  247. if(isset(self::$_imports[$alias])) // previously imported
  248. return self::$_imports[$alias];
  249. if(class_exists($alias,false) || interface_exists($alias,false))
  250. return self::$_imports[$alias]=$alias;
  251. if(($pos=strrpos($alias,'\\'))!==false) // a class name in PHP 5.3 namespace format
  252. {
  253. $namespace=str_replace('\\','.',ltrim(substr($alias,0,$pos),'\\'));
  254. if(($path=self::getPathOfAlias($namespace))!==false)
  255. {
  256. $classFile=$path.DIRECTORY_SEPARATOR.substr($alias,$pos+1).'.php';
  257. if($forceInclude)
  258. {
  259. if(is_file($classFile))
  260. require($classFile);
  261. else
  262. throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing PHP file and the file is readable.',array('{alias}'=>$alias)));
  263. self::$_imports[$alias]=$alias;
  264. }
  265. else
  266. self::$classMap[$alias]=$classFile;
  267. return $alias;
  268. }
  269. else
  270. {
  271. // try to autoload the class with an autoloader
  272. if (class_exists($alias,true))
  273. return self::$_imports[$alias]=$alias;
  274. else
  275. throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing directory or file.',
  276. array('{alias}'=>$namespace)));
  277. }
  278. }
  279. if(($pos=strrpos($alias,'.'))===false) // a simple class name
  280. {
  281. if($forceInclude && self::autoload($alias))
  282. self::$_imports[$alias]=$alias;
  283. return $alias;
  284. }
  285. $className=(string)substr($alias,$pos+1);
  286. $isClass=$className!=='*';
  287. if($isClass && (class_exists($className,false) || interface_exists($className,false)))
  288. return self::$_imports[$alias]=$className;
  289. if(($path=self::getPathOfAlias($alias))!==false)
  290. {
  291. if($isClass)
  292. {
  293. if($forceInclude)
  294. {
  295. if(is_file($path.'.php'))
  296. require($path.'.php');
  297. else
  298. throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing PHP file and the file is readable.',array('{alias}'=>$alias)));
  299. self::$_imports[$alias]=$className;
  300. }
  301. else
  302. self::$classMap[$className]=$path.'.php';
  303. return $className;
  304. }
  305. else // a directory
  306. {
  307. if(self::$_includePaths===null)
  308. {
  309. self::$_includePaths=array_unique(explode(PATH_SEPARATOR,get_include_path()));
  310. if(($pos=array_search('.',self::$_includePaths,true))!==false)
  311. unset(self::$_includePaths[$pos]);
  312. }
  313. array_unshift(self::$_includePaths,$path);
  314. if(self::$enableIncludePath && set_include_path('.'.PATH_SEPARATOR.implode(PATH_SEPARATOR,self::$_includePaths))===false)
  315. self::$enableIncludePath=false;
  316. return self::$_imports[$alias]=$path;
  317. }
  318. }
  319. else
  320. throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing directory or file.',
  321. array('{alias}'=>$alias)));
  322. }
  323. /**
  324. * Translates an alias into a file path.
  325. * Note, this method does not ensure the existence of the resulting file path.
  326. * It only checks if the root alias is valid or not.
  327. * @param string $alias alias (e.g. system.web.CController)
  328. * @return mixed file path corresponding to the alias, false if the alias is invalid.
  329. */
  330. public static function getPathOfAlias($alias)
  331. {
  332. if(isset(self::$_aliases[$alias]))
  333. return self::$_aliases[$alias];
  334. elseif(($pos=strpos($alias,'.'))!==false)
  335. {
  336. $rootAlias=substr($alias,0,$pos);
  337. if(isset(self::$_aliases[$rootAlias]))
  338. return self::$_aliases[$alias]=rtrim(self::$_aliases[$rootAlias].DIRECTORY_SEPARATOR.str_replace('.',DIRECTORY_SEPARATOR,substr($alias,$pos+1)),'*'.DIRECTORY_SEPARATOR);
  339. elseif(self::$_app instanceof CWebApplication)
  340. {
  341. if(self::$_app->findModule($rootAlias)!==null)
  342. return self::getPathOfAlias($alias);
  343. }
  344. }
  345. return false;
  346. }
  347. /**
  348. * Create a path alias.
  349. * Note, this method neither checks the existence of the path nor normalizes the path.
  350. * @param string $alias alias to the path
  351. * @param string $path the path corresponding to the alias. If this is null, the corresponding
  352. * path alias will be removed.
  353. */
  354. public static function setPathOfAlias($alias,$path)
  355. {
  356. if(empty($path))
  357. unset(self::$_aliases[$alias]);
  358. else
  359. self::$_aliases[$alias]=rtrim($path,'\\/');
  360. }
  361. /**
  362. * Class autoload loader.
  363. * This method is provided to be invoked within an __autoload() magic method.
  364. * @param string $className class name
  365. * @return boolean whether the class has been loaded successfully
  366. */
  367. public static function autoload($className)
  368. {
  369. // use include so that the error PHP file may appear
  370. if(isset(self::$classMap[$className]))
  371. include(self::$classMap[$className]);
  372. elseif(isset(self::$_coreClasses[$className]))
  373. include(YII_PATH.self::$_coreClasses[$className]);
  374. else
  375. {
  376. // include class file relying on include_path
  377. if(strpos($className,'\\')===false) // class without namespace
  378. {
  379. if(self::$enableIncludePath===false)
  380. {
  381. foreach(self::$_includePaths as $path)
  382. {
  383. $classFile=$path.DIRECTORY_SEPARATOR.$className.'.php';
  384. if(is_file($classFile))
  385. {
  386. include($classFile);
  387. if(YII_DEBUG && basename(realpath($classFile))!==$className.'.php')
  388. throw new CException(Yii::t('yii','Class name "{class}" does not match class file "{file}".', array(
  389. '{class}'=>$className,
  390. '{file}'=>$classFile,
  391. )));
  392. break;
  393. }
  394. }
  395. }
  396. else
  397. include($className.'.php');
  398. }
  399. else // class name with namespace in PHP 5.3
  400. {
  401. $namespace=str_replace('\\','.',ltrim($className,'\\'));
  402. if(($path=self::getPathOfAlias($namespace))!==false)
  403. include($path.'.php');
  404. else
  405. return false;
  406. }
  407. return class_exists($className,false) || interface_exists($className,false);
  408. }
  409. return true;
  410. }
  411. /**
  412. * Writes a trace message.
  413. * This method will only log a message when the application is in debug mode.
  414. * @param string $msg message to be logged
  415. * @param string $category category of the message
  416. * @see log
  417. */
  418. public static function trace($msg,$category='application')
  419. {
  420. if(YII_DEBUG)
  421. self::log($msg,CLogger::LEVEL_TRACE,$category);
  422. }
  423. /**
  424. * Logs a message.
  425. * Messages logged by this method may be retrieved via {@link CLogger::getLogs}
  426. * and may be recorded in different media, such as file, email, database, using
  427. * {@link CLogRouter}.
  428. * @param string $msg message to be logged
  429. * @param string $level level of the message (e.g. 'trace', 'warning', 'error'). It is case-insensitive.
  430. * @param string $category category of the message (e.g. 'system.web'). It is case-insensitive.
  431. */
  432. public static function log($msg,$level=CLogger::LEVEL_INFO,$category='application')
  433. {
  434. if(self::$_logger===null)
  435. self::$_logger=new CLogger;
  436. if(YII_DEBUG && YII_TRACE_LEVEL>0 && $level!==CLogger::LEVEL_PROFILE)
  437. {
  438. $traces=debug_backtrace();
  439. $count=0;
  440. foreach($traces as $trace)
  441. {
  442. if(isset($trace['file'],$trace['line']) && strpos($trace['file'],YII_PATH)!==0)
  443. {
  444. $msg.="\nin ".$trace['file'].' ('.$trace['line'].')';
  445. if(++$count>=YII_TRACE_LEVEL)
  446. break;
  447. }
  448. }
  449. }
  450. self::$_logger->log($msg,$level,$category);
  451. }
  452. /**
  453. * Marks the beginning of a code block for profiling.
  454. * This has to be matched with a call to {@link endProfile()} with the same token.
  455. * The begin- and end- calls must also be properly nested, e.g.,
  456. * <pre>
  457. * Yii::beginProfile('block1');
  458. * Yii::beginProfile('block2');
  459. * Yii::endProfile('block2');
  460. * Yii::endProfile('block1');
  461. * </pre>
  462. * The following sequence is not valid:
  463. * <pre>
  464. * Yii::beginProfile('block1');
  465. * Yii::beginProfile('block2');
  466. * Yii::endProfile('block1');
  467. * Yii::endProfile('block2');
  468. * </pre>
  469. * @param string $token token for the code block
  470. * @param string $category the category of this log message
  471. * @see endProfile
  472. */
  473. public static function beginProfile($token,$category='application')
  474. {
  475. self::log('begin:'.$token,CLogger::LEVEL_PROFILE,$category);
  476. }
  477. /**
  478. * Marks the end of a code block for profiling.
  479. * This has to be matched with a previous call to {@link beginProfile()} with the same token.
  480. * @param string $token token for the code block
  481. * @param string $category the category of this log message
  482. * @see beginProfile
  483. */
  484. public static function endProfile($token,$category='application')
  485. {
  486. self::log('end:'.$token,CLogger::LEVEL_PROFILE,$category);
  487. }
  488. /**
  489. * @return CLogger message logger
  490. */
  491. public static function getLogger()
  492. {
  493. if(self::$_logger!==null)
  494. return self::$_logger;
  495. else
  496. return self::$_logger=new CLogger;
  497. }
  498. /**
  499. * Sets the logger object.
  500. * @param CLogger $logger the logger object.
  501. * @since 1.1.8
  502. */
  503. public static function setLogger($logger)
  504. {
  505. self::$_logger=$logger;
  506. }
  507. /**
  508. * Returns a string that can be displayed on your Web page showing Powered-by-Yii information
  509. * @return string a string that can be displayed on your Web page showing Powered-by-Yii information
  510. */
  511. public static function powered()
  512. {
  513. return Yii::t('yii','Powered by {yii}.', array('{yii}'=>'<a href="http://www.yiiframework.com/" rel="external">Yii Framework</a>'));
  514. }
  515. /**
  516. * Translates a message to the specified language.
  517. * This method supports choice format (see {@link CChoiceFormat}),
  518. * i.e., the message returned will be chosen from a few candidates according to the given
  519. * number value. This feature is mainly used to solve plural format issue in case
  520. * a message has different plural forms in some languages.
  521. * @param string $category message category. Please use only word letters. Note, category 'yii' is
  522. * reserved for Yii framework core code use. See {@link CPhpMessageSource} for
  523. * more interpretation about message category.
  524. * @param string $message the original message
  525. * @param array $params parameters to be applied to the message using <code>strtr</code>.
  526. * The first parameter can be a number without key.
  527. * And in this case, the method will call {@link CChoiceFormat::format} to choose
  528. * an appropriate message translation.
  529. * Starting from version 1.1.6 you can pass parameter for {@link CChoiceFormat::format}
  530. * or plural forms format without wrapping it with array.
  531. * This parameter is then available as <code>{n}</code> in the message translation string.
  532. * @param string $source which message source application component to use.
  533. * Defaults to null, meaning using 'coreMessages' for messages belonging to
  534. * the 'yii' category and using 'messages' for the rest messages.
  535. * @param string $language the target language. If null (default), the {@link CApplication::getLanguage application language} will be used.
  536. * @return string the translated message
  537. * @see CMessageSource
  538. */
  539. public static function t($category,$message,$params=array(),$source=null,$language=null)
  540. {
  541. if(self::$_app!==null)
  542. {
  543. if($source===null)
  544. $source=($category==='yii'||$category==='zii')?'coreMessages':'messages';
  545. if(($source=self::$_app->getComponent($source))!==null)
  546. $message=$source->translate($category,$message,$language);
  547. }
  548. if($params===array())
  549. return $message;
  550. if(!is_array($params))
  551. $params=array($params);
  552. if(isset($params[0])) // number choice
  553. {
  554. if(strpos($message,'|')!==false)
  555. {
  556. if(strpos($message,'#')===false)
  557. {
  558. $chunks=explode('|',$message);
  559. $expressions=self::$_app->getLocale($language)->getPluralRules();
  560. if($n=min(count($chunks),count($expressions)))
  561. {
  562. for($i=0;$i<$n;$i++)
  563. $chunks[$i]=$expressions[$i].'#'.$chunks[$i];
  564. $message=implode('|',$chunks);
  565. }
  566. }
  567. $message=CChoiceFormat::format($message,$params[0]);
  568. }
  569. if(!isset($params['{n}']))
  570. $params['{n}']=$params[0];
  571. unset($params[0]);
  572. }
  573. return $params!==array() ? strtr($message,$params) : $message;
  574. }
  575. /**
  576. * Registers a new class autoloader.
  577. * The new autoloader will be placed before {@link autoload} and after
  578. * any other existing autoloaders.
  579. * @param callback $callback a valid PHP callback (function name or array($className,$methodName)).
  580. * @param boolean $append whether to append the new autoloader after the default Yii autoloader.
  581. * Be careful using this option as it will disable {@link enableIncludePath autoloading via include path}
  582. * when set to true. After this the Yii autoloader can not rely on loading classes via simple include anymore
  583. * and you have to {@link import} all classes explicitly.
  584. */
  585. public static function registerAutoloader($callback, $append=false)
  586. {
  587. if($append)
  588. {
  589. self::$enableIncludePath=false;
  590. spl_autoload_register($callback);
  591. }
  592. else
  593. {
  594. spl_autoload_unregister(array('YiiBase','autoload'));
  595. spl_autoload_register($callback);
  596. spl_autoload_register(array('YiiBase','autoload'));
  597. }
  598. }
  599. /**
  600. * @var array class map for core Yii classes.
  601. * NOTE, DO NOT MODIFY THIS ARRAY MANUALLY. IF YOU CHANGE OR ADD SOME CORE CLASSES,
  602. * PLEASE RUN 'build autoload' COMMAND TO UPDATE THIS ARRAY.
  603. */
  604. private static $_coreClasses=array(
  605. 'CApplication' => '/base/CApplication.php',
  606. 'CApplicationComponent' => '/base/CApplicationComponent.php',
  607. 'CBehavior' => '/base/CBehavior.php',
  608. 'CComponent' => '/base/CComponent.php',
  609. 'CErrorEvent' => '/base/CErrorEvent.php',
  610. 'CErrorHandler' => '/base/CErrorHandler.php',
  611. 'CException' => '/base/CException.php',
  612. 'CExceptionEvent' => '/base/CExceptionEvent.php',
  613. 'CHttpException' => '/base/CHttpException.php',
  614. 'CModel' => '/base/CModel.php',
  615. 'CModelBehavior' => '/base/CModelBehavior.php',
  616. 'CModelEvent' => '/base/CModelEvent.php',
  617. 'CModule' => '/base/CModule.php',
  618. 'CSecurityManager' => '/base/CSecurityManager.php',
  619. 'CStatePersister' => '/base/CStatePersister.php',
  620. 'CApcCache' => '/caching/CApcCache.php',
  621. 'CCache' => '/caching/CCache.php',
  622. 'CDbCache' => '/caching/CDbCache.php',
  623. 'CDummyCache' => '/caching/CDummyCache.php',
  624. 'CEAcceleratorCache' => '/caching/CEAcceleratorCache.php',
  625. 'CFileCache' => '/caching/CFileCache.php',
  626. 'CMemCache' => '/caching/CMemCache.php',
  627. 'CRedisCache' => '/caching/CRedisCache.php',
  628. 'CWinCache' => '/caching/CWinCache.php',
  629. 'CXCache' => '/caching/CXCache.php',
  630. 'CZendDataCache' => '/caching/CZendDataCache.php',
  631. 'CCacheDependency' => '/caching/dependencies/CCacheDependency.php',
  632. 'CChainedCacheDependency' => '/caching/dependencies/CChainedCacheDependency.php',
  633. 'CDbCacheDependency' => '/caching/dependencies/CDbCacheDependency.php',
  634. 'CDirectoryCacheDependency' => '/caching/dependencies/CDirectoryCacheDependency.php',
  635. 'CExpressionDependency' => '/caching/dependencies/CExpressionDependency.php',
  636. 'CFileCacheDependency' => '/caching/dependencies/CFileCacheDependency.php',
  637. 'CGlobalStateCacheDependency' => '/caching/dependencies/CGlobalStateCacheDependency.php',
  638. 'CAttributeCollection' => '/collections/CAttributeCollection.php',
  639. 'CConfiguration' => '/collections/CConfiguration.php',
  640. 'CList' => '/collections/CList.php',
  641. 'CListIterator' => '/collections/CListIterator.php',
  642. 'CMap' => '/collections/CMap.php',
  643. 'CMapIterator' => '/collections/CMapIterator.php',
  644. 'CQueue' => '/collections/CQueue.php',
  645. 'CQueueIterator' => '/collections/CQueueIterator.php',
  646. 'CStack' => '/collections/CStack.php',
  647. 'CStackIterator' => '/collections/CStackIterator.php',
  648. 'CTypedList' => '/collections/CTypedList.php',
  649. 'CTypedMap' => '/collections/CTypedMap.php',
  650. 'CConsoleApplication' => '/console/CConsoleApplication.php',
  651. 'CConsoleCommand' => '/console/CConsoleCommand.php',
  652. 'CConsoleCommandBehavior' => '/console/CConsoleCommandBehavior.php',
  653. 'CConsoleCommandEvent' => '/console/CConsoleCommandEvent.php',
  654. 'CConsoleCommandRunner' => '/console/CConsoleCommandRunner.php',
  655. 'CHelpCommand' => '/console/CHelpCommand.php',
  656. 'CDbCommand' => '/db/CDbCommand.php',
  657. 'CDbConnection' => '/db/CDbConnection.php',
  658. 'CDbDataReader' => '/db/CDbDataReader.php',
  659. 'CDbException' => '/db/CDbException.php',
  660. 'CDbMigration' => '/db/CDbMigration.php',
  661. 'CDbTransaction' => '/db/CDbTransaction.php',
  662. 'CActiveFinder' => '/db/ar/CActiveFinder.php',
  663. 'CActiveRecord' => '/db/ar/CActiveRecord.php',
  664. 'CActiveRecordBehavior' => '/db/ar/CActiveRecordBehavior.php',
  665. 'CDbColumnSchema' => '/db/schema/CDbColumnSchema.php',
  666. 'CDbCommandBuilder' => '/db/schema/CDbCommandBuilder.php',
  667. 'CDbCriteria' => '/db/schema/CDbCriteria.php',
  668. 'CDbExpression' => '/db/schema/CDbExpression.php',
  669. 'CDbSchema' => '/db/schema/CDbSchema.php',
  670. 'CDbTableSchema' => '/db/schema/CDbTableSchema.php',
  671. 'CMssqlColumnSchema' => '/db/schema/mssql/CMssqlColumnSchema.php',
  672. 'CMssqlCommandBuilder' => '/db/schema/mssql/CMssqlCommandBuilder.php',
  673. 'CMssqlPdoAdapter' => '/db/schema/mssql/CMssqlPdoAdapter.php',
  674. 'CMssqlSchema' => '/db/schema/mssql/CMssqlSchema.php',
  675. 'CMssqlSqlsrvPdoAdapter' => '/db/schema/mssql/CMssqlSqlsrvPdoAdapter.php',
  676. 'CMssqlTableSchema' => '/db/schema/mssql/CMssqlTableSchema.php',
  677. 'CMysqlColumnSchema' => '/db/schema/mysql/CMysqlColumnSchema.php',
  678. 'CMysqlCommandBuilder' => '/db/schema/mysql/CMysqlCommandBuilder.php',
  679. 'CMysqlSchema' => '/db/schema/mysql/CMysqlSchema.php',
  680. 'CMysqlTableSchema' => '/db/schema/mysql/CMysqlTableSchema.php',
  681. 'COciColumnSchema' => '/db/schema/oci/COciColumnSchema.php',
  682. 'COciCommandBuilder' => '/db/schema/oci/COciCommandBuilder.php',
  683. 'COciSchema' => '/db/schema/oci/COciSchema.php',
  684. 'COciTableSchema' => '/db/schema/oci/COciTableSchema.php',
  685. 'CPgsqlColumnSchema' => '/db/schema/pgsql/CPgsqlColumnSchema.php',
  686. 'CPgsqlCommandBuilder' => '/db/schema/pgsql/CPgsqlCommandBuilder.php',
  687. 'CPgsqlSchema' => '/db/schema/pgsql/CPgsqlSchema.php',
  688. 'CPgsqlTableSchema' => '/db/schema/pgsql/CPgsqlTableSchema.php',
  689. 'CSqliteColumnSchema' => '/db/schema/sqlite/CSqliteColumnSchema.php',
  690. 'CSqliteCommandBuilder' => '/db/schema/sqlite/CSqliteCommandBuilder.php',
  691. 'CSqliteSchema' => '/db/schema/sqlite/CSqliteSchema.php',
  692. 'CChoiceFormat' => '/i18n/CChoiceFormat.php',
  693. 'CDateFormatter' => '/i18n/CDateFormatter.php',
  694. 'CDbMessageSource' => '/i18n/CDbMessageSource.php',
  695. 'CGettextMessageSource' => '/i18n/CGettextMessageSource.php',
  696. 'CLocale' => '/i18n/CLocale.php',
  697. 'CMessageSource' => '/i18n/CMessageSource.php',
  698. 'CNumberFormatter' => '/i18n/CNumberFormatter.php',
  699. 'CPhpMessageSource' => '/i18n/CPhpMessageSource.php',
  700. 'CGettextFile' => '/i18n/gettext/CGettextFile.php',
  701. 'CGettextMoFile' => '/i18n/gettext/CGettextMoFile.php',
  702. 'CGettextPoFile' => '/i18n/gettext/CGettextPoFile.php',
  703. 'CChainedLogFilter' => '/logging/CChainedLogFilter.php',
  704. 'CDbLogRoute' => '/logging/CDbLogRoute.php',
  705. 'CEmailLogRoute' => '/logging/CEmailLogRoute.php',
  706. 'CFileLogRoute' => '/logging/CFileLogRoute.php',
  707. 'CLogFilter' => '/logging/CLogFilter.php',
  708. 'CLogRoute' => '/logging/CLogRoute.php',
  709. 'CLogRouter' => '/logging/CLogRouter.php',
  710. 'CLogger' => '/logging/CLogger.php',
  711. 'CProfileLogRoute' => '/logging/CProfileLogRoute.php',
  712. 'CWebLogRoute' => '/logging/CWebLogRoute.php',
  713. 'CDateTimeParser' => '/utils/CDateTimeParser.php',
  714. 'CFileHelper' => '/utils/CFileHelper.php',
  715. 'CFormatter' => '/utils/CFormatter.php',
  716. 'CLocalizedFormatter' => '/utils/CLocalizedFormatter.php',
  717. 'CMarkdownParser' => '/utils/CMarkdownParser.php',
  718. 'CPasswordHelper' => '/utils/CPasswordHelper.php',
  719. 'CPropertyValue' => '/utils/CPropertyValue.php',
  720. 'CTimestamp' => '/utils/CTimestamp.php',
  721. 'CVarDumper' => '/utils/CVarDumper.php',
  722. 'CBooleanValidator' => '/validators/CBooleanValidator.php',
  723. 'CCaptchaValidator' => '/validators/CCaptchaValidator.php',
  724. 'CCompareValidator' => '/validators/CCompareValidator.php',
  725. 'CDateValidator' => '/validators/CDateValidator.php',
  726. 'CDefaultValueValidator' => '/validators/CDefaultValueValidator.php',
  727. 'CEmailValidator' => '/validators/CEmailValidator.php',
  728. 'CExistValidator' => '/validators/CExistValidator.php',
  729. 'CFileValidator' => '/validators/CFileValidator.php',
  730. 'CFilterValidator' => '/validators/CFilterValidator.php',
  731. 'CInlineValidator' => '/validators/CInlineValidator.php',
  732. 'CNumberValidator' => '/validators/CNumberValidator.php',
  733. 'CRangeValidator' => '/validators/CRangeValidator.php',
  734. 'CRegularExpressionValidator' => '/validators/CRegularExpressionValidator.php',
  735. 'CRequiredValidator' => '/validators/CRequiredValidator.php',
  736. 'CSafeValidator' => '/validators/CSafeValidator.php',
  737. 'CStringValidator' => '/validators/CStringValidator.php',
  738. 'CTypeValidator' => '/validators/CTypeValidator.php',
  739. 'CUniqueValidator' => '/validators/CUniqueValidator.php',
  740. 'CUnsafeValidator' => '/validators/CUnsafeValidator.php',
  741. 'CUrlValidator' => '/validators/CUrlValidator.php',
  742. 'CValidator' => '/validators/CValidator.php',
  743. 'CActiveDataProvider' => '/web/CActiveDataProvider.php',
  744. 'CArrayDataProvider' => '/web/CArrayDataProvider.php',
  745. 'CAssetManager' => '/web/CAssetManager.php',
  746. 'CBaseController' => '/web/CBaseController.php',
  747. 'CCacheHttpSession' => '/web/CCacheHttpSession.php',
  748. 'CClientScript' => '/web/CClientScript.php',
  749. 'CController' => '/web/CController.php',
  750. 'CDataProvider' => '/web/CDataProvider.php',
  751. 'CDataProviderIterator' => '/web/CDataProviderIterator.php',
  752. 'CDbHttpSession' => '/web/CDbHttpSession.php',
  753. 'CExtController' => '/web/CExtController.php',
  754. 'CFormModel' => '/web/CFormModel.php',
  755. 'CHttpCookie' => '/web/CHttpCookie.php',
  756. 'CHttpRequest' => '/web/CHttpRequest.php',
  757. 'CHttpSession' => '/web/CHttpSession.php',
  758. 'CHttpSessionIterator' => '/web/CHttpSessionIterator.php',
  759. 'COutputEvent' => '/web/COutputEvent.php',
  760. 'CPagination' => '/web/CPagination.php',
  761. 'CSort' => '/web/CSort.php',
  762. 'CSqlDataProvider' => '/web/CSqlDataProvider.php',
  763. 'CTheme' => '/web/CTheme.php',
  764. 'CThemeManager' => '/web/CThemeManager.php',
  765. 'CUploadedFile' => '/web/CUploadedFile.php',
  766. 'CUrlManager' => '/web/CUrlManager.php',
  767. 'CWebApplication' => '/web/CWebApplication.php',
  768. 'CWebModule' => '/web/CWebModule.php',
  769. 'CWidgetFactory' => '/web/CWidgetFactory.php',
  770. 'CAction' => '/web/actions/CAction.php',
  771. 'CInlineAction' => '/web/actions/CInlineAction.php',
  772. 'CViewAction' => '/web/actions/CViewAction.php',
  773. 'CAccessControlFilter' => '/web/auth/CAccessControlFilter.php',
  774. 'CAuthAssignment' => '/web/auth/CAuthAssignment.php',
  775. 'CAuthItem' => '/web/auth/CAuthItem.php',
  776. 'CAuthManager' => '/web/auth/CAuthManager.php',
  777. 'CBaseUserIdentity' => '/web/auth/CBaseUserIdentity.php',
  778. 'CDbAuthManager' => '/web/auth/CDbAuthManager.php',
  779. 'CPhpAuthManager' => '/web/auth/CPhpAuthManager.php',
  780. 'CUserIdentity' => '/web/auth/CUserIdentity.php',
  781. 'CWebUser' => '/web/auth/CWebUser.php',
  782. 'CFilter' => '/web/filters/CFilter.php',
  783. 'CFilterChain' => '/web/filters/CFilterChain.php',
  784. 'CHttpCacheFilter' => '/web/filters/CHttpCacheFilter.php',
  785. 'CInlineFilter' => '/web/filters/CInlineFilter.php',
  786. 'CForm' => '/web/form/CForm.php',
  787. 'CFormButtonElement' => '/web/form/CFormButtonElement.php',
  788. 'CFormElement' => '/web/form/CFormElement.php',
  789. 'CFormElementCollection' => '/web/form/CFormElementCollection.php',
  790. 'CFormInputElement' => '/web/form/CFormInputElement.php',
  791. 'CFormStringElement' => '/web/form/CFormStringElement.php',
  792. 'CGoogleApi' => '/web/helpers/CGoogleApi.php',
  793. 'CHtml' => '/web/helpers/CHtml.php',
  794. 'CJSON' => '/web/helpers/CJSON.php',
  795. 'CJavaScript' => '/web/helpers/CJavaScript.php',
  796. 'CJavaScriptExpression' => '/web/helpers/CJavaScriptExpression.php',
  797. 'CPradoViewRenderer' => '/web/renderers/CPradoViewRenderer.php',
  798. 'CViewRenderer' => '/web/renderers/CViewRenderer.php',
  799. 'CWebService' => '/web/services/CWebService.php',
  800. 'CWebServiceAction' => '/web/services/CWebServiceAction.php',
  801. 'CWsdlGenerator' => '/web/services/CWsdlGenerator.php',
  802. 'CActiveForm' => '/web/widgets/CActiveForm.php',
  803. 'CAutoComplete' => '/web/widgets/CAutoComplete.php',
  804. 'CClipWidget' => '/web/widgets/CClipWidget.php',
  805. 'CContentDecorator' => '/web/widgets/CContentDecorator.php',
  806. 'CFilterWidget' => '/web/widgets/CFilterWidget.php',
  807. 'CFlexWidget' => '/web/widgets/CFlexWidget.php',
  808. 'CHtmlPurifier' => '/web/widgets/CHtmlPurifier.php',
  809. 'CInputWidget' => '/web/widgets/CInputWidget.php',
  810. 'CMarkdown' => '/web/widgets/CMarkdown.php',
  811. 'CMaskedTextField' => '/web/widgets/CMaskedTextField.php',
  812. 'CMultiFileUpload' => '/web/widgets/CMultiFileUpload.php',
  813. 'COutputCache' => '/web/widgets/COutputCache.php',
  814. 'COutputProcessor' => '/web/widgets/COutputProcessor.php',
  815. 'CStarRating' => '/web/widgets/CStarRating.php',
  816. 'CTabView' => '/web/widgets/CTabView.php',
  817. 'CTextHighlighter' => '/web/widgets/CTextHighlighter.php',
  818. 'CTreeView' => '/web/widgets/CTreeView.php',
  819. 'CWidget' => '/web/widgets/CWidget.php',
  820. 'CCaptcha' => '/web/widgets/captcha/CCaptcha.php',
  821. 'CCaptchaAction' => '/web/widgets/captcha/CCaptchaAction.php',
  822. 'CBasePager' => '/web/widgets/pagers/CBasePager.php',
  823. 'CLinkPager' => '/web/widgets/pagers/CLinkPager.php',
  824. 'CListPager' => '/web/widgets/pagers/CListPager.php',
  825. );
  826. }
  827. spl_autoload_register(array('YiiBase','autoload'));
  828. require(YII_PATH.'/base/interfaces.php');