From c1aa20bcf556207734df521ce4a54925994760b6 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 2 Jan 2023 21:36:48 +0100 Subject: [PATCH] Added ExporterInterface in 2.x (#623) --- composer.json | 2 +- .../Symfony/Resources/config/services.php | 2 ++ src/Exporter.php | 2 +- src/ExporterInterface.php | 26 +++++++++++++++++++ src/Writer/InMemoryWriter.php | 2 +- src/Writer/SitemapWriter.php | 8 +++--- .../DoctrineODMQuerySourceIteratorTest.php | 2 +- tests/Source/IteratorSourceIteratorTest.php | 2 +- 8 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 src/ExporterInterface.php diff --git a/composer.json b/composer.json index 37fc0a82..f51bfef1 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,7 @@ "propel/propel1": "^1.6", "psalm/plugin-phpunit": "^0.16", "psalm/plugin-symfony": "^3.0", - "rector/rector": "^0.13", + "rector/rector": "^0.15", "symfony/config": "^4.4 || ^5.4 || ^6.0", "symfony/dependency-injection": "^4.4 || ^5.4 || ^6.0", "symfony/http-foundation": "^4.4 || ^5.4 || ^6.0", diff --git a/src/Bridge/Symfony/Resources/config/services.php b/src/Bridge/Symfony/Resources/config/services.php index 259e7fff..c69770e6 100644 --- a/src/Bridge/Symfony/Resources/config/services.php +++ b/src/Bridge/Symfony/Resources/config/services.php @@ -13,6 +13,7 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet; use Sonata\Exporter\Exporter; +use Sonata\Exporter\ExporterInterface; use Sonata\Exporter\Writer\CsvWriter; use Sonata\Exporter\Writer\JsonWriter; use Sonata\Exporter\Writer\XlsWriter; @@ -65,4 +66,5 @@ ->public(); $services->alias(Exporter::class, 'sonata.exporter.exporter'); + $services->alias(ExporterInterface::class, 'sonata.exporter.exporter'); }; diff --git a/src/Exporter.php b/src/Exporter.php index 01103abf..2f71cd36 100644 --- a/src/Exporter.php +++ b/src/Exporter.php @@ -19,7 +19,7 @@ /** * @author Grégoire Paris */ -final class Exporter +final class Exporter implements ExporterInterface { /** * @var array diff --git a/src/ExporterInterface.php b/src/ExporterInterface.php new file mode 100644 index 00000000..70a76d54 --- /dev/null +++ b/src/ExporterInterface.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\Exporter; + +use Symfony\Component\HttpFoundation\StreamedResponse; + +interface ExporterInterface +{ + /** + * @return string[] + */ + public function getAvailableFormats(): array; + + public function getResponse(string $format, string $filename, \Iterator $source): StreamedResponse; +} diff --git a/src/Writer/InMemoryWriter.php b/src/Writer/InMemoryWriter.php index 1d30c0d6..ef66f6af 100644 --- a/src/Writer/InMemoryWriter.php +++ b/src/Writer/InMemoryWriter.php @@ -18,7 +18,7 @@ final class InMemoryWriter implements WriterInterface /** * @var array */ - private $elements; + private array $elements = []; public function open(): void { diff --git a/src/Writer/SitemapWriter.php b/src/Writer/SitemapWriter.php index 7b90c2b5..f5f33e89 100644 --- a/src/Writer/SitemapWriter.php +++ b/src/Writer/SitemapWriter.php @@ -134,7 +134,7 @@ public function close(): void */ public static function generateSitemapIndex(string $folder, string $baseUrl, string $pattern = 'sitemap*.xml', string $filename = 'sitemap.xml'): void { - $content = "\n\n"; + $content = "\n\n"; foreach (glob(sprintf('%s/%s', $folder, $pattern)) as $file) { $stat = stat($file); $content .= sprintf( @@ -250,7 +250,7 @@ private function fixDataType(array &$data): void */ private function generateDefaultLine(array $data): string { - return sprintf(' '.'%s%s%s%s'."\n", $data['url'], date('Y-m-d', strtotime($data['lastmod'])), $data['changefreq'], $data['priority']); + return sprintf(' %s%s%s%s'."\n", $data['url'], date('Y-m-d', strtotime($data['lastmod'])), $data['changefreq'], $data['priority']); } /** @@ -281,7 +281,7 @@ private function generateImageLine(array $data): string $images .= ''; } - return sprintf(' '.'%s%s'."\n", $data['url'], $images); + return sprintf(' %s%s'."\n", $data['url'], $images); } /** @@ -300,7 +300,7 @@ private function generateVideoLine(array $data): string $videos .= sprintf('%2$s', $builder[$key] ?? $key, $video); } - return sprintf(' '.'%s%s'."\n", $data['url'], $videos); + return sprintf(' %s%s'."\n", $data['url'], $videos); } /** diff --git a/tests/Source/DoctrineODMQuerySourceIteratorTest.php b/tests/Source/DoctrineODMQuerySourceIteratorTest.php index a00a39d9..a52e168c 100644 --- a/tests/Source/DoctrineODMQuerySourceIteratorTest.php +++ b/tests/Source/DoctrineODMQuerySourceIteratorTest.php @@ -85,7 +85,7 @@ private function createConfiguration(): Configuration $config->setHydratorNamespace('Hydrators'); $config->setPersistentCollectionDir($directory); $config->setPersistentCollectionNamespace('PersistentCollections'); - $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver()); // @phpstan-ignore-line + $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver()); return $config; } diff --git a/tests/Source/IteratorSourceIteratorTest.php b/tests/Source/IteratorSourceIteratorTest.php index 4a1be3a7..5b5e12cf 100644 --- a/tests/Source/IteratorSourceIteratorTest.php +++ b/tests/Source/IteratorSourceIteratorTest.php @@ -24,7 +24,7 @@ final class IteratorSourceIteratorTest extends TestCase /** * @var \Iterator&MockObject */ - private $iterator; + private \Iterator $iterator; protected function setUp(): void {