new exceptions and handler for log writer file

Dieser Commit ist enthalten in:
o.pinke 2024-03-11 15:47:48 +01:00
Ursprung a34f26177e
Commit ed6b5569fd
3 geänderte Dateien mit 58 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,8 @@
<?php
namespace ConLite\Exceptions;
class FileNotFoundException extends RuntimeException
{
}

Datei anzeigen

@ -0,0 +1,8 @@
<?php
namespace ConLite\Exceptions;
class RuntimeException extends Exception
{
}

Datei anzeigen

@ -2,7 +2,49 @@
namespace ConLite\Log;
use ConLite\Exceptions\Exception;
use ConLite\Exceptions\FileNotFoundException;
class LogWriterFile extends LogWriter
{
/**
* @var resource
*/
protected $handle = NULL;
/**
* @throws FileNotFoundException
* @throws Exception
*/
public function __construct(array $options = []) {
parent::__construct($options);
$this->createHandle();
}
/**
* @param string $message
* @param int $priority
* @return bool
*/
public function write($message, $priority): bool
{
return fwrite($this->handle, $message) != false;
}
/**
* @throws Exception
* @throws FileNotFoundException
*/
protected function createHandle(): void
{
$destination = $this->getOption('destination');
if ($destination == '') {
throw new Exception('No destination was specified.');
}
if (($this->handle = fopen($destination, 'a')) === false) {
throw new FileNotFoundException('Destination handle could not be created.');
}
}
}