PageRenderTime 68ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 2ms

/yii/framework/yiilite.php

https://bitbucket.org/zurmo/zurmo/
PHP | 10608 lines | 10527 code | 2 blank | 79 comment | 1094 complexity | 745d6fb1c194841250da2105edc9ef84 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-2-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.17';
  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 && is_file($path.'.php'))
  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. 'CDbStatePersister' => '/base/CDbStatePersister.php',
  363. 'CErrorEvent' => '/base/CErrorEvent.php',
  364. 'CErrorHandler' => '/base/CErrorHandler.php',
  365. 'CException' => '/base/CException.php',
  366. 'CExceptionEvent' => '/base/CExceptionEvent.php',
  367. 'CHttpException' => '/base/CHttpException.php',
  368. 'CModel' => '/base/CModel.php',
  369. 'CModelBehavior' => '/base/CModelBehavior.php',
  370. 'CModelEvent' => '/base/CModelEvent.php',
  371. 'CModule' => '/base/CModule.php',
  372. 'CSecurityManager' => '/base/CSecurityManager.php',
  373. 'CStatePersister' => '/base/CStatePersister.php',
  374. 'CApcCache' => '/caching/CApcCache.php',
  375. 'CCache' => '/caching/CCache.php',
  376. 'CDbCache' => '/caching/CDbCache.php',
  377. 'CDummyCache' => '/caching/CDummyCache.php',
  378. 'CEAcceleratorCache' => '/caching/CEAcceleratorCache.php',
  379. 'CFileCache' => '/caching/CFileCache.php',
  380. 'CMemCache' => '/caching/CMemCache.php',
  381. 'CRedisCache' => '/caching/CRedisCache.php',
  382. 'CWinCache' => '/caching/CWinCache.php',
  383. 'CXCache' => '/caching/CXCache.php',
  384. 'CZendDataCache' => '/caching/CZendDataCache.php',
  385. 'CCacheDependency' => '/caching/dependencies/CCacheDependency.php',
  386. 'CChainedCacheDependency' => '/caching/dependencies/CChainedCacheDependency.php',
  387. 'CDbCacheDependency' => '/caching/dependencies/CDbCacheDependency.php',
  388. 'CDirectoryCacheDependency' => '/caching/dependencies/CDirectoryCacheDependency.php',
  389. 'CExpressionDependency' => '/caching/dependencies/CExpressionDependency.php',
  390. 'CFileCacheDependency' => '/caching/dependencies/CFileCacheDependency.php',
  391. 'CGlobalStateCacheDependency' => '/caching/dependencies/CGlobalStateCacheDependency.php',
  392. 'CAttributeCollection' => '/collections/CAttributeCollection.php',
  393. 'CConfiguration' => '/collections/CConfiguration.php',
  394. 'CList' => '/collections/CList.php',
  395. 'CListIterator' => '/collections/CListIterator.php',
  396. 'CMap' => '/collections/CMap.php',
  397. 'CMapIterator' => '/collections/CMapIterator.php',
  398. 'CQueue' => '/collections/CQueue.php',
  399. 'CQueueIterator' => '/collections/CQueueIterator.php',
  400. 'CStack' => '/collections/CStack.php',
  401. 'CStackIterator' => '/collections/CStackIterator.php',
  402. 'CTypedList' => '/collections/CTypedList.php',
  403. 'CTypedMap' => '/collections/CTypedMap.php',
  404. 'CConsoleApplication' => '/console/CConsoleApplication.php',
  405. 'CConsoleCommand' => '/console/CConsoleCommand.php',
  406. 'CConsoleCommandBehavior' => '/console/CConsoleCommandBehavior.php',
  407. 'CConsoleCommandEvent' => '/console/CConsoleCommandEvent.php',
  408. 'CConsoleCommandRunner' => '/console/CConsoleCommandRunner.php',
  409. 'CHelpCommand' => '/console/CHelpCommand.php',
  410. 'CDbCommand' => '/db/CDbCommand.php',
  411. 'CDbConnection' => '/db/CDbConnection.php',
  412. 'CDbDataReader' => '/db/CDbDataReader.php',
  413. 'CDbException' => '/db/CDbException.php',
  414. 'CDbMigration' => '/db/CDbMigration.php',
  415. 'CDbTransaction' => '/db/CDbTransaction.php',
  416. 'CActiveFinder' => '/db/ar/CActiveFinder.php',
  417. 'CActiveRecord' => '/db/ar/CActiveRecord.php',
  418. 'CActiveRecordBehavior' => '/db/ar/CActiveRecordBehavior.php',
  419. 'CDbColumnSchema' => '/db/schema/CDbColumnSchema.php',
  420. 'CDbCommandBuilder' => '/db/schema/CDbCommandBuilder.php',
  421. 'CDbCriteria' => '/db/schema/CDbCriteria.php',
  422. 'CDbExpression' => '/db/schema/CDbExpression.php',
  423. 'CDbSchema' => '/db/schema/CDbSchema.php',
  424. 'CDbTableSchema' => '/db/schema/CDbTableSchema.php',
  425. 'CCubridColumnSchema' => '/db/schema/cubrid/CCubridColumnSchema.php',
  426. 'CCubridSchema' => '/db/schema/cubrid/CCubridSchema.php',
  427. 'CCubridTableSchema' => '/db/schema/cubrid/CCubridTableSchema.php',
  428. 'CMssqlColumnSchema' => '/db/schema/mssql/CMssqlColumnSchema.php',
  429. 'CMssqlCommandBuilder' => '/db/schema/mssql/CMssqlCommandBuilder.php',
  430. 'CMssqlPdoAdapter' => '/db/schema/mssql/CMssqlPdoAdapter.php',
  431. 'CMssqlSchema' => '/db/schema/mssql/CMssqlSchema.php',
  432. 'CMssqlSqlsrvPdoAdapter' => '/db/schema/mssql/CMssqlSqlsrvPdoAdapter.php',
  433. 'CMssqlTableSchema' => '/db/schema/mssql/CMssqlTableSchema.php',
  434. 'CMysqlColumnSchema' => '/db/schema/mysql/CMysqlColumnSchema.php',
  435. 'CMysqlCommandBuilder' => '/db/schema/mysql/CMysqlCommandBuilder.php',
  436. 'CMysqlSchema' => '/db/schema/mysql/CMysqlSchema.php',
  437. 'CMysqlTableSchema' => '/db/schema/mysql/CMysqlTableSchema.php',
  438. 'COciColumnSchema' => '/db/schema/oci/COciColumnSchema.php',
  439. 'COciCommandBuilder' => '/db/schema/oci/COciCommandBuilder.php',
  440. 'COciSchema' => '/db/schema/oci/COciSchema.php',
  441. 'COciTableSchema' => '/db/schema/oci/COciTableSchema.php',
  442. 'CPgsqlColumnSchema' => '/db/schema/pgsql/CPgsqlColumnSchema.php',
  443. 'CPgsqlCommandBuilder' => '/db/schema/pgsql/CPgsqlCommandBuilder.php',
  444. 'CPgsqlSchema' => '/db/schema/pgsql/CPgsqlSchema.php',
  445. 'CPgsqlTableSchema' => '/db/schema/pgsql/CPgsqlTableSchema.php',
  446. 'CSqliteColumnSchema' => '/db/schema/sqlite/CSqliteColumnSchema.php',
  447. 'CSqliteCommandBuilder' => '/db/schema/sqlite/CSqliteCommandBuilder.php',
  448. 'CSqliteSchema' => '/db/schema/sqlite/CSqliteSchema.php',
  449. 'CChoiceFormat' => '/i18n/CChoiceFormat.php',
  450. 'CDateFormatter' => '/i18n/CDateFormatter.php',
  451. 'CDbMessageSource' => '/i18n/CDbMessageSource.php',
  452. 'CGettextMessageSource' => '/i18n/CGettextMessageSource.php',
  453. 'CLocale' => '/i18n/CLocale.php',
  454. 'CMessageSource' => '/i18n/CMessageSource.php',
  455. 'CNumberFormatter' => '/i18n/CNumberFormatter.php',
  456. 'CPhpMessageSource' => '/i18n/CPhpMessageSource.php',
  457. 'CGettextFile' => '/i18n/gettext/CGettextFile.php',
  458. 'CGettextMoFile' => '/i18n/gettext/CGettextMoFile.php',
  459. 'CGettextPoFile' => '/i18n/gettext/CGettextPoFile.php',
  460. 'CChainedLogFilter' => '/logging/CChainedLogFilter.php',
  461. 'CDbLogRoute' => '/logging/CDbLogRoute.php',
  462. 'CEmailLogRoute' => '/logging/CEmailLogRoute.php',
  463. 'CFileLogRoute' => '/logging/CFileLogRoute.php',
  464. 'CLogFilter' => '/logging/CLogFilter.php',
  465. 'CLogRoute' => '/logging/CLogRoute.php',
  466. 'CLogRouter' => '/logging/CLogRouter.php',
  467. 'CLogger' => '/logging/CLogger.php',
  468. 'CProfileLogRoute' => '/logging/CProfileLogRoute.php',
  469. 'CSysLogRoute' => '/logging/CSysLogRoute.php',
  470. 'CWebLogRoute' => '/logging/CWebLogRoute.php',
  471. 'CDateTimeParser' => '/utils/CDateTimeParser.php',
  472. 'CFileHelper' => '/utils/CFileHelper.php',
  473. 'CFormatter' => '/utils/CFormatter.php',
  474. 'CLocalizedFormatter' => '/utils/CLocalizedFormatter.php',
  475. 'CMarkdownParser' => '/utils/CMarkdownParser.php',
  476. 'CPasswordHelper' => '/utils/CPasswordHelper.php',
  477. 'CPropertyValue' => '/utils/CPropertyValue.php',
  478. 'CTimestamp' => '/utils/CTimestamp.php',
  479. 'CVarDumper' => '/utils/CVarDumper.php',
  480. 'CBooleanValidator' => '/validators/CBooleanValidator.php',
  481. 'CCaptchaValidator' => '/validators/CCaptchaValidator.php',
  482. 'CCompareValidator' => '/validators/CCompareValidator.php',
  483. 'CDateValidator' => '/validators/CDateValidator.php',
  484. 'CDefaultValueValidator' => '/validators/CDefaultValueValidator.php',
  485. 'CEmailValidator' => '/validators/CEmailValidator.php',
  486. 'CExistValidator' => '/validators/CExistValidator.php',
  487. 'CFileValidator' => '/validators/CFileValidator.php',
  488. 'CFilterValidator' => '/validators/CFilterValidator.php',
  489. 'CInlineValidator' => '/validators/CInlineValidator.php',
  490. 'CNumberValidator' => '/validators/CNumberValidator.php',
  491. 'CRangeValidator' => '/validators/CRangeValidator.php',
  492. 'CRegularExpressionValidator' => '/validators/CRegularExpressionValidator.php',
  493. 'CRequiredValidator' => '/validators/CRequiredValidator.php',
  494. 'CSafeValidator' => '/validators/CSafeValidator.php',
  495. 'CStringValidator' => '/validators/CStringValidator.php',
  496. 'CTypeValidator' => '/validators/CTypeValidator.php',
  497. 'CUniqueValidator' => '/validators/CUniqueValidator.php',
  498. 'CUnsafeValidator' => '/validators/CUnsafeValidator.php',
  499. 'CUrlValidator' => '/validators/CUrlValidator.php',
  500. 'CValidator' => '/validators/CValidator.php',
  501. 'CActiveDataProvider' => '/web/CActiveDataProvider.php',
  502. 'CArrayDataProvider' => '/web/CArrayDataProvider.php',
  503. 'CAssetManager' => '/web/CAssetManager.php',
  504. 'CBaseController' => '/web/CBaseController.php',
  505. 'CCacheHttpSession' => '/web/CCacheHttpSession.php',
  506. 'CClientScript' => '/web/CClientScript.php',
  507. 'CController' => '/web/CController.php',
  508. 'CDataProvider' => '/web/CDataProvider.php',
  509. 'CDataProviderIterator' => '/web/CDataProviderIterator.php',
  510. 'CDbHttpSession' => '/web/CDbHttpSession.php',
  511. 'CExtController' => '/web/CExtController.php',
  512. 'CFormModel' => '/web/CFormModel.php',
  513. 'CHttpCookie' => '/web/CHttpCookie.php',
  514. 'CHttpRequest' => '/web/CHttpRequest.php',
  515. 'CHttpSession' => '/web/CHttpSession.php',
  516. 'CHttpSessionIterator' => '/web/CHttpSessionIterator.php',
  517. 'COutputEvent' => '/web/COutputEvent.php',
  518. 'CPagination' => '/web/CPagination.php',
  519. 'CSort' => '/web/CSort.php',
  520. 'CSqlDataProvider' => '/web/CSqlDataProvider.php',
  521. 'CTheme' => '/web/CTheme.php',
  522. 'CThemeManager' => '/web/CThemeManager.php',
  523. 'CUploadedFile' => '/web/CUploadedFile.php',
  524. 'CUrlManager' => '/web/CUrlManager.php',
  525. 'CWebApplication' => '/web/CWebApplication.php',
  526. 'CWebModule' => '/web/CWebModule.php',
  527. 'CWidgetFactory' => '/web/CWidgetFactory.php',
  528. 'CAction' => '/web/actions/CAction.php',
  529. 'CInlineAction' => '/web/actions/CInlineAction.php',
  530. 'CViewAction' => '/web/actions/CViewAction.php',
  531. 'CAccessControlFilter' => '/web/auth/CAccessControlFilter.php',
  532. 'CAuthAssignment' => '/web/auth/CAuthAssignment.php',
  533. 'CAuthItem' => '/web/auth/CAuthItem.php',
  534. 'CAuthManager' => '/web/auth/CAuthManager.php',
  535. 'CBaseUserIdentity' => '/web/auth/CBaseUserIdentity.php',
  536. 'CDbAuthManager' => '/web/auth/CDbAuthManager.php',
  537. 'CPhpAuthManager' => '/web/auth/CPhpAuthManager.php',
  538. 'CUserIdentity' => '/web/auth/CUserIdentity.php',
  539. 'CWebUser' => '/web/auth/CWebUser.php',
  540. 'CFilter' => '/web/filters/CFilter.php',
  541. 'CFilterChain' => '/web/filters/CFilterChain.php',
  542. 'CHttpCacheFilter' => '/web/filters/CHttpCacheFilter.php',
  543. 'CInlineFilter' => '/web/filters/CInlineFilter.php',
  544. 'CForm' => '/web/form/CForm.php',
  545. 'CFormButtonElement' => '/web/form/CFormButtonElement.php',
  546. 'CFormElement' => '/web/form/CFormElement.php',
  547. 'CFormElementCollection' => '/web/form/CFormElementCollection.php',
  548. 'CFormInputElement' => '/web/form/CFormInputElement.php',
  549. 'CFormStringElement' => '/web/form/CFormStringElement.php',
  550. 'CGoogleApi' => '/web/helpers/CGoogleApi.php',
  551. 'CHtml' => '/web/helpers/CHtml.php',
  552. 'CJSON' => '/web/helpers/CJSON.php',
  553. 'CJavaScript' => '/web/helpers/CJavaScript.php',
  554. 'CJavaScriptExpression' => '/web/helpers/CJavaScriptExpression.php',
  555. 'CPradoViewRenderer' => '/web/renderers/CPradoViewRenderer.php',
  556. 'CViewRenderer' => '/web/renderers/CViewRenderer.php',
  557. 'CWebService' => '/web/services/CWebService.php',
  558. 'CWebServiceAction' => '/web/services/CWebServiceAction.php',
  559. 'CWsdlGenerator' => '/web/services/CWsdlGenerator.php',
  560. 'CActiveForm' => '/web/widgets/CActiveForm.php',
  561. 'CAutoComplete' => '/web/widgets/CAutoComplete.php',
  562. 'CClipWidget' => '/web/widgets/CClipWidget.php',
  563. 'CContentDecorator' => '/web/widgets/CContentDecorator.php',
  564. 'CFilterWidget' => '/web/widgets/CFilterWidget.php',
  565. 'CFlexWidget' => '/web/widgets/CFlexWidget.php',
  566. 'CHtmlPurifier' => '/web/widgets/CHtmlPurifier.php',
  567. 'CInputWidget' => '/web/widgets/CInputWidget.php',
  568. 'CMarkdown' => '/web/widgets/CMarkdown.php',
  569. 'CMaskedTextField' => '/web/widgets/CMaskedTextField.php',
  570. 'CMultiFileUpload' => '/web/widgets/CMultiFileUpload.php',
  571. 'COutputCache' => '/web/widgets/COutputCache.php',
  572. 'COutputProcessor' => '/web/widgets/COutputProcessor.php',
  573. 'CStarRating' => '/web/widgets/CStarRating.php',
  574. 'CTabView' => '/web/widgets/CTabView.php',
  575. 'CTextHighlighter' => '/web/widgets/CTextHighlighter.php',
  576. 'CTreeView' => '/web/widgets/CTreeView.php',
  577. 'CWidget' => '/web/widgets/CWidget.php',
  578. 'CCaptcha' => '/web/widgets/captcha/CCaptcha.php',
  579. 'CCaptchaAction' => '/web/widgets/captcha/CCaptchaAction.php',
  580. 'CBasePager' => '/web/widgets/pagers/CBasePager.php',
  581. 'CLinkPager' => '/web/widgets/pagers/CLinkPager.php',
  582. 'CListPager' => '/web/widgets/pagers/CListPager.php',
  583. );
  584. }
  585. spl_autoload_register(array('YiiBase','autoload'));
  586. if(!class_exists('YiiBase', false))
  587. require(dirname(__FILE__).'/YiiBase.php');
  588. class Yii extends YiiBase
  589. {
  590. }
  591. class CComponent
  592. {
  593. private $_e;
  594. private $_m;
  595. public function __get($name)
  596. {
  597. $getter='get'.$name;
  598. if(method_exists($this,$getter))
  599. return $this->$getter();
  600. elseif(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  601. {
  602. // duplicating getEventHandlers() here for performance
  603. $name=strtolower($name);
  604. if(!isset($this->_e[$name]))
  605. $this->_e[$name]=new CList;
  606. return $this->_e[$name];
  607. }
  608. elseif(isset($this->_m[$name]))
  609. return $this->_m[$name];
  610. elseif(is_array($this->_m))
  611. {
  612. foreach($this->_m as $object)
  613. {
  614. if($object->getEnabled() && (property_exists($object,$name) || $object->canGetProperty($name)))
  615. return $object->$name;
  616. }
  617. }
  618. throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',
  619. array('{class}'=>get_class($this), '{property}'=>$name)));
  620. }
  621. public function __set($name,$value)
  622. {
  623. $setter='set'.$name;
  624. if(method_exists($this,$setter))
  625. return $this->$setter($value);
  626. elseif(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  627. {
  628. // duplicating getEventHandlers() here for performance
  629. $name=strtolower($name);
  630. if(!isset($this->_e[$name]))
  631. $this->_e[$name]=new CList;
  632. return $this->_e[$name]->add($value);
  633. }
  634. elseif(is_array($this->_m))
  635. {
  636. foreach($this->_m as $object)
  637. {
  638. if($object->getEnabled() && (property_exists($object,$name) || $object->canSetProperty($name)))
  639. return $object->$name=$value;
  640. }
  641. }
  642. if(method_exists($this,'get'.$name))
  643. throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.',
  644. array('{class}'=>get_class($this), '{property}'=>$name)));
  645. else
  646. throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',
  647. array('{class}'=>get_class($this), '{property}'=>$name)));
  648. }
  649. public function __isset($name)
  650. {
  651. $getter='get'.$name;
  652. if(method_exists($this,$getter))
  653. return $this->$getter()!==null;
  654. elseif(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  655. {
  656. $name=strtolower($name);
  657. return isset($this->_e[$name]) && $this->_e[$name]->getCount();
  658. }
  659. elseif(is_array($this->_m))
  660. {
  661. if(isset($this->_m[$name]))
  662. return true;
  663. foreach($this->_m as $object)
  664. {
  665. if($object->getEnabled() && (property_exists($object,$name) || $object->canGetProperty($name)))
  666. return $object->$name!==null;
  667. }
  668. }
  669. return false;
  670. }
  671. public function __unset($name)
  672. {
  673. $setter='set'.$name;
  674. if(method_exists($this,$setter))
  675. $this->$setter(null);
  676. elseif(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  677. unset($this->_e[strtolower($name)]);
  678. elseif(is_array($this->_m))
  679. {
  680. if(isset($this->_m[$name]))
  681. $this->detachBehavior($name);
  682. else
  683. {
  684. foreach($this->_m as $object)
  685. {
  686. if($object->getEnabled())
  687. {
  688. if(property_exists($object,$name))
  689. return $object->$name=null;
  690. elseif($object->canSetProperty($name))
  691. return $object->$setter(null);
  692. }
  693. }
  694. }
  695. }
  696. elseif(method_exists($this,'get'.$name))
  697. throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.',
  698. array('{class}'=>get_class($this), '{property}'=>$name)));
  699. }
  700. public function __call($name,$parameters)
  701. {
  702. if($this->_m!==null)
  703. {
  704. foreach($this->_m as $object)
  705. {
  706. if($object->getEnabled() && method_exists($object,$name))
  707. return call_user_func_array(array($object,$name),$parameters);
  708. }
  709. }
  710. if(class_exists('Closure', false) && ($this->canGetProperty($name) || property_exists($this, $name)) && $this->$name instanceof Closure)
  711. return call_user_func_array($this->$name, $parameters);
  712. throw new CException(Yii::t('yii','{class} and its behaviors do not have a method or closure named "{name}".',
  713. array('{class}'=>get_class($this), '{name}'=>$name)));
  714. }
  715. public function asa($behavior)
  716. {
  717. return isset($this->_m[$behavior]) ? $this->_m[$behavior] : null;
  718. }
  719. public function attachBehaviors($behaviors)
  720. {
  721. foreach($behaviors as $name=>$behavior)
  722. $this->attachBehavior($name,$behavior);
  723. }
  724. public function detachBehaviors()
  725. {
  726. if($this->_m!==null)
  727. {
  728. foreach($this->_m as $name=>$behavior)
  729. $this->detachBehavior($name);
  730. $this->_m=null;
  731. }
  732. }
  733. public function attachBehavior($name,$behavior)
  734. {
  735. if(!($behavior instanceof IBehavior))
  736. $behavior=Yii::createComponent($behavior);
  737. $behavior->setEnabled(true);
  738. $behavior->attach($this);
  739. return $this->_m[$name]=$behavior;
  740. }
  741. public function detachBehavior($name)
  742. {
  743. if(isset($this->_m[$name]))
  744. {
  745. $this->_m[$name]->detach($this);
  746. $behavior=$this->_m[$name];
  747. unset($this->_m[$name]);
  748. return $behavior;
  749. }
  750. }
  751. public function enableBehaviors()
  752. {
  753. if($this->_m!==null)
  754. {
  755. foreach($this->_m as $behavior)
  756. $behavior->setEnabled(true);
  757. }
  758. }
  759. public function disableBehaviors()
  760. {
  761. if($this->_m!==null)
  762. {
  763. foreach($this->_m as $behavior)
  764. $behavior->setEnabled(false);
  765. }
  766. }
  767. public function enableBehavior($name)
  768. {
  769. if(isset($this->_m[$name]))
  770. $this->_m[$name]->setEnabled(true);
  771. }
  772. public function disableBehavior($name)
  773. {
  774. if(isset($this->_m[$name]))
  775. $this->_m[$name]->setEnabled(false);
  776. }
  777. public function hasProperty($name)
  778. {
  779. return method_exists($this,'get'.$name) || method_exists($this,'set'.$name);
  780. }
  781. public function canGetProperty($name)
  782. {
  783. return method_exists($this,'get'.$name);
  784. }
  785. public function canSetProperty($name)
  786. {
  787. return method_exists($this,'set'.$name);
  788. }
  789. public function hasEvent($name)
  790. {
  791. return !strncasecmp($name,'on',2) && method_exists($this,$name);
  792. }
  793. public function hasEventHandler($name)
  794. {
  795. $name=strtolower($name);
  796. return isset($this->_e[$name]) && $this->_e[$name]->getCount()>0;
  797. }
  798. public function getEventHandlers($name)
  799. {
  800. if($this->hasEvent($name))
  801. {
  802. $name=strtolower($name);
  803. if(!isset($this->_e[$name]))
  804. $this->_e[$name]=new CList;
  805. return $this->_e[$name];
  806. }
  807. else
  808. throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.',
  809. array('{class}'=>get_class($this), '{event}'=>$name)));
  810. }
  811. public function attachEventHandler($name,$handler)
  812. {
  813. $this->getEventHandlers($name)->add($handler);
  814. }
  815. public function detachEventHandler($name,$handler)
  816. {
  817. if($this->hasEventHandler($name))
  818. return $this->getEventHandlers($name)->remove($handler)!==false;
  819. else
  820. return false;
  821. }
  822. public function raiseEvent($name,$event)
  823. {
  824. $name=strtolower($name);
  825. if(isset($this->_e[$name]))
  826. {
  827. foreach($this->_e[$name] as $handler)
  828. {
  829. if(is_string($handler))
  830. call_user_func($handler,$event);
  831. elseif(is_callable($handler,true))
  832. {
  833. if(is_array($handler))
  834. {
  835. // an array: 0 - object, 1 - method name
  836. list($object,$method)=$handler;
  837. if(is_string($object)) // static method call
  838. call_user_func($handler,$event);
  839. elseif(method_exists($object,$method))
  840. $object->$method($event);
  841. else
  842. throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".',
  843. array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>$handler[1])));
  844. }
  845. else // PHP 5.3: anonymous function
  846. call_user_func($handler,$event);
  847. }
  848. else
  849. throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".',
  850. array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>gettype($handler))));
  851. // stop further handling if param.handled is set true
  852. if(($event instanceof CEvent) && $event->handled)
  853. return;
  854. }
  855. }
  856. elseif(YII_DEBUG && !$this->hasEvent($name))
  857. throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.',
  858. array('{class}'=>get_class($this), '{event}'=>$name)));
  859. }
  860. public function evaluateExpression($_expression_,$_data_=array())
  861. {
  862. if(is_string($_expression_))
  863. {
  864. extract($_data_);
  865. return eval('return '.$_expression_.';');
  866. }
  867. else
  868. {
  869. $_data_[]=$this;
  870. return call_user_func_array($_expression_, $_data_);
  871. }
  872. }
  873. }
  874. class CEvent extends CComponent
  875. {
  876. public $sender;
  877. public $handled=false;
  878. public $params;
  879. public function __construct($sender=null,$params=null)
  880. {
  881. $this->sender=$sender;
  882. $this->params=$params;
  883. }
  884. }
  885. class CEnumerable
  886. {
  887. }
  888. abstract class CModule extends CComponent
  889. {
  890. public $preload=array();
  891. public $behaviors=array();
  892. private $_id;
  893. private $_parentModule;
  894. private $_basePath;
  895. private $_modulePath;
  896. private $_params;
  897. private $_modules=array();
  898. private $_moduleConfig=array();
  899. private $_components=array();
  900. private $_componentConfig=array();
  901. public function __construct($id,$parent,$config=null)
  902. {
  903. $this->_id=$id;
  904. $this->_parentModule=$parent;
  905. // set basePath at early as possible to avoid trouble
  906. if(is_string($config))
  907. $config=require($config);
  908. if(isset($config['basePath']))
  909. {
  910. $this->setBasePath($config['basePath']);
  911. unset($config['basePath']);
  912. }
  913. Yii::setPathOfAlias($id,$this->getBasePath());
  914. $this->preinit();
  915. $this->configure($config);
  916. $this->attachBehaviors($this->behaviors);
  917. $this->preloadComponents();
  918. $this->init();
  919. }
  920. public function __get($name)
  921. {
  922. if($this->hasComponent($name))
  923. return $this->getComponent($name);
  924. else
  925. return parent::__get($name);
  926. }
  927. public function __isset($name)
  928. {
  929. if($this->hasComponent($name))
  930. return $this->getComponent($name)!==null;
  931. else
  932. return parent::__isset($name);
  933. }
  934. public function getId()
  935. {
  936. return $this->_id;
  937. }
  938. public function setId($id)
  939. {
  940. $this->_id=$id;
  941. }
  942. public function getBasePath()
  943. {
  944. if($this->_basePath===null)
  945. {
  946. $class=new ReflectionClass(get_class($this));
  947. $this->_basePath=dirname($class->getFileName());
  948. }
  949. return $this->_basePath;
  950. }
  951. public function setBasePath($path)
  952. {
  953. if(($this->_basePath=realpath($path))===false || !is_dir($this->_basePath))
  954. throw new CException(Yii::t('yii','Base path "{path}" is not a valid directory.',
  955. array('{path}'=>$path)));
  956. }
  957. public function getParams()
  958. {
  959. if($this->_params!==null)
  960. return $this->_params;
  961. else
  962. {
  963. $this->_params=new CAttributeCollection;
  964. $this->_params->caseSensitive=true;
  965. return $this->_params;
  966. }
  967. }
  968. public function setParams($value)
  969. {
  970. $params=$this->getParams();
  971. foreach($value as $k=>$v)
  972. $params->add($k,$v);
  973. }
  974. public function getModulePath()
  975. {
  976. if($this->_modulePath!==null)
  977. return $this->_modulePath;
  978. else
  979. return $this->_modulePath=$this->getBasePath().DIRECTORY_SEPARATOR.'modules';
  980. }
  981. public function setModulePath($value)
  982. {
  983. if(($this->_modulePath=realpath($value))===false || !is_dir($this->_modulePath))
  984. throw new CException(Yii::t('yii','The module path "{path}" is not a valid directory.',
  985. array('{path}'=>$value)));
  986. }
  987. public function setImport($aliases)
  988. {
  989. foreach($aliases as $alias)
  990. Yii::import($alias);
  991. }
  992. public function setAliases($mappings)
  993. {
  994. foreach($mappings as $name=>$alias)
  995. {
  996. if(($path=Yii::getPathOfAlias($alias))!==false)
  997. Yii::setPathOfAlias($name,$path);
  998. else
  999. Yii::setPathOfAlias($name,$alias);
  1000. }
  1001. }
  1002. public function getParentModule()
  1003. {
  1004. return $this->_parentModule;
  1005. }
  1006. public function getModule($id)
  1007. {
  1008. if(isset($this->_modules[$id]) || array_key_exists($id,$this->_modules))
  1009. return $this->_modules[$id];
  1010. elseif(isset($this->_moduleConfig[$id]))
  1011. {
  1012. $config=$this->_moduleConfig[$id];
  1013. if(!isset($config['enabled']) || $config['enabled'])
  1014. {
  1015. $class=$config['class'];
  1016. unset($config['class'], $config['enabled']);
  1017. if($this===Yii::app())
  1018. $module=Yii::createComponent($class,$id,null,$config);
  1019. else
  1020. $module=Yii::createComponent($class,$this->getId().'/'.$id,$this,$config);
  1021. return $this->_modules[$id]=$module;
  1022. }
  1023. }
  1024. }
  1025. public function hasModule($id)
  1026. {
  1027. return isset($this->_moduleConfig[$id]) || isset($this->_modules[$id]);
  1028. }
  1029. public function getModules()
  1030. {
  1031. return $this->_moduleConfig;
  1032. }
  1033. public function setModules($modules,$merge=true)
  1034. {
  1035. foreach($modules as $id=>$module)
  1036. {
  1037. if(is_int($id))
  1038. {
  1039. $id=$module;
  1040. $module=array();
  1041. }
  1042. if(isset($this->_moduleConfig[$id]) && $merge)
  1043. $this->_moduleConfig[$id]=CMap::mergeArray($this->_moduleConfig[$id],$module);
  1044. else
  1045. {
  1046. if(!isset($module['class']))
  1047. {
  1048. if (Yii::getPathOfAlias($id)===false)
  1049. Yii::setPathOfAlias($id,$this->getModulePath().DIRECTORY_SEPARATOR.$id);
  1050. $module['class']=$id.'.'.ucfirst($id).'Module';
  1051. }
  1052. $this->_moduleConfig[$id]=$module;
  1053. }
  1054. }
  1055. }
  1056. public function hasComponent($id)
  1057. {
  1058. return isset($this->_components[$id]) || isset($this->_componentConfig[$id]);
  1059. }
  1060. public function getComponent($id,$createIfNull=true)
  1061. {
  1062. if(isset($this->_components[$id]))
  1063. return $this->_components[$id];
  1064. elseif(isset($this->_componentConfig[$id]) && $createIfNull)
  1065. {
  1066. $config=$this->_componentConfig[$id];
  1067. if(!isset($config['enabled']) || $config['enabled'])
  1068. {
  1069. unset($config['enabled']);
  1070. $component=Yii::createComponent($config);
  1071. $component->init();
  1072. return $this->_components[$id]=$component;
  1073. }
  1074. }
  1075. }
  1076. public function setComponent($id,$component,$merge=true)
  1077. {
  1078. if($component===null)
  1079. {
  1080. unset($this->_components[$id]);
  1081. return;
  1082. }
  1083. elseif($component instanceof IApplicationComponent)
  1084. {
  1085. $this->_components[$id]=$component;
  1086. if(!$component->getIsInitialized())
  1087. $component->init();
  1088. return;
  1089. }
  1090. elseif(isset($this->_components[$id]))
  1091. {
  1092. if(isset($component['class']) && get_class($this->_components[$id])!==$component['class'])
  1093. {
  1094. unset($this->_components[$id]);
  1095. $this->_componentConfig[$id]=$component; //we should ignore merge here
  1096. return;
  1097. }
  1098. foreach($component as $key=>$value)
  1099. {
  1100. if($key!=='class')
  1101. $this->_components[$id]->$key=$value;
  1102. }
  1103. }
  1104. elseif(isset($this->_componentConfig[$id]['class'],$component['class'])
  1105. && $this->_componentConfig[$id]['class']!==$component['class'])
  1106. {
  1107. $this->_componentConfig[$id]=$component; //we should ignore merge here
  1108. return;
  1109. }
  1110. if(isset($this->_componentConfig[$id]) && $merge)
  1111. $this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$component);
  1112. else
  1113. $this->_componentConfig[$id]=$component;
  1114. }
  1115. public function getComponents($loadedOnly=true)
  1116. {
  1117. if($loadedOnly)
  1118. return $this->_components;
  1119. else
  1120. return array_merge($this->_componentConfig, $this->_components);
  1121. }
  1122. public function setComponents($components,$merge=true)
  1123. {
  1124. foreach($components as $id=>$component)
  1125. $this->setComponent($id,$component,$merge);
  1126. }
  1127. public function configure($config)
  1128. {
  1129. if(is_array($config))
  1130. {
  1131. foreach($config as $key=>$value)
  1132. $this->$key=$value;
  1133. }
  1134. }
  1135. protected function preloadComponents()
  1136. {
  1137. foreach($this->preload as $id)
  1138. $this->getComponent($id);
  1139. }
  1140. protected function preinit()
  1141. {
  1142. }
  1143. protected function init()
  1144. {
  1145. }
  1146. }
  1147. abstract class CApplication extends CModule
  1148. {
  1149. public $name='My Application';
  1150. public $charset='UTF-8';
  1151. public $sourceLanguage='en_us';
  1152. public $localeClass='CLocale';
  1153. private $_id;
  1154. private $_basePath;
  1155. private $_runtimePath;
  1156. private $_extensionPath;
  1157. private $_globalState;
  1158. private $_stateChanged;
  1159. private $_ended=false;
  1160. private $_language;
  1161. private $_homeUrl;
  1162. abstract public function processRequest();
  1163. public function __construct($config=null)
  1164. {
  1165. Yii::setApplication($this);
  1166. // set basePath at early as possible to avoid trouble
  1167. if(is_string($config))
  1168. $config=require($config);
  1169. if(isset($config['basePath']))
  1170. {
  1171. $this->setBasePath($config['basePath']);
  1172. unset($config['basePath']);
  1173. }
  1174. else
  1175. $this->setBasePath('protected');
  1176. Yii::setPathOfAlias('application',$this->getBasePath());
  1177. Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
  1178. if(isset($config['extensionPath']))
  1179. {
  1180. $this->setExtensionPath($config['extensionPath']);
  1181. unset($config['extensionPath']);
  1182. }
  1183. else
  1184. Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');
  1185. if(isset($config['aliases']))
  1186. {
  1187. $this->setAliases($config['aliases']);
  1188. unset($config['aliases']);
  1189. }
  1190. $this->preinit();
  1191. $this->initSystemHandlers();
  1192. $this->registerCoreComponents();
  1193. $this->configure($config);
  1194. $this->attachBehaviors($this->behaviors);
  1195. $this->preloadComponents();
  1196. $this->init();
  1197. }
  1198. public function run()
  1199. {
  1200. if($this->hasEventHandler('onBeginRequest'))
  1201. $this->onBeginRequest(new CEvent($this));
  1202. register_shutdown_function(array($this,'end'),0,false);
  1203. $this->processRequest();
  1204. if($this->hasEventHandler('onEndRequest'))
  1205. $this->onEndRequest(new CEvent($this));
  1206. }
  1207. public function end($status=0,$exit=true)
  1208. {
  1209. if($this->hasEventHandler('onEndRequest'))
  1210. $this->onEndRequest(new CEvent($this));
  1211. if($exit)
  1212. exit($status);
  1213. }
  1214. public function onBeginRequest($event)
  1215. {
  1216. $this->raiseEvent('onBeginRequest',$event);
  1217. }
  1218. public function onEndRequest($event)
  1219. {
  1220. if(!$this->_ended)
  1221. {
  1222. $this->_ended=true;
  1223. $this->raiseEvent('onEndRequest',$event);
  1224. }
  1225. }
  1226. public function getId()
  1227. {
  1228. if($this->_id!==null)
  1229. return $this->_id;
  1230. else
  1231. return $this->_id=sprintf('%x',crc32($this->getBasePath().$this->name));
  1232. }
  1233. public function setId($id)
  1234. {
  1235. $this->_id=$id;
  1236. }
  1237. public function getBasePath()
  1238. {
  1239. return $this->_basePath;
  1240. }
  1241. public function setBasePath($path)
  1242. {
  1243. if(($this->_basePath=realpath($path))===false || !is_dir($this->_basePath))
  1244. throw new CException(Yii::t('yii','Application base path "{path}" is not a valid directory.',
  1245. array('{path}'=>$path)));
  1246. }
  1247. public function getRuntimePath()
  1248. {
  1249. if($this->_runtimePath!==null)
  1250. return $this->_runtimePath;
  1251. else
  1252. {
  1253. $this->setRuntimePath($this->getBasePath().DIRECTORY_SEPARATOR.'runtime');
  1254. return $this->_runtimePath;
  1255. }
  1256. }
  1257. public function setRuntimePath($path)
  1258. {
  1259. if(($runtimePath=realpath($path))===false || !is_dir($runtimePath) || !is_writable($runtimePath))
  1260. 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.',
  1261. array('{path}'=>$path)));
  1262. $this->_runtimePath=$runtimePath;
  1263. }
  1264. public function getExtensionPath()
  1265. {
  1266. return Yii::getPathOfAlias('ext');
  1267. }
  1268. public function setExtensionPath($path)
  1269. {
  1270. if(($extensionPath=realpath($path))===false || !is_dir($extensionPath))
  1271. throw new CException(Yii::t('yii','Extension path "{path}" does not exist.',
  1272. array('{path}'=>$path)));
  1273. Yii::setPathOfAlias('ext',$extensionPath);
  1274. }
  1275. public function getLanguage()
  1276. {
  1277. return $this->_language===null ? $this->sourceLanguage : $this->_language;
  1278. }
  1279. public function setLanguage($language)
  1280. {
  1281. $this->_language=$language;
  1282. }
  1283. public function getTimeZone()
  1284. {
  1285. return date_default_timezone_get();
  1286. }
  1287. public function setTimeZone($value)
  1288. {
  1289. date_default_timezone_set($value);
  1290. }
  1291. public function findLocalizedFile($srcFile,$srcLanguage=null,$language=null)
  1292. {
  1293. if($srcLanguage===null)
  1294. $srcLanguage=$this->sourceLanguage;
  1295. if($language===null)
  1296. $language=$this->getLanguage();
  1297. if($language===$srcLanguage)
  1298. return $srcFile;
  1299. $desiredFile=dirname($srcFile).DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.basename($srcFile);
  1300. return is_file($desiredFile) ? $desiredFile : $srcFile;
  1301. }
  1302. public function getLocale($localeID=null)
  1303. {
  1304. return call_user_func_array(array($this->localeClass, 'getInstance'),array($localeID===null?$this->getLanguage():$localeID));
  1305. }
  1306. public function getLocaleDataPath()
  1307. {
  1308. $vars=get_class_vars($this->localeClass);
  1309. if(empty($vars['dataPath']))
  1310. return Yii::getPathOfAlias('system.i18n.data');
  1311. return $vars['dataPath'];
  1312. }
  1313. public function setLocaleDataPath($value)
  1314. {
  1315. $property=new ReflectionProperty($this->localeClass,'dataPath');
  1316. $property->setValue($value);
  1317. }
  1318. public function getNumberFormatter()
  1319. {
  1320. return $this->getLocale()->getNumberFormatter();
  1321. }
  1322. public function getDateFormatter()
  1323. {
  1324. return $this->getLocale()->getDateFormatter();
  1325. }
  1326. public function getDb()
  1327. {
  1328. return $this->getComponent('db');
  1329. }
  1330. public function getErrorHandler()
  1331. {
  1332. return $this->getComponent('errorHandler');
  1333. }
  1334. public function getSecurityManager()
  1335. {
  1336. return $this->getComponent('securityManager');
  1337. }
  1338. public function getStatePersister()
  1339. {
  1340. return $this->getComponent('statePersister');
  1341. }
  1342. public function getCache()
  1343. {
  1344. return $this->getComponent('cache');
  1345. }
  1346. public function getCoreMessages()
  1347. {
  1348. return $this->getComponent('coreMessages');
  1349. }
  1350. public function getMessages()
  1351. {
  1352. return $this->getComponent('messages');
  1353. }
  1354. public function getRequest()
  1355. {
  1356. return $this->getComponent('request');
  1357. }
  1358. public function getUrlManager()
  1359. {
  1360. return $this->getComponent('urlManager');
  1361. }
  1362. public function getFormat()
  1363. {
  1364. return $this->getComponent('format');
  1365. }
  1366. public function getController()
  1367. {
  1368. return null;
  1369. }
  1370. public function createUrl($route,$params=array(),$ampersand='&')
  1371. {
  1372. return $this->getUrlManager()->createUrl($route,$params,$ampersand);
  1373. }
  1374. public function createAbsoluteUrl($route,$params=array(),$schema='',$ampersand='&')
  1375. {
  1376. $url=$this->createUrl($route,$params,$ampersand);
  1377. if(strpos($url,'http')===0)
  1378. return $url;
  1379. else
  1380. return $this->getRequest()->getHostInfo($schema).$url;
  1381. }
  1382. public function getBaseUrl($absolute=false)
  1383. {
  1384. return $this->getRequest()->getBaseUrl($absolute);
  1385. }
  1386. public function getHomeUrl()
  1387. {
  1388. if($this->_homeUrl===null)
  1389. {
  1390. if($this->getUrlManager()->showScriptName)
  1391. return $this->getRequest()->getScriptUrl();
  1392. else
  1393. return $this->getRequest()->getBaseUrl().'/';
  1394. }
  1395. else
  1396. return $this->_homeUrl;
  1397. }
  1398. public function setHomeUrl($value)
  1399. {
  1400. $this->_homeUrl=$value;
  1401. }
  1402. public function getGlobalState($key,$defaultValue=null)
  1403. {
  1404. if($this->_globalState===null)
  1405. $this->loadGlobalState();
  1406. if(isset($this->_globalState[$key]))
  1407. return $this->_globalState[$key];
  1408. else
  1409. return $defaultValue;
  1410. }
  1411. public function setGlobalState($key,$value,$defaultValue=null)
  1412. {
  1413. if($this->_globalState===null)
  1414. $this->loadGlobalState();
  1415. $changed=$this->_stateChanged;
  1416. if($value===$defaultValue)
  1417. {
  1418. if(isset($this->_globalState[$key]))
  1419. {
  1420. unset($this->_globalState[$key]);
  1421. $this->_stateChanged=true;
  1422. }
  1423. }
  1424. elseif(!isset($this->_globalState[$key]) || $this->_globalState[$key]!==$value)
  1425. {
  1426. $this->_globalState[$key]=$value;
  1427. $this->_stateChanged=true;
  1428. }
  1429. if($this->_stateChanged!==$changed)
  1430. $this->attachEventHandler('onEndRequest',array($this,'saveGlobalState'));
  1431. }
  1432. public function clearGlobalState($key)
  1433. {
  1434. $this->setGlobalState($key,true,true);
  1435. }
  1436. public function loadGlobalState()
  1437. {
  1438. $persister=$this->getStatePersister();
  1439. if(($this->_globalState=$persister->load())===null)
  1440. $this->_globalState=array();
  1441. $this->_stateChanged=false;
  1442. $this->detachEventHandler('onEndRequest',array($this,'saveGlobalState'));
  1443. }
  1444. public function saveGlobalState()
  1445. {
  1446. if($this->_stateChanged)
  1447. {
  1448. $this->_stateChanged=false;
  1449. $this->detachEventHandler('onEndRequest',array($this,'saveGlobalState'));
  1450. $this->getStatePersister()->save($this->_globalState);
  1451. }
  1452. }
  1453. public function handleException($exception)
  1454. {
  1455. // disable error capturing to avoid recursive errors
  1456. restore_error_handler();
  1457. restore_exception_handler();
  1458. $category='exception.'.get_class($exception);
  1459. if($exception instanceof CHttpException)
  1460. $category.='.'.$exception->statusCode;
  1461. // php <5.2 doesn't support string conversion auto-magically
  1462. $message=$exception->__toString();
  1463. if(isset($_SERVER['REQUEST_URI']))
  1464. $message.="\nREQUEST_URI=".$_SERVER['REQUEST_URI'];
  1465. if(isset($_SERVER['HTTP_REFERER']))
  1466. $message.="\nHTTP_REFERER=".$_SERVER['HTTP_REFERER'];
  1467. $message.="\n---";
  1468. Yii::log($message,CLogger::LEVEL_ERROR,$category);
  1469. try
  1470. {
  1471. $event=new CExceptionEvent($this,$exception);
  1472. $this->onException($event);
  1473. if(!$event->handled)
  1474. {
  1475. // try an error handler
  1476. if(($handler=$this->getErrorHandler())!==null)
  1477. $handler->handle($event);
  1478. else
  1479. $this->displayException($exception);
  1480. }
  1481. }
  1482. catch(Exception $e)
  1483. {
  1484. $this->displayException($e);
  1485. }
  1486. try
  1487. {
  1488. $this->end(1);
  1489. }
  1490. catch(Exception $e)
  1491. {
  1492. // use the most primitive way to log error
  1493. $msg = get_class($e).': '.$e->getMessage().' ('.$e->getFile().':'.$e->getLine().")\n";
  1494. $msg .= $e->getTraceAsString()."\n";
  1495. $msg .= "Previous exception:\n";
  1496. $msg .= get_class($exception).': '.$exception->getMessage().' ('.$exception->getFile().':'.$exception->getLine().")\n";
  1497. $msg .= $exception->getTraceAsString()."\n";
  1498. $msg .= '$_SERVER='.var_export($_SERVER,true);
  1499. error_log($msg);
  1500. exit(1);
  1501. }
  1502. }
  1503. public function handleError($code,$message,$file,$line)
  1504. {
  1505. if($code & error_reporting())
  1506. {
  1507. // disable error capturing to avoid recursive errors
  1508. restore_error_handler();
  1509. restore_exception_handler();
  1510. $log="$message ($file:$line)\nStack trace:\n";
  1511. $trace=debug_backtrace();
  1512. // skip the first 3 stacks as they do not tell the error position
  1513. if(count($trace)>3)
  1514. $trace=array_slice($trace,3);
  1515. foreach($trace as $i=>$t)
  1516. {
  1517. if(!isset($t['file']))
  1518. $t['file']='unknown';
  1519. if(!isset($t['line']))
  1520. $t['line']=0;
  1521. if(!isset($t['function']))
  1522. $t['function']='unknown';
  1523. $log.="#$i {$t['file']}({$t['line']}): ";
  1524. if(isset($t['object']) && is_object($t['object']))
  1525. $log.=get_class($t['object']).'->';
  1526. $log.="{$t['function']}()\n";
  1527. }
  1528. if(isset($_SERVER['REQUEST_URI']))
  1529. $log.='REQUEST_URI='.$_SERVER['REQUEST_URI'];
  1530. Yii::log($log,CLogger::LEVEL_ERROR,'php');
  1531. try
  1532. {
  1533. Yii::import('CErrorEvent',true);
  1534. $event=new CErrorEvent($this,$code,$message,$file,$line);
  1535. $this->onError($event);
  1536. if(!$event->handled)
  1537. {
  1538. // try an error handler
  1539. if(($handler=$this->getErrorHandler())!==null)
  1540. $handler->handle($event);
  1541. else
  1542. $this->displayError($code,$message,$file,$line);
  1543. }
  1544. }
  1545. catch(Exception $e)
  1546. {
  1547. $this->displayException($e);
  1548. }
  1549. try
  1550. {
  1551. $this->end(1);
  1552. }
  1553. catch(Exception $e)
  1554. {
  1555. // use the most primitive way to log error
  1556. $msg = get_class($e).': '.$e->getMessage().' ('.$e->getFile().':'.$e->getLine().")\n";
  1557. $msg .= $e->getTraceAsString()."\n";
  1558. $msg .= "Previous error:\n";
  1559. $msg .= $log."\n";
  1560. $msg .= '$_SERVER='.var_export($_SERVER,true);
  1561. error_log($msg);
  1562. exit(1);
  1563. }
  1564. }
  1565. }
  1566. public function onException($event)
  1567. {
  1568. $thi

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