diff --git a/lib/Logger/StreamHandler.php b/lib/Logger/StreamHandler.php index 5eb4622..e77df16 100644 --- a/lib/Logger/StreamHandler.php +++ b/lib/Logger/StreamHandler.php @@ -16,8 +16,8 @@ use MensBeam\{ class StreamHandler extends Handler { protected int $chunkSize = 10 * 1024 * 1024; protected $resource = null; - protected ?string $url = null; - protected ?string $urlScheme = null; + protected ?string $uri = null; + protected ?string $uriScheme = null; protected ?string $_entryFormat = '%time% %channel% %level_name% %message%'; @@ -54,14 +54,26 @@ class StreamHandler extends Handler { public function getStream() { - return $this->resource ?? $this->url; + return $this->resource; + } + + public function getURI(): ?string { + return $this->uri; } public function setStream($value): void { - if (is_resource($value)) { + $isResource = is_resource($value); + if (!$isResource && !is_string($value)) { + $type = gettype($value); + $type = ($type === 'object') ? $value::class : $type; + throw new InvalidArgumentException(sprintf('Argument #1 ($value) must be of type resource|string, %s given', $type)); + } elseif ($isResource) { $this->resource = $value; + $value = stream_get_meta_data($value)['uri'] ?? null; stream_set_chunk_size($this->resource, $this->chunkSize); - } elseif(is_string($value)) { + } + + if ($value !== null) { $value = Path::canonicalize($value); // This wouldn't be useful for validating a URI schema, but it's fine for what this needs preg_match('/^(?:(?[^:\s\/]+):)?(?\/*)/i', $value, $matches); @@ -70,14 +82,10 @@ class StreamHandler extends Handler { $relative = ($matches['scheme'] === 'file') ? ($slashCount === 0 || $slashCount === 2) : ($slashCount === 0); $value = (($relative) ? getcwd() : '') . '/' . substr($value, strlen($matches[0])); } - - $this->url = $value; - $this->urlScheme = $matches['scheme'] ?: 'file'; - } else { - $type = gettype($value); - $type = ($type === 'object') ? $value::class : $type; - throw new InvalidArgumentException(sprintf('Argument #1 ($value) must be of type resource|string, %s given', $type)); + $this->uriScheme = $matches['scheme'] ?: 'file'; } + + $this->uri = $value; } @@ -99,17 +107,22 @@ class StreamHandler extends Handler { } $output .= \PHP_EOL; + if ($this->uriScheme === 'file') { + Fs::mkdir(dirname($this->uri)); + } + if ($this->resource === null) { - if ($this->urlScheme === 'file') { - Fs::mkdir(dirname($this->url)); - } + $this->resource = fopen($this->uri, 'a'); + stream_set_chunk_size($this->resource, $this->chunkSize); + } + fwrite($this->resource, $output); + } + - $fp = fopen($this->url, 'a'); - stream_set_chunk_size($fp, $this->chunkSize); - fwrite($fp, $output); - fclose($fp); - } else { - fwrite($this->resource, $output); + public function __destruct() { + if (!is_resource($this->resource)) { + return; } + fclose($this->resource); } } \ No newline at end of file diff --git a/tests/cases/TestLogger.php b/tests/cases/TestLogger.php index b21515c..64473ad 100644 --- a/tests/cases/TestLogger.php +++ b/tests/cases/TestLogger.php @@ -30,10 +30,10 @@ class TestLogger extends ErrorHandlingTestCase { $this->assertEquals(2, count($h)); $this->assertInstanceOf(StreamHandler::class, $h[0]); $this->assertInstanceOf(StreamHandler::class, $h[1]); - $s = $h[0]->getStream(); + $s = $h[0]->getURI(); $this->assertIsString($s); $this->assertSame('php://stderr', $s); - $s = $h[1]->getStream(); + $s = $h[1]->getURI(); $this->assertIsString($s); $this->assertSame('php://stdout', $s); } @@ -44,8 +44,8 @@ class TestLogger extends ErrorHandlingTestCase { // Should truncate the channel to 30 characters $this->assertSame('ooooooooooooooooooooooooooooo', $l->getChannel()); $this->assertEquals(2, count($h)); - $this->assertSame(CWD . '/ook.log', $h[0]->getStream()); - $this->assertSame(CWD . '/eek.log', $h[1]->getStream()); + $this->assertSame(CWD . '/ook.log', $h[0]->getURI()); + $this->assertSame(CWD . '/eek.log', $h[1]->getURI()); } public function testHandlerStackManipulation(): void { @@ -57,7 +57,7 @@ class TestLogger extends ErrorHandlingTestCase { $this->assertInstanceOf(StreamHandler::class, $h[0]); $this->assertInstanceOf(StreamHandler::class, $h[1]); $this->assertInstanceOf(StreamHandler::class, $h[2]); - $this->assertSame(CWD . '/ook', end($h)->getStream()); + $this->assertSame(CWD . '/ook', end($h)->getURI()); $l->setHandlers( new StreamHandler(stream: 'ook'), @@ -65,25 +65,25 @@ class TestLogger extends ErrorHandlingTestCase { ); $h = $l->getHandlers(); $this->assertEquals(2, count($h)); - $this->assertSame(CWD . '/ook', $h[0]->getStream()); - $this->assertSame(CWD . '/eek', $h[1]->getStream()); + $this->assertSame(CWD . '/ook', $h[0]->getURI()); + $this->assertSame(CWD . '/eek', $h[1]->getURI()); $h1 = $l->shiftHandler(); $this->assertSame($h[0], $h1); $h = $l->getHandlers(); - $this->assertSame(CWD . '/eek', $h[0]->getStream()); + $this->assertSame(CWD . '/eek', $h[0]->getURI()); $this->assertEquals(1, count($l->getHandlers())); $l->unshiftHandler($h1); $h = $l->getHandlers(); $this->assertSame($h[0], $h1); - $this->assertSame(CWD . '/ook', $h[0]->getStream()); + $this->assertSame(CWD . '/ook', $h[0]->getURI()); $this->assertEquals(2, count($h)); $h2 = $l->popHandler(); $this->assertSame($h[1], $h2); $h = $l->getHandlers(); - $this->assertSame(CWD . '/ook', $h[0]->getStream()); + $this->assertSame(CWD . '/ook', $h[0]->getURI()); $this->assertEquals(1, count($l->getHandlers())); } diff --git a/tests/cases/TestStreamHandler.php b/tests/cases/TestStreamHandler.php index 7c3753d..8af26e1 100644 --- a/tests/cases/TestStreamHandler.php +++ b/tests/cases/TestStreamHandler.php @@ -27,7 +27,16 @@ class TestStreamHandler extends \PHPUnit\Framework\TestCase { public function testGetStream(): void { $h = new StreamHandler('ook'); - $this->assertSame(CWD . '/ook', $h->getStream()); + $this->assertNull($h->getStream()); + $v = vfsStream::setup('ook', 0777, [ 'ook.log' => '' ]); + $f = $v->url() . '/ook.log'; + $h = new StreamHandler(fopen($f, 'a')); + $this->assertTrue(is_resource($h->getStream())); + } + + public function testGetURI(): void { + $h = new StreamHandler('ook'); + $this->assertSame(CWD . '/ook', $h->getURI()); } /** @dataProvider provideEntryFormattingTests */