1 : <?php
2 : /**
3 : * PradoBase class file.
4 : *
5 : * This is the file that establishes the PRADO component model
6 : * and error handling mechanism.
7 : *
8 : * @author Qiang Xue <qiang.xue@gmail.com>
9 : * @link http://www.pradosoft.com/
10 : * @copyright Copyright © 2005 PradoSoft
11 : * @license http://www.pradosoft.com/license/
12 : * @version $Id: PradoBase.php 2283 2007-10-01 00:33:28Z xue $
13 : * @package System
14 : */
15 :
16 : /**
17 : * Defines the PRADO framework installation path.
18 : */
19 : if(!defined('PRADO_DIR'))
20 : define('PRADO_DIR',dirname(__FILE__));
21 : /**
22 : * Defines the default permission for writable directories and files
23 : */
24 : if(!defined('PRADO_CHMOD'))
25 : define('PRADO_CHMOD',0777);
26 :
27 : /**
28 : * PradoBase class.
29 : *
30 : * PradoBase implements a few fundamental static methods.
31 : *
32 : * To use the static methods, Use Prado as the class name rather than PradoBase.
33 : * PradoBase is meant to serve as the base class of Prado. The latter might be
34 : * rewritten for customization.
35 : *
36 : * @author Qiang Xue <qiang.xue@gmail.com>
37 : * @version $Id: PradoBase.php 2283 2007-10-01 00:33:28Z xue $
38 : * @package System
39 : * @since 3.0
40 : */
41 : class PradoBase
42 : {
43 : /**
44 : * File extension for Prado class files.
45 : */
46 : const CLASS_FILE_EXT='.php';
47 : /**
48 : * @var array list of path aliases
49 : */
50 : private static $_aliases=array('System'=>PRADO_DIR);
51 : /**
52 : * @var array list of namespaces currently in use
53 : */
54 : private static $_usings=array();
55 : /**
56 : * @var TApplication the application instance
57 : */
58 : private static $_application=null;
59 : /**
60 : * @var TLogger logger instance
61 : */
62 : private static $_logger=null;
63 :
64 : /**
65 : * @return string the version of Prado framework
66 : */
67 : public static function getVersion()
68 : {
69 3 : return '3.1.2a';
70 : }
71 :
72 : /**
73 : * Initializes error handlers.
74 : * This method set error and exception handlers to be functions
75 : * defined in this class.
76 : */
77 : public static function initErrorHandlers()
78 : {
79 : /**
80 : * Sets error handler to be Prado::phpErrorHandler
81 : */
82 0 : set_error_handler(array('PradoBase','phpErrorHandler'),error_reporting());
83 : /**
84 : * Sets exception handler to be Prado::exceptionHandler
85 : */
86 0 : set_exception_handler(array('PradoBase','exceptionHandler'));
87 0 : }
88 :
89 : /**
90 : * Class autoload loader.
91 : * This method is provided to be invoked within an __autoload() magic method.
92 : * @param string class name
93 : */
94 : public static function autoload($className)
95 : {
96 0 : include_once($className.self::CLASS_FILE_EXT);
97 0 : if(!class_exists($className,false) && !interface_exists($className,false))
98 0 : self::fatalError("Class file for '$className' cannot be found.");
99 0 : }
100 :
101 : /**
102 : * @param integer the type of "powered logo". Valid values include 0 and 1.
103 : * @return string a string that can be displayed on your Web page showing powered-by-PRADO information
104 : */
105 : public static function poweredByPrado($logoType=0)
106 : {
107 0 : $logoName=$logoType==1?'powered2':'powered';
108 0 : if(self::$_application!==null)
109 0 : {
110 0 : $am=self::$_application->getAssetManager();
111 0 : $url=$am->publishFilePath(self::getPathOfNamespace('System.'.$logoName,'.gif'));
112 0 : }
113 : else
114 0 : $url='http://www.pradosoft.com/images/'.$logoName.'.gif';
115 0 : return '<a title="Powered by PRADO" href="http://www.pradosoft.com/" target="_blank"><img src="'.$url.'" style="border-width:0px;" alt="Powered by PRADO" /></a>';
116 : }
117 :
118 : /**
119 : * PHP error handler.
120 : * This method should be registered as PHP error handler using
121 : * {@link set_error_handler}. The method throws an exception that
122 : * contains the error information.
123 : * @param integer the level of the error raised
124 : * @param string the error message
125 : * @param string the filename that the error was raised in
126 : * @param integer the line number the error was raised at
127 : */
128 : public static function phpErrorHandler($errno,$errstr,$errfile,$errline)
129 : {
130 55 : if(error_reporting()!=0)
131 55 : throw new TPhpErrorException($errno,$errstr,$errfile,$errline);
132 55 : }
133 :
134 : /**
135 : * Default exception handler.
136 : * This method should be registered as default exception handler using
137 : * {@link set_exception_handler}. The method tries to use the errorhandler
138 : * module of the Prado application to handle the exception.
139 : * If the application or the module does not exist, it simply echoes the
140 : * exception.
141 : * @param Exception exception that is not caught
142 : */
143 : public static function exceptionHandler($exception)
144 : {
145 0 : if(self::$_application!==null && ($errorHandler=self::$_application->getErrorHandler())!==null)
146 0 : {
147 0 : $errorHandler->handleError(null,$exception);
148 0 : }
149 : else
150 : {
151 0 : echo $exception;
152 : }
153 0 : exit(1);
154 : }
155 :
156 : /**
157 : * Stores the application instance in the class static member.
158 : * This method helps implement a singleton pattern for TApplication.
159 : * Repeated invocation of this method or the application constructor
160 : * will cause the throw of an exception.
161 : * This method should only be used by framework developers.
162 : * @param TApplication the application instance
163 : * @throws TInvalidOperationException if this method is invoked twice or more.
164 : */
165 : public static function setApplication($application)
166 : {
167 0 : if(self::$_application!==null)
168 0 : throw new TInvalidOperationException('prado_application_singleton_required');
169 0 : self::$_application=$application;
170 0 : }
171 :
172 : /**
173 : * @return TApplication the application singleton, null if the singleton has not be created yet.
174 : */
175 : public static function getApplication()
176 : {
177 0 : return self::$_application;
178 : }
179 :
180 : /**
181 : * @return string the path of the framework
182 : */
183 : public static function getFrameworkPath()
184 : {
185 51 : return PRADO_DIR;
186 : }
187 :
188 : /**
189 : * Serializes a data.
190 : * The original PHP serialize function has a bug that may not serialize
191 : * properly an object.
192 : * @param mixed data to be serialized
193 : * @return string the serialized data
194 : */
195 : public static function serialize($data)
196 : {
197 0 : $arr[0]=$data;
198 0 : return serialize($arr);
199 : }
200 :
201 : /**
202 : * Unserializes a data.
203 : * The original PHP unserialize function has a bug that may not unserialize
204 : * properly an object.
205 : * @param string data to be unserialized
206 : * @return mixed unserialized data, null if unserialize failed
207 : */
208 : public static function unserialize($str)
209 : {
210 0 : $arr=unserialize($str);
211 0 : return isset($arr[0])?$arr[0]:null;
212 : }
213 :
214 : /**
215 : * Creates a component with the specified type.
216 : * A component type can be either the component class name
217 : * or a namespace referring to the path of the component class file.
218 : * For example, 'TButton', 'System.Web.UI.WebControls.TButton' are both
219 : * valid component type.
220 : * This method can also pass parameters to component constructors.
221 : * All parameters passed to this method except the first one (the component type)
222 : * will be supplied as component constructor parameters.
223 : * @param string component type
224 : * @return TComponent component instance of the specified type
225 : * @throws TInvalidDataValueException if the component type is unknown
226 : */
227 : public static function createComponent($type)
228 : {
229 3 : self::using($type);
230 3 : if(($pos=strrpos($type,'.'))!==false)
231 3 : $type=substr($type,$pos+1);
232 3 : if(($n=func_num_args())>1)
233 3 : {
234 1 : $args=func_get_args();
235 1 : $s='$args[1]';
236 1 : for($i=2;$i<$n;++$i)
237 0 : $s.=",\$args[$i]";
238 1 : eval("\$component=new $type($s);");
239 1 : return $component;
240 : }
241 : else
242 2 : return new $type;
243 : }
244 :
245 : /**
246 : * Uses a namespace.
247 : * A namespace ending with an asterisk '*' refers to a directory, otherwise it represents a PHP file.
248 : * If the namespace corresponds to a directory, the directory will be appended
249 : * to the include path. If the namespace corresponds to a file, it will be included (include_once).
250 : * @param string namespace to be used
251 : * @param boolean whether to check the existence of the class after the class file is included
252 : * @throws TInvalidDataValueException if the namespace is invalid
253 : */
254 : public static function using($namespace,$checkClassExistence=true)
255 : {
256 3 : if(isset(self::$_usings[$namespace]) || class_exists($namespace,false))
257 3 : return;
258 1 : if(($pos=strrpos($namespace,'.'))===false) // a class name
259 1 : {
260 : try
261 : {
262 0 : include_once($namespace.self::CLASS_FILE_EXT);
263 : }
264 0 : catch(Exception $e)
265 : {
266 : if($checkClassExistence && !class_exists($namespace,false))
267 : throw new TInvalidOperationException('prado_component_unknown',$namespace,$e->getMessage());
268 : else
269 : throw $e;
270 : }
271 0 : }
272 1 : else if(($path=self::getPathOfNamespace($namespace,self::CLASS_FILE_EXT))!==null)
273 1 : {
274 1 : $className=substr($namespace,$pos+1);
275 1 : if($className==='*') // a directory
276 1 : {
277 0 : self::$_usings[$namespace]=$path;
278 0 : set_include_path(get_include_path().PATH_SEPARATOR.$path);
279 0 : }
280 : else // a file
281 : {
282 1 : self::$_usings[$namespace]=$path;
283 1 : if(!$checkClassExistence || !class_exists($className,false))
284 1 : {
285 : try
286 : {
287 0 : include_once($path);
288 : }
289 0 : catch(Exception $e)
290 : {
291 : if($checkClassExistence && !class_exists($className,false))
292 : throw new TInvalidOperationException('prado_component_unknown',$className,$e->getMessage());
293 : else
294 : throw $e;
295 : }
296 0 : }
297 : }
298 1 : }
299 : else
300 0 : throw new TInvalidDataValueException('prado_using_invalid',$namespace);
301 1 : }
302 :
303 : /**
304 : * Translates a namespace into a file path.
305 : * The first segment of the namespace is considered as a path alias
306 : * which is replaced with the actual path. The rest segments are
307 : * subdirectory names appended to the aliased path.
308 : * If the namespace ends with an asterisk '*', it represents a directory;
309 : * Otherwise it represents a file whose extension name is specified by the second parameter (defaults to empty).
310 : * Note, this method does not ensure the existence of the resulting file path.
311 : * @param string namespace
312 : * @param string extension to be appended if the namespace refers to a file
313 : * @return string file path corresponding to the namespace, null if namespace is invalid
314 : */
315 : public static function getPathOfNamespace($namespace,$ext='')
316 : {
317 1 : if(isset(self::$_usings[$namespace]))
318 1 : return self::$_usings[$namespace];
319 1 : else if(isset(self::$_aliases[$namespace]))
320 1 : return self::$_aliases[$namespace];
321 : else
322 : {
323 1 : $segs=explode('.',$namespace);
324 1 : $alias=array_shift($segs);
325 1 : if(($file=array_pop($segs))!==null && ($root=self::getPathOfAlias($alias))!==null)
326 1 : return rtrim($root.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR ,$segs),'/\\').(($file==='*')?'':DIRECTORY_SEPARATOR.$file.$ext);
327 : else
328 0 : return null;
329 : } |