YiiBase.php 35 KB

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