Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/main/php/lang/ClassLoader.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php namespace lang;

use lang\archive\ArchiveClassLoader;
use lang\reflect\Module;
use util\Objects;

/**
Expand Down Expand Up @@ -53,16 +52,18 @@ static function __static() {
} else {
$cl= ArchiveClassLoader::instanceFor($element, false);
}
if (isset(self::$delegates[$cl->instanceId()])) continue;

self::$delegates[$cl->instanceId()]= $cl;
if ($cl->providesResource('module.xp')) $modules[]= $cl;
$id= $cl->instanceId();
if (isset(self::$delegates[$id])) continue;

self::$delegates[$id]= $cl;
if ($cl->providesResource('module.xp')) $modules[$id]= $cl;
}

// Initialize modules
\xp::$loader= new self();
foreach ($modules as $cl) {
self::$modules[$cl->instanceId()]= Module::register(self::declareModule($cl));
foreach ($modules as $id => $cl) {
self::$modules[$id]= Module::register(self::declareModule($cl));
}
}

Expand Down Expand Up @@ -110,7 +111,7 @@ public static function registerPath($element, $before= false) {
public static function registerLoader(IClassLoader $l, $before= false) {
$id= $l->instanceId();
if ($before) {
self::$delegates= array_merge([$id => $l], self::$delegates);
self::$delegates= [$id => $l] + self::$delegates;
} else {
self::$delegates[$id]= $l;
}
Expand Down Expand Up @@ -149,7 +150,7 @@ public static function declareModule($l) {
if (strstr($m[2], 'extends')) {
$parent= $m[2];
} else {
$parent= ' extends \lang\reflect\Module '.$m[2];
$parent= ' extends \lang\Module '.$m[2];
}

$dyn= DynamicClassLoader::instanceFor('modules');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php namespace lang\reflect;

use lang\{IClassLoader, ElementNotFoundException, Value};
<?php namespace lang;

/**
* Represents a module
Expand Down
3 changes: 1 addition & 2 deletions src/main/php/xp/runtime/Modules.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php namespace xp\runtime;

use lang\reflect\Module;
use lang\{Environment, ElementNotFoundException, FormatException};
use lang\{Module, Environment, ElementNotFoundException, FormatException};

/** @test xp.unittest.ModulesTest */
class Modules {
Expand Down
52 changes: 27 additions & 25 deletions src/test/php/lang/unittest/ModuleLoadingTest.class.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
<?php namespace lang\unittest;

use lang\reflect\Module;
use lang\{ClassLoader, ElementNotFoundException, XPClass};
use lang\{Module, ClassLoader, ElementNotFoundException, XPClass};
use test\{After, Assert, Expect, Test};

class ModuleLoadingTest {
protected $registered= [];
private $registered= [];

/**
* Register a loader with the CL
*
* @param lang.IClassLoader $l
* @return void
* @return lang.IClassLoader
*/
protected function register($l) {
private function register($l) {
$this->registered[]= ClassLoader::registerLoader($l);
return $l;
}

#[After]
public function tearDown() {
/**
* Removes all registered loaders
*
* @return void
*/
public function remove() {
foreach ($this->registered as $l) {
ClassLoader::removeLoader($l);
}
Expand All @@ -29,7 +33,7 @@ public function simple_module() {
try {
$this->register(new LoaderProviding(['module.xp' => 'module xp-framework/simple { }']));
} finally {
$this->tearDown();
$this->remove();
}
}

Expand All @@ -38,7 +42,7 @@ public function leading_php_tag_is_stripped() {
try {
$this->register(new LoaderProviding(['module.xp' => '<?php module xp-framework/tagstart { }']));
} finally {
$this->tearDown();
$this->remove();
}
}

Expand All @@ -47,7 +51,7 @@ public function leading_and_trailing_php_tags_are_stripped() {
try {
$this->register(new LoaderProviding(['module.xp' => '<?php module xp-framework/tagboth { } ?>']));
} finally {
$this->tearDown();
$this->remove();
}
}

Expand All @@ -56,7 +60,7 @@ public function trailing_php_tag_is_stripped() {
try {
$this->register(new LoaderProviding(['module.xp' => 'module xp-framework/tagend { } ?>']));
} finally {
$this->tearDown();
$this->remove();
}
}

Expand All @@ -68,7 +72,7 @@ public function module_in_namespace() {

}']));
} finally {
$this->tearDown();
$this->remove();
}
}

Expand All @@ -77,7 +81,7 @@ public function empty_module_file() {
try {
$this->register(new LoaderProviding(['module.xp' => '']));
} finally {
$this->tearDown();
$this->remove();
}
}

Expand All @@ -86,18 +90,17 @@ public function module_without_name() {
try {
$this->register(new LoaderProviding(['module.xp' => 'module { }']));
} finally {
$this->tearDown();
$this->remove();
}
}

#[Test]
public function loaded_module() {
try {
$cl= new LoaderProviding(['module.xp' => 'module xp-framework/loaded { }']);
$this->register($cl);
$cl= $this->register(new LoaderProviding(['module.xp' => 'module xp-framework/loaded { }']));
Assert::equals(new Module('xp-framework/loaded', $cl), Module::forName('xp-framework/loaded'));
} finally {
$this->tearDown();
$this->remove();
}
}

Expand All @@ -113,7 +116,7 @@ public function initialize() {
}']));
Assert::equals(true, Module::forName('xp-framework/initialized')->initialized);
} finally {
$this->tearDown();
$this->remove();
}
}

Expand All @@ -135,7 +138,7 @@ public function initialized() {
$this->register($tracksInit);
Assert::equals(1, Module::forName('xp-framework/tracks-init')->initialized());
} finally {
$this->tearDown();
$this->remove();
}
}

Expand All @@ -148,7 +151,7 @@ public function module_inheritance() {
]));
Assert::equals($cl, new XPClass(typeof(Module::forName('xp-framework/child'))->reflect()->getParentclass()));
} finally {
$this->tearDown();
$this->remove();
}
}

Expand All @@ -161,21 +164,20 @@ public function module_implementation() {
]));
Assert::true(typeof(Module::forName('xp-framework/impl'))->reflect()->isSubclassOf($cl->reflect()));
} finally {
$this->tearDown();
$this->remove();
}
}

#[Test]
public function modules_initializer_can_register_itself_upfront_without_causing_endless_recursion() {
try {
$selfUpfront= new LoaderProviding(['module.xp' => 'module xp-framework/self-upfront {
$this->register(new LoaderProviding(['module.xp' => 'module xp-framework/self-upfront {
public function initialize() {
\lang\ClassLoader::registerLoader($this->classLoader(), true);
}
}']);
$this->register($selfUpfront);
}']));
} finally {
$this->tearDown();
$this->remove();
}
}
}
5 changes: 2 additions & 3 deletions src/test/php/lang/unittest/ModuleTest.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php namespace lang\unittest;

use lang\reflect\Module;
use lang\{ClassLoader, ElementNotFoundException};
use lang\{Module, ClassLoader, ElementNotFoundException};
use test\{After, Assert, Before, Expect, Test};

class ModuleTest {
Expand Down Expand Up @@ -58,7 +57,7 @@ public function does_not_equal_module_with_different_name() {
#[Test]
public function string_representation() {
Assert::equals(
'lang.reflect.Module<xp-framework/test@lang.ClassLoader>',
'lang.Module<xp-framework/test@lang.ClassLoader>',
(new Module('xp-framework/test', $this->cl))->toString()
);
}
Expand Down
Loading