root/tags/RELEASE_0_2_1/XJConf/XJConfLoader.php

Revision 101, 3.2 kB (checked in by mikey, 2 years ago)

temporary bugfixes when loading classes with net.xjconf prefix and without (todo: use this prefix in xjconf itself, too)

Line 
1 <?php
2 /**
3  * Class loader for all XJConf classes.
4  *
5  * @author  Frank Kleine <frank.kleine@schlund.de>
6  */
7 /**
8  * Class loader for all XJConf classes.
9  *
10  * The class loader takes care that all class files are only loaded once. It
11  * allows all classes to include the required files without knowing where they
12  * reside or if they have been loaded before.
13  *
14  * @package  XJConf
15  */
16 class XJConfLoader
17 {
18     /**
19      * list of loaded files
20      *
21      * @var  array<String>
22      */
23     private static $loadedClasses = array();
24
25     /**
26      * method to load files from source path
27      *
28      * Usage: XJConfLoader::load('path.to.Classfile');
29      * or load more than one at once:
30      * XJConfLoader::load('path.to.first.Class',
31                           'path.to.second.Class'
32        );
33      * You may name as many files as you like, there is no restriction
34      * on the number of arguments.
35      *
36      * @param   string  list of file names to load
37      */
38     public static function load()
39     {
40         $classes = func_get_args();
41         if (count($classes) == 0) {
42             // its ok to call this without any arguments, this won't cause any harm
43             return;
44         }
45
46         $realFiles = array();
47         foreach ($classes as $className) {
48             if (in_array(str_replace('net.xjconf.', '', $className), self::$loadedClasses) == TRUE) {
49                 continue; // step to next file if file is already loaded
50             }
51
52             $uri = null;
53             array_push(self::$loadedClasses, str_replace('net.xjconf.', '', $className));
54             if (class_exists('StarClassRegistry', false) === true) {
55                 if (substr($className, 0, 11) != 'net.xjconf.') {
56                     $uri = StarClassRegistry::getUriForClass('net.xjconf.' . $className);
57                 } else {
58                     $uri = StarClassRegistry::getUriForClass($className);
59                 }
60             }
61             if ($uri === null) {
62                 $uri = dirname(__FILE__) . '/' . self::mapClassname($className);
63             }
64             
65             require $uri;
66         }
67     }
68
69     /**
70      * checks whether a file with the given class exists
71      *
72      * @param   string  $fqClassName
73      * @return  bool
74      */
75     public static function classFileExists($className)
76     {
77         if (class_exists('StarClassRegistry') == false) {
78             return file_exists(dirname(__FILE__) . '/' . self::mapClassname($className));
79         }
80
81         if (substr($className, 0, 11) != 'net.xjconf.') {
82             $fqClassName = 'net.xjconf.' . $className;
83         } else {
84             $fqClassName = $className;
85         }
86         
87         if (StarClassRegistry::getFileForClass($fqClassName) != null) {
88             return true;
89         }
90         if (substr(dirname(__FILE__), 0, 7) == 'star://') {
91             return false;
92         }
93         
94         return file_exists(dirname(__FILE__) . '/' . self::mapClassname($className));
95     }
96
97     /**
98      * maps classnames given to loadClass() into required ones for load()
99      *
100      * @param  string  $classname  name of class given to loadClass()
101      * @return string  name of class required for load()
102      */
103     private static function mapClassname($classname)
104     {
105         return str_replace('.', '/', $classname) . '.php';
106     }
107 }
108 ?>
Note: See TracBrowser for help on using the browser.