Skip to content

Commit

Permalink
Issue #44: Added support for laminas/laminas-servicemanager:4.x
Browse files Browse the repository at this point in the history
Signed-off-by: alexmerlin <[email protected]>
  • Loading branch information
alexmerlin committed Jan 10, 2025
1 parent e54eedf commit 6bf62b6
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 88 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# dot-log

![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-log)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-log/4.0.0)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-log/4.1.0)

[![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-log)](https://github.com/dotkernel/dot-log/issues)
[![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-log)](https://github.com/dotkernel/dot-log/network)
Expand All @@ -11,8 +11,6 @@
[![Build Static](https://github.com/dotkernel/dot-log/actions/workflows/continuous-integration.yml/badge.svg?branch=4.0)](https://github.com/dotkernel/dot-log/actions/workflows/continuous-integration.yml)
[![codecov](https://codecov.io/gh/dotkernel/dot-log/graph/badge.svg?token=JX19KTBRCZ)](https://codecov.io/gh/dotkernel/dot-log)

[![SymfonyInsight](https://insight.symfony.com/projects/287e81e8-b4fb-4452-bd8f-4f12c0ab1f76/big.svg)](https://insight.symfony.com/projects/287e81e8-b4fb-4452-bd8f-4f12c0ab1f76)

## Adding The Config Provider

* Enter config/config.php
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
],
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"laminas/laminas-servicemanager": "^3.22",
"laminas/laminas-validator": "^2.64"
"laminas/laminas-servicemanager": "^4.0",
"laminas/laminas-validator": "^3.0"
},
"require-dev": {
"laminas/laminas-coding-standard": "^3.0",
"phpunit/phpunit": "^10.2",
"laminas/laminas-coding-standard": "^2.5",
"vimeo/psalm": "^5.13"
},
"autoload": {
Expand All @@ -48,7 +48,8 @@
"scripts": {
"check": [
"@cs-check",
"@test"
"@test",
"@static-analysis"
],
"cs-check": "phpcs",
"cs-fix": "phpcbf",
Expand Down
25 changes: 18 additions & 7 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Laminas\ServiceManager\ServiceManager;
use Laminas\Stdlib\ArrayUtils;
use Laminas\Stdlib\SplPriorityQueue;
use Psr\Container\ContainerExceptionInterface;
use Traversable;

use function array_reverse;
Expand Down Expand Up @@ -133,6 +134,8 @@ class Logger implements LoggerInterface
* - writers: array of writers to add to this logger
* - exceptionhandler: if true register this logger as exceptionhandler
* - errorhandler: if true register this logger as errorhandler
*
* @throws ContainerExceptionInterface
*/
public function __construct(?iterable $options = null)
{
Expand Down Expand Up @@ -210,7 +213,7 @@ public function __destruct()
foreach ($this->writers as $writer) {
try {
$writer->shutdown();
} catch (Exception $e) {
} catch (Exception) {
}
}
}
Expand All @@ -232,15 +235,17 @@ public function setWriterPluginManager(WriterPluginManager $writerPlugins): stat
/**
* Get writer instance
*
* @psalm-suppress InvalidReturnStatement
* @throws ContainerExceptionInterface
*/
public function writerPlugin(string $name, ?array $options = null): WriterInterface
public function writerPlugin(string $name, ?array $options = null): ?WriterInterface
{
return $this->getWriterPluginManager()->get($name, $options);
return $this->getWriterPluginManager()?->build($name, $options);
}

/**
* Add a writer to a logger
*
* @throws ContainerExceptionInterface
*/
public function addWriter(WriterInterface|string $writer, int $priority = 1, ?array $options = null): static
{
Expand Down Expand Up @@ -284,6 +289,9 @@ public function getProcessorPluginManager(): ?ProcessorPluginManager
return $this->processorPlugins;
}

/**
* @param class-string|ProcessorPluginManager $plugins
*/
public function setProcessorPluginManager(string|ProcessorPluginManager $plugins): static
{
if (is_string($plugins)) {
Expand All @@ -302,13 +310,16 @@ public function setProcessorPluginManager(string|ProcessorPluginManager $plugins
}

/**
* @psalm-suppress InvalidReturnStatement
* @throws ContainerExceptionInterface
*/
public function processorPlugin(string $name, ?array $options = null): ProcessorInterface
public function processorPlugin(string $name, ?array $options = null): ?ProcessorInterface
{
return $this->getProcessorPluginManager()->get($name, $options);
return $this->getProcessorPluginManager()?->build($name, $options);
}

/**
* @throws ContainerExceptionInterface
*/
public function addProcessor(
ProcessorInterface|string $processor,
int $priority = 1,
Expand Down
11 changes: 4 additions & 7 deletions src/Manager/FilterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class FilterPluginManager extends AbstractPluginManager
{
/** @var string[] */
protected $aliases = [
protected array $aliases = [
'priority' => Priority::class,
'regex' => Regex::class,
'suppress' => SuppressFilter::class,
Expand All @@ -33,22 +33,19 @@ class FilterPluginManager extends AbstractPluginManager
];

/** @var string[]|callable[] */
protected $factories = [
protected array $factories = [
Priority::class => InvokableFactory::class,
Regex::class => InvokableFactory::class,
SuppressFilter::class => InvokableFactory::class,
Validator::class => InvokableFactory::class,
];

/** @var ?string */
protected $instanceOf = FilterInterface::class;
protected string $instanceOf = FilterInterface::class;

/**
* Allow many filters of the same type
*
* @var bool
*/
protected $sharedByDefault = false;
protected bool $sharedByDefault = false;

/**
* Validate the plugin is of the expected type.
Expand Down
11 changes: 4 additions & 7 deletions src/Manager/FormatterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,21 @@
class FormatterPluginManager extends AbstractPluginManager
{
/** @var string[] */
protected $aliases = [
protected array $aliases = [
'simple' => Simple::class,
];

/** @var string[]|callable[] */
protected $factories = [
protected array $factories = [
Simple::class => InvokableFactory::class,
];

/** @var ?string */
protected $instanceOf = FormatterInterface::class;
protected string $instanceOf = FormatterInterface::class;

/**
* Allow many formatters of the same type
*
* @var bool
*/
protected $sharedByDefault = false;
protected bool $sharedByDefault = false;

/**
* Validate the plugin is of the expected type.
Expand Down
11 changes: 4 additions & 7 deletions src/Manager/ProcessorPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,27 @@
class ProcessorPluginManager extends AbstractPluginManager
{
/** @var string[] */
protected $aliases = [
protected array $aliases = [
'backtrace' => Backtrace::class,
'psrplaceholder' => PsrPlaceholder::class,
'referenceid' => ReferenceId::class,
'requestid' => RequestId::class,
];

/** @var string[]|callable[] */
protected $factories = [
protected array $factories = [
Backtrace::class => InvokableFactory::class,
PsrPlaceholder::class => InvokableFactory::class,
ReferenceId::class => InvokableFactory::class,
RequestId::class => InvokableFactory::class,
];

/** @var ?string */
protected $instanceOf = ProcessorInterface::class;
protected string $instanceOf = ProcessorInterface::class;

/**
* Allow many processors of the same type
*
* @var bool
*/
protected $sharedByDefault = false;
protected bool $sharedByDefault = false;

/**
* Validate the plugin is of the expected type.
Expand Down
11 changes: 4 additions & 7 deletions src/Manager/WriterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class WriterPluginManager extends AbstractPluginManager
{
/** @var string[] */
protected $aliases = [
protected array $aliases = [
'noop' => Noop::class,
'stream' => Stream::class,

Expand All @@ -33,20 +33,17 @@ class WriterPluginManager extends AbstractPluginManager
];

/** @var string[]|callable[] */
protected $factories = [
protected array $factories = [
Noop::class => WriterFactory::class,
Stream::class => WriterFactory::class,
];

/** @var ?string */
protected $instanceOf = WriterInterface::class;
protected string $instanceOf = WriterInterface::class;

/**
* Allow many writers of the same type
*
* @var bool
*/
protected $sharedByDefault = false;
protected bool $sharedByDefault = false;

/**
* Validate the plugin is of the expected type.
Expand Down
37 changes: 32 additions & 5 deletions src/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
use Exception;
use Laminas\ServiceManager\ServiceManager;
use Laminas\Stdlib\ErrorHandler;
use Psr\Container\ContainerExceptionInterface;
use Traversable;

use function class_exists;
use function gettype;
use function is_array;
use function is_int;
Expand All @@ -35,7 +37,7 @@ abstract class AbstractWriter implements WriterInterface

protected array $filters = [];

protected FormatterInterface $formatter;
protected ?FormatterInterface $formatter = null;

/**
* Use Laminas\Stdlib\ErrorHandler to report errors during calls to write
Expand All @@ -53,6 +55,8 @@ abstract class AbstractWriter implements WriterInterface
* Set options for a writer. Accepted options are:
* - filters: array of filters to add to this filter
* - formatter: formatter for this writer
*
* @throws ContainerExceptionInterface
*/
public function __construct(?iterable $options = null)
{
Expand Down Expand Up @@ -99,14 +103,22 @@ public function __construct(?iterable $options = null)
throw new InvalidArgumentException('Options must contain a name for the formatter');
}
$formatterOptions = $formatter['options'] ?? null;
$this->setFormatter($formatter['name'], $formatterOptions);

$formatterClass = $formatter['name'];
if (class_exists($formatterClass)) {
$formatterClass = new $formatterClass();
}

$this->setFormatter($formatterClass, $formatterOptions);
}
}
}
}

/**
* Add a filter specific to this writer.
*
* @throws ContainerExceptionInterface
*/
public function addFilter(int|string|FilterInterface $filter, ?array $options = null): WriterInterface
{
Expand Down Expand Up @@ -138,6 +150,9 @@ public function getFilterPluginManager(): ?FilterPluginManager
return $this->filterPlugins;
}

/**
* @param class-string|FilterPluginManager $plugins
*/
public function setFilterPluginManager(string|FilterPluginManager $plugins): static
{
if (is_string($plugins)) {
Expand All @@ -155,9 +170,12 @@ public function setFilterPluginManager(string|FilterPluginManager $plugins): sta
return $this;
}

/**
* @throws ContainerExceptionInterface
*/
public function filterPlugin(string $name, ?array $options = null): mixed
{
return $this->getFilterPluginManager()->get($name, $options);
return $this->getFilterPluginManager()?->build($name, $options);
}

public function getFormatterPluginManager(): ?FormatterPluginManager
Expand All @@ -168,6 +186,9 @@ public function getFormatterPluginManager(): ?FormatterPluginManager
return $this->formatterPlugins;
}

/**
* @param class-string|FormatterPluginManager $plugins
*/
public function setFormatterPluginManager(string|FormatterPluginManager $plugins): static
{
if (is_string($plugins)) {
Expand All @@ -187,9 +208,12 @@ public function setFormatterPluginManager(string|FormatterPluginManager $plugins
return $this;
}

/**
* @throws ContainerExceptionInterface
*/
public function formatterPlugin(string $name, ?array $options = null): mixed
{
return $this->getFormatterPluginManager()->get($name, $options);
return $this->getFormatterPluginManager()?->build($name, $options);
}

/**
Expand Down Expand Up @@ -229,6 +253,9 @@ public function write(array $event): void
}
}

/**
* @throws ContainerExceptionInterface
*/
public function setFormatter(FormatterInterface|string $formatter, ?array $options = null): WriterInterface
{
if (is_string($formatter)) {
Expand All @@ -247,7 +274,7 @@ public function setFormatter(FormatterInterface|string $formatter, ?array $optio
return $this;
}

protected function getFormatter(): FormatterInterface
protected function getFormatter(): ?FormatterInterface
{
return $this->formatter;
}
Expand Down
Loading

0 comments on commit 6bf62b6

Please sign in to comment.