summaryrefslogtreecommitdiff
path: root/vendor/symfony/cache/Messenger/EarlyExpirationMessage.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/cache/Messenger/EarlyExpirationMessage.php')
-rw-r--r--vendor/symfony/cache/Messenger/EarlyExpirationMessage.php100
1 files changed, 100 insertions, 0 deletions
diff --git a/vendor/symfony/cache/Messenger/EarlyExpirationMessage.php b/vendor/symfony/cache/Messenger/EarlyExpirationMessage.php
new file mode 100644
index 0000000..6056eba
--- /dev/null
+++ b/vendor/symfony/cache/Messenger/EarlyExpirationMessage.php
@@ -0,0 +1,100 @@
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Cache\Messenger;
13
14use Symfony\Component\Cache\Adapter\AdapterInterface;
15use Symfony\Component\Cache\CacheItem;
16use Symfony\Component\DependencyInjection\ReverseContainer;
17
18/**
19 * Conveys a cached value that needs to be computed.
20 */
21final class EarlyExpirationMessage
22{
23 private CacheItem $item;
24 private string $pool;
25 private string|array $callback;
26
27 public static function create(ReverseContainer $reverseContainer, callable $callback, CacheItem $item, AdapterInterface $pool): ?self
28 {
29 try {
30 $item = clone $item;
31 $item->set(null);
32 } catch (\Exception) {
33 return null;
34 }
35
36 $pool = $reverseContainer->getId($pool);
37
38 if (\is_object($callback)) {
39 if (null === $id = $reverseContainer->getId($callback)) {
40 return null;
41 }
42
43 $callback = '@'.$id;
44 } elseif (!\is_array($callback)) {
45 $callback = (string) $callback;
46 } elseif (!\is_object($callback[0])) {
47 $callback = [(string) $callback[0], (string) $callback[1]];
48 } else {
49 if (null === $id = $reverseContainer->getId($callback[0])) {
50 return null;
51 }
52
53 $callback = ['@'.$id, (string) $callback[1]];
54 }
55
56 return new self($item, $pool, $callback);
57 }
58
59 public function getItem(): CacheItem
60 {
61 return $this->item;
62 }
63
64 public function getPool(): string
65 {
66 return $this->pool;
67 }
68
69 /**
70 * @return string|string[]
71 */
72 public function getCallback(): string|array
73 {
74 return $this->callback;
75 }
76
77 public function findPool(ReverseContainer $reverseContainer): AdapterInterface
78 {
79 return $reverseContainer->getService($this->pool);
80 }
81
82 public function findCallback(ReverseContainer $reverseContainer): callable
83 {
84 if (\is_string($callback = $this->callback)) {
85 return '@' === $callback[0] ? $reverseContainer->getService(substr($callback, 1)) : $callback;
86 }
87 if ('@' === $callback[0][0]) {
88 $callback[0] = $reverseContainer->getService(substr($callback[0], 1));
89 }
90
91 return $callback;
92 }
93
94 private function __construct(CacheItem $item, string $pool, string|array $callback)
95 {
96 $this->item = $item;
97 $this->pool = $pool;
98 $this->callback = $callback;
99 }
100}