PageRenderTime 66ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 2ms

/framework/yiilite.php

https://bitbucket.org/gencer/yii
PHP | 10565 lines | 10484 code | 2 blank | 79 comment | 1084 complexity | ff02d4bfa538d8b609734f7b6ebc44fc MD5 | raw file
Possible License(s): BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Yii bootstrap file.
  4. *
  5. * This file is automatically generated using 'build lite' command.
  6. * It is the result of merging commonly used Yii class files with
  7. * comments and trace statements removed away.
  8. *
  9. * By using this file instead of yii.php, an Yii application may
  10. * improve performance due to the reduction of PHP parsing time.
  11. * The performance improvement is especially obvious when PHP APC extension
  12. * is enabled.
  13. *
  14. * DO NOT modify this file manually.
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @link http://www.yiiframework.com/
  18. * @copyright 2008-2013 Yii Software LLC
  19. * @license http://www.yiiframework.com/license/
  20. * @version $Id: $
  21. * @since 1.0
  22. */
  23. defined('YII_BEGIN_TIME') or define('YII_BEGIN_TIME',microtime(true));
  24. defined('YII_DEBUG') or define('YII_DEBUG',false);
  25. defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',0);
  26. defined('YII_ENABLE_EXCEPTION_HANDLER') or define('YII_ENABLE_EXCEPTION_HANDLER',true);
  27. defined('YII_ENABLE_ERROR_HANDLER') or define('YII_ENABLE_ERROR_HANDLER',true);
  28. defined('YII_PATH') or define('YII_PATH',dirname(__FILE__));
  29. defined('YII_ZII_PATH') or define('YII_ZII_PATH',YII_PATH.DIRECTORY_SEPARATOR.'zii');
  30. class YiiBase
  31. {
  32. public static $classMap=array();
  33. public static $enableIncludePath=true;
  34. private static $_aliases=array('system'=>YII_PATH,'zii'=>YII_ZII_PATH); // alias => path
  35. private static $_imports=array(); // alias => class name or directory
  36. private static $_includePaths; // list of include paths
  37. private static $_app;
  38. private static $_logger;
  39. public static function getVersion()
  40. {
  41. return '1.1.16-dev';
  42. }
  43. public static function createWebApplication($config=null)
  44. {
  45. return self::createApplication('CWebApplication',$config);
  46. }
  47. public static function createConsoleApplication($config=null)
  48. {
  49. return self::createApplication('CConsoleApplication',$config);
  50. }
  51. public static function createApplication($class,$config=null)
  52. {
  53. return new $class($config);
  54. }
  55. public static function app()
  56. {
  57. return self::$_app;
  58. }
  59. public static function setApplication($app)
  60. {
  61. if(self::$_app===null || $app===null)
  62. self::$_app=$app;
  63. else
  64. throw new CException(Yii::t('yii','Yii application can only be created once.'));
  65. }
  66. public static function getFrameworkPath()
  67. {
  68. return YII_PATH;
  69. }
  70. public static function createComponent($config)
  71. {
  72. if(is_string($config))
  73. {
  74. $type=$config;
  75. $config=array();
  76. }
  77. elseif(isset($config['class']))
  78. {
  79. $type=$config['class'];
  80. unset($config['class']);
  81. }
  82. else
  83. throw new CException(Yii::t('yii','Object configuration must be an array containing a "class" element.'));
  84. if(!class_exists($type,false))
  85. $type=Yii::import($type,true);
  86. if(($n=func_num_args())>1)
  87. {
  88. $args=func_get_args();
  89. if($n===2)
  90. $object=new $type($args[1]);
  91. elseif($n===3)
  92. $object=new $type($args[1],$args[2]);
  93. elseif($n===4)
  94. $object=new $type($args[1],$args[2],$args[3]);
  95. else
  96. {
  97. unset($args[0]);
  98. $class=new ReflectionClass($type);
  99. // Note: ReflectionClass::newInstanceArgs() is available for PHP 5.1.3+
  100. // $object=$class->newInstanceArgs($args);
  101. $object=call_user_func_array(array($class,'newInstance'),$args);
  102. }
  103. }
  104. else
  105. $object=new $type;
  106. foreach($config as $key=>$value)
  107. $object->$key=$value;
  108. return $object;
  109. }
  110. public static function import($alias,$forceInclude=false)
  111. {
  112. if(isset(self::$_imports[$alias])) // previously imported
  113. return self::$_imports[$alias];
  114. if(class_exists($alias,false) || interface_exists($alias,false))
  115. return self::$_imports[$alias]=$alias;
  116. if(($pos=strrpos($alias,'\\'))!==false) // a class name in PHP 5.3 namespace format
  117. {
  118. $namespace=str_replace('\\','.',ltrim(substr($alias,0,$pos),'\\'));
  119. if(($path=self::getPathOfAlias($namespace))!==false)
  120. {
  121. $classFile=$path.DIRECTORY_SEPARATOR.substr($alias,$pos+1).'.php';
  122. if($forceInclude)
  123. {
  124. if(is_file($classFile))
  125. require($classFile);
  126. else
  127. 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)));
  128. self::$_imports[$alias]=$alias;
  129. }
  130. else
  131. self::$classMap[$alias]=$classFile;
  132. return $alias;
  133. }
  134. else
  135. {
  136. // try to autoload the class with an autoloader
  137. if (class_exists($alias,true))
  138. return self::$_imports[$alias]=$alias;
  139. else
  140. throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing directory or file.',
  141. array('{alias}'=>$namespace)));
  142. }
  143. }
  144. if(($pos=strrpos($alias,'.'))===false) // a simple class name
  145. {
  146. // try to autoload the class with an autoloader if $forceInclude is true
  147. if($forceInclude && (Yii::autoload($alias,true) || class_exists($alias,true)))
  148. self::$_imports[$alias]=$alias;
  149. return $alias;
  150. }
  151. $className=(string)substr($alias,$pos+1);
  152. $isClass=$className!=='*';
  153. if($isClass && (class_exists($className,false) || interface_exists($className,false)))
  154. return self::$_imports[$alias]=$className;
  155. if(($path=self::getPathOfAlias($alias))!==false)
  156. {
  157. if($isClass)
  158. {
  159. if($forceInclude)
  160. {
  161. if(is_file($path.'.php'))
  162. require($path.'.php');
  163. else
  164. 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)));
  165. self::$_imports[$alias]=$className;
  166. }
  167. else
  168. self::$classMap[$className]=$path.'.php';
  169. return $className;
  170. }
  171. else // a directory
  172. {
  173. if(self::$_includePaths===null)
  174. {
  175. self::$_includePaths=array_unique(explode(PATH_SEPARATOR,get_include_path()));
  176. if(($pos=array_search('.',self::$_includePaths,true))!==false)
  177. unset(self::$_includePaths[$pos]);
  178. }
  179. array_unshift(self::$_includePaths,$path);
  180. if(self::$enableIncludePath && set_include_path('.'.PATH_SEPARATOR.implode(PATH_SEPARATOR,self::$_includePaths))===false)
  181. self::$enableIncludePath=false;
  182. return self::$_imports[$alias]=$path;
  183. }
  184. }
  185. else
  186. throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing directory or file.',
  187. array('{alias}'=>$alias)));
  188. }
  189. public static function getPathOfAlias($alias)
  190. {
  191. if(isset(self::$_aliases[$alias]))
  192. return self::$_aliases[$alias];
  193. elseif(($pos=strpos($alias,'.'))!==false)
  194. {
  195. $rootAlias=substr($alias,0,$pos);
  196. if(isset(self::$_aliases[$rootAlias]))
  197. return self::$_aliases[$alias]=rtrim(self::$_aliases[$rootAlias].DIRECTORY_SEPARATOR.str_replace('.',DIRECTORY_SEPARATOR,substr($alias,$pos+1)),'*'.DIRECTORY_SEPARATOR);
  198. elseif(self::$_app instanceof CWebApplication)
  199. {
  200. if(self::$_app->findModule($rootAlias)!==null)
  201. return self::getPathOfAlias($alias);
  202. }
  203. }
  204. return false;
  205. }
  206. public static function setPathOfAlias($alias,$path)
  207. {
  208. if(empty($path))
  209. unset(self::$_aliases[$alias]);
  210. else
  211. self::$_aliases[$alias]=rtrim($path,'\\/');
  212. }
  213. public static function autoload($className,$classMapOnly=false)
  214. {
  215. // use include so that the error PHP file may appear
  216. if(isset(self::$classMap[$className]))
  217. include(self::$classMap[$className]);
  218. elseif(isset(self::$_coreClasses[$className]))
  219. include(YII_PATH.self::$_coreClasses[$className]);
  220. elseif($classMapOnly)
  221. return false;
  222. else
  223. {
  224. // include class file relying on include_path
  225. if(strpos($className,'\\')===false) // class without namespace
  226. {
  227. if(self::$enableIncludePath===false)
  228. {
  229. foreach(self::$_includePaths as $path)
  230. {
  231. $classFile=$path.DIRECTORY_SEPARATOR.$className.'.php';
  232. if(is_file($classFile))
  233. {
  234. include($classFile);
  235. if(YII_DEBUG && basename(realpath($classFile))!==$className.'.php')
  236. throw new CException(Yii::t('yii','Class name "{class}" does not match class file "{file}".', array(
  237. '{class}'=>$className,
  238. '{file}'=>$classFile,
  239. )));
  240. break;
  241. }
  242. }
  243. }
  244. else
  245. include($className.'.php');
  246. }
  247. else // class name with namespace in PHP 5.3
  248. {
  249. $namespace=str_replace('\\','.',ltrim($className,'\\'));
  250. if(($path=self::getPathOfAlias($namespace))!==false)
  251. include($path.'.php');
  252. else
  253. return false;
  254. }
  255. return class_exists($className,false) || interface_exists($className,false);
  256. }
  257. return true;
  258. }
  259. public static function trace($msg,$category='application')
  260. {
  261. if(YII_DEBUG)
  262. self::log($msg,CLogger::LEVEL_TRACE,$category);
  263. }
  264. public static function log($msg,$level=CLogger::LEVEL_INFO,$category='application')
  265. {
  266. if(self::$_logger===null)
  267. self::$_logger=new CLogger;
  268. if(YII_DEBUG && YII_TRACE_LEVEL>0 && $level!==CLogger::LEVEL_PROFILE)
  269. {
  270. $traces=debug_backtrace();
  271. $count=0;
  272. foreach($traces as $trace)
  273. {
  274. if(isset($trace['file'],$trace['line']) && strpos($trace['file'],YII_PATH)!==0)
  275. {
  276. $msg.="\nin ".$trace['file'].' ('.$trace['line'].')';
  277. if(++$count>=YII_TRACE_LEVEL)
  278. break;
  279. }
  280. }
  281. }
  282. self::$_logger->log($msg,$level,$category);
  283. }
  284. public static function beginProfile($token,$category='application')
  285. {
  286. self::log('begin:'.$token,CLogger::LEVEL_PROFILE,$category);
  287. }
  288. public static function endProfile($token,$category='application')
  289. {
  290. self::log('end:'.$token,CLogger::LEVEL_PROFILE,$category);
  291. }
  292. public static function getLogger()
  293. {
  294. if(self::$_logger!==null)
  295. return self::$_logger;
  296. else
  297. return self::$_logger=new CLogger;
  298. }
  299. public static function setLogger($logger)
  300. {
  301. self::$_logger=$logger;
  302. }
  303. public static function powered()
  304. {
  305. return Yii::t('yii','Powered by {yii}.', array('{yii}'=>'<a href="http://www.yiiframework.com/" rel="external">Yii Framework</a>'));
  306. }
  307. public static function t($category,$message,$params=array(),$source=null,$language=null)
  308. {
  309. if(self::$_app!==null)
  310. {
  311. if($source===null)
  312. $source=($category==='yii'||$category==='zii')?'coreMessages':'messages';
  313. if(($source=self::$_app->getComponent($source))!==null)
  314. $message=$source->translate($category,$message,$language);
  315. }
  316. if($params===array())
  317. return $message;
  318. if(!is_array($params))
  319. $params=array($params);
  320. if(isset($params[0])) // number choice
  321. {
  322. if(strpos($message,'|')!==false)
  323. {
  324. if(strpos($message,'#')===false)
  325. {
  326. $chunks=explode('|',$message);
  327. $expressions=self::$_app->getLocale($language)->getPluralRules();
  328. if($n=min(count($chunks),count($expressions)))
  329. {
  330. for($i=0;$i<$n;$i++)
  331. $chunks[$i]=$expressions[$i].'#'.$chunks[$i];
  332. $message=implode('|',$chunks);
  333. }
  334. }
  335. $message=CChoiceFormat::format($message,$params[0]);
  336. }
  337. if(!isset($params['{n}']))
  338. $params['{n}']=$params[0];
  339. unset($params[0]);
  340. }
  341. return $params!==array() ? strtr($message,$params) : $message;
  342. }
  343. public static function registerAutoloader($callback, $append=false)
  344. {
  345. if($append)
  346. {
  347. self::$enableIncludePath=false;
  348. spl_autoload_register($callback);
  349. }
  350. else
  351. {
  352. spl_autoload_unregister(array('YiiBase','autoload'));
  353. spl_autoload_register($callback);
  354. spl_autoload_register(array('YiiBase','autoload'));
  355. }
  356. }
  357. private static $_coreClasses=array(
  358. 'CApplication' => '/base/CApplication.php',
  359. 'CApplicationComponent' => '/base/CApplicationComponent.php',
  360. 'CBehavior' => '/base/CBehavior.php',
  361. 'CComponent' => '/base/CComponent.php',
  362. 'CErrorEvent' => '/base/CErrorEvent.php',
  363. 'CErrorHandler' => '/base/CErrorHandler.php',
  364. 'CException' => '/base/CException.php',
  365. 'CExceptionEvent' => '/base/CExceptionEvent.php',
  366. 'CHttpException' => '/base/CHttpException.php',
  367. 'CModel' => '/base/CModel.php',
  368. 'CModelBehavior' => '/base/CModelBehavior.php',
  369. 'CModelEvent' => '/base/CModelEvent.php',
  370. 'CModule' => '/base/CModule.php',
  371. 'CSecurityManager' => '/base/CSecurityManager.php',
  372. 'CStatePersister' => '/base/CStatePersister.php',
  373. 'CApcCache' => '/caching/CApcCache.php',
  374. 'CCache' => '/caching/CCache.php',
  375. 'CDbCache' => '/caching/CDbCache.php',
  376. 'CDummyCache' => '/caching/CDummyCache.php',
  377. 'CEAcceleratorCache' => '/caching/CEAcceleratorCache.php',
  378. 'CFileCache' => '/caching/CFileCache.php',
  379. 'CMemCache' => '/caching/CMemCache.php',
  380. 'CRedisCache' => '/caching/CRedisCache.php',
  381. 'CWinCache' => '/caching/CWinCache.php',
  382. 'CXCache' => '/caching/CXCache.php',
  383. 'CZendDataCache' => '/caching/CZendDataCache.php',
  384. 'CCacheDependency' => '/caching/dependencies/CCacheDependency.php',
  385. 'CChainedCacheDependency' => '/caching/dependencies/CChainedCacheDependency.php',
  386. 'CDbCacheDependency' => '/caching/dependencies/CDbCacheDependency.php',
  387. 'CDirectoryCacheDependency' => '/caching/dependencies/CDirectoryCacheDependency.php',
  388. 'CExpressionDependency' => '/caching/dependencies/CExpressionDependency.php',
  389. 'CFileCacheDependency' => '/caching/dependencies/CFileCacheDependency.php',
  390. 'CGlobalStateCacheDependency' => '/caching/dependencies/CGlobalStateCacheDependency.php',
  391. 'CAttributeCollection' => '/collections/CAttributeCollection.php',
  392. 'CConfiguration' => '/collections/CConfiguration.php',
  393. 'CList' => '/collections/CList.php',
  394. 'CListIterator' => '/collections/CListIterator.php',
  395. 'CMap' => '/collections/CMap.php',
  396. 'CMapIterator' => '/collections/CMapIterator.php',
  397. 'CQueue' => '/collections/CQueue.php',
  398. 'CQueueIterator' => '/collections/CQueueIterator.php',
  399. 'CStack' => '/collections/CStack.php',
  400. 'CStackIterator' => '/collections/CStackIterator.php',
  401. 'CTypedList' => '/collections/CTypedList.php',
  402. 'CTypedMap' => '/collections/CTypedMap.php',
  403. 'CConsoleApplication' => '/console/CConsoleApplication.php',
  404. 'CConsoleCommand' => '/console/CConsoleCommand.php',
  405. 'CConsoleCommandBehavior' => '/console/CConsoleCommandBehavior.php',
  406. 'CConsoleCommandEvent' => '/console/CConsoleCommandEvent.php',
  407. 'CConsoleCommandRunner' => '/console/CConsoleCommandRunner.php',
  408. 'CHelpCommand' => '/console/CHelpCommand.php',
  409. 'CDbCommand' => '/db/CDbCommand.php',
  410. 'CDbConnection' => '/db/CDbConnection.php',
  411. 'CDbDataReader' => '/db/CDbDataReader.php',
  412. 'CDbException' => '/db/CDbException.php',
  413. 'CDbMigration' => '/db/CDbMigration.php',
  414. 'CDbTransaction' => '/db/CDbTransaction.php',
  415. 'CActiveFinder' => '/db/ar/CActiveFinder.php',
  416. 'CActiveRecord' => '/db/ar/CActiveRecord.php',
  417. 'CActiveRecordBehavior' => '/db/ar/CActiveRecordBehavior.php',
  418. 'CDbColumnSchema' => '/db/schema/CDbColumnSchema.php',
  419. 'CDbCommandBuilder' => '/db/schema/CDbCommandBuilder.php',
  420. 'CDbCriteria' => '/db/schema/CDbCriteria.php',
  421. 'CDbExpression' => '/db/schema/CDbExpression.php',
  422. 'CDbSchema' => '/db/schema/CDbSchema.php',
  423. 'CDbTableSchema' => '/db/schema/CDbTableSchema.php',
  424. 'CCubridColumnSchema' => '/db/schema/cubrid/CCubridColumnSchema.php',
  425. 'CCubridSchema' => '/db/schema/cubrid/CCubridSchema.php',
  426. 'CCubridTableSchema' => '/db/schema/cubrid/CCubridTableSchema.php',
  427. 'CMssqlColumnSchema' => '/db/schema/mssql/CMssqlColumnSchema.php',
  428. 'CMssqlCommandBuilder' => '/db/schema/mssql/CMssqlCommandBuilder.php',
  429. 'CMssqlPdoAdapter' => '/db/schema/mssql/CMssqlPdoAdapter.php',
  430. 'CMssqlSchema' => '/db/schema/mssql/CMssqlSchema.php',
  431. 'CMssqlSqlsrvPdoAdapter' => '/db/schema/mssql/CMssqlSqlsrvPdoAdapter.php',
  432. 'CMssqlTableSchema' => '/db/schema/mssql/CMssqlTableSchema.php',
  433. 'CMysqlColumnSchema' => '/db/schema/mysql/CMysqlColumnSchema.php',
  434. 'CMysqlCommandBuilder' => '/db/schema/mysql/CMysqlCommandBuilder.php',
  435. 'CMysqlSchema' => '/db/schema/mysql/CMysqlSchema.php',
  436. 'CMysqlTableSchema' => '/db/schema/mysql/CMysqlTableSchema.php',
  437. 'COciColumnSchema' => '/db/schema/oci/COciColumnSchema.php',
  438. 'COciCommandBuilder' => '/db/schema/oci/COciCommandBuilder.php',
  439. 'COciSchema' => '/db/schema/oci/COciSchema.php',
  440. 'COciTableSchema' => '/db/schema/oci/COciTableSchema.php',
  441. 'CPgsqlColumnSchema' => '/db/schema/pgsql/CPgsqlColumnSchema.php',
  442. 'CPgsqlCommandBuilder' => '/db/schema/pgsql/CPgsqlCommandBuilder.php',
  443. 'CPgsqlSchema' => '/db/schema/pgsql/CPgsqlSchema.php',
  444. 'CPgsqlTableSchema' => '/db/schema/pgsql/CPgsqlTableSchema.php',
  445. 'CSqliteColumnSchema' => '/db/schema/sqlite/CSqliteColumnSchema.php',
  446. 'CSqliteCommandBuilder' => '/db/schema/sqlite/CSqliteCommandBuilder.php',
  447. 'CSqliteSchema' => '/db/schema/sqlite/CSqliteSchema.php',
  448. 'CChoiceFormat' => '/i18n/CChoiceFormat.php',
  449. 'CDateFormatter' => '/i18n/CDateFormatter.php',
  450. 'CDbMessageSource' => '/i18n/CDbMessageSource.php',
  451. 'CGettextMessageSource' => '/i18n/CGettextMessageSource.php',
  452. 'CLocale' => '/i18n/CLocale.php',
  453. 'CMessageSource' => '/i18n/CMessageSource.php',
  454. 'CNumberFormatter' => '/i18n/CNumberFormatter.php',
  455. 'CPhpMessageSource' => '/i18n/CPhpMessageSource.php',
  456. 'CGettextFile' => '/i18n/gettext/CGettextFile.php',
  457. 'CGettextMoFile' => '/i18n/gettext/CGettextMoFile.php',
  458. 'CGettextPoFile' => '/i18n/gettext/CGettextPoFile.php',
  459. 'CChainedLogFilter' => '/logging/CChainedLogFilter.php',
  460. 'CDbLogRoute' => '/logging/CDbLogRoute.php',
  461. 'CEmailLogRoute' => '/logging/CEmailLogRoute.php',
  462. 'CFileLogRoute' => '/logging/CFileLogRoute.php',
  463. 'CLogFilter' => '/logging/CLogFilter.php',
  464. 'CLogRoute' => '/logging/CLogRoute.php',
  465. 'CLogRouter' => '/logging/CLogRouter.php',
  466. 'CLogger' => '/logging/CLogger.php',
  467. 'CProfileLogRoute' => '/logging/CProfileLogRoute.php',
  468. 'CSysLogRoute' => '/logging/CSysLogRoute.php',
  469. 'CWebLogRoute' => '/logging/CWebLogRoute.php',
  470. 'CDateTimeParser' => '/utils/CDateTimeParser.php',
  471. 'CFileHelper' => '/utils/CFileHelper.php',
  472. 'CFormatter' => '/utils/CFormatter.php',
  473. 'CLocalizedFormatter' => '/utils/CLocalizedFormatter.php',
  474. 'CMarkdownParser' => '/utils/CMarkdownParser.php',
  475. 'CPasswordHelper' => '/utils/CPasswordHelper.php',
  476. 'CPropertyValue' => '/utils/CPropertyValue.php',
  477. 'CTimestamp' => '/utils/CTimestamp.php',
  478. 'CVarDumper' => '/utils/CVarDumper.php',
  479. 'CBooleanValidator' => '/validators/CBooleanValidator.php',
  480. 'CCaptchaValidator' => '/validators/CCaptchaValidator.php',
  481. 'CCompareValidator' => '/validators/CCompareValidator.php',
  482. 'CDateValidator' => '/validators/CDateValidator.php',
  483. 'CDefaultValueValidator' => '/validators/CDefaultValueValidator.php',
  484. 'CEmailValidator' => '/validators/CEmailValidator.php',
  485. 'CExistValidator' => '/validators/CExistValidator.php',
  486. 'CFileValidator' => '/validators/CFileValidator.php',
  487. 'CFilterValidator' => '/validators/CFilterValidator.php',
  488. 'CInlineValidator' => '/validators/CInlineValidator.php',
  489. 'CNumberValidator' => '/validators/CNumberValidator.php',
  490. 'CRangeValidator' => '/validators/CRangeValidator.php',
  491. 'CRegularExpressionValidator' => '/validators/CRegularExpressionValidator.php',
  492. 'CRequiredValidator' => '/validators/CRequiredValidator.php',
  493. 'CSafeValidator' => '/validators/CSafeValidator.php',
  494. 'CStringValidator' => '/validators/CStringValidator.php',
  495. 'CTypeValidator' => '/validators/CTypeValidator.php',
  496. 'CUniqueValidator' => '/validators/CUniqueValidator.php',
  497. 'CUnsafeValidator' => '/validators/CUnsafeValidator.php',
  498. 'CUrlValidator' => '/validators/CUrlValidator.php',
  499. 'CValidator' => '/validators/CValidator.php',
  500. 'CActiveDataProvider' => '/web/CActiveDataProvider.php',
  501. 'CArrayDataProvider' => '/web/CArrayDataProvider.php',
  502. 'CAssetManager' => '/web/CAssetManager.php',
  503. 'CBaseController' => '/web/CBaseController.php',
  504. 'CCacheHttpSession' => '/web/CCacheHttpSession.php',
  505. 'CClientScript' => '/web/CClientScript.php',
  506. 'CController' => '/web/CController.php',
  507. 'CDataProvider' => '/web/CDataProvider.php',
  508. 'CDataProviderIterator' => '/web/CDataProviderIterator.php',
  509. 'CDbHttpSession' => '/web/CDbHttpSession.php',
  510. 'CExtController' => '/web/CExtController.php',
  511. 'CFormModel' => '/web/CFormModel.php',
  512. 'CHttpCookie' => '/web/CHttpCookie.php',
  513. 'CHttpRequest' => '/web/CHttpRequest.php',
  514. 'CHttpSession' => '/web/CHttpSession.php',
  515. 'CHttpSessionIterator' => '/web/CHttpSessionIterator.php',
  516. 'COutputEvent' => '/web/COutputEvent.php',
  517. 'CPagination' => '/web/CPagination.php',
  518. 'CSort' => '/web/CSort.php',
  519. 'CSqlDataProvider' => '/web/CSqlDataProvider.php',
  520. 'CTheme' => '/web/CTheme.php',
  521. 'CThemeManager' => '/web/CThemeManager.php',
  522. 'CUploadedFile' => '/web/CUploadedFile.php',
  523. 'CUrlManager' => '/web/CUrlManager.php',
  524. 'CWebApplication' => '/web/CWebApplication.php',
  525. 'CWebModule' => '/web/CWebModule.php',
  526. 'CWidgetFactory' => '/web/CWidgetFactory.php',
  527. 'CAction' => '/web/actions/CAction.php',
  528. 'CInlineAction' => '/web/actions/CInlineAction.php',
  529. 'CViewAction' => '/web/actions/CViewAction.php',
  530. 'CAccessControlFilter' => '/web/auth/CAccessControlFilter.php',
  531. 'CAuthAssignment' => '/web/auth/CAuthAssignment.php',
  532. 'CAuthItem' => '/web/auth/CAuthItem.php',
  533. 'CAuthManager' => '/web/auth/CAuthManager.php',
  534. 'CBaseUserIdentity' => '/web/auth/CBaseUserIdentity.php',
  535. 'CDbAuthManager' => '/web/auth/CDbAuthManager.php',
  536. 'CPhpAuthManager' => '/web/auth/CPhpAuthManager.php',
  537. 'CUserIdentity' => '/web/auth/CUserIdentity.php',
  538. 'CWebUser' => '/web/auth/CWebUser.php',
  539. 'CFilter' => '/web/filters/CFilter.php',
  540. 'CFilterChain' => '/web/filters/CFilterChain.php',
  541. 'CHttpCacheFilter' => '/web/filters/CHttpCacheFilter.php',
  542. 'CInlineFilter' => '/web/filters/CInlineFilter.php',
  543. 'CForm' => '/web/form/CForm.php',
  544. 'CFormButtonElement' => '/web/form/CFormButtonElement.php',
  545. 'CFormElement' => '/web/form/CFormElement.php',
  546. 'CFormElementCollection' => '/web/form/CFormElementCollection.php',
  547. 'CFormInputElement' => '/web/form/CFormInputElement.php',
  548. 'CFormStringElement' => '/web/form/CFormStringElement.php',
  549. 'CGoogleApi' => '/web/helpers/CGoogleApi.php',
  550. 'CHtml' => '/web/helpers/CHtml.php',
  551. 'CJSON' => '/web/helpers/CJSON.php',
  552. 'CJavaScript' => '/web/helpers/CJavaScript.php',
  553. 'CJavaScriptExpression' => '/web/helpers/CJavaScriptExpression.php',
  554. 'CPradoViewRenderer' => '/web/renderers/CPradoViewRenderer.php',
  555. 'CViewRenderer' => '/web/renderers/CViewRenderer.php',
  556. 'CWebService' => '/web/services/CWebService.php',
  557. 'CWebServiceAction' => '/web/services/CWebServiceAction.php',
  558. 'CWsdlGenerator' => '/web/services/CWsdlGenerator.php',
  559. 'CActiveForm' => '/web/widgets/CActiveForm.php',
  560. 'CAutoComplete' => '/web/widgets/CAutoComplete.php',
  561. 'CClipWidget' => '/web/widgets/CClipWidget.php',
  562. 'CContentDecorator' => '/web/widgets/CContentDecorator.php',
  563. 'CFilterWidget' => '/web/widgets/CFilterWidget.php',
  564. 'CFlexWidget' => '/web/widgets/CFlexWidget.php',
  565. 'CHtmlPurifier' => '/web/widgets/CHtmlPurifier.php',
  566. 'CInputWidget' => '/web/widgets/CInputWidget.php',
  567. 'CMarkdown' => '/web/widgets/CMarkdown.php',
  568. 'CMaskedTextField' => '/web/widgets/CMaskedTextField.php',
  569. 'CMultiFileUpload' => '/web/widgets/CMultiFileUpload.php',
  570. 'COutputCache' => '/web/widgets/COutputCache.php',
  571. 'COutputProcessor' => '/web/widgets/COutputProcessor.php',
  572. 'CStarRating' => '/web/widgets/CStarRating.php',
  573. 'CTabView' => '/web/widgets/CTabView.php',
  574. 'CTextHighlighter' => '/web/widgets/CTextHighlighter.php',
  575. 'CTreeView' => '/web/widgets/CTreeView.php',
  576. 'CWidget' => '/web/widgets/CWidget.php',
  577. 'CCaptcha' => '/web/widgets/captcha/CCaptcha.php',
  578. 'CCaptchaAction' => '/web/widgets/captcha/CCaptchaAction.php',
  579. 'CBasePager' => '/web/widgets/pagers/CBasePager.php',
  580. 'CLinkPager' => '/web/widgets/pagers/CLinkPager.php',
  581. 'CListPager' => '/web/widgets/pagers/CListPager.php',
  582. );
  583. }
  584. spl_autoload_register(array('YiiBase','autoload'));
  585. class Yii extends YiiBase
  586. {
  587. }
  588. class CComponent
  589. {
  590. private $_e;
  591. private $_m;
  592. public function __get($name)
  593. {
  594. $getter='get'.$name;
  595. if(method_exists($this,$getter))
  596. return $this->$getter();
  597. elseif(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  598. {
  599. // duplicating getEventHandlers() here for performance
  600. $name=strtolower($name);
  601. if(!isset($this->_e[$name]))
  602. $this->_e[$name]=new CList;
  603. return $this->_e[$name];
  604. }
  605. elseif(isset($this->_m[$name]))
  606. return $this->_m[$name];
  607. elseif(is_array($this->_m))
  608. {
  609. foreach($this->_m as $object)
  610. {
  611. if($object->getEnabled() && (property_exists($object,$name) || $object->canGetProperty($name)))
  612. return $object->$name;
  613. }
  614. }
  615. throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',
  616. array('{class}'=>get_class($this), '{property}'=>$name)));
  617. }
  618. public function __set($name,$value)
  619. {
  620. $setter='set'.$name;
  621. if(method_exists($this,$setter))
  622. return $this->$setter($value);
  623. elseif(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  624. {
  625. // duplicating getEventHandlers() here for performance
  626. $name=strtolower($name);
  627. if(!isset($this->_e[$name]))
  628. $this->_e[$name]=new CList;
  629. return $this->_e[$name]->add($value);
  630. }
  631. elseif(is_array($this->_m))
  632. {
  633. foreach($this->_m as $object)
  634. {
  635. if($object->getEnabled() && (property_exists($object,$name) || $object->canSetProperty($name)))
  636. return $object->$name=$value;
  637. }
  638. }
  639. if(method_exists($this,'get'.$name))
  640. throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.',
  641. array('{class}'=>get_class($this), '{property}'=>$name)));
  642. else
  643. throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',
  644. array('{class}'=>get_class($this), '{property}'=>$name)));
  645. }
  646. public function __isset($name)
  647. {
  648. $getter='get'.$name;
  649. if(method_exists($this,$getter))
  650. return $this->$getter()!==null;
  651. elseif(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  652. {
  653. $name=strtolower($name);
  654. return isset($this->_e[$name]) && $this->_e[$name]->getCount();
  655. }
  656. elseif(is_array($this->_m))
  657. {
  658. if(isset($this->_m[$name]))
  659. return true;
  660. foreach($this->_m as $object)
  661. {
  662. if($object->getEnabled() && (property_exists($object,$name) || $object->canGetProperty($name)))
  663. return $object->$name!==null;
  664. }
  665. }
  666. return false;
  667. }
  668. public function __unset($name)
  669. {
  670. $setter='set'.$name;
  671. if(method_exists($this,$setter))
  672. $this->$setter(null);
  673. elseif(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  674. unset($this->_e[strtolower($name)]);
  675. elseif(is_array($this->_m))
  676. {
  677. if(isset($this->_m[$name]))
  678. $this->detachBehavior($name);
  679. else
  680. {
  681. foreach($this->_m as $object)
  682. {
  683. if($object->getEnabled())
  684. {
  685. if(property_exists($object,$name))
  686. return $object->$name=null;
  687. elseif($object->canSetProperty($name))
  688. return $object->$setter(null);
  689. }
  690. }
  691. }
  692. }
  693. elseif(method_exists($this,'get'.$name))
  694. throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.',
  695. array('{class}'=>get_class($this), '{property}'=>$name)));
  696. }
  697. public function __call($name,$parameters)
  698. {
  699. if($this->_m!==null)
  700. {
  701. foreach($this->_m as $object)
  702. {
  703. if($object->getEnabled() && method_exists($object,$name))
  704. return call_user_func_array(array($object,$name),$parameters);
  705. }
  706. }
  707. if(class_exists('Closure', false) && ($this->canGetProperty($name) || property_exists($this, $name)) && $this->$name instanceof Closure)
  708. return call_user_func_array($this->$name, $parameters);
  709. throw new CException(Yii::t('yii','{class} and its behaviors do not have a method or closure named "{name}".',
  710. array('{class}'=>get_class($this), '{name}'=>$name)));
  711. }
  712. public function asa($behavior)
  713. {
  714. return isset($this->_m[$behavior]) ? $this->_m[$behavior] : null;
  715. }
  716. public function attachBehaviors($behaviors)
  717. {
  718. foreach($behaviors as $name=>$behavior)
  719. $this->attachBehavior($name,$behavior);
  720. }
  721. public function detachBehaviors()
  722. {
  723. if($this->_m!==null)
  724. {
  725. foreach($this->_m as $name=>$behavior)
  726. $this->detachBehavior($name);
  727. $this->_m=null;
  728. }
  729. }
  730. public function attachBehavior($name,$behavior)
  731. {
  732. if(!($behavior instanceof IBehavior))
  733. $behavior=Yii::createComponent($behavior);
  734. $behavior->setEnabled(true);
  735. $behavior->attach($this);
  736. return $this->_m[$name]=$behavior;
  737. }
  738. public function detachBehavior($name)
  739. {
  740. if(isset($this->_m[$name]))
  741. {
  742. $this->_m[$name]->detach($this);
  743. $behavior=$this->_m[$name];
  744. unset($this->_m[$name]);
  745. return $behavior;
  746. }
  747. }
  748. public function enableBehaviors()
  749. {
  750. if($this->_m!==null)
  751. {
  752. foreach($this->_m as $behavior)
  753. $behavior->setEnabled(true);
  754. }
  755. }
  756. public function disableBehaviors()
  757. {
  758. if($this->_m!==null)
  759. {
  760. foreach($this->_m as $behavior)
  761. $behavior->setEnabled(false);
  762. }
  763. }
  764. public function enableBehavior($name)
  765. {
  766. if(isset($this->_m[$name]))
  767. $this->_m[$name]->setEnabled(true);
  768. }
  769. public function disableBehavior($name)
  770. {
  771. if(isset($this->_m[$name]))
  772. $this->_m[$name]->setEnabled(false);
  773. }
  774. public function hasProperty($name)
  775. {
  776. return method_exists($this,'get'.$name) || method_exists($this,'set'.$name);
  777. }
  778. public function canGetProperty($name)
  779. {
  780. return method_exists($this,'get'.$name);
  781. }
  782. public function canSetProperty($name)
  783. {
  784. return method_exists($this,'set'.$name);
  785. }
  786. public function hasEvent($name)
  787. {
  788. return !strncasecmp($name,'on',2) && method_exists($this,$name);
  789. }
  790. public function hasEventHandler($name)
  791. {
  792. $name=strtolower($name);
  793. return isset($this->_e[$name]) && $this->_e[$name]->getCount()>0;
  794. }
  795. public function getEventHandlers($name)
  796. {
  797. if($this->hasEvent($name))
  798. {
  799. $name=strtolower($name);
  800. if(!isset($this->_e[$name]))
  801. $this->_e[$name]=new CList;
  802. return $this->_e[$name];
  803. }
  804. else
  805. throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.',
  806. array('{class}'=>get_class($this), '{event}'=>$name)));
  807. }
  808. public function attachEventHandler($name,$handler)
  809. {
  810. $this->getEventHandlers($name)->add($handler);
  811. }
  812. public function detachEventHandler($name,$handler)
  813. {
  814. if($this->hasEventHandler($name))
  815. return $this->getEventHandlers($name)->remove($handler)!==false;
  816. else
  817. return false;
  818. }
  819. public function raiseEvent($name,$event)
  820. {
  821. $name=strtolower($name);
  822. if(isset($this->_e[$name]))
  823. {
  824. foreach($this->_e[$name] as $handler)
  825. {
  826. if(is_string($handler))
  827. call_user_func($handler,$event);
  828. elseif(is_callable($handler,true))
  829. {
  830. if(is_array($handler))
  831. {
  832. // an array: 0 - object, 1 - method name
  833. list($object,$method)=$handler;
  834. if(is_string($object)) // static method call
  835. call_user_func($handler,$event);
  836. elseif(method_exists($object,$method))
  837. $object->$method($event);
  838. else
  839. throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".',
  840. array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>$handler[1])));
  841. }
  842. else // PHP 5.3: anonymous function
  843. call_user_func($handler,$event);
  844. }
  845. else
  846. throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".',
  847. array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>gettype($handler))));
  848. // stop further handling if param.handled is set true
  849. if(($event instanceof CEvent) && $event->handled)
  850. return;
  851. }
  852. }
  853. elseif(YII_DEBUG && !$this->hasEvent($name))
  854. throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.',
  855. array('{class}'=>get_class($this), '{event}'=>$name)));
  856. }
  857. public function evaluateExpression($_expression_,$_data_=array())
  858. {
  859. if(is_string($_expression_))
  860. {
  861. extract($_data_);
  862. return eval('return '.$_expression_.';');
  863. }
  864. else
  865. {
  866. $_data_[]=$this;
  867. return call_user_func_array($_expression_, $_data_);
  868. }
  869. }
  870. }
  871. class CEvent extends CComponent
  872. {
  873. public $sender;
  874. public $handled=false;
  875. public $params;
  876. public function __construct($sender=null,$params=null)
  877. {
  878. $this->sender=$sender;
  879. $this->params=$params;
  880. }
  881. }
  882. class CEnumerable
  883. {
  884. }
  885. abstract class CModule extends CComponent
  886. {
  887. public $preload=array();
  888. public $behaviors=array();
  889. private $_id;
  890. private $_parentModule;
  891. private $_basePath;
  892. private $_modulePath;
  893. private $_params;
  894. private $_modules=array();
  895. private $_moduleConfig=array();
  896. private $_components=array();
  897. private $_componentConfig=array();
  898. public function __construct($id,$parent,$config=null)
  899. {
  900. $this->_id=$id;
  901. $this->_parentModule=$parent;
  902. // set basePath at early as possible to avoid trouble
  903. if(is_string($config))
  904. $config=require($config);
  905. if(isset($config['basePath']))
  906. {
  907. $this->setBasePath($config['basePath']);
  908. unset($config['basePath']);
  909. }
  910. Yii::setPathOfAlias($id,$this->getBasePath());
  911. $this->preinit();
  912. $this->configure($config);
  913. $this->attachBehaviors($this->behaviors);
  914. $this->preloadComponents();
  915. $this->init();
  916. }
  917. public function __get($name)
  918. {
  919. if($this->hasComponent($name))
  920. return $this->getComponent($name);
  921. else
  922. return parent::__get($name);
  923. }
  924. public function __isset($name)
  925. {
  926. if($this->hasComponent($name))
  927. return $this->getComponent($name)!==null;
  928. else
  929. return parent::__isset($name);
  930. }
  931. public function getId()
  932. {
  933. return $this->_id;
  934. }
  935. public function setId($id)
  936. {
  937. $this->_id=$id;
  938. }
  939. public function getBasePath()
  940. {
  941. if($this->_basePath===null)
  942. {
  943. $class=new ReflectionClass(get_class($this));
  944. $this->_basePath=dirname($class->getFileName());
  945. }
  946. return $this->_basePath;
  947. }
  948. public function setBasePath($path)
  949. {
  950. if(($this->_basePath=realpath($path))===false || !is_dir($this->_basePath))
  951. throw new CException(Yii::t('yii','Base path "{path}" is not a valid directory.',
  952. array('{path}'=>$path)));
  953. }
  954. public function getParams()
  955. {
  956. if($this->_params!==null)
  957. return $this->_params;
  958. else
  959. {
  960. $this->_params=new CAttributeCollection;
  961. $this->_params->caseSensitive=true;
  962. return $this->_params;
  963. }
  964. }
  965. public function setParams($value)
  966. {
  967. $params=$this->getParams();
  968. foreach($value as $k=>$v)
  969. $params->add($k,$v);
  970. }
  971. public function getModulePath()
  972. {
  973. if($this->_modulePath!==null)
  974. return $this->_modulePath;
  975. else
  976. return $this->_modulePath=$this->getBasePath().DIRECTORY_SEPARATOR.'modules';
  977. }
  978. public function setModulePath($value)
  979. {
  980. if(($this->_modulePath=realpath($value))===false || !is_dir($this->_modulePath))
  981. throw new CException(Yii::t('yii','The module path "{path}" is not a valid directory.',
  982. array('{path}'=>$value)));
  983. }
  984. public function setImport($aliases)
  985. {
  986. foreach($aliases as $alias)
  987. Yii::import($alias);
  988. }
  989. public function setAliases($mappings)
  990. {
  991. foreach($mappings as $name=>$alias)
  992. {
  993. if(($path=Yii::getPathOfAlias($alias))!==false)
  994. Yii::setPathOfAlias($name,$path);
  995. else
  996. Yii::setPathOfAlias($name,$alias);
  997. }
  998. }
  999. public function getParentModule()
  1000. {
  1001. return $this->_parentModule;
  1002. }
  1003. public function getModule($id)
  1004. {
  1005. if(isset($this->_modules[$id]) || array_key_exists($id,$this->_modules))
  1006. return $this->_modules[$id];
  1007. elseif(isset($this->_moduleConfig[$id]))
  1008. {
  1009. $config=$this->_moduleConfig[$id];
  1010. if(!isset($config['enabled']) || $config['enabled'])
  1011. {
  1012. $class=$config['class'];
  1013. unset($config['class'], $config['enabled']);
  1014. if($this===Yii::app())
  1015. $module=Yii::createComponent($class,$id,null,$config);
  1016. else
  1017. $module=Yii::createComponent($class,$this->getId().'/'.$id,$this,$config);
  1018. return $this->_modules[$id]=$module;
  1019. }
  1020. }
  1021. }
  1022. public function hasModule($id)
  1023. {
  1024. return isset($this->_moduleConfig[$id]) || isset($this->_modules[$id]);
  1025. }
  1026. public function getModules()
  1027. {
  1028. return $this->_moduleConfig;
  1029. }
  1030. public function setModules($modules,$merge=true)
  1031. {
  1032. foreach($modules as $id=>$module)
  1033. {
  1034. if(is_int($id))
  1035. {
  1036. $id=$module;
  1037. $module=array();
  1038. }
  1039. if(isset($this->_moduleConfig[$id]) && $merge)
  1040. $this->_moduleConfig[$id]=CMap::mergeArray($this->_moduleConfig[$id],$module);
  1041. else
  1042. {
  1043. if(!isset($module['class']))
  1044. {
  1045. Yii::setPathOfAlias($id,$this->getModulePath().DIRECTORY_SEPARATOR.$id);
  1046. $module['class']=$id.'.'.ucfirst($id).'Module';
  1047. }
  1048. $this->_moduleConfig[$id]=$module;
  1049. }
  1050. }
  1051. }
  1052. public function hasComponent($id)
  1053. {
  1054. return isset($this->_components[$id]) || isset($this->_componentConfig[$id]);
  1055. }
  1056. public function getComponent($id,$createIfNull=true)
  1057. {
  1058. if(isset($this->_components[$id]))
  1059. return $this->_components[$id];
  1060. elseif(isset($this->_componentConfig[$id]) && $createIfNull)
  1061. {
  1062. $config=$this->_componentConfig[$id];
  1063. if(!isset($config['enabled']) || $config['enabled'])
  1064. {
  1065. unset($config['enabled']);
  1066. $component=Yii::createComponent($config);
  1067. $component->init();
  1068. return $this->_components[$id]=$component;
  1069. }
  1070. }
  1071. }
  1072. public function setComponent($id,$component,$merge=true)
  1073. {
  1074. if($component===null)
  1075. {
  1076. unset($this->_components[$id]);
  1077. return;
  1078. }
  1079. elseif($component instanceof IApplicationComponent)
  1080. {
  1081. $this->_components[$id]=$component;
  1082. if(!$component->getIsInitialized())
  1083. $component->init();
  1084. return;
  1085. }
  1086. elseif(isset($this->_components[$id]))
  1087. {
  1088. if(isset($component['class']) && get_class($this->_components[$id])!==$component['class'])
  1089. {
  1090. unset($this->_components[$id]);
  1091. $this->_componentConfig[$id]=$component; //we should ignore merge here
  1092. return;
  1093. }
  1094. foreach($component as $key=>$value)
  1095. {
  1096. if($key!=='class')
  1097. $this->_components[$id]->$key=$value;
  1098. }
  1099. }
  1100. elseif(isset($this->_componentConfig[$id]['class'],$component['class'])
  1101. && $this->_componentConfig[$id]['class']!==$component['class'])
  1102. {
  1103. $this->_componentConfig[$id]=$component; //we should ignore merge here
  1104. return;
  1105. }
  1106. if(isset($this->_componentConfig[$id]) && $merge)
  1107. $this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$component);
  1108. else
  1109. $this->_componentConfig[$id]=$component;
  1110. }
  1111. public function getComponents($loadedOnly=true)
  1112. {
  1113. if($loadedOnly)
  1114. return $this->_components;
  1115. else
  1116. return array_merge($this->_componentConfig, $this->_components);
  1117. }
  1118. public function setComponents($components,$merge=true)
  1119. {
  1120. foreach($components as $id=>$component)
  1121. $this->setComponent($id,$component,$merge);
  1122. }
  1123. public function configure($config)
  1124. {
  1125. if(is_array($config))
  1126. {
  1127. foreach($config as $key=>$value)
  1128. $this->$key=$value;
  1129. }
  1130. }
  1131. protected function preloadComponents()
  1132. {
  1133. foreach($this->preload as $id)
  1134. $this->getComponent($id);
  1135. }
  1136. protected function preinit()
  1137. {
  1138. }
  1139. protected function init()
  1140. {
  1141. }
  1142. }
  1143. abstract class CApplication extends CModule
  1144. {
  1145. public $name='My Application';
  1146. public $charset='UTF-8';
  1147. public $sourceLanguage='en_us';
  1148. public $localeClass='CLocale';
  1149. private $_id;
  1150. private $_basePath;
  1151. private $_runtimePath;
  1152. private $_extensionPath;
  1153. private $_globalState;
  1154. private $_stateChanged;
  1155. private $_ended=false;
  1156. private $_language;
  1157. private $_homeUrl;
  1158. abstract public function processRequest();
  1159. public function __construct($config=null)
  1160. {
  1161. Yii::setApplication($this);
  1162. // set basePath at early as possible to avoid trouble
  1163. if(is_string($config))
  1164. $config=require($config);
  1165. if(isset($config['basePath']))
  1166. {
  1167. $this->setBasePath($config['basePath']);
  1168. unset($config['basePath']);
  1169. }
  1170. else
  1171. $this->setBasePath('protected');
  1172. Yii::setPathOfAlias('application',$this->getBasePath());
  1173. Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
  1174. if(isset($config['extensionPath']))
  1175. {
  1176. $this->setExtensionPath($config['extensionPath']);
  1177. unset($config['extensionPath']);
  1178. }
  1179. else
  1180. Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');
  1181. if(isset($config['aliases']))
  1182. {
  1183. $this->setAliases($config['aliases']);
  1184. unset($config['aliases']);
  1185. }
  1186. $this->preinit();
  1187. $this->initSystemHandlers();
  1188. $this->registerCoreComponents();
  1189. $this->configure($config);
  1190. $this->attachBehaviors($this->behaviors);
  1191. $this->preloadComponents();
  1192. $this->init();
  1193. }
  1194. public function run()
  1195. {
  1196. if($this->hasEventHandler('onBeginRequest'))
  1197. $this->onBeginRequest(new CEvent($this));
  1198. register_shutdown_function(array($this,'end'),0,false);
  1199. $this->processRequest();
  1200. if($this->hasEventHandler('onEndRequest'))
  1201. $this->onEndRequest(new CEvent($this));
  1202. }
  1203. public function end($status=0,$exit=true)
  1204. {
  1205. if($this->hasEventHandler('onEndRequest'))
  1206. $this->onEndRequest(new CEvent($this));
  1207. if($exit)
  1208. exit($status);
  1209. }
  1210. public function onBeginRequest($event)
  1211. {
  1212. $this->raiseEvent('onBeginRequest',$event);
  1213. }
  1214. public function onEndRequest($event)
  1215. {
  1216. if(!$this->_ended)
  1217. {
  1218. $this->_ended=true;
  1219. $this->raiseEvent('onEndRequest',$event);
  1220. }
  1221. }
  1222. public function getId()
  1223. {
  1224. if($this->_id!==null)
  1225. return $this->_id;
  1226. else
  1227. return $this->_id=sprintf('%x',crc32($this->getBasePath().$this->name));
  1228. }
  1229. public function setId($id)
  1230. {
  1231. $this->_id=$id;
  1232. }
  1233. public function getBasePath()
  1234. {
  1235. return $this->_basePath;
  1236. }
  1237. public function setBasePath($path)
  1238. {
  1239. if(($this->_basePath=realpath($path))===false || !is_dir($this->_basePath))
  1240. throw new CException(Yii::t('yii','Application base path "{path}" is not a valid directory.',
  1241. array('{path}'=>$path)));
  1242. }
  1243. public function getRuntimePath()
  1244. {
  1245. if($this->_runtimePath!==null)
  1246. return $this->_runtimePath;
  1247. else
  1248. {
  1249. $this->setRuntimePath($this->getBasePath().DIRECTORY_SEPARATOR.'runtime');
  1250. return $this->_runtimePath;
  1251. }
  1252. }
  1253. public function setRuntimePath($path)
  1254. {
  1255. if(($runtimePath=realpath($path))===false || !is_dir($runtimePath) || !is_writable($runtimePath))
  1256. throw new CException(Yii::t('yii','Application runtime path "{path}" is not valid. Please make sure it is a directory writable by the Web server process.',
  1257. array('{path}'=>$path)));
  1258. $this->_runtimePath=$runtimePath;
  1259. }
  1260. public function getExtensionPath()
  1261. {
  1262. return Yii::getPathOfAlias('ext');
  1263. }
  1264. public function setExtensionPath($path)
  1265. {
  1266. if(($extensionPath=realpath($path))===false || !is_dir($extensionPath))
  1267. throw new CException(Yii::t('yii','Extension path "{path}" does not exist.',
  1268. array('{path}'=>$path)));
  1269. Yii::setPathOfAlias('ext',$extensionPath);
  1270. }
  1271. public function getLanguage()
  1272. {
  1273. return $this->_language===null ? $this->sourceLanguage : $this->_language;
  1274. }
  1275. public function setLanguage($language)
  1276. {
  1277. $this->_language=$language;
  1278. }
  1279. public function getTimeZone()
  1280. {
  1281. return date_default_timezone_get();
  1282. }
  1283. public function setTimeZone($value)
  1284. {
  1285. date_default_timezone_set($value);
  1286. }
  1287. public function findLocalizedFile($srcFile,$srcLanguage=null,$language=null)
  1288. {
  1289. if($srcLanguage===null)
  1290. $srcLanguage=$this->sourceLanguage;
  1291. if($language===null)
  1292. $language=$this->getLanguage();
  1293. if($language===$srcLanguage)
  1294. return $srcFile;
  1295. $desiredFile=dirname($srcFile).DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.basename($srcFile);
  1296. return is_file($desiredFile) ? $desiredFile : $srcFile;
  1297. }
  1298. public function getLocale($localeID=null)
  1299. {
  1300. return call_user_func_array(array($this->localeClass, 'getInstance'),array($localeID===null?$this->getLanguage():$localeID));
  1301. }
  1302. public function getLocaleDataPath()
  1303. {
  1304. $vars=get_class_vars($this->localeClass);
  1305. if(empty($vars['dataPath']))
  1306. return Yii::getPathOfAlias('system.i18n.data');
  1307. return $vars['dataPath'];
  1308. }
  1309. public function setLocaleDataPath($value)
  1310. {
  1311. $property=new ReflectionProperty($this->localeClass,'dataPath');
  1312. $property->setValue($value);
  1313. }
  1314. public function getNumberFormatter()
  1315. {
  1316. return $this->getLocale()->getNumberFormatter();
  1317. }
  1318. public function getDateFormatter()
  1319. {
  1320. return $this->getLocale()->getDateFormatter();
  1321. }
  1322. public function getDb()
  1323. {
  1324. return $this->getComponent('db');
  1325. }
  1326. public function getErrorHandler()
  1327. {
  1328. return $this->getComponent('errorHandler');
  1329. }
  1330. public function getSecurityManager()
  1331. {
  1332. return $this->getComponent('securityManager');
  1333. }
  1334. public function getStatePersister()
  1335. {
  1336. return $this->getComponent('statePersister');
  1337. }
  1338. public function getCache()
  1339. {
  1340. return $this->getComponent('cache');
  1341. }
  1342. public function getCoreMessages()
  1343. {
  1344. return $this->getComponent('coreMessages');
  1345. }
  1346. public function getMessages()
  1347. {
  1348. return $this->getComponent('messages');
  1349. }
  1350. public function getRequest()
  1351. {
  1352. return $this->getComponent('request');
  1353. }
  1354. public function getUrlManager()
  1355. {
  1356. return $this->getComponent('urlManager');
  1357. }
  1358. public function getController()
  1359. {
  1360. return null;
  1361. }
  1362. public function createUrl($route,$params=array(),$ampersand='&')
  1363. {
  1364. return $this->getUrlManager()->createUrl($route,$params,$ampersand);
  1365. }
  1366. public function createAbsoluteUrl($route,$params=array(),$schema='',$ampersand='&')
  1367. {
  1368. $url=$this->createUrl($route,$params,$ampersand);
  1369. if(strpos($url,'http')===0)
  1370. return $url;
  1371. else
  1372. return $this->getRequest()->getHostInfo($schema).$url;
  1373. }
  1374. public function getBaseUrl($absolute=false)
  1375. {
  1376. return $this->getRequest()->getBaseUrl($absolute);
  1377. }
  1378. public function getHomeUrl()
  1379. {
  1380. if($this->_homeUrl===null)
  1381. {
  1382. if($this->getUrlManager()->showScriptName)
  1383. return $this->getRequest()->getScriptUrl();
  1384. else
  1385. return $this->getRequest()->getBaseUrl().'/';
  1386. }
  1387. else
  1388. return $this->_homeUrl;
  1389. }
  1390. public function setHomeUrl($value)
  1391. {
  1392. $this->_homeUrl=$value;
  1393. }
  1394. public function getGlobalState($key,$defaultValue=null)
  1395. {
  1396. if($this->_globalState===null)
  1397. $this->loadGlobalState();
  1398. if(isset($this->_globalState[$key]))
  1399. return $this->_globalState[$key];
  1400. else
  1401. return $defaultValue;
  1402. }
  1403. public function setGlobalState($key,$value,$defaultValue=null)
  1404. {
  1405. if($this->_globalState===null)
  1406. $this->loadGlobalState();
  1407. $changed=$this->_stateChanged;
  1408. if($value===$defaultValue)
  1409. {
  1410. if(isset($this->_globalState[$key]))
  1411. {
  1412. unset($this->_globalState[$key]);
  1413. $this->_stateChanged=true;
  1414. }
  1415. }
  1416. elseif(!isset($this->_globalState[$key]) || $this->_globalState[$key]!==$value)
  1417. {
  1418. $this->_globalState[$key]=$value;
  1419. $this->_stateChanged=true;
  1420. }
  1421. if($this->_stateChanged!==$changed)
  1422. $this->attachEventHandler('onEndRequest',array($this,'saveGlobalState'));
  1423. }
  1424. public function clearGlobalState($key)
  1425. {
  1426. $this->setGlobalState($key,true,true);
  1427. }
  1428. public function loadGlobalState()
  1429. {
  1430. $persister=$this->getStatePersister();
  1431. if(($this->_globalState=$persister->load())===null)
  1432. $this->_globalState=array();
  1433. $this->_stateChanged=false;
  1434. $this->detachEventHandler('onEndRequest',array($this,'saveGlobalState'));
  1435. }
  1436. public function saveGlobalState()
  1437. {
  1438. if($this->_stateChanged)
  1439. {
  1440. $this->_stateChanged=false;
  1441. $this->detachEventHandler('onEndRequest',array($this,'saveGlobalState'));
  1442. $this->getStatePersister()->save($this->_globalState);
  1443. }
  1444. }
  1445. public function handleException($exception)
  1446. {
  1447. // disable error capturing to avoid recursive errors
  1448. restore_error_handler();
  1449. restore_exception_handler();
  1450. $category='exception.'.get_class($exception);
  1451. if($exception instanceof CHttpException)
  1452. $category.='.'.$exception->statusCode;
  1453. // php <5.2 doesn't support string conversion auto-magically
  1454. $message=$exception->__toString();
  1455. if(isset($_SERVER['REQUEST_URI']))
  1456. $message.="\nREQUEST_URI=".$_SERVER['REQUEST_URI'];
  1457. if(isset($_SERVER['HTTP_REFERER']))
  1458. $message.="\nHTTP_REFERER=".$_SERVER['HTTP_REFERER'];
  1459. $message.="\n---";
  1460. Yii::log($message,CLogger::LEVEL_ERROR,$category);
  1461. try
  1462. {
  1463. $event=new CExceptionEvent($this,$exception);
  1464. $this->onException($event);
  1465. if(!$event->handled)
  1466. {
  1467. // try an error handler
  1468. if(($handler=$this->getErrorHandler())!==null)
  1469. $handler->handle($event);
  1470. else
  1471. $this->displayException($exception);
  1472. }
  1473. }
  1474. catch(Exception $e)
  1475. {
  1476. $this->displayException($e);
  1477. }
  1478. try
  1479. {
  1480. $this->end(1);
  1481. }
  1482. catch(Exception $e)
  1483. {
  1484. // use the most primitive way to log error
  1485. $msg = get_class($e).': '.$e->getMessage().' ('.$e->getFile().':'.$e->getLine().")\n";
  1486. $msg .= $e->getTraceAsString()."\n";
  1487. $msg .= "Previous exception:\n";
  1488. $msg .= get_class($exception).': '.$exception->getMessage().' ('.$exception->getFile().':'.$exception->getLine().")\n";
  1489. $msg .= $exception->getTraceAsString()."\n";
  1490. $msg .= '$_SERVER='.var_export($_SERVER,true);
  1491. error_log($msg);
  1492. exit(1);
  1493. }
  1494. }
  1495. public function handleError($code,$message,$file,$line)
  1496. {
  1497. if($code & error_reporting())
  1498. {
  1499. // disable error capturing to avoid recursive errors
  1500. restore_error_handler();
  1501. restore_exception_handler();
  1502. $log="$message ($file:$line)\nStack trace:\n";
  1503. $trace=debug_backtrace();
  1504. // skip the first 3 stacks as they do not tell the error position
  1505. if(count($trace)>3)
  1506. $trace=array_slice($trace,3);
  1507. foreach($trace as $i=>$t)
  1508. {
  1509. if(!isset($t['file']))
  1510. $t['file']='unknown';
  1511. if(!isset($t['line']))
  1512. $t['line']=0;
  1513. if(!isset($t['function']))
  1514. $t['function']='unknown';
  1515. $log.="#$i {$t['file']}({$t['line']}): ";
  1516. if(isset($t['object']) && is_object($t['object']))
  1517. $log.=get_class($t['object']).'->';
  1518. $log.="{$t['function']}()\n";
  1519. }
  1520. if(isset($_SERVER['REQUEST_URI']))
  1521. $log.='REQUEST_URI='.$_SERVER['REQUEST_URI'];
  1522. Yii::log($log,CLogger::LEVEL_ERROR,'php');
  1523. try
  1524. {
  1525. Yii::import('CErrorEvent',true);
  1526. $event=new CErrorEvent($this,$code,$message,$file,$line);
  1527. $this->onError($event);
  1528. if(!$event->handled)
  1529. {
  1530. // try an error handler
  1531. if(($handler=$this->getErrorHandler())!==null)
  1532. $handler->handle($event);
  1533. else
  1534. $this->displayError($code,$message,$file,$line);
  1535. }
  1536. }
  1537. catch(Exception $e)
  1538. {
  1539. $this->displayException($e);
  1540. }
  1541. try
  1542. {
  1543. $this->end(1);
  1544. }
  1545. catch(Exception $e)
  1546. {
  1547. // use the most primitive way to log error
  1548. $msg = get_class($e).': '.$e->getMessage().' ('.$e->getFile().':'.$e->getLine().")\n";
  1549. $msg .= $e->getTraceAsString()."\n";
  1550. $msg .= "Previous error:\n";
  1551. $msg .= $log."\n";
  1552. $msg .= '$_SERVER='.var_export($_SERVER,true);
  1553. error_log($msg);
  1554. exit(1);
  1555. }
  1556. }
  1557. }
  1558. public function onException($event)
  1559. {
  1560. $this->raiseEvent('onException',$event);
  1561. }
  1562. public function onError($event)
  1563. {
  1564. $this->raiseEvent('onError',$event);
  1565. }
  1566. public function displayError($code,$message,$file,$line)
  1567. {
  1568. if(YII_DEBUG)
  1569. {
  1570. echo "<h1>PHP Error [$code]</h1>\n";
  1571. echo "<p>$message ($file:$line)</p…

Large files files are truncated, but you can click here to view the full file