@@ -21,6 +21,9 @@ public function __construct(... $components) {
2121 $ this ->name = rtrim (strtr (implode ('. ' , $ components ), '\\' , '. ' ), '. ' );
2222 }
2323
24+ /** Returns whether this is the global package */
25+ public function global (): bool { return '' === $ this ->name ; }
26+
2427 /** Returns this package's name (in dotted form) */
2528 public function name (): string { return $ this ->name ; }
2629
@@ -39,13 +42,16 @@ public function classLoaders() {
3942 }
4043
4144 /**
42- * Returns this package's parent, if any
45+ * Returns this package's parent. Returns NULL if this package refers to the
46+ * global package.
4347 *
4448 * @return ?self
4549 */
4650 public function parent () {
51+ if ('' === $ this ->name ) return null ;
52+
4753 $ p = strrpos ($ this ->name , '. ' );
48- return false === $ p ? null : new Package (substr ($ this ->name , 0 , $ p ));
54+ return false === $ p ? new Package () : new Package (substr ($ this ->name , 0 , $ p ));
4955 }
5056
5157 /**
@@ -54,7 +60,7 @@ public function parent() {
5460 * @return iterable
5561 */
5662 public function children () {
57- $ base = $ this ->name .'. ' ;
63+ $ base = $ this ->name ? $ this -> name .'. ' : ' ' ;
5864 $ loader = ClassLoader::getDefault ();
5965 foreach ($ loader ->packageContents ($ this ->name ) as $ entry ) {
6066 if ('/ ' === $ entry [strlen ($ entry ) - 1 ]) {
@@ -70,7 +76,7 @@ public function children() {
7076 */
7177 public function types () {
7278 $ ext = strlen (\xp::CLASS_FILE_EXT );
73- $ base = $ this ->name .'. ' ;
79+ $ base = $ this ->name ? $ this -> name .'. ' : ' ' ;
7480 $ loader = ClassLoader::getDefault ();
7581 foreach ($ loader ->packageContents ($ this ->name ) as $ entry ) {
7682 if (0 === substr_compare ($ entry , \xp::CLASS_FILE_EXT , -$ ext )) {
@@ -87,15 +93,17 @@ public function types() {
8793 * @throws lang.IllegalArgumentException
8894 */
8995 public function type ($ name ) {
96+ if ('' === $ this ->name ) return Reflection::type ($ name );
97+
98+ // Compare type package and this package
9099 $ type = strtr ($ name , '\\' , '. ' );
91100 $ p = strrpos ($ type , '. ' );
92-
93101 if (false === $ p ) {
94- return Reflection::of ($ this ->name .'. ' .$ type );
102+ return Reflection::type ($ this ->name .'. ' .$ type );
95103 } else if (0 === strncmp ($ this ->name , $ type , $ p )) {
96- return Reflection::of ($ type );
97- } else {
98- throw new IllegalArgumentException ('Given type ' .$ type .' is not in package ' .$ this ->name );
104+ return Reflection::type ($ type );
99105 }
106+
107+ throw new IllegalArgumentException ('Given type ' .$ type .' is not in package ' .$ this ->name );
100108 }
101109}
0 commit comments