Skip to content

Commit

Permalink
Add support for the global package
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Mar 28, 2024
1 parent 802fbfd commit 43dc06d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/main/php/lang/reflection/Package.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public function __construct(... $components) {
$this->name= rtrim(strtr(implode('.', $components), '\\', '.'), '.');
}

/** Returns whether this is the global package */
public function global(): bool { return '' === $this->name; }

/** Returns this package's name (in dotted form) */
public function name(): string { return $this->name; }

Expand All @@ -39,13 +42,16 @@ public function classLoaders() {
}

/**
* Returns this package's parent, if any
* Returns this package's parent. Returns NULL if this package refers to the
* global package.
*
* @return ?self
*/
public function parent() {
if ('' === $this->name) return null;

$p= strrpos($this->name, '.');
return false === $p ? null : new Package(substr($this->name, 0, $p));
return false === $p ? new Package() : new Package(substr($this->name, 0, $p));
}

/**
Expand All @@ -54,7 +60,7 @@ public function parent() {
* @return iterable
*/
public function children() {
$base= $this->name.'.';
$base= $this->name ? $this->name.'.' : '';
$loader= ClassLoader::getDefault();
foreach ($loader->packageContents($this->name) as $entry) {
if ('/' === $entry[strlen($entry) - 1]) {
Expand All @@ -70,7 +76,7 @@ public function children() {
*/
public function types() {
$ext= strlen(\xp::CLASS_FILE_EXT);
$base= $this->name.'.';
$base= $this->name ? $this->name.'.' : '';
$loader= ClassLoader::getDefault();
foreach ($loader->packageContents($this->name) as $entry) {
if (0 === substr_compare($entry, \xp::CLASS_FILE_EXT, -$ext)) {
Expand All @@ -87,15 +93,17 @@ public function types() {
* @throws lang.IllegalArgumentException
*/
public function type($name) {
if ('' === $this->name) return Reflection::type($name);

// Compare type package and this package
$type= strtr($name, '\\', '.');
$p= strrpos($type, '.');

if (false === $p) {
return Reflection::of($this->name.'.'.$type);
return Reflection::type($this->name.'.'.$type);
} else if (0 === strncmp($this->name, $type, $p)) {
return Reflection::of($type);
} else {
throw new IllegalArgumentException('Given type '.$type.' is not in package '.$this->name);
return Reflection::type($type);
}

throw new IllegalArgumentException('Given type '.$type.' is not in package '.$this->name);
}
}
25 changes: 25 additions & 0 deletions src/test/php/lang/reflection/unittest/PackageTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ public function name($arg) {
Assert::equals('lang.reflection', (new Package($arg))->name());
}

#[Test]
public function global() {
Assert::true((new Package())->global());
}

#[Test, Values(['lang', 'lang.reflection', 'lang.reflect.unittest'])]
public function leveled($package) {
Assert::false((new Package($package))->global());
}

#[Test]
public function global_name() {
Assert::equals('', (new Package())->name());
}

#[Test]
public function create_via_components() {
Assert::equals('lang.reflection.unittest', (new Package('lang', 'reflection', 'unittest'))->name());
Expand All @@ -26,6 +41,16 @@ public function parent() {
Assert::equals(new Package('lang'), (new Package('lang.reflection'))->parent());
}

#[Test]
public function parent_of_toplevel() {
Assert::equals(new Package(), (new Package('lang'))->parent());
}

#[Test]
public function global_package_has_no_parent() {
Assert::null((new Package())->parent());
}

#[Test]
public function children() {
Assert::equals(
Expand Down

0 comments on commit 43dc06d

Please sign in to comment.