From 1fc438a0b11932eda65bbbd3f2f05874c949c0cb Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 9 Jul 2022 14:39:07 +0200 Subject: [PATCH 1/3] Add Application::initialize() --- src/main/php/web/Application.class.php | 14 +++++++++++++- src/main/php/xp/web/srv/Develop.class.php | 5 ++++- src/main/php/xp/web/srv/Standalone.class.php | 1 + 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/php/web/Application.class.php b/src/main/php/web/Application.class.php index fe468694..8c70562e 100755 --- a/src/main/php/web/Application.class.php +++ b/src/main/php/web/Application.class.php @@ -1,11 +1,13 @@ routing; } + /** + * Initializes this application, being run once when the server starts. + * Empty in this default implementation, overwrite in subclasses. + * + * @return void + */ + public function initialize() { + // Empty + } + /** * Returns this application's routes, which are either a `Routing` * instance or a map of paths to routing targets. diff --git a/src/main/php/xp/web/srv/Develop.class.php b/src/main/php/xp/web/srv/Develop.class.php index 56b773cc..3561bd94 100755 --- a/src/main/php/xp/web/srv/Develop.class.php +++ b/src/main/php/xp/web/srv/Develop.class.php @@ -5,7 +5,7 @@ use lang\{ClassLoader, CommandLine, FileSystemClassLoader, Runtime, RuntimeOptions}; use peer\Socket; use util\cmd\Console; -use web\Logging; +use web\{Application, Environment, Logging}; class Develop extends Server { @@ -21,6 +21,9 @@ class Develop extends Server { * @param string[] $logging */ public function serve($source, $profile, $webroot, $docroot, $config, $args, $logging) { + $environment= new Environment($profile, $webroot, $docroot, $config, $args, $logging); + $application= (new Source($source, $environment))->application($args); + $application->initialize(); // PHP doesn't start with a nonexistant document root if (!$docroot->exists()) { diff --git a/src/main/php/xp/web/srv/Standalone.class.php b/src/main/php/xp/web/srv/Standalone.class.php index a060127d..8d5a2349 100755 --- a/src/main/php/xp/web/srv/Standalone.class.php +++ b/src/main/php/xp/web/srv/Standalone.class.php @@ -56,6 +56,7 @@ public function __construct($address, $impl) { public function serve($source, $profile, $webroot, $docroot, $config, $args, $logging) { $environment= new Environment($profile, $webroot, $docroot, $config, $args, $logging); $application= (new Source($source, $environment))->application($args); + $application->initialize(); $application->routing(); $socket= new ServerSocket($this->host, $this->port); From 89c5cbf9b44cf44861b9e29e7bd560485ebf1469 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Tue, 30 Jan 2024 18:41:36 +0100 Subject: [PATCH 2/3] Fix test assertions --- src/test/php/web/unittest/ResponseTest.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/php/web/unittest/ResponseTest.class.php b/src/test/php/web/unittest/ResponseTest.class.php index 31dc5c56..20873dee 100755 --- a/src/test/php/web/unittest/ResponseTest.class.php +++ b/src/test/php/web/unittest/ResponseTest.class.php @@ -119,7 +119,7 @@ public function overwrite_cookie() { $res= new Response(new TestOutput()); $res->cookie(new Cookie('theme', 'light')); $res->cookie(new Cookie('theme', 'dark')); - $this->assertEquals(['dark'], array_map(function($c) { return $c->value(); }, $res->cookies())); + Assert::equals(['dark'], array_map(function($c) { return $c->value(); }, $res->cookies())); } #[Test] @@ -127,7 +127,7 @@ public function append_cookie() { $res= new Response(new TestOutput()); $res->cookie(new Cookie('theme', 'light')); $res->cookie(new Cookie('theme', 'dark'), true); - $this->assertEquals(['light', 'dark'], array_map(function($c) { return $c->value(); }, $res->cookies())); + Assert::equals(['light', 'dark'], array_map(function($c) { return $c->value(); }, $res->cookies())); } #[Test] From b41d279ff07bdb0b8f309c365a72605fe1455000 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Tue, 30 Jan 2024 18:51:32 +0100 Subject: [PATCH 3/3] Make output for development and standalone servers consistent --- src/main/php/xp/web/srv/Develop.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/php/xp/web/srv/Develop.class.php b/src/main/php/xp/web/srv/Develop.class.php index 1c7a878b..26a1d7d4 100755 --- a/src/main/php/xp/web/srv/Develop.class.php +++ b/src/main/php/xp/web/srv/Develop.class.php @@ -6,6 +6,7 @@ use peer\Socket; use util\cmd\Console; use web\{Application, Environment, Logging}; +use xp\web\Source; class Develop extends Server { @@ -63,9 +64,8 @@ public function serve($source, $profile, $webroot, $docroot, $config, $args, $lo putenv('WEB_LOG='.$logging); Console::writeLine("\e[33m@", nameof($this), "(HTTP @ `php ", implode(' ', $arguments), "`)\e[0m"); - Console::writeLine("\e[1mServing {$profile}:", $source, $config, "\e[0m > ", Logging::of($logging)->target()); + Console::writeLine("\e[1mServing {$profile}:", $application, $config, "\e[0m > ", $environment->logging()->target()); Console::writeLine("\e[36m", str_repeat('═', 72), "\e[0m"); - Console::writeLine(); if ('WINDOWS' === $os->name()) { $nul= 'NUL';