PageRenderTime 60ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 2ms

/framework/yiilite.php

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

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