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
6 changes: 3 additions & 3 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,16 +400,16 @@ like this:
// …
```

Likewise, you can disable writing an access log by passing an absolute path to
`/dev/null` (Unix) or `nul` (Windows) like this:
Likewise, you can disable writing an access log by passing `/dev/null` (Unix) or
`nul` (Windows) like this:

```php title="public/index.php"
<?php

require __DIR__ . '/../vendor/autoload.php';

$container = new FrameworkX\Container([
'accesslog' => DIRECTORY_SEPARATOR !== '\\' ? '/dev/null' : __DIR__ . '\\nul'
'accesslog' => DIRECTORY_SEPARATOR !== '\\' ? '/dev/null' : 'nul',
FrameworkX\AccessLogHandler::class => fn(string $accesslog) => new FrameworkX\AccessLogHandler($accesslog),
]);

Expand Down
4 changes: 2 additions & 2 deletions src/AccessLogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class AccessLogHandler
private $hasHighResolution;

/**
* @param ?string $path (optional) absolute log file path or will log to console output by default
* @throws \InvalidArgumentException if given `$path` is not an absolute file path
* @param ?string $path (optional) logs to console output by default, otherwise can be an absolute log file path, a `nul` path on Windows or a `php://` stream
* @throws \InvalidArgumentException if given `$path` is not an absolute file path, `nul` on Windows or `php://` stream
* @throws \RuntimeException if given `$path` can not be opened in append mode
*/
public function __construct(?string $path = null)
Expand Down
6 changes: 3 additions & 3 deletions src/Io/LogStreamHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class LogStreamHandler
private $stream;

/**
* @param string $path absolute log file path
* @throws \InvalidArgumentException if given `$path` is not an absolute file path
* @param string $path absolute log file path, a `nul` path on Windows or a `php://` stream
* @throws \InvalidArgumentException if given `$path` is not an absolute file path, `nul` on Windows or `php://` stream
* @throws \RuntimeException if given `$path` can not be opened in append mode
*/
public function __construct(string $path)
Expand Down Expand Up @@ -90,6 +90,6 @@ public function log(string $message): void

private function isAbsolutePath(string $path): bool
{
return \DIRECTORY_SEPARATOR !== '\\' ? \substr($path, 0, 1) === '/' : (bool) \preg_match('#^[A-Z]:[/\\\\]#i', $path);
return \DIRECTORY_SEPARATOR !== '\\' ? \substr($path, 0, 1) === '/' : (bool) \preg_match('#^(?:[A-Z]:[/\\\\]|nul$)#i', $path);
}
}
2 changes: 1 addition & 1 deletion tests/AccessLogHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function testInvokeWithPathToExistingFileWillAppendLogMessage(): void
*/
public function testInvokeWithDevNullWritesNothing(): void
{
$handler = new AccessLogHandler(DIRECTORY_SEPARATOR !== '\\' ? '/dev/null' : __DIR__ . '\\nul');
$handler = new AccessLogHandler(DIRECTORY_SEPARATOR !== '\\' ? '/dev/null' : 'nul');

$request = new ServerRequest('GET', 'http://localhost:8080/users', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']);
$response = new Response(200, [], "Hello\n");
Expand Down
2 changes: 1 addition & 1 deletion tests/AppMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ public function testInvokeWithGlobalMiddlewareReturnsResponseWhenGlobalMiddlewar
private function createAppWithoutLogger(...$middleware): App
{
return new App(
new AccessLogHandler(DIRECTORY_SEPARATOR !== '\\' ? '/dev/null' : __DIR__ . '\\nul'),
new AccessLogHandler(DIRECTORY_SEPARATOR !== '\\' ? '/dev/null' : 'nul'),
new ErrorHandler(),
...$middleware
);
Expand Down
2 changes: 1 addition & 1 deletion tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1997,7 +1997,7 @@ public function testInvokeWithMatchingRouteReturnsInternalServerErrorResponseWhe
private function createAppWithoutLogger(callable ...$middleware): App
{
return new App(
new AccessLogHandler(DIRECTORY_SEPARATOR !== '\\' ? '/dev/null' : __DIR__ . '\\nul'),
new AccessLogHandler(DIRECTORY_SEPARATOR !== '\\' ? '/dev/null' : 'nul'),
new ErrorHandler(),
...$middleware
);
Expand Down
13 changes: 11 additions & 2 deletions tests/Io/LogStreamHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,23 @@ public static function provideFilesystemPaths(): \Generator
];
yield [
'nul',
DIRECTORY_SEPARATOR === '\\'
];
yield [
'NUL',
DIRECTORY_SEPARATOR === '\\'
];
yield [
'nul.txt',
false
];
yield [
'\\\\.\\nul',
false
];
if (DIRECTORY_SEPARATOR === '\\') {
if (DIRECTORY_SEPARATOR === '\\' && @fopen(__DIR__ . '\\nul', 'a') !== false) {
// valid path on Windows, but we don't want to write here on Unix
// not supported on newer Windows versions due to https://github.com/php/php-src/security/advisories/GHSA-9f67-6fw4-hpfp
yield [
__DIR__ . DIRECTORY_SEPARATOR . 'nul',
true
Expand Down Expand Up @@ -286,7 +295,7 @@ public function testLogWithPathToExistingFileWillAppendLogMessageWithCurrentDate
*/
public function testLogWithDevNullWritesNothing(): void
{
$logger = new LogStreamHandler(DIRECTORY_SEPARATOR !== '\\' ? '/dev/null' : __DIR__ . '\\nul');
$logger = new LogStreamHandler(DIRECTORY_SEPARATOR !== '\\' ? '/dev/null' : 'nul');

$logger->log('Hello');
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function asleep(float $s): PromiseInterface
$container = new FrameworkX\Container([
FrameworkX\AccessLogHandler::class => function (?string $X_EXPERIMENTAL_RUNNER = null) {
// log to /dev/null when running in experimental runner mode to avoid cluttering output
return new FrameworkX\AccessLogHandler($X_EXPERIMENTAL_RUNNER !== null ? (DIRECTORY_SEPARATOR !== '\\' ? '/dev/null' : __DIR__ . '\\nul') : null);
return new FrameworkX\AccessLogHandler($X_EXPERIMENTAL_RUNNER !== null ? (DIRECTORY_SEPARATOR !== '\\' ? '/dev/null' : 'nul') : null);
}
]);

Expand Down