1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the sauls/widget package. |
4
|
|
|
* |
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
7
|
|
|
* @copyright 2020 |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace Sauls\Component\Widget\Widgets; |
16
|
|
|
|
17
|
|
|
use Psr\Cache\InvalidArgumentException; |
18
|
|
|
use Sauls\Component\OptionsResolver\OptionsResolver; |
19
|
|
|
use Sauls\Component\Widget\Exception\NotAWidgetException; |
20
|
|
|
use Sauls\Component\Widget\Named; |
21
|
|
|
use Sauls\Component\Widget\Widget; |
22
|
|
|
use Sauls\Component\Widget\WidgetInterface; |
23
|
|
|
use Symfony\Component\OptionsResolver\Options; |
24
|
|
|
use Symfony\Contracts\Cache\CacheInterface; |
25
|
|
|
use Symfony\Contracts\Cache\ItemInterface; |
26
|
|
|
|
27
|
|
|
use function Sauls\Component\Helper\object_ucnp; |
28
|
|
|
use function sprintf; |
29
|
|
|
|
30
|
|
|
class CacheableWidget extends Widget implements Named |
31
|
|
|
{ |
32
|
|
|
public static $prefix = 'cw'; |
33
|
|
|
public static $total = 0; |
34
|
|
|
private CacheInterface $cache; |
35
|
|
|
|
36
|
4 |
|
public function __construct(CacheInterface $cache) |
37
|
|
|
{ |
38
|
4 |
|
$this->cache = $cache; |
39
|
4 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @throws InvalidArgumentException |
43
|
|
|
*/ |
44
|
2 |
|
public function render(): string |
45
|
|
|
{ |
46
|
2 |
|
$self = $this; |
47
|
|
|
|
48
|
2 |
|
return $this->cache->get( |
49
|
2 |
|
$this->createKey(), |
50
|
2 |
|
function (ItemInterface $item) use ($self) { |
51
|
1 |
|
$item->expiresAfter($self->getOption('ttl')); |
52
|
|
|
|
53
|
1 |
|
return (string)$self->getOption('widget'); |
54
|
2 |
|
} |
55
|
2 |
|
) ?? ''; |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
private function createKey(): string |
59
|
|
|
{ |
60
|
2 |
|
return sprintf( |
61
|
2 |
|
'__%1$s%2$s__%3$s__', |
62
|
2 |
|
$this->getOption('namespace') . self::$prefix, |
63
|
2 |
|
strtolower(object_ucnp($this->getOption('widget'))), |
64
|
2 |
|
$this->getId() |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function getName(): string |
69
|
|
|
{ |
70
|
1 |
|
return 'cacheable_widget'; |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
protected function configureOptions(OptionsResolver $resolver): void |
74
|
|
|
{ |
75
|
|
|
$resolver |
76
|
3 |
|
->setDefined( |
77
|
|
|
[ |
78
|
3 |
|
'widget', |
79
|
|
|
'namespace', |
80
|
|
|
'ttl', |
81
|
|
|
] |
82
|
|
|
) |
83
|
3 |
|
->setRequired(['widget']) |
84
|
3 |
|
->addAllowedTypes('widget', ['object']) |
85
|
3 |
|
->addAllowedTypes('namespace', ['string']) |
86
|
3 |
|
->addAllowedTypes('ttl', ['int']) |
87
|
3 |
|
->setDefaults( |
88
|
|
|
[ |
89
|
3 |
|
'namespace' => '__widget__', |
90
|
|
|
'ttl' => 3600, |
91
|
|
|
] |
92
|
|
|
) |
93
|
3 |
|
->setNormalizer( |
94
|
3 |
|
'widget', |
95
|
3 |
|
function (Options $options, $value) { |
96
|
3 |
|
if (!$value instanceof WidgetInterface) { |
97
|
1 |
|
throw new NotAWidgetException( |
98
|
1 |
|
sprintf( |
99
|
1 |
|
'Given object must implement %1$s interface', |
100
|
1 |
|
WidgetInterface::class |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
return $value; |
106
|
3 |
|
} |
107
|
|
|
); |
108
|
3 |
|
} |
109
|
|
|
} |
110
|
|
|
|