Skip to content

Commit 76b4714

Browse files
committed
add symfony bundle
1 parent 63a4827 commit 76b4714

22 files changed

+482
-150
lines changed

composer.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
{
2-
"name": "spiriitlabs/rustsheet",
2+
"name": "spiriitlabs/excel-rust",
33
"require": {
44
"symfony/filesystem": "5.4 | 6.4 | 7.*",
55
"symfony/process": "5.4 | 6.4 | 7.*",
66
"symfony/serializer": "5.4 | 6.4 | 7.*",
77
"flix-tech/avro-php": "^5.0",
88
"symfony/options-resolver": "5.4 | 6.4 | 7.*",
9-
"psr/log": "^3.0"
9+
"psr/log": "^3.0",
10+
"symfony/framework-bundle": "6.4 | 7.*",
11+
"symfony/string": "6.4 | 7.*"
1012
},
1113
"require-dev": {
12-
"phpunit/phpunit": "11.*",
14+
"phpunit/phpunit": "10.*",
1315
"symfony/var-dumper": "5.4 | 6.4 | 7.*",
1416
"friendsofphp/php-cs-fixer": "^3.64",
1517
"phpstan/phpstan": "^1.12",
16-
"rector/rector": "^1.2"
18+
"rector/rector": "^1.2",
19+
"matthiasnoback/symfony-dependency-injection-test": "^6.0"
1720
},
1821
"autoload": {
1922
"psr-4": {

examples/WorkbookFactoryStub.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Spiriit\Rustsheet\Structure\Workbook;
4+
use Spiriit\Rustsheet\WorkbookBuilder;
5+
use Spiriit\Tests\Fixtures\MyExcel;
6+
7+
require __DIR__.'/../vendor/autoload.php';
8+
9+
class WorkbookFactoryStub implements \Spiriit\Rustsheet\WorkbookFactoryInterface
10+
{
11+
12+
public function create(string $name): array
13+
{
14+
$excel = new MyExcel();
15+
16+
$builder = new WorkbookBuilder(new Workbook('made_avro.xlsx'));
17+
18+
$excel->buildSheet($builder);
19+
20+
return $builder->build();
21+
}
22+
}

examples/example_avro.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
<?php
22

33
require __DIR__.'/../vendor/autoload.php';
4+
require __DIR__.'/WorkbookFactoryStub.php';
45

56
use Spiriit\Rustsheet\ExcelRust;
6-
use Spiriit\Rustsheet\WorkbookFactory;
77
use Spiriit\Tests\Fixtures\MyExcel;
88
use Psr\Log\NullLogger;
99

10+
1011
@unlink($output = __DIR__.'/../myexcel.xlsx');
1112

12-
$excelGeneratorFromAvro = new ExcelRust(
13-
workbookFactory: new WorkbookFactory(),
14-
rustGenLocation: __DIR__ . '/../vendor/bin/excel_gen'
13+
$factory = new WorkbookFactoryStub();
14+
15+
$excelRust = new ExcelRust(
16+
workbookFactory: $factory,
17+
rustGenLocation: __DIR__ . '/../excel_gen',
18+
defaultOutputFolder: __DIR__.'/../',
1519
);
16-
$excelGeneratorFromAvro->setLogger(new NullLogger());
20+
$excelRust->setLogger(new NullLogger());
21+
22+
$output = $excelRust->generateExcelFromAvro(MyExcel::class);
1723

18-
$excelGeneratorFromAvro->generateExcelFromAvro(new MyExcel());
24+
// echo $output;
1925

2026
@unlink($output);

examples/example_html.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
<?php
22

33
require __DIR__.'/../vendor/autoload.php';
4+
require __DIR__.'/WorkbookFactoryStub.php';
45

56
use Spiriit\Rustsheet\ExcelRust;
6-
use Spiriit\Rustsheet\WorkbookFactory;
77
use Psr\Log\NullLogger;
88

99
@unlink($output = 'myexcel_html.xlsx');
1010

11-
$avroFactory = new ExcelRust(
12-
workbookFactory: new WorkbookFactory(),
13-
rustGenLocation: __DIR__ . '/../vendor/bin/excel_gen'
11+
$factory = new WorkbookFactoryStub();
12+
13+
$excelRust = new ExcelRust(
14+
workbookFactory: $factory,
15+
rustGenLocation: __DIR__ . '/../excel_gen',
16+
defaultOutputFolder: __DIR__.'/../',
1417
);
15-
$avroFactory->setLogger(new NullLogger());
18+
$excelRust->setLogger(new NullLogger());
1619

1720
$htmlFile = __DIR__.DIRECTORY_SEPARATOR.'fixtures.html';
1821

19-
$avroFactory->generateExcelFromHtml($htmlFile, $output);
22+
$excelRust->generateExcelFromHtml($htmlFile, $output);
2023

2124
@unlink($output = 'myexcel_html.xlsx');

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd"
55
backupGlobals="false" colors="true"
66
bootstrap="vendor/autoload.php"
7+
displayDetailsOnTestsThatTriggerWarnings="true"
78
cacheDirectory=".phpunit.cache">
89
<php>
910
<ini name="error_reporting" value="-1"/>

src/Attributes/AsExcelRust.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the SpiriitLabs php-excel-rust package.
5+
* Copyright (c) SpiriitLabs <https://www.spiriit.com/>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Spiriit\Rustsheet\Attributes;
11+
12+
#[\Attribute(\Attribute::TARGET_CLASS)]
13+
class AsExcelRust
14+
{
15+
public function __construct(
16+
private readonly ?string $name = null,
17+
private readonly ?string $outputName = null,
18+
) {
19+
}
20+
21+
public function serviceConfig(): array
22+
{
23+
return [
24+
'key' => $this->name,
25+
'outputName' => $this->outputName,
26+
];
27+
}
28+
}

src/ExcelInterface.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99

1010
namespace Spiriit\Rustsheet;
1111

12-
use Symfony\Component\OptionsResolver\OptionsResolver;
13-
1412
interface ExcelInterface
1513
{
1614
public function buildSheet(WorkbookBuilder $builder): void;
17-
18-
public function configureOptions(OptionsResolver $resolver): void;
1915
}

src/ExcelRust.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Spiriit\Rustsheet\ExportAvro\ExportAvro;
1515
use Symfony\Component\Process\Process;
1616

17+
use function Symfony\Component\String\u;
18+
1719
class ExcelRust implements LoggerAwareInterface
1820
{
1921
use LoggerAwareTrait;
@@ -22,20 +24,29 @@ class ExcelRust implements LoggerAwareInterface
2224
public const RUST_GEN_HTML = 'rust_gen_html';
2325

2426
public function __construct(
25-
private WorkbookFactory $workbookFactory,
27+
private WorkbookFactoryInterface $workbookFactory,
2628
private string $rustGenLocation,
29+
private string $defaultOutputFolder,
2730
) {
2831
}
2932

30-
public function generateExcelFromAvro(ExcelInterface $excel): void
33+
public function generateExcelFromAvro(string $name): string
3134
{
32-
$results = $this->buildExcel($excel);
33-
$filenameOutput = $results['filename'];
35+
$results = $this->buildExcel($name);
36+
37+
$filenameOutput = u($this->defaultOutputFolder)
38+
->append(\DIRECTORY_SEPARATOR)
39+
->append($results['filename'])
40+
->ensureEnd('.xlsx')
41+
->toString();
42+
3443
unset($results['filename']);
3544

3645
$avro = $this->exportAvro($results);
3746

3847
$this->execute($avro, $filenameOutput, self::RUST_GEN_AVRO);
48+
49+
return $filenameOutput;
3950
}
4051

4152
public function generateExcelFromHtml(string $htmlFile, string $filenameOutput): void
@@ -65,9 +76,9 @@ private function execute(string $filePath, string $filenameOutput, string $rustG
6576
]);
6677
}
6778

68-
private function buildExcel(ExcelInterface $excel): array
79+
private function buildExcel(string $name): array
6980
{
70-
return $this->workbookFactory->create($excel);
81+
return $this->workbookFactory->create($name);
7182
}
7283

7384
private function exportAvro(array $results)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the SpiriitLabs php-excel-rust package.
5+
* Copyright (c) SpiriitLabs <https://www.spiriit.com/>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Spiriit\Rustsheet\Symfony\Bundle\DependencyInjection\Compiler;
11+
12+
use Spiriit\Rustsheet\WorkbookFactory;
13+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
14+
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Exception\LogicException;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
19+
class ExcelInterfacePass implements CompilerPassInterface
20+
{
21+
public function process(ContainerBuilder $container)
22+
{
23+
if (!$container->hasDefinition(WorkbookFactory::class)) {
24+
return;
25+
}
26+
27+
$excelsReferences = [];
28+
$excelsNames = [];
29+
$excelsConfig = [];
30+
31+
foreach ($container->findTaggedServiceIds('spiriit_excel_rust.excel_rust') as $id => $tags) {
32+
$definition = $container->findDefinition($id);
33+
34+
$name = $definition->getClass();
35+
36+
foreach ($tags as $tag) {
37+
if (!\array_key_exists('key', $tag)) {
38+
if (\in_array($name, $excelsNames, true)) {
39+
throw new LogicException(\sprintf('Failed creating the "%s" excel with the automatic name "%s": another excel already has this name. To fix this, give the excel an explicit name (hint: using "%s" will override the existing excel).', $fqcn, $name, $name));
40+
}
41+
42+
$tag['key'] = $name;
43+
}
44+
45+
$excelsConfig[$tag['key']] = $tag;
46+
$excelsReferences[$tag['key']] = new Reference($id);
47+
$excelsNames[] = $tag['key'];
48+
}
49+
}
50+
51+
$factoryDefinition = $container->findDefinition(WorkbookFactory::class);
52+
53+
$factoryDefinition->setArgument(0, ServiceLocatorTagPass::register($container, $excelsReferences));
54+
$factoryDefinition->setArgument(1, $excelsConfig);
55+
}
56+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the SpiriitLabs php-excel-rust package.
5+
* Copyright (c) SpiriitLabs <https://www.spiriit.com/>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Spiriit\Rustsheet\Symfony\Bundle\DependencyInjection;
11+
12+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
13+
use Symfony\Component\Config\Definition\ConfigurationInterface;
14+
15+
class Configuration implements ConfigurationInterface
16+
{
17+
public function getConfigTreeBuilder(): TreeBuilder
18+
{
19+
$treeBuilder = new TreeBuilder('spiriit_excel_rust');
20+
$rootNode = $treeBuilder->getRootNode();
21+
22+
$rootNode
23+
->children()
24+
->scalarNode('rust_binary')
25+
->info('The rust binary path')
26+
->isRequired()
27+
->end()
28+
->scalarNode('default_output_folder')
29+
->info('The default folder for output excel')
30+
->defaultValue('/tmp')
31+
->end()
32+
;
33+
34+
return $treeBuilder;
35+
}
36+
}

0 commit comments

Comments
 (0)