1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: require(dirname(__FILE__).'/YiiBase.php');
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24:
25: class Yii extends YiiBase
26: {
27: public static $paths = array();
28: public static $systemuser;
29: public static $translationLog = array();
30: protected static $rootPath;
31:
32: 33: 34: 35: 36:
37: private static $_scriptFile;
38: private static function getScriptFile()
39: {
40: if(self::$_scriptFile!==null)
41: return self::$_scriptFile;
42: else
43: return self::$_scriptFile=realpath($_SERVER['SCRIPT_FILENAME']);
44: }
45:
46: 47: 48: 49:
50: public static function getRootPath() {
51: if (YII_UNIT_TESTING) {
52:
53: $path = array ();
54: exec ('pwd', $path);
55: self::$rootPath = dirname (preg_replace ('/\/tests/', '', $path[0]));
56: } elseif (!isset(self::$rootPath)) {
57: self::$rootPath = dirname(self::getScriptFile ());
58: }
59:
60: return self::$rootPath;
61: }
62:
63: 64: 65: 66: 67:
68: public static function createWebApplication($config=null) {
69: require(implode(DIRECTORY_SEPARATOR,array(
70: __DIR__,
71: '..',
72: 'protected',
73: 'components',
74: 'X2WebApplication.php'
75: )));
76: return parent::createApplication('X2WebApplication',$config);
77: }
78:
79: 80: 81: 82: 83: 84:
85: public static function getCustomPath($path) {
86:
87: $customPath = str_replace(
88: self::getRootPath(),self::getRootPath().DIRECTORY_SEPARATOR.'custom',$path);
89:
90: if(file_exists($customPath))
91: $path = $customPath;
92: return $path;
93: }
94:
95: 96: 97: 98: 99: 100:
101: public static function resetCustomPath($customPath) {
102: return str_replace(
103: self::getRootPath().DIRECTORY_SEPARATOR.'custom',self::getRootPath(),$customPath);
104: }
105:
106: 107: 108: 109: 110:
111: public static function import($alias,$forceInclude=false)
112: {
113: if(isset(self::$_imports[$alias]))
114: return self::$_imports[$alias];
115:
116: if(class_exists($alias,false) || interface_exists($alias,false))
117: return self::$_imports[$alias]=$alias;
118:
119: if(($pos=strrpos($alias,'\\'))!==false)
120: {
121: $namespace=str_replace('\\','.',ltrim(substr($alias,0,$pos),'\\'));
122: if(($path=self::getPathOfAlias($namespace))!==false)
123: {
124: $classFile=$path.DIRECTORY_SEPARATOR.substr($alias,$pos+1).'.php';
125: if($forceInclude)
126: {
127: if(is_file($classFile))
128:
129: require(self::getCustomPath ($classFile));
130:
131: else
132: 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)));
133: self::$_imports[$alias]=$alias;
134: }
135: else
136: self::$classMap[$alias]=$classFile;
137: return $alias;
138: }
139: else
140: {
141:
142: if (class_exists($alias,true))
143: return self::$_imports[$alias]=$alias;
144: else
145: throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing directory or file.',
146: array('{alias}'=>$namespace)));
147: }
148: }
149:
150: if(($pos=strrpos($alias,'.'))===false)
151: {
152:
153:
154: if($forceInclude && (self::x2_autoload($alias,true) || class_exists($alias,true)))
155:
156: self::$_imports[$alias]=$alias;
157: return $alias;
158: }
159:
160: $className=(string)substr($alias,$pos+1);
161: $isClass=$className!=='*';
162:
163: if($isClass && (class_exists($className,false) || interface_exists($className,false)))
164: return self::$_imports[$alias]=$className;
165:
166: if(($path=self::getPathOfAlias($alias))!==false)
167: {
168: if($isClass)
169: {
170: if($forceInclude)
171: {
172: if(is_file($path.'.php'))
173:
174: require(self::getCustomPath ($path.'.php'));
175:
176: else
177: 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)));
178: self::$_imports[$alias]=$className;
179: }
180: else
181: self::$classMap[$className]=$path.'.php';
182: return $className;
183: }
184: else
185: {
186: if(self::$_includePaths===null)
187: {
188: self::$_includePaths=array_unique(explode(PATH_SEPARATOR,get_include_path()));
189: if(($pos=array_search('.',self::$_includePaths,true))!==false)
190: unset(self::$_includePaths[$pos]);
191: }
192:
193: array_unshift(self::$_includePaths,$path);
194:
195: if(self::$enableIncludePath && set_include_path('.'.PATH_SEPARATOR.implode(PATH_SEPARATOR,self::$_includePaths))===false)
196: self::$enableIncludePath=false;
197:
198: return self::$_imports[$alias]=$path;
199: }
200: }
201: else
202: throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing directory or file.',
203: array('{alias}'=>$alias)));
204: }
205:
206: 207: 208: 209: 210:
211: public static function x2_autoload($className,$classMapOnly=false)
212: {
213:
214: if(isset(self::$classMap[$className]))
215:
216: include(self::getCustomPath(self::$classMap[$className]));
217:
218: elseif(isset(self::$_coreClasses[$className]))
219: include(YII_PATH.self::$_coreClasses[$className]);
220: elseif($classMapOnly)
221: return false;
222: else
223: {
224:
225: if(strpos($className,'\\')===false)
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:
235: include(self::getCustomPath ($classFile));
236:
237: if(YII_DEBUG && basename(realpath($classFile))!==$className.'.php')
238: throw new CException(Yii::t('yii','Class name "{class}" does not match class file "{file}".', array(
239: '{class}'=>$className,
240: '{file}'=>$classFile,
241: )));
242: break;
243: }
244: }
245: }
246: else
247:
248: include(self::getCustomPath ($className.'.php'));
249:
250: }
251: else
252: {
253: $namespace=str_replace('\\','.',ltrim($className,'\\'));
254: if(($path=self::getPathOfAlias($namespace))!==false)
255:
256: include(self::getCustomPath ($path.'.php'));
257:
258: else
259: return false;
260: }
261: return class_exists($className,false) || interface_exists($className,false);
262: }
263: return true;
264: }
265:
266: public static function t($category,$message,$params=array(),$source=null,$language=null) {
267: X2_TRANSLATION_LOGGING && Yii::logTranslation($category, $message);
268: if(isset($_GET['t']) && $_GET['t'])
269: return '<dt class="yii-t">'
270: .CHtml::hiddenField('cat',$category)
271: .CHtml::hiddenField('msg',$message)
272: .parent::t($category,$message,$params,$source,$language)
273: .'</dt>';
274:
275: else
276: return parent::t($category,$message,$params,$source,$language);
277: }
278:
279: public static function logTranslation($category, $message){
280: if(!isset(Yii::$translationLog[$category])){
281: Yii::$translationLog[$category] = array();
282: }
283: Yii::$translationLog[$category][$message] = '';
284: }
285:
286: }
287: