Skip to content
Merged
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
10 changes: 10 additions & 0 deletions src/main/php/web/Application.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public final function routing() {
return $this->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.
Expand Down
9 changes: 6 additions & 3 deletions src/main/php/xp/web/srv/Develop.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use lang\{ClassLoader, CommandLine, FileSystemClassLoader, Runtime, RuntimeOptions};
use peer\Socket;
use util\cmd\Console;
use web\Logging;
use web\{Application, Environment, Logging};
use xp\web\Source;

class Develop extends Server {

Expand All @@ -21,6 +22,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()) {
Expand Down Expand Up @@ -60,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';
Expand Down
1 change: 1 addition & 0 deletions src/main/php/xp/web/srv/Standalone.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/test/php/web/unittest/ResponseTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ 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]
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]
Expand Down