1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
|
<?php
/**
* Prado core interfaces and classes.
*
* This file contains and includes the definitions of prado core interfaces and classes.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.pradosoft.com/
* @copyright Copyright © 2005 PradoSoft
* @license http://www.pradosoft.com/license/
* @version $Revision: $ $Date: $
* @package System
*/
/**
* The framework installation path.
*/
define('PRADO_DIR',dirname(__FILE__));
/**
* Includes TComponent definition
*/
require_once(PRADO_DIR.'/TComponent.php');
/**
* Includes exception definitions
*/
require_once(PRADO_DIR.'/Exceptions/TException.php');
/**
* Includes TList definition
*/
require_once(PRADO_DIR.'/Collections/TList.php');
/**
* Includes TMap definition
*/
require_once(PRADO_DIR.'/Collections/TMap.php');
/**
* Includes TXmlDocument, TXmlElement definition
*/
require_once(PRADO_DIR.'/Data/TXmlDocument.php');
/**
* IApplication interface.
*
* This interface must be implemented by application classes.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
* @package System
* @since 3.0
*/
interface IApplication
{
/**
* Defines Error event.
*/
public function onError($param);
/**
* Defines BeginRequest event.
* @param mixed event parameter
*/
public function onBeginRequest($param);
/**
* Defines Authentication event.
* @param mixed event parameter
*/
public function onAuthentication($param);
/**
* Defines PostAuthentication event.
* @param mixed event parameter
*/
public function onPostAuthentication($param);
/**
* Defines Authorization event.
* @param mixed event parameter
*/
public function onAuthorization($param);
/**
* Defines PostAuthorization event.
* @param mixed event parameter
*/
public function onPostAuthorization($param);
/**
* Defines LoadState event.
* @param mixed event parameter
*/
public function onLoadState($param);
/**
* Defines PostLoadState event.
* @param mixed event parameter
*/
public function onPostLoadState($param);
/**
* Defines PreRunService event.
* @param mixed event parameter
*/
public function onPreRunService($param);
/**
* Defines RunService event.
* @param mixed event parameter
*/
public function onRunService($param);
/**
* Defines PostRunService event.
* @param mixed event parameter
*/
public function onPostRunService($param);
/**
* Defines SaveState event.
* @param mixed event parameter
*/
public function onSaveState($param);
/**
* Defines PostSaveState event.
* @param mixed event parameter
*/
public function onPostSaveState($param);
/**
* Defines EndRequest event.
* @param mixed event parameter
*/
public function onEndRequest($param);
/**
* Runs the application.
*/
public function run();
/**
* Completes and terminates the current request processing.
*/
public function completeRequest();
/**
* @return string application ID
*/
public function getID();
/**
* @param string application ID
*/
public function setID($id);
/**
* @return string a unique ID that can uniquely identify the application from the others
*/
public function getUniqueID();
/**
* @return IUser application user
*/
public function getUser();
/**
* @param IUser application user
*/
public function setUser(IUser $user);
/**
* @param string module ID
* @return IModule module corresponding to the ID, null if not found
*/
public function getModule($id);
/**
* Adds a module into application.
* @param string module ID
* @param IModule module to be added
* @throws TInvalidOperationException if module with the same ID already exists
*/
public function setModule($id,IModule $module);
/**
* @return array list of modules
*/
public function getModules();
/**
* @return TMap list of parameters
*/
public function getParameters();
/**
* @return IService the currently requested service
*/
public function getService();
/**
* @return THttpRequest the current user request
*/
public function getRequest();
/**
* @return THttpResponse the response to the request
*/
public function getResponse();
/**
* @return THttpSession the user session
*/
public function getSession();
/**
* @return ICache cache that is available to use
*/
public function getCache();
/**
* @return IErrorHandler error handler
*/
public function getErrorHandler();
/**
* @return IAuthManager the auth (authentication/authorization) manager
*/
public function getAuthManager();
}
/**
* IModule interface.
*
* This interface must be implemented by application modules.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
* @package System
* @since 3.0
*/
interface IModule
{
/**
* Initializes the module.
* @param IApplication the application object
* @param TXmlElement the configuration for the module
*/
public function init($application,$configuration);
/**
* @return string ID of the module
*/
public function getID();
/**
* @param string ID of the module
*/
public function setID($id);
}
/**
* IService interface.
*
* This interface must be implemented by services.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
* @package System
* @since 3.0
*/
interface IService
{
/**
* Initializes the service.
* @param IApplication the application object
* @param TXmlElement the configuration for the service
*/
public function init($application,$configuration);
/**
* @return string ID of the service
*/
public function getID();
/**
* @param string ID of the service
*/
public function setID($id);
/**
* Runs the service.
*/
public function run();
}
interface IErrorHandler
{
public function handle($sender,$param);
}
/**
* ICache interface.
*
* This interface must be implemented by cache managers.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
* @package System
* @since 3.0
*/
interface ICache
{
/**
* Retrieves a value from cache with a specified key.
* @return mixed the value stored in cache, false if the value is not in the cache or expired.
*/
public function get($id);
/**
* Stores a value identified by a key into cache.
* If the cache already contains such a key, the existing value and
* expiration time will be replaced with the new ones.
*
* @param string the key identifying the value to be cached
* @param mixed the value to be cached
* @param integer the expiration time of the value,
* 0 means never expire,
* a number less or equal than 60*60*24*30 means the number of seconds that the value will remain valid.
* a number greater than 60 means a UNIX timestamp after which the value will expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
public function set($id,$value,$expire=0);
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* Nothing will be done if the cache already contains the key.
* @param string the key identifying the value to be cached
* @param mixed the value to be cached
* @param integer the expiration time of the value,
* 0 means never expire,
* a number less or equal than 60*60*24*30 means the number of seconds that the value will remain valid.
* a number greater than 60 means a UNIX timestamp after which the value will expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
public function add($id,$value,$expire=0);
/**
* Stores a value identified by a key into cache only if the cache contains this key.
* The existing value and expiration time will be overwritten with the new ones.
* @param string the key identifying the value to be cached
* @param mixed the value to be cached
* @param integer the expiration time of the value,
* 0 means never expire,
* a number less or equal than 60*60*24*30 means the number of seconds that the value will remain valid.
* a number greater than 60 means a UNIX timestamp after which the value will expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
public function replace($id,$value,$expire=0);
/**
* Deletes a value with the specified key from cache
* @param string the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
public function delete($id);
/**
* Deletes all values from cache.
* Be careful of performing this operation if the cache is shared by multiple applications.
*/
public function flush();
}
/**
* ITextWriter interface.
*
* This interface must be implemented by writers.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
* @package System
* @since 3.0
*/
interface ITextWriter
{
/**
* Writes a string.
* @param string string to be written
*/
public function write($str);
/**
* Flushes the content that has been written.
*/
public function flush();
}
/**
* ITheme interface.
*
* This interface must be implemented by theme.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
* @package System
* @since 3.0
*/
interface ITheme
{
/**
* Applies this theme to the specified control.
* @param TControl the control to be applied with this theme
*/
public function apply($control);
}
/**
* ITemplate interface
*
* ITemplate specifies the interface for classes encapsulating
* parsed template structures.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
* @package System
* @since 3.0
*/
interface ITemplate
{
/**
* Instantiates the template.
* Content in the template will be instantiated as components and text strings
* and passed to the specified parent control.
* @param TControl the parent control
*/
public function instantiateIn($parent);
}
/**
* PradoBase class.
*
* PradoBase implements a few fundamental static methods.
*
* To use the static methods, Use Prado as the class name rather than PradoBase.
* PradoBase is meant to serve as the base class of Prado. The latter might be
* rewritten for customization.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
* @package System
* @since 3.0
*/
class PradoBase
{
/**
* File extension for Prado class files.
*/
const CLASS_FILE_EXT='.php';
/**
* @var array list of path aliases
*/
private static $_aliases=array('System'=>PRADO_DIR);
/**
* @var array list of namespaces currently in use
*/
private static $_usings=array();
/**
* @var IApplication the application instance
*/
private static $_application=null;
/**
* @return string the version of Prado framework
*/
public static function getVersion()
{
return '3.0a';
}
/**
* PHP error handler.
* This method should be registered as PHP error handler using
* {@link set_error_handler}. The method throws an exception that
* contains the error information.
* @param integer the level of the error raised
* @param string the error message
* @param string the filename that the error was raised in
* @param integer the line number the error was raised at
*/
public static function phpErrorHandler($errno,$errstr,$errfile,$errline)
{
if(error_reporting()!=0)
throw new TPhpErrorException($errno,$errstr,$errfile,$errline);
}
/**
* Default exception handler.
* This method should be registered as default exception handler using
* {@link set_exception_handler}. The method tries to use the errorhandler
* module of the Prado application to handle the exception.
* If the application or the module does not exist, it simply echoes the
* exception.
* @param Exception exception that is not caught
*/
public static function exceptionHandler($exception)
{
if(self::$_application!==null && ($errorHandler=self::$_application->getErrorHandler())!==null)
{
$errorHandler->handle($exception);
}
else
{
echo $exception;
}
exit(1);
}
/**
* Stores the application instance in the class static member.
* This method helps implement a singleton pattern for TApplication.
* Repeated invocation of this method or the application constructor
* will cause the throw of an exception.
* This method should only be used by framework developers.
* @param IApplication the application instance
* @throws TInvalidOperationException if this method is invoked twice or more.
*/
public static function setApplication(IApplication $app)
{
if(self::$_application!==null)
throw new TInvalidOperationException('prado_application_singleton_required');
self::$_application=$app;
}
/**
* @return IApplication the application singleton, null if the singleton has not be created yet.
*/
public static function getApplication()
{
return self::$_application;
}
/**
* @return string the path of the framework
*/
public static function getFrameworkPath()
{
return PRADO_DIR;
}
/**
* Serializes a data.
* Original PHP serialize function has a bug that may not serialize
* properly an object.
* @param mixed data to be serialized
* @return string the serialized data
*/
public static function serialize($data)
{
$arr[0]=$data;
return serialize($arr);
}
/**
* Unserializes a data.
* Original PHP unserialize function has a bug that may not unserialize
* properly an object.
* @param string data to be unserialized
* @return mixed unserialized data, null if unserialize failed
*/
public static function unserialize($str)
{
$arr=unserialize($str);
return isset($arr[0])?$arr[0]:null;
}
/**
* Creates a component with the specified type.
* A component type can be either the component class name
* or a namespace referring to the path of the component class file.
* For example, 'TButton', 'System.Web.UI.WebControls.TButton' are both
* valid component type.
* @param string component type
* @return TComponent component instance of the specified type
* @throws TInvalidDataValueException if the component type is unknown
*/
public static function createComponent($type)
{
if(class_exists($type,false))
return new $type;
if(($pos=strrpos($type,'.'))===false)
{
// a class name is supplied
$className=$type;
if(!class_exists($className,false))
{
include_once($className.self::CLASS_FILE_EXT);
}
if(class_exists($className,false))
return new $className;
else
throw new TInvalidDataValueException('prado_component_unknown',$type);
}
else
{
$className=substr($type,$pos+1);
if(($path=self::getPathOfNamespace($type))!==null)
{
// the class type is given in a namespace form
if(!class_exists($className,false))
{
require_once($path.self::CLASS_FILE_EXT);
}
if(class_exists($className,false))
return new $className;
}
throw new TInvalidDataValueException('prado_component_unknown',$type);
}
}
/**
* Uses a namespace.
* A namespace ending with an asterisk '*' refers to a directory, otherwise it represents a PHP file.
* If the namespace corresponds to a directory, the directory will be appended
* to the include path. If the namespace corresponds to a file, it will be included (require_once).
* @param string namespace to be used
* @throws TInvalidDataValueException if the namespace is invalid
*/
public static function using($namespace)
{
if(!isset(self::$_usings[$namespace]))
{
if(($path=self::getPathOfNamespace($namespace,self::CLASS_FILE_EXT))===null)
throw new TInvalidDataValueException('prado_using_invalid',$namespace);
else
{
if($namespace[strlen($namespace)-1]==='*') // a file
{
if(is_dir($path))
{
self::$_usings[$namespace]=$path;
set_include_path(get_include_path().PATH_SEPARATOR.$path);
}
else
throw new TInvalidDataValueException('prado_using_invalid',$namespace);
}
else // a directory
{
if(is_file($path))
{
self::$_usings[$namespace]=$path;
require_once($path);
}
else
throw new TInvalidDataValueException('prado_using_invalid',$namespace);
}
}
}
}
/**
* Translates a namespace into a file path.
* The first segment of the namespace is considered as a path alias
* which is replaced with the actual path. The rest segments are
* subdirectory names appended to the aliased path.
* If the namespace ends with an asterisk '*', it represents a directory;
* Otherwise it represents a file whose extension name is specified by the second parameter (defaults to empty).
* Note, this method does not ensure the existence of the resulting file path.
* @param string namespace
* @param string extension to be appended if the namespace refers to a file
* @return string file path corresponding to the namespace, null if namespace is invalid
*/
public static function getPathOfNamespace($namespace,$ext='')
{
if(isset(self::$_usings[$namespace]))
return self::$_usings[$namespace];
else
{
$segs=explode('.',$namespace);
$alias=array_shift($segs);
if(($file=array_pop($segs))!==null && ($root=self::getPathOfAlias($alias))!==null)
return rtrim($root.'/'.implode('/',$segs),'/').(($file==='*')?'':'/'.$file.$ext);
else
return null;
}
}
/**
* @param string alias to the path
* @return string the path corresponding to the alias, null if alias not defined.
*/
public static function getPathOfAlias($alias)
{
if(isset(self::$_aliases[$alias]))
return self::$_aliases[$alias];
else
return null;
}
/**
* @param string alias to the path
* @param string the path corresponding to the alias
* @throws TInvalidOperationException if the alias is already defined
* @throws TInvalidDataValueException if the path is not a valid file path
*/
public static function setPathOfAlias($alias,$path)
{
if(isset(self::$_aliases[$alias]))
throw new TInvalidOperationException('prado_alias_redefined',$alias);
else if(($rp=realpath($path))!==false && is_dir($rp))
self::$_aliases[$alias]=$rp;
else
throw new TInvalidDataValueException('prado_alias_invalid',$alias,$path);
}
/**
* Fatal error handler.
* This method is used in places where exceptions usually cannot be raised
* (e.g. magic methods).
* It displays the debug backtrace.
* @param string error message
*/
function fatalError($msg)
{
echo '<h1>Fatal Error</h1>';
echo '<p>'.$msg.'</p>';
if(!function_exists('debug_backtrace'))
return;
echo '<h2>Debug Backtrace</h2>';
echo '<pre>';
$index=-1;
foreach(debug_backtrace() as $t)
{
$index++;
if($index==0) // hide the backtrace of this function
continue;
echo '#'.$index.' ';
if(isset($t['file']))
echo basename($t['file']) . ':' . $t['line'];
else
echo '<PHP inner-code>';
echo ' -- ';
if(isset($t['class']))
echo $t['class'] . $t['type'];
echo $t['function'];
if(isset($t['args']) && sizeof($t['args']) > 0)
echo '(...)';
else
echo '()';
echo "\n";
}
echo '</pre>';
exit(1);
}
}
/**
* Includes TErrorHandler class
*/
require_once(PRADO_DIR.'/Exceptions/TErrorHandler.php');
/**
* Includes THttpRequest class
*/
require_once(PRADO_DIR.'/Web/THttpRequest.php'); // include TUser
/**
* Includes THttpResponse class
*/
require_once(PRADO_DIR.'/Web/THttpResponse.php');
/**
* Includes THttpSession class
*/
require_once(PRADO_DIR.'/Web/THttpSession.php');
/**
* Includes TAuthorizationRule class
*/
require_once(PRADO_DIR.'/Security/TAuthorizationRule.php');
?>
|