-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Flasher.php
62 lines (49 loc) · 1.77 KB
/
Flasher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
namespace Flasher\Prime;
use Flasher\Prime\Factory\FlasherFactory;
use Flasher\Prime\Factory\NotificationFactoryInterface;
use Flasher\Prime\Factory\NotificationFactoryLocatorInterface;
use Flasher\Prime\Response\ResponseManagerInterface;
use Flasher\Prime\Storage\StorageManagerInterface;
use Flasher\Prime\Support\Traits\ForwardsCalls;
final readonly class Flasher implements FlasherInterface
{
use ForwardsCalls;
public const VERSION = '2.1.1';
public function __construct(
private string $default,
private NotificationFactoryLocatorInterface $factoryLocator,
private ResponseManagerInterface $responseManager,
private StorageManagerInterface $storageManager,
) {
}
public function use(?string $alias): NotificationFactoryInterface
{
$alias = trim($alias ?: $this->default);
if ('' === $alias) {
throw new \InvalidArgumentException('Unable to resolve empty factory.');
}
if ('flasher' !== $alias && $this->factoryLocator->has($alias)) {
return $this->factoryLocator->get($alias);
}
return new FlasherFactory($this->storageManager, $alias);
}
public function create(?string $alias): NotificationFactoryInterface
{
return $this->use($alias);
}
public function render(string $presenter = 'html', array $criteria = [], array $context = []): mixed
{
return $this->responseManager->render($presenter, $criteria, $context);
}
/**
* Dynamically call the default factory instance.
*
* @param mixed[] $parameters
*/
public function __call(string $method, array $parameters): mixed
{
return $this->forwardCallTo($this->use(null), $method, $parameters);
}
}