Skip to content

Commit

Permalink
Make xp web [name] load the class xp.[name].Web
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Nov 20, 2023
1 parent 1847d36 commit 4a34434
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/main/php/xp/web/Source.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ private function newInstance($application, $environment) {

$cl= ClassLoader::getDefault();
try {
$class= is_file($application) ? $cl->loadUri($application) : $cl->loadClass($application);
if (is_file($application)) {
$class= $cl->loadUri($application);
} else if ($application[0] < 'a' || false !== strpos($application, '.')) {
$class= $cl->loadClass($application);
} else {
$class= $cl->loadClass("xp.{$application}.Web");
}
} catch (ClassLoadingException $e) {
throw new IllegalArgumentException('Cannot load class '.$application, $e);
}
Expand Down
25 changes: 22 additions & 3 deletions src/test/php/web/unittest/SourceTest.class.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php namespace web\unittest;

use io\Path;
use lang\{XPClass, IllegalArgumentException};
use lang\{XPClass, IllegalArgumentException, ClassLoader};
use test\{Assert, Before, Expect, Test};
use web\Environment;
use web\{Application, Environment};
use xp\web\{ServeDocumentRootStatically, Source};

class SourceTest {
Expand All @@ -26,9 +26,18 @@ public function application_class() {
Assert::instance(HelloWorld::class, $src->application());
}

#[Test]
public function application_class_in_global_namespace() {
$class= ClassLoader::defineClass('HelloWorld', Application::class, [], [
'routes' => function() { }
]);
$src= new Source('HelloWorld', $this->environment);
Assert::instance($class, $src->application());
}

#[Test]
public function application_file() {
$base= XPClass::forName('web.unittest.HelloWorld')->getClassLoader()->path;
$base= ClassLoader::getDefault()->findClass(HelloWorld::class)->path;
$src= new Source(new Path($base, 'web/unittest/HelloWorld.class.php'), $this->environment);
Assert::instance(HelloWorld::class, $src->application());
}
Expand All @@ -48,4 +57,14 @@ public function non_existant_class() {
public function unrelated_class() {
(new Source('util.Date', $this->environment))->application();
}

#[Test]
public function lowercase_xp_namespaced_web_app() {
$class= ClassLoader::defineClass('xp.lambda.Web', Application::class, [], [
'routes' => function() { }
]);

$src= new Source('lambda', $this->environment);
Assert::instance($class, $src->application());
}
}

0 comments on commit 4a34434

Please sign in to comment.