root/trunk/src/main/php/net/stubbles/reflection/stubReflectionPackage.php

Revision 1307, 5.7 kB (checked in by mikey, 4 months ago)

fixed several class loading issues

Line 
1 <?php
2 /**
3  * Extended Reflection class for packages.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @package     stubbles
7  * @subpackage  reflection
8  */
9 stubClassLoader::load('net::stubbles::reflection::stubBaseReflectionClass',
10                       'net::stubbles::reflection::stubReflectionClass'
11 );
12 /**
13  * Extended Reflection class for packages.
14  *
15  * @package     stubbles
16  * @subpackage  reflection
17  */
18 class stubReflectionPackage
19 {
20     /**
21      * the package separator character
22      */
23     const SEPARATOR = '::';
24     /**
25      * name of the reflected package
26      *
27      * @var  string
28      */
29     protected $packageName;
30
31     /**
32      * constructor
33      *
34      * @param  string  $packageName  name of package to reflect
35      */
36     public function __construct($packageName)
37     {
38         $this->packageName = $packageName;
39     }
40
41     /**
42      * checks whether a value is equal to the package
43      *
44      * @param   mixed  $compare
45      * @return  bool
46      */
47     public function equals($compare)
48     {
49         if ($compare instanceof self) {
50             return ($compare->packageName == $this->packageName);
51         }
52
53         return false;
54     }
55
56     /**
57      * returns a string representation of the class
58      *
59      * The result is a short but informative representation about the class and
60      * its values. Per default, this method returns:
61      * 'net::stubbles::reflection::stubReflectionPackage['[name-of-reflected-package]']  {}'
62      * <code>
63      * net::stubbles::reflection::stubReflectionPackage[mypackage] {
64      * }
65      * </code>
66      *
67      * @return  string
68      */
69     public function __toString()
70     {
71         return 'net::stubbles::reflection::stubReflectionPackage[' . $this->packageName . "] {\n}\n";
72     }
73
74     /**
75      * returns the full qualified class name of the reflected package
76      *
77      * @return  string
78      */
79     public function getName()
80     {
81         return $this->packageName;
82     }
83
84     /**
85      * checks whether the package has a class with the given name
86      *
87      * @param   string  $nqClassName  non qualified name of class to check
88      * @return  bool
89      */
90     public function hasClass($nqClassName)
91     {
92         $classNames = stubClassLoader::getClassNames($this->packageName, false);
93         return in_array($this->packageName . self::SEPARATOR . $nqClassName, $classNames);
94     }
95
96     /**
97      * returns the specified class
98      *
99      * @param   string               $nqClassName  non qualified name of class to return
100      * @return  stubReflectionClass
101      * @throws  stubClassNotFoundException
102      */
103     public function getClass($nqClassName)
104     {
105         $stubRefClass = new stubReflectionClass($this->packageName . self::SEPARATOR . $nqClassName);
106         return $stubRefClass;
107     }
108
109     /**
110      * returns a list of all class names within the package
111      *
112      * @param   bool           $recursive  optional  true if classes of subpackages should be included
113      * @return  array<string>
114      */
115     public function getClassNames($recursive = false)
116     {
117         return stubClassLoader::getClassNames($this->packageName, $recursive);
118     }
119
120     /**
121      * returns a list of all classes within the package
122      *
123      * @param   bool                        $recursive  optional  true if classes of subpackages should be included
124      * @return  array<stubReflectionClass>
125      */
126     public function getClasses($recursive = false)
127     {
128         $classes = array();
129         $classNames = stubClassLoader::getClassNames($this->packageName, $recursive);
130         foreach ($classNames as $fqClassName) {
131             $classes[] = new stubReflectionClass($fqClassName);
132         }
133
134         return $classes;
135     }
136
137     /**
138      * check whether the package contains a subpackage with the given name
139      *
140      * @param   string  $name  name of subpackage
141      * @return  bool
142      */
143     public function hasPackage($name)
144     {
145         return in_array($name, $this->getPackageNames(true));
146     }
147
148     /**
149      * returns the subpackage
150      *
151      * @param   string                 $name  name of subpackage (without name of current package)
152      * @return  stubReflectionPackage
153      */
154     public function getPackage($name)
155     {
156         return new self($this->packageName . self::SEPARATOR . $name);
157     }
158
159     /**
160      * returns a list of all subpackage names
161      *
162      * @param   bool           $recursive  optional  true if subpackages of subpackages should be included
163      * @return  array<string>
164      */
165     public function getPackageNames($recursive = false)
166     {
167         $packages   = array();
168         $classNames = stubClassLoader::getClassNames($this->packageName, true);
169         foreach ($classNames as $fqClassName) {
170             $packageName = stubClassLoader::getPackageName($fqClassName);
171             if ($packageName == $this->packageName) {
172                 continue;
173             }
174
175             $shortName = str_replace($this->packageName . self::SEPARATOR, '', $packageName);
176             if (strstr($shortName, self::SEPARATOR) !== false && false === $recursive) {
177                 continue;
178             }
179
180             $packages[$shortName] = $shortName;
181         }
182
183         $return = array_keys($packages);
184         sort($return);
185         return $return;
186     }
187
188     /**
189      * returns a list of all subpackages
190      *
191      * @param   bool                          $recursive  optional  true if subpackages of subpackages should be included
192      * @return  array<stubReflectionPackage>
193      */
194     public function getPackages($recursive = false)
195     {
196         $packages    = $this->getPackageNames($recursive);
197         $refPackages = array();
198         foreach ($packages as $package) {
199             $refPackages[] = new self($this->packageName . self::SEPARATOR . $package);
200         }
201
202         return $refPackages;
203     }
204 }
205 ?>
Note: See TracBrowser for help on using the browser.