Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit 1c39853

Browse files
committed
Enabled smart type casting for CLI "set" command
1 parent 0314dd4 commit 1c39853

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

src/Command/PhpfastcacheSetCommand.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Console\Command\Command;
2323
use Symfony\Component\Console\Input\InputArgument;
2424
use Symfony\Component\Console\Input\InputInterface;
25+
use Symfony\Component\Console\Input\InputOption;
2526
use Symfony\Component\Console\Output\OutputInterface;
2627
use Symfony\Component\Console\Style\SymfonyStyle;
2728

@@ -74,7 +75,15 @@ protected function configure()
7475
'ttl',
7576
InputArgument::OPTIONAL,
7677
'Cache ttl (in second)'
77-
);
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+
;
7887
}
7988

8089
/**
@@ -91,12 +100,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
91100
$cacheKey = $input->getArgument('key');
92101
$cacheValue = $input->getArgument('value');
93102
$cacheTtl = $input->getArgument('ttl');
103+
$autoTypeCast = $input->getOption('auto-type-cast');
94104

95105
if (\array_key_exists($driver, $caches[ 'drivers' ])) {
96106
$io->section($driver);
97107
$driverInstance = $this->phpfastcache->get($driver);
98108
$cacheItem = $driverInstance->getItem($cacheKey);
99-
$cacheItem->set($cacheValue);
109+
$castedCacheValue = (bool) $this->autoTypeCast($cacheValue);
110+
111+
$cacheItem->set($autoTypeCast ? $castedCacheValue : $cacheValue);
100112

101113
if($cacheTtl){
102114
if(\is_numeric($cacheTtl)){
@@ -107,11 +119,45 @@ protected function execute(InputInterface $input, OutputInterface $output)
107119
}
108120
}
109121

110-
$io->success(\sprintf('Cache item "%s" set to "%s" for %d seconds', $cacheKey, $cacheValue, $cacheItem->getTtl()));
122+
if($autoTypeCast && $castedCacheValue !== $cacheValue){
123+
$io->success(\sprintf('Cache item "%s" set to "%s" for %d seconds (automatically type-casted to %s)', $cacheKey, $cacheValue, $cacheItem->getTtl(), \gettype($castedCacheValue)));
124+
}else{
125+
$io->success(\sprintf('Cache item "%s" set to "%s" for %d seconds', $cacheKey, $cacheValue, $cacheItem->getTtl()));
126+
}
111127

112128
$driverInstance->save($cacheItem);
113129
} else {
114130
$io->error("Cache instance {$driver} does not exists");
115131
}
116132
}
133+
134+
/**
135+
* @param $string
136+
* @return bool|float|int|null|array
137+
*/
138+
protected function autoTypeCast($string)
139+
{
140+
if(\in_array($string, ['true', 'false'], true)){
141+
return $string === 'true';
142+
}
143+
144+
if($string === 'null'){
145+
return null;
146+
}
147+
148+
if(\is_numeric($string))
149+
{
150+
if(\strpos($string, '.') !== false){
151+
return (float) $string;
152+
}
153+
return (int) $string;
154+
}
155+
156+
$jsonArray = json_decode($string, true);
157+
if(json_last_error() === JSON_ERROR_NONE){
158+
return (array) $jsonArray;
159+
}
160+
161+
return $string;
162+
}
117163
}

0 commit comments

Comments
 (0)