Skip to content

Commit

Permalink
update flysystem to 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wadjeroudi committed Jun 13, 2022
1 parent 08c84fa commit c65a4c8
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 56 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/Action/Shop/ShowFeedAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +24,7 @@ final class ShowFeedAction

private FeedPathGeneratorInterface $feedPathGenerator;

private FilesystemInterface $filesystem;
private FilesystemOperator $filesystem;

private MimeTypesInterface $mimeTypes;

Expand All @@ -33,7 +33,7 @@ public function __construct(
ChannelContextInterface $channelContext,
LocaleContextInterface $localeContext,
FeedPathGeneratorInterface $feedPathGenerator,
FilesystemInterface $filesystem,
FilesystemOperator $filesystem,
MimeTypesInterface $mimeTypes
) {
$this->repository = $repository;
Expand Down
8 changes: 4 additions & 4 deletions src/DependencyInjection/Compiler/RegisterFilesystemPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
));
}

Expand Down
12 changes: 6 additions & 6 deletions src/EventListener/DeleteGeneratedFilesSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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) {
}
}
}
16 changes: 8 additions & 8 deletions src/EventListener/MoveGeneratedFeedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
) {
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Generator/TemporaryFeedPathGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
24 changes: 7 additions & 17 deletions src/Message/Handler/FinishGenerationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,15 +24,14 @@
use Symfony\Component\Workflow\Registry;
use Throwable;
use Twig\Environment;
use Webmozart\Assert\Assert;

final class FinishGenerationHandler implements MessageHandlerInterface
{
use GetFeedTrait;

private ObjectManager $feedManager;

private FilesystemInterface $filesystem;
private FilesystemOperator $filesystem;

private Registry $workflowRegistry;

Expand All @@ -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,
Expand Down Expand Up @@ -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',
Expand All @@ -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');
}
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/Message/Handler/GenerateBatchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,7 +42,6 @@
use Symfony\Component\Workflow\Workflow;
use Throwable;
use Twig\Environment;
use Webmozart\Assert\Assert;

final class GenerateBatchHandler implements MessageHandlerInterface
{
Expand All @@ -58,7 +57,7 @@ final class GenerateBatchHandler implements MessageHandlerInterface

private Environment $twig;

private FilesystemInterface $filesystem;
private FilesystemOperator $filesystem;

private FeedPathGeneratorInterface $temporaryFeedPathGenerator;

Expand All @@ -83,7 +82,7 @@ public function __construct(
ObjectManager $feedManager,
FeedTypeRegistryInterface $feedTypeRegistry,
Environment $twig,
FilesystemInterface $filesystem,
FilesystemOperator $filesystem,
FeedPathGeneratorInterface $temporaryFeedPathGenerator,
EventDispatcherInterface $eventDispatcher,
Registry $workflowRegistry,
Expand Down Expand Up @@ -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();

Expand Down
12 changes: 6 additions & 6 deletions tests/Behat/Context/Cli/ProcessFeedsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +32,7 @@ final class ProcessFeedsContext implements Context
/** @var ProcessFeedsCommand */
private $command;

/** @var FilesystemInterface */
/** @var FilesystemOperator */
private $filesystem;

/** @var FeedProcessorInterface */
Expand All @@ -46,7 +46,7 @@ final class ProcessFeedsContext implements Context

public function __construct(
KernelInterface $kernel,
FilesystemInterface $filesystem,
FilesystemOperator $filesystem,
FeedProcessorInterface $processor,
FeedPathGeneratorInterface $feedPathGenerator,
RepositoryInterface $feedRepository
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c65a4c8

Please sign in to comment.