2021-03-18 01:11:54 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 16:43:29 +00:00
|
|
|
namespace RectorPrefix20220606\Rector\Caching;
|
2021-03-18 01:11:54 +00:00
|
|
|
|
2022-06-06 16:43:29 +00:00
|
|
|
use RectorPrefix20220606\Rector\Caching\ValueObject\Storage\FileCacheStorage;
|
|
|
|
use RectorPrefix20220606\Rector\Caching\ValueObject\Storage\MemoryCacheStorage;
|
|
|
|
use RectorPrefix20220606\Rector\Core\Configuration\Option;
|
2022-06-06 06:00:07 +00:00
|
|
|
use RectorPrefix20220606\Symplify\PackageBuilder\Parameter\ParameterProvider;
|
|
|
|
use RectorPrefix20220606\Symplify\SmartFileSystem\SmartFileSystem;
|
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
|
2021-08-23 00:20:32 +00:00
|
|
|
* @var \Symplify\PackageBuilder\Parameter\ParameterProvider
|
2021-03-18 01:11:54 +00:00
|
|
|
*/
|
|
|
|
private $parameterProvider;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-08-23 00:20:32 +00:00
|
|
|
* @var \Symplify\SmartFileSystem\SmartFileSystem
|
2021-03-18 01:11:54 +00:00
|
|
|
*/
|
|
|
|
private $smartFileSystem;
|
2022-06-06 16:43:29 +00:00
|
|
|
public function __construct(ParameterProvider $parameterProvider, SmartFileSystem $smartFileSystem)
|
2021-03-18 01:11:54 +00:00
|
|
|
{
|
|
|
|
$this->parameterProvider = $parameterProvider;
|
|
|
|
$this->smartFileSystem = $smartFileSystem;
|
|
|
|
}
|
2022-06-06 16:43:29 +00:00
|
|
|
public function create() : Cache
|
2021-03-18 01:11:54 +00:00
|
|
|
{
|
2022-06-06 16:43: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-06 16:43:29 +00:00
|
|
|
if ($cacheClass === FileCacheStorage::class) {
|
2021-07-28 07:05:44 +00:00
|
|
|
// ensure cache directory exists
|
|
|
|
if (!$this->smartFileSystem->exists($cacheDirectory)) {
|
|
|
|
$this->smartFileSystem->mkdir($cacheDirectory);
|
|
|
|
}
|
2022-06-06 16:43:29 +00:00
|
|
|
$fileCacheStorage = new FileCacheStorage($cacheDirectory, $this->smartFileSystem);
|
|
|
|
return new Cache($fileCacheStorage);
|
2021-07-28 07:05:44 +00:00
|
|
|
}
|
2022-06-06 16:43:29 +00:00
|
|
|
return new Cache(new MemoryCacheStorage());
|
2021-03-18 01:11:54 +00:00
|
|
|
}
|
|
|
|
}
|