|
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\Widget\Factory\WidgetFactoryInterface; |
|
19
|
|
|
use Sauls\Component\Widget\Named; |
|
20
|
|
|
use Sauls\Component\Widget\Widget; |
|
21
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
22
|
|
|
use Symfony\Contracts\Cache\CacheInterface; |
|
23
|
|
|
use Symfony\Contracts\Cache\ItemInterface; |
|
24
|
|
|
|
|
25
|
|
|
use function sprintf; |
|
26
|
|
|
|
|
27
|
|
|
class CacheableWidget extends Widget implements Named |
|
28
|
|
|
{ |
|
29
|
|
|
public static $prefix = 'cw'; |
|
30
|
|
|
public static $total = 0; |
|
31
|
|
|
private CacheInterface $cache; |
|
32
|
|
|
private WidgetFactoryInterface $widgetFactory; |
|
33
|
|
|
|
|
34
|
3 |
|
public function __construct(CacheInterface $cache, WidgetFactoryInterface $widgetFactory) |
|
35
|
|
|
{ |
|
36
|
3 |
|
$this->cache = $cache; |
|
37
|
3 |
|
$this->widgetFactory = $widgetFactory; |
|
38
|
3 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @throws InvalidArgumentException |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function render(): string |
|
44
|
|
|
{ |
|
45
|
2 |
|
$self = $this; |
|
46
|
|
|
|
|
47
|
2 |
|
return $this->cache->get( |
|
48
|
2 |
|
$this->createKey(), |
|
49
|
2 |
|
function (ItemInterface $item) use ($self) { |
|
50
|
1 |
|
$item->expiresAfter($self->getOption('ttl')); |
|
51
|
|
|
|
|
52
|
1 |
|
return $self->resolveContentToCache(); |
|
53
|
2 |
|
} |
|
54
|
2 |
|
) ?? ''; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
private function createKey(): string |
|
58
|
|
|
{ |
|
59
|
2 |
|
return md5( |
|
60
|
2 |
|
sprintf( |
|
61
|
2 |
|
'__%1$s%2$s__%3$s__', |
|
62
|
2 |
|
$this->getOption('namespace') . self::$prefix, |
|
63
|
2 |
|
$this->getOption('widget.id'), |
|
64
|
2 |
|
$this->getId() |
|
65
|
|
|
) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
private function resolveContentToCache(): string |
|
70
|
|
|
{ |
|
71
|
1 |
|
return (string)$this->widgetFactory->create( |
|
72
|
1 |
|
$this->getOption('widget.id') ?? '', |
|
73
|
1 |
|
$this->getOption('widget.options') ?? [] |
|
74
|
1 |
|
) ?? ''; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
public function getName(): string |
|
78
|
|
|
{ |
|
79
|
1 |
|
return 'cacheable_widget'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
protected function configureOptions(OptionsResolver $resolver): void |
|
83
|
|
|
{ |
|
84
|
|
|
$resolver |
|
85
|
2 |
|
->setDefined( |
|
86
|
|
|
[ |
|
87
|
2 |
|
'widget', |
|
88
|
|
|
'namespace', |
|
89
|
|
|
'ttl', |
|
90
|
|
|
] |
|
91
|
|
|
) |
|
92
|
2 |
|
->addAllowedTypes('widget', ['array']) |
|
93
|
2 |
|
->addAllowedTypes('namespace', ['string']) |
|
94
|
2 |
|
->addAllowedTypes('ttl', ['int']) |
|
95
|
2 |
|
->setDefaults( |
|
96
|
|
|
[ |
|
97
|
2 |
|
'namespace' => '__widget__', |
|
98
|
|
|
'ttl' => 3600, |
|
99
|
|
|
'widget' => [ |
|
100
|
|
|
'id' => '', |
|
101
|
|
|
'options' => [], |
|
102
|
|
|
], |
|
103
|
|
|
] |
|
104
|
|
|
); |
|
105
|
2 |
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|