From c18b6fb54ea92e9c5e9fa8ba41afde550660d98f Mon Sep 17 00:00:00 2001 From: tinect Date: Sat, 5 Oct 2024 12:13:34 +0200 Subject: [PATCH] feat: add support for writeBatch --- .github/workflows/test.yml | 3 +- CHANGELOG_en-GB.md | 5 ++ README.md | 2 +- composer.json | 2 +- src/Adapter/BunnyCdnFactory.php | 12 +++- .../Shopware6BunnyCdnWriteBatchAdapter.php | 70 +++++++++++++++++++ tests/unit/Adapter/BunnyCdnFactoryTest.php | 15 ++++ 7 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 src/Adapter/Shopware6BunnyCdnWriteBatchAdapter.php diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index eec5ba1..37c3bb6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,6 +25,7 @@ jobs: "v6.5.8.2", "6.5.x", "v6.6.0.3", + "v6.6.8.1", "trunk" ] php: ["8.2"] @@ -70,4 +71,4 @@ jobs: with: files: ./clover.xml root_dir: ${{ github.workspace }}/custom/plugins/${{ env.PLUGIN_NAME }} - working-directory: ${{ github.workspace }}/custom/plugins/${{ env.PLUGIN_NAME }} \ No newline at end of file + working-directory: ${{ github.workspace }}/custom/plugins/${{ env.PLUGIN_NAME }} diff --git a/CHANGELOG_en-GB.md b/CHANGELOG_en-GB.md index 48b87e3..7622c33 100644 --- a/CHANGELOG_en-GB.md +++ b/CHANGELOG_en-GB.md @@ -1,3 +1,8 @@ +# 3.4.0 + +* Feat: (for Shopware >= 6.6.8.0) Add support for writeBatch. + With this you can use storage for theme and asset with more speed. Instead of 2,5 hours it now takes 30 seconds for assets on storage with SSD with demo shop. + # 3.3.3 * Fix: correct config message for Shopware >6.6.3.0 diff --git a/README.md b/README.md index ca3fb1f..ba51ae9 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ https://github.com/FriendsOfShopware/FroshPlatformBunnycdnMediaStorage/releases/ ## Recommendations and warnings -- Don't store theme or asset files on external storage if possible. Due to some improvements in shopware with 6.5 and 6.6 there are created many files. This is the reason why this would slow down theme compilation and asset installation significantly. +- Before Shopware 6.6.8.0 and plugin version 3.4.0: Don't store theme or asset files on external storage. Due to some improvements in shopware with 6.5 and 6.6 there are created many files. This is the reason why this would slow down theme compilation and asset installation significantly. - Utilize storage zones with SSDs as they are significantly faster than standard option (means HDDs), with up to 80x faster upload speeds in our tests. ## Suggestions diff --git a/composer.json b/composer.json index a0acfa3..68e8e31 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ ], "require": { "shopware/core": "~6.5.3||~6.6.0", - "platformcommunity/flysystem-bunnycdn": "^3.3.4", + "platformcommunity/flysystem-bunnycdn": "^3.3.5", "league/flysystem-path-prefixing": "^3.10.3", "ajgl/flysystem-replicate": "^2.2", "tinect/flysystem-garbage": "^1.0" diff --git a/src/Adapter/BunnyCdnFactory.php b/src/Adapter/BunnyCdnFactory.php index 74605d0..e7e0198 100644 --- a/src/Adapter/BunnyCdnFactory.php +++ b/src/Adapter/BunnyCdnFactory.php @@ -7,6 +7,7 @@ use League\Flysystem\Local\LocalFilesystemAdapter; use League\Flysystem\PathPrefixing\PathPrefixedAdapter; use Shopware\Core\Framework\Adapter\Filesystem\Adapter\AdapterFactoryInterface; +use Shopware\Core\Framework\Adapter\Filesystem\Plugin\WriteBatchInterface; use Tinect\Flysystem\Garbage\GarbageFilesystemAdapter; class BunnyCdnFactory implements AdapterFactoryInterface @@ -19,7 +20,7 @@ public function create(array $config): FilesystemAdapter $adapterConfig = new AdapterConfig(); $adapterConfig->assign($config); - $adapter = new Shopware6BunnyCdnAdapter($adapterConfig); + $adapter = $this->getBasicAdapter($adapterConfig); if (!empty($adapterConfig->isUseGarbage())) { $adapter = new GarbageFilesystemAdapter($adapter); @@ -36,6 +37,15 @@ public function create(array $config): FilesystemAdapter return $adapter; } + private function getBasicAdapter(AdapterConfig $adapterConfig): FilesystemAdapter + { + if (\interface_exists(WriteBatchInterface::class)) { + return new Shopware6BunnyCdnWriteBatchAdapter($adapterConfig); + } + + return new Shopware6BunnyCdnAdapter($adapterConfig); + } + public function getType(): string { return 'bunnycdn'; diff --git a/src/Adapter/Shopware6BunnyCdnWriteBatchAdapter.php b/src/Adapter/Shopware6BunnyCdnWriteBatchAdapter.php new file mode 100644 index 0000000..d5cdb9b --- /dev/null +++ b/src/Adapter/Shopware6BunnyCdnWriteBatchAdapter.php @@ -0,0 +1,70 @@ +getTargetFiles() as $targetFile) { + $sourceFile = $data->getSourceFile(); + + if (\is_string($sourceFile)) { + $files[] = new WriteBatchFile($sourceFile, $targetFile); + + continue; + } + + $metaData = stream_get_meta_data($sourceFile); + if (empty($metaData['uri'])) { + throw new \InvalidArgumentException('Cannot get source file path from stream.'); + } + + $sourcePath = $metaData['uri']; + + $files[] = new WriteBatchFile($sourcePath, $targetFile); + } + + continue; + } + + if ($data instanceof Config) { + $config = $data; + + continue; + } + + if (\is_array($data)) { + foreach ($data as $item) { + if (!$item instanceof WriteBatchFile) { + throw new \InvalidArgumentException('Each value of array must be a WriteBatchFile object.'); + } + + $files[] = $item; + } + } + } + + if (empty($files)) { + return; + } + + parent::writeBatch($files, $config); + } + } +} \ No newline at end of file diff --git a/tests/unit/Adapter/BunnyCdnFactoryTest.php b/tests/unit/Adapter/BunnyCdnFactoryTest.php index 43d7086..0922e33 100644 --- a/tests/unit/Adapter/BunnyCdnFactoryTest.php +++ b/tests/unit/Adapter/BunnyCdnFactoryTest.php @@ -5,8 +5,10 @@ use Ajgl\Flysystem\Replicate\ReplicateFilesystemAdapter; use Frosh\BunnycdnMediaStorage\Adapter\BunnyCdnFactory; use Frosh\BunnycdnMediaStorage\Adapter\Shopware6BunnyCdnAdapter; +use Frosh\BunnycdnMediaStorage\Adapter\Shopware6BunnyCdnWriteBatchAdapter; use League\Flysystem\PathPrefixing\PathPrefixedAdapter; use PHPUnit\Framework\TestCase; +use Shopware\Core\Framework\Adapter\Filesystem\Plugin\WriteBatchInterface; use Tinect\Flysystem\Garbage\GarbageFilesystemAdapter; class BunnyCdnFactoryTest extends TestCase @@ -47,6 +49,19 @@ public function testFactoryCreatesReplicateFilesystemAdapter(): void static::assertInstanceOf(ReplicateFilesystemAdapter::class, $adapter); } + public function testFactoryCreatesWriteBatchAdapter(): void + { + if (!\interface_exists(WriteBatchInterface::class)) { + static::markTestSkipped('WriteBatchInterface not found'); + } + + $factory = new BunnyCdnFactory(); + $config = ['endpoint' => 'https://storage.bunnycdn.com', 'apiKey' => 'a', 'storageName' => 'a', 'useGarbage' => false]; + $adapter = $factory->create($config); + + static::assertInstanceOf(WriteBatchInterface::class, $adapter); + } + public function testFactoryReturnsBunnycdnType(): void { $factory = new BunnyCdnFactory();