2021-03-18 01:11:54 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Caching;
|
2021-03-18 01:11:54 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
|
|
|
|
use Rector\Caching\ValueObject\Storage\MemoryCacheStorage;
|
|
|
|
use Rector\Core\Configuration\Option;
|
2022-09-01 19:50:06 +00:00
|
|
|
use Rector\Core\Configuration\Parameter\ParameterProvider;
|
2023-05-01 01:36:32 +00:00
|
|
|
use RectorPrefix202305\Symfony\Component\Filesystem\Filesystem;
|
2021-06-11 11:03:31 +00:00
|
|
|
final class CacheFactory
|
2021-03-18 01:11:54 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2022-09-01 19:50:06 +00:00
|
|
|
* @var \Rector\Core\Configuration\Parameter\ParameterProvider
|
2021-03-18 01:11:54 +00:00
|
|
|
*/
|
|
|
|
private $parameterProvider;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2022-08-29 22:42:57 +00:00
|
|
|
* @var \Symfony\Component\Filesystem\Filesystem
|
2021-03-18 01:11:54 +00:00
|
|
|
*/
|
2022-08-29 22:42:57 +00:00
|
|
|
private $fileSystem;
|
|
|
|
public function __construct(ParameterProvider $parameterProvider, Filesystem $fileSystem)
|
2021-03-18 01:11:54 +00:00
|
|
|
{
|
|
|
|
$this->parameterProvider = $parameterProvider;
|
2022-08-29 22:42:57 +00:00
|
|
|
$this->fileSystem = $fileSystem;
|
2021-03-18 01:11:54 +00:00
|
|
|
}
|
2022-12-23 17:10:25 +00:00
|
|
|
/**
|
|
|
|
* @api config factory
|
|
|
|
*/
|
2022-06-06 17:12:56 +00:00
|
|
|
public function create() : \Rector\Caching\Cache
|
2021-03-18 01:11:54 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$cacheDirectory = $this->parameterProvider->provideStringParameter(Option::CACHE_DIR);
|
|
|
|
$cacheClass = FileCacheStorage::class;
|
|
|
|
if ($this->parameterProvider->hasParameter(Option::CACHE_CLASS)) {
|
|
|
|
$cacheClass = $this->parameterProvider->provideStringParameter(Option::CACHE_CLASS);
|
2021-03-18 01:11:54 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($cacheClass === FileCacheStorage::class) {
|
2021-07-28 07:05:44 +00:00
|
|
|
// ensure cache directory exists
|
2022-08-29 22:42:57 +00:00
|
|
|
if (!$this->fileSystem->exists($cacheDirectory)) {
|
|
|
|
$this->fileSystem->mkdir($cacheDirectory);
|
2021-07-28 07:05:44 +00:00
|
|
|
}
|
2022-08-29 22:42:57 +00:00
|
|
|
$fileCacheStorage = new FileCacheStorage($cacheDirectory, $this->fileSystem);
|
2022-06-06 17:12:56 +00:00
|
|
|
return new \Rector\Caching\Cache($fileCacheStorage);
|
2021-07-28 07:05:44 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new \Rector\Caching\Cache(new MemoryCacheStorage());
|
2021-03-18 01:11:54 +00:00
|
|
|
}
|
|
|
|
}
|