Skip to content

Commit

Permalink
Added ExporterInterface in 2.x (#623)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jan 2, 2023
1 parent 2134571 commit c1aa20b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/Bridge/Symfony/Resources/config/services.php
Expand Up @@ -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;
Expand Down Expand Up @@ -65,4 +66,5 @@
->public();

$services->alias(Exporter::class, 'sonata.exporter.exporter');
$services->alias(ExporterInterface::class, 'sonata.exporter.exporter');
};
2 changes: 1 addition & 1 deletion src/Exporter.php
Expand Up @@ -19,7 +19,7 @@
/**
* @author Grégoire Paris <postmaster@greg0ire.fr>
*/
final class Exporter
final class Exporter implements ExporterInterface
{
/**
* @var array<TypedWriterInterface>
Expand Down
26 changes: 26 additions & 0 deletions src/ExporterInterface.php
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* 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;
}
2 changes: 1 addition & 1 deletion src/Writer/InMemoryWriter.php
Expand Up @@ -18,7 +18,7 @@ final class InMemoryWriter implements WriterInterface
/**
* @var array<mixed>
*/
private $elements;
private array $elements = [];

public function open(): void
{
Expand Down
8 changes: 4 additions & 4 deletions src/Writer/SitemapWriter.php
Expand Up @@ -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 = "<?xml version='1.0' encoding='UTF-8'?".">\n<sitemapindex xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/1.0 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd' xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\n";
$content = "<?xml version='1.0' encoding='UTF-8'?>\n<sitemapindex xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/1.0 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd' xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\n";
foreach (glob(sprintf('%s/%s', $folder, $pattern)) as $file) {
$stat = stat($file);
$content .= sprintf(
Expand Down Expand Up @@ -250,7 +250,7 @@ private function fixDataType(array &$data): void
*/
private function generateDefaultLine(array $data): string
{
return sprintf(' '.'<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%s</priority></url>'."\n", $data['url'], date('Y-m-d', strtotime($data['lastmod'])), $data['changefreq'], $data['priority']);
return sprintf(' <url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%s</priority></url>'."\n", $data['url'], date('Y-m-d', strtotime($data['lastmod'])), $data['changefreq'], $data['priority']);
}

/**
Expand Down Expand Up @@ -281,7 +281,7 @@ private function generateImageLine(array $data): string
$images .= '</image:image>';
}

return sprintf(' '.'<url><loc>%s</loc>%s</url>'."\n", $data['url'], $images);
return sprintf(' <url><loc>%s</loc>%s</url>'."\n", $data['url'], $images);
}

/**
Expand All @@ -300,7 +300,7 @@ private function generateVideoLine(array $data): string
$videos .= sprintf('<video:%1$s>%2$s</video:%1$s>', $builder[$key] ?? $key, $video);
}

return sprintf(' '.'<url><loc>%s</loc><video:video>%s</video:video></url>'."\n", $data['url'], $videos);
return sprintf(' <url><loc>%s</loc><video:video>%s</video:video></url>'."\n", $data['url'], $videos);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Source/DoctrineODMQuerySourceIteratorTest.php
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Source/IteratorSourceIteratorTest.php
Expand Up @@ -24,7 +24,7 @@ final class IteratorSourceIteratorTest extends TestCase
/**
* @var \Iterator<mixed, mixed>&MockObject
*/
private $iterator;
private \Iterator $iterator;

protected function setUp(): void
{
Expand Down

0 comments on commit c1aa20b

Please sign in to comment.