From c65a4c8a69ae7771ebced9bc867ebb78d2ef428d Mon Sep 17 00:00:00 2001 From: wadjeroudi Date: Mon, 13 Jun 2022 10:45:35 +0200 Subject: [PATCH] update flysystem to 2.0 --- composer.json | 4 ++-- src/Controller/Action/Shop/ShowFeedAction.php | 6 ++--- .../Compiler/RegisterFilesystemPass.php | 8 +++---- .../DeleteGeneratedFilesSubscriber.php | 12 +++++----- .../MoveGeneratedFeedSubscriber.php | 16 ++++++------- src/Generator/TemporaryFeedPathGenerator.php | 6 ++--- .../Handler/FinishGenerationHandler.php | 24 ++++++------------- src/Message/Handler/GenerateBatchHandler.php | 11 ++++----- .../Behat/Context/Cli/ProcessFeedsContext.php | 12 +++++----- 9 files changed, 43 insertions(+), 56 deletions(-) diff --git a/composer.json b/composer.json index 984e741c..a7c9dd2c 100644 --- a/composer.json +++ b/composer.json @@ -10,8 +10,8 @@ "doctrine/orm": "^2.7", "doctrine/persistence": "^1.3 || ^2.0", "knplabs/knp-menu": "^3.1", - "league/flysystem": "^1.1", - "league/flysystem-bundle": "^1.1", + "league/flysystem": "^2.0", + "league/flysystem-bundle": "^2.2", "liip/imagine-bundle": "^2.4", "psr/event-dispatcher": "^1.0", "psr/log": "^1.1", diff --git a/src/Controller/Action/Shop/ShowFeedAction.php b/src/Controller/Action/Shop/ShowFeedAction.php index 4aa6871a..5a35fd16 100644 --- a/src/Controller/Action/Shop/ShowFeedAction.php +++ b/src/Controller/Action/Shop/ShowFeedAction.php @@ -4,7 +4,7 @@ namespace Setono\SyliusFeedPlugin\Controller\Action\Shop; -use League\Flysystem\FilesystemInterface; +use League\Flysystem\FilesystemOperator; use RuntimeException; use Setono\SyliusFeedPlugin\Generator\FeedPathGeneratorInterface; use Setono\SyliusFeedPlugin\Repository\FeedRepositoryInterface; @@ -24,7 +24,7 @@ final class ShowFeedAction private FeedPathGeneratorInterface $feedPathGenerator; - private FilesystemInterface $filesystem; + private FilesystemOperator $filesystem; private MimeTypesInterface $mimeTypes; @@ -33,7 +33,7 @@ public function __construct( ChannelContextInterface $channelContext, LocaleContextInterface $localeContext, FeedPathGeneratorInterface $feedPathGenerator, - FilesystemInterface $filesystem, + FilesystemOperator $filesystem, MimeTypesInterface $mimeTypes ) { $this->repository = $repository; diff --git a/src/DependencyInjection/Compiler/RegisterFilesystemPass.php b/src/DependencyInjection/Compiler/RegisterFilesystemPass.php index d3358b65..0cab5e40 100644 --- a/src/DependencyInjection/Compiler/RegisterFilesystemPass.php +++ b/src/DependencyInjection/Compiler/RegisterFilesystemPass.php @@ -5,7 +5,7 @@ namespace Setono\SyliusFeedPlugin\DependencyInjection\Compiler; use InvalidArgumentException; -use League\Flysystem\FilesystemInterface; +use League\Flysystem\FilesystemOperator; use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -38,13 +38,13 @@ public function process(ContainerBuilder $container): void $definitionClass = $container->getDefinition($parameterValue)->getClass(); Assert::notNull($definitionClass); - if (!is_a($definitionClass, FilesystemInterface::class, true)) { + if (!is_a($definitionClass, FilesystemOperator::class, true)) { throw new InvalidDefinitionException(sprintf( 'The config parameter "%s" references a service %s, which is not an instance of %s. Fix this by creating a valid service that implements %s.', $parameter, $definitionClass, - FilesystemInterface::class, - FilesystemInterface::class + FilesystemOperator::class, + FilesystemOperator::class )); } diff --git a/src/EventListener/DeleteGeneratedFilesSubscriber.php b/src/EventListener/DeleteGeneratedFilesSubscriber.php index e836b7a6..c178c59e 100644 --- a/src/EventListener/DeleteGeneratedFilesSubscriber.php +++ b/src/EventListener/DeleteGeneratedFilesSubscriber.php @@ -4,8 +4,8 @@ namespace Setono\SyliusFeedPlugin\EventListener; -use League\Flysystem\FilesystemInterface; -use League\Flysystem\RootViolationException; +use League\Flysystem\FilesystemOperator; +use League\Flysystem\UnableToDeleteDirectory; use Setono\SyliusFeedPlugin\Model\FeedInterface; use Setono\SyliusFeedPlugin\Workflow\FeedGraph; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -14,9 +14,9 @@ final class DeleteGeneratedFilesSubscriber implements EventSubscriberInterface { - private FilesystemInterface $filesystem; + private FilesystemOperator $filesystem; - public function __construct(FilesystemInterface $filesystem) + public function __construct(FilesystemOperator $filesystem) { $this->filesystem = $filesystem; } @@ -38,8 +38,8 @@ public function delete(TransitionEvent $event): void Assert::isInstanceOf($feed, FeedInterface::class); try { - $this->filesystem->deleteDir($feed->getCode()); - } catch (RootViolationException $e) { + $this->filesystem->deleteDirectory($feed->getCode()); + } catch (UnableToDeleteDirectory $e) { } } } diff --git a/src/EventListener/MoveGeneratedFeedSubscriber.php b/src/EventListener/MoveGeneratedFeedSubscriber.php index 273b975a..aa142e9b 100644 --- a/src/EventListener/MoveGeneratedFeedSubscriber.php +++ b/src/EventListener/MoveGeneratedFeedSubscriber.php @@ -4,8 +4,8 @@ namespace Setono\SyliusFeedPlugin\EventListener; -use League\Flysystem\FileNotFoundException; -use League\Flysystem\FilesystemInterface; +use League\Flysystem\FilesystemOperator; +use League\Flysystem\UnableToDeleteFile; use RuntimeException; use Setono\SyliusFeedPlugin\Generator\FeedPathGeneratorInterface; use Setono\SyliusFeedPlugin\Generator\TemporaryFeedPathGenerator; @@ -17,17 +17,17 @@ final class MoveGeneratedFeedSubscriber implements EventSubscriberInterface { - private FilesystemInterface $temporaryFilesystem; + private FilesystemOperator $temporaryFilesystem; - private FilesystemInterface $filesystem; + private FilesystemOperator $filesystem; private FeedPathGeneratorInterface $temporaryFeedPathGenerator; private FeedPathGeneratorInterface $feedPathGenerator; public function __construct( - FilesystemInterface $temporaryFilesystem, - FilesystemInterface $filesystem, + FilesystemOperator $temporaryFilesystem, + FilesystemOperator $filesystem, FeedPathGeneratorInterface $temporaryFeedPathGenerator, FeedPathGeneratorInterface $feedPathGenerator ) { @@ -86,10 +86,10 @@ public function move(TransitionEvent $event): void try { $this->filesystem->delete((string) $newPath); - } catch (FileNotFoundException $e) { + } catch (UnableToDeleteFile $e) { } - $this->filesystem->rename($path, (string) $newPath); + $this->filesystem->move($path, (string) $newPath); $this->temporaryFilesystem->delete((string) $temporaryPath); } diff --git a/src/Generator/TemporaryFeedPathGenerator.php b/src/Generator/TemporaryFeedPathGenerator.php index 30708d09..a1c8dd4e 100644 --- a/src/Generator/TemporaryFeedPathGenerator.php +++ b/src/Generator/TemporaryFeedPathGenerator.php @@ -4,7 +4,7 @@ namespace Setono\SyliusFeedPlugin\Generator; -use League\Flysystem\FilesystemInterface; +use League\Flysystem\FilesystemOperator; use Setono\SyliusFeedPlugin\Model\FeedInterface; use SplFileInfo; use Webmozart\Assert\Assert; @@ -29,11 +29,11 @@ public static function getBaseFile(SplFileInfo $dir): SplFileInfo return new SplFileInfo($dir->getPathname() . '/' . self::BASE_FILENAME); } - public static function getPartialFile(SplFileInfo $dir, FilesystemInterface $filesystem): SplFileInfo + public static function getPartialFile(SplFileInfo $dir, FilesystemOperator $filesystem): SplFileInfo { do { $path = $dir->getPathname() . '/' . uniqid('partial-', true); - } while ($filesystem->has($path)); + } while ($filesystem->fileExists($path)); return new SplFileInfo($path); } diff --git a/src/Message/Handler/FinishGenerationHandler.php b/src/Message/Handler/FinishGenerationHandler.php index 23ea657b..1b096905 100644 --- a/src/Message/Handler/FinishGenerationHandler.php +++ b/src/Message/Handler/FinishGenerationHandler.php @@ -6,7 +6,7 @@ use Doctrine\Persistence\ObjectManager; use InvalidArgumentException; -use League\Flysystem\FilesystemInterface; +use League\Flysystem\FilesystemOperator; use Psr\Log\LoggerInterface; use RuntimeException; use Setono\SyliusFeedPlugin\FeedType\FeedTypeInterface; @@ -24,7 +24,6 @@ use Symfony\Component\Workflow\Registry; use Throwable; use Twig\Environment; -use Webmozart\Assert\Assert; final class FinishGenerationHandler implements MessageHandlerInterface { @@ -32,7 +31,7 @@ final class FinishGenerationHandler implements MessageHandlerInterface private ObjectManager $feedManager; - private FilesystemInterface $filesystem; + private FilesystemOperator $filesystem; private Registry $workflowRegistry; @@ -47,7 +46,7 @@ final class FinishGenerationHandler implements MessageHandlerInterface public function __construct( FeedRepositoryInterface $feedRepository, ObjectManager $feedManager, - FilesystemInterface $filesystem, + FilesystemOperator $filesystem, Registry $workflowRegistry, Environment $twig, FeedTypeRegistryInterface $feedTypeRegistry, @@ -93,17 +92,12 @@ public function __invoke(FinishGeneration $message): void fwrite($batchStream, $feedStart); $files = $this->filesystem->listContents((string) $dir); - /** @var array{basename: string, path: string} $file */ foreach ($files as $file) { - Assert::isArray($file); - Assert::keyExists($file, 'basename'); - Assert::keyExists($file, 'path'); - - if (TemporaryFeedPathGenerator::BASE_FILENAME === $file['basename']) { + if (TemporaryFeedPathGenerator::BASE_FILENAME === basename($file->path())) { continue; } - $fp = $this->filesystem->readStream($file['path']); + $fp = $this->filesystem->readStream($file->path()); if (false === $fp) { throw new \RuntimeException(sprintf( 'The file "%s" could not be opened as a resource', @@ -117,19 +111,15 @@ public function __invoke(FinishGeneration $message): void fclose($fp); - $this->filesystem->delete($file['path']); + $this->filesystem->delete($file->path()); } fwrite($batchStream, $feedEnd); - $res = $this->filesystem->writeStream((string) TemporaryFeedPathGenerator::getBaseFile($dir), $batchStream); + $this->filesystem->writeStream((string) TemporaryFeedPathGenerator::getBaseFile($dir), $batchStream); // tries to close the file pointer although it may already have been closed by flysystem fclose($batchStream); - - if (false === $res) { - throw new RuntimeException('An error occurred when trying to write the finished feed write'); - } } } diff --git a/src/Message/Handler/GenerateBatchHandler.php b/src/Message/Handler/GenerateBatchHandler.php index 5a8235d0..bb171393 100644 --- a/src/Message/Handler/GenerateBatchHandler.php +++ b/src/Message/Handler/GenerateBatchHandler.php @@ -11,7 +11,7 @@ use const JSON_PRETTY_PRINT; use const JSON_UNESCAPED_SLASHES; use const JSON_UNESCAPED_UNICODE; -use League\Flysystem\FilesystemInterface; +use League\Flysystem\FilesystemOperator; use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; use Setono\SyliusFeedPlugin\Event\BatchGeneratedEvent; @@ -42,7 +42,6 @@ use Symfony\Component\Workflow\Workflow; use Throwable; use Twig\Environment; -use Webmozart\Assert\Assert; final class GenerateBatchHandler implements MessageHandlerInterface { @@ -58,7 +57,7 @@ final class GenerateBatchHandler implements MessageHandlerInterface private Environment $twig; - private FilesystemInterface $filesystem; + private FilesystemOperator $filesystem; private FeedPathGeneratorInterface $temporaryFeedPathGenerator; @@ -83,7 +82,7 @@ public function __construct( ObjectManager $feedManager, FeedTypeRegistryInterface $feedTypeRegistry, Environment $twig, - FilesystemInterface $filesystem, + FilesystemOperator $filesystem, FeedPathGeneratorInterface $temporaryFeedPathGenerator, EventDispatcherInterface $eventDispatcher, Registry $workflowRegistry, @@ -206,12 +205,10 @@ public function __invoke(GenerateBatch $message): void $dir = $this->temporaryFeedPathGenerator->generate($feed, (string) $channel->getCode(), (string) $locale->getCode()); $path = TemporaryFeedPathGenerator::getPartialFile($dir, $this->filesystem); - $res = $this->filesystem->writeStream((string) $path, $stream); + $this->filesystem->writeStream((string) $path, $stream); fclose($stream); - Assert::true($res, 'An error occurred when trying to write a feed item'); - $this->feedManager->flush(); $this->feedManager->clear(); diff --git a/tests/Behat/Context/Cli/ProcessFeedsContext.php b/tests/Behat/Context/Cli/ProcessFeedsContext.php index 2cc3fd96..dfdc9612 100644 --- a/tests/Behat/Context/Cli/ProcessFeedsContext.php +++ b/tests/Behat/Context/Cli/ProcessFeedsContext.php @@ -6,7 +6,7 @@ use Behat\Behat\Context\Context; use InvalidArgumentException; -use League\Flysystem\FilesystemInterface; +use League\Flysystem\FilesystemOperator; use Setono\SyliusFeedPlugin\Command\ProcessFeedsCommand; use Setono\SyliusFeedPlugin\Generator\FeedPathGeneratorInterface; use Setono\SyliusFeedPlugin\Model\FeedInterface; @@ -32,7 +32,7 @@ final class ProcessFeedsContext implements Context /** @var ProcessFeedsCommand */ private $command; - /** @var FilesystemInterface */ + /** @var FilesystemOperator */ private $filesystem; /** @var FeedProcessorInterface */ @@ -46,7 +46,7 @@ final class ProcessFeedsContext implements Context public function __construct( KernelInterface $kernel, - FilesystemInterface $filesystem, + FilesystemOperator $filesystem, FeedProcessorInterface $processor, FeedPathGeneratorInterface $feedPathGenerator, RepositoryInterface $feedRepository @@ -94,12 +94,12 @@ public function aFileShouldExistWithTheRightContent(): void /** @var ChannelInterface $channel */ foreach ($feed->getChannels() as $channel) { foreach ($channel->getLocales() as $locale) { - $path = $this->feedPathGenerator->generate($feed, $channel->getCode(), $locale->getCode()); + $file = $this->feedPathGenerator->generate($feed, $channel->getCode(), $locale->getCode()); - Assert::true($this->filesystem->has($path)); + Assert::true($this->filesystem->fileExists($file->getRealPath())); $expectedContent = $this->getExpectedContent($channel->getCode()); - $actualContent = $this->removeWhitespace($this->filesystem->read($path)); + $actualContent = $this->removeWhitespace($this->filesystem->read($file->getRealPath())); $actualContent = $this->normalizeImageLink($actualContent); Assert::same($actualContent, $expectedContent);