|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of phpFastCache. |
|
6
|
|
|
* |
|
7
|
|
|
* @license MIT License (MIT) |
|
8
|
|
|
* |
|
9
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
|
10
|
|
|
* |
|
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
|
12
|
|
|
* @author PastisD https://github.com/PastisD |
|
13
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
declare(strict_types=1); |
|
17
|
|
|
|
|
18
|
|
|
namespace Phpfastcache\Bundle\Command; |
|
19
|
|
|
|
|
20
|
|
|
use Phpfastcache\Bundle\Service\Phpfastcache; |
|
21
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException; |
|
22
|
|
|
use Symfony\Component\Console\Command\Command; |
|
23
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
24
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
25
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
26
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
27
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
28
|
|
|
|
|
29
|
|
|
class PhpfastcacheSetCommand extends Command |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var \Phpfastcache\Bundle\Service\Phpfastcache |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $phpfastcache; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $parameters; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* PhpfastcacheCommand constructor. |
|
43
|
|
|
* @param \Phpfastcache\Bundle\Service\Phpfastcache $phpfastcache |
|
44
|
|
|
* @param $parameters |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(Phpfastcache $phpfastcache, $parameters) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->phpfastcache = $phpfastcache; |
|
49
|
|
|
$this->parameters = $parameters; |
|
50
|
|
|
|
|
51
|
|
|
parent::__construct(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function configure() |
|
55
|
|
|
{ |
|
56
|
|
|
$this |
|
57
|
|
|
->setName('phpfastcache:set') |
|
58
|
|
|
->setDescription('Set phpfastcache cache value') |
|
59
|
|
|
->addArgument( |
|
60
|
|
|
'driver', |
|
61
|
|
|
InputArgument::REQUIRED, |
|
62
|
|
|
'Cache name to clear' |
|
63
|
|
|
) |
|
64
|
|
|
->addArgument( |
|
65
|
|
|
'key', |
|
66
|
|
|
InputArgument::REQUIRED, |
|
67
|
|
|
'Cache key' |
|
68
|
|
|
) |
|
69
|
|
|
->addArgument( |
|
70
|
|
|
'value', |
|
71
|
|
|
InputArgument::REQUIRED, |
|
72
|
|
|
'Cache value' |
|
73
|
|
|
) |
|
74
|
|
|
->addArgument( |
|
75
|
|
|
'ttl', |
|
76
|
|
|
InputArgument::OPTIONAL, |
|
77
|
|
|
'Cache ttl (in second)' |
|
78
|
|
|
) |
|
79
|
|
|
->addOption( |
|
80
|
|
|
'auto-type-cast', |
|
81
|
|
|
'a', |
|
82
|
|
|
InputOption::VALUE_OPTIONAL, |
|
83
|
|
|
'Allows to automatically type-cast the value of the cache item.', |
|
84
|
|
|
1 |
|
85
|
|
|
) |
|
86
|
|
|
; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
|
91
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
|
92
|
|
|
* @return int|null|void |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
95
|
|
|
{ |
|
96
|
|
|
$io = new SymfonyStyle($input, $output); |
|
97
|
|
|
$caches = $this->parameters; |
|
98
|
|
|
|
|
99
|
|
|
$driver = $input->getArgument('driver'); |
|
100
|
|
|
$cacheKey = $input->getArgument('key'); |
|
101
|
|
|
$cacheValue = $input->getArgument('value'); |
|
102
|
|
|
$cacheTtl = $input->getArgument('ttl'); |
|
103
|
|
|
$autoTypeCast = (bool) $input->getOption('auto-type-cast'); |
|
104
|
|
|
|
|
105
|
|
|
if (\array_key_exists($driver, $caches[ 'drivers' ])) { |
|
106
|
|
|
$io->section($driver); |
|
107
|
|
|
$driverInstance = $this->phpfastcache->get($driver); |
|
108
|
|
|
$cacheItem = $driverInstance->getItem($cacheKey); |
|
109
|
|
|
$castedCacheValue = $this->autoTypeCast($cacheValue); |
|
110
|
|
|
|
|
111
|
|
|
$cacheItem->set($autoTypeCast ? $castedCacheValue : $cacheValue); |
|
112
|
|
|
|
|
113
|
|
|
if($cacheTtl){ |
|
114
|
|
|
if(\is_numeric($cacheTtl)){ |
|
115
|
|
|
$cacheItem->expiresAfter($cacheTtl); |
|
116
|
|
|
}else{ |
|
117
|
|
|
$io->error(\sprintf('Invalid ttl value format "%s", must be a valid integer, aborting...', $cacheTtl)); |
|
118
|
|
|
return; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if(!$autoTypeCast || $castedCacheValue === $cacheValue) { |
|
123
|
|
|
$io->success(\sprintf( |
|
124
|
|
|
'Cache item "%s" set to "%s" for %d seconds', |
|
125
|
|
|
$cacheKey, |
|
126
|
|
|
$cacheValue, |
|
127
|
|
|
$cacheItem->getTtl() |
|
128
|
|
|
)); |
|
129
|
|
|
} else { |
|
130
|
|
|
$io->success(\sprintf( |
|
131
|
|
|
'Cache item "%s" set to "%s" for %d seconds (automatically type-casted to %s)', |
|
132
|
|
|
$cacheKey, |
|
133
|
|
|
$cacheValue, |
|
134
|
|
|
$cacheItem->getTtl(), |
|
135
|
|
|
\gettype($castedCacheValue) |
|
136
|
|
|
)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$driverInstance->save($cacheItem); |
|
140
|
|
|
} else { |
|
141
|
|
|
$io->error("Cache instance {$driver} does not exists"); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param $string |
|
147
|
|
|
* @return bool|float|int|null|array |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function autoTypeCast($string) |
|
150
|
|
|
{ |
|
151
|
|
|
if(\in_array(\strtolower($string), ['true', 'false'], true)){ |
|
152
|
|
|
return $string === 'true'; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if(\strtolower($string) === 'null'){ |
|
156
|
|
|
return null; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if(\is_numeric($string)) |
|
160
|
|
|
{ |
|
161
|
|
|
if(\strpos($string, '.') !== false){ |
|
162
|
|
|
return (float) $string; |
|
163
|
|
|
} |
|
164
|
|
|
return (int) $string; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$jsonArray = json_decode($string, true); |
|
168
|
|
|
if(json_last_error() === JSON_ERROR_NONE){ |
|
169
|
|
|
return (array) $jsonArray; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $string; |
|
173
|
|
|
} |
|
174
|
|
|
} |