From 85f27f270e5b113b84fa7476444e4a5c8149253f Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 9 Oct 2019 11:02:41 +0200 Subject: [PATCH] [Cache] remove implicit dependency on symfony/filesystem --- .../Adapter/FilesystemTagAwareAdapter.php | 31 ++++++------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/src/Symfony/Component/Cache/Adapter/FilesystemTagAwareAdapter.php b/src/Symfony/Component/Cache/Adapter/FilesystemTagAwareAdapter.php index 0dd81a99704ed..899e570ef6a36 100644 --- a/src/Symfony/Component/Cache/Adapter/FilesystemTagAwareAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/FilesystemTagAwareAdapter.php @@ -16,7 +16,6 @@ use Symfony\Component\Cache\Marshaller\MarshallerInterface; use Symfony\Component\Cache\PruneableInterface; use Symfony\Component\Cache\Traits\FilesystemTrait; -use Symfony\Component\Filesystem\Filesystem; /** * Stores tag id <> cache id relationship as a symlink, and lookup on invalidation calls. @@ -29,8 +28,8 @@ class FilesystemTagAwareAdapter extends AbstractTagAwareAdapter implements PruneableInterface { use FilesystemTrait { - doSave as doSaveCache; - doDelete as doDeleteCache; + doSave as private doSaveCache; + doDelete as private doDeleteCache; } /** @@ -38,11 +37,6 @@ class FilesystemTagAwareAdapter extends AbstractTagAwareAdapter implements Prune */ private const TAG_FOLDER = 'tags'; - /** - * @var Filesystem|null - */ - private $fs; - public function __construct(string $namespace = '', int $defaultLifetime = 0, string $directory = null, MarshallerInterface $marshaller = null) { $this->marshaller = $marshaller ?? new DefaultMarshaller(); @@ -57,7 +51,6 @@ protected function doSave(array $values, ?int $lifetime, array $addTagData = [], { $failed = $this->doSaveCache($values, $lifetime); - $fs = $this->getFilesystem(); // Add Tags as symlinks foreach ($addTagData as $tagId => $ids) { $tagFolder = $this->getTagFolder($tagId); @@ -67,12 +60,15 @@ protected function doSave(array $values, ?int $lifetime, array $addTagData = [], } $file = $this->getFile($id); - $fs->symlink($file, $this->getFile($id, true, $tagFolder)); + + if (!@symlink($file, $this->getFile($id, true, $tagFolder))) { + @unlink($file); + $failed[] = $id; + } } } // Unlink removed Tags - $files = []; foreach ($removeTagData as $tagId => $ids) { $tagFolder = $this->getTagFolder($tagId); foreach ($ids as $id) { @@ -80,10 +76,9 @@ protected function doSave(array $values, ?int $lifetime, array $addTagData = [], continue; } - $files[] = $this->getFile($id, false, $tagFolder); + @unlink($this->getFile($id, false, $tagFolder)); } } - $fs->remove($files); return $failed; } @@ -96,15 +91,12 @@ protected function doDelete(array $ids, array $tagData = []): bool $ok = $this->doDeleteCache($ids); // Remove tags - $files = []; - $fs = $this->getFilesystem(); foreach ($tagData as $tagId => $idMap) { $tagFolder = $this->getTagFolder($tagId); foreach ($idMap as $id) { - $files[] = $this->getFile($id, false, $tagFolder); + @unlink($this->getFile($id, false, $tagFolder)); } } - $fs->remove($files); return $ok; } @@ -137,11 +129,6 @@ protected function doInvalidate(array $tagIds): bool return true; } - private function getFilesystem(): Filesystem - { - return $this->fs ?? $this->fs = new Filesystem(); - } - private function getTagFolder(string $tagId): string { return $this->getFile($tagId, false, $this->directory.self::TAG_FOLDER.\DIRECTORY_SEPARATOR).\DIRECTORY_SEPARATOR;