Repository navigation
Expand file tree
/
Copy pathEndroidQrCodeBundle.php
More file actions
214 lines (190 loc) · 8.55 KB
/
Copy pathEndroidQrCodeBundle.php
File metadata and controls
214 lines (190 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
declare(strict_types=1);
namespace Endroid\QrCodeBundle;
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Builder\BuilderInterface;
use Endroid\QrCode\Builder\BuilderRegistry;
use Endroid\QrCode\Builder\BuilderRegistryInterface;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\Label\Font\Font;
use Endroid\QrCode\Label\Font\OpenSans;
use Endroid\QrCode\Label\LabelAlignment;
use Endroid\QrCode\RoundBlockSizeMode;
use Endroid\QrCode\Writer\BinaryWriter;
use Endroid\QrCode\Writer\DebugWriter;
use Endroid\QrCode\Writer\EpsWriter;
use Endroid\QrCode\Writer\GifWriter;
use Endroid\QrCode\Writer\PdfWriter;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\SvgWriter;
use Endroid\QrCode\Writer\WebPWriter;
use Endroid\QrCodeBundle\Controller\GenerateController;
use Endroid\QrCodeBundle\DependencyInjection\RouteLoaderCompilerPass;
use Endroid\QrCodeBundle\Routing\QrCodeRouteLoader;
use Endroid\QrCodeBundle\Twig\QrCodeExtension;
use Endroid\QrCodeBundle\Twig\QrCodeRuntime;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
final class EndroidQrCodeBundle extends AbstractBundle
{
#[\Override]
public function configure(DefinitionConfigurator $definition): void
{
/** @var \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $rootNode */
$rootNode = $definition->rootNode();
/** @var \Symfony\Component\Config\Definition\Builder\NodeBuilder $children */
$children = $rootNode->children();
$children->scalarNode('route_prefix')->defaultValue('/qr-code');
$children->booleanNode('route_enabled')->defaultTrue();
$children->integerNode('route_priority')->defaultValue(0);
$children
->arrayNode('builders')
->useAttributeAsKey('name')
->arrayPrototype()
->useAttributeAsKey('name')
->variablePrototype();
}
#[\Override]
public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new RouteLoaderCompilerPass());
}
/** @param array<array-key, mixed> $config */
#[\Override]
public function loadExtension(array $config, ContainerConfigurator $configurator, ContainerBuilder $container): void
{
$services = $configurator->services();
$services->defaults()->autowire()->autoconfigure();
// Writers
$services->set(BinaryWriter::class);
$services->set(DebugWriter::class);
$services->set(EpsWriter::class);
$services->set(GifWriter::class);
$services->set(PdfWriter::class);
$services->set(PngWriter::class);
$services->set(SvgWriter::class);
$services->set(WebPWriter::class);
// Builder
$services->set(Builder::class);
$services->alias(BuilderInterface::class, Builder::class);
// Registry
$services->set(BuilderRegistry::class);
$services->alias(BuilderRegistryInterface::class, BuilderRegistry::class);
// Twig
$services->set(QrCodeExtension::class);
$services->set(QrCodeRuntime::class);
// Route prefix parameter (always set so the container never fails on an undefined parameter)
$routePrefix = (string) ($config['route_prefix'] ?? '/qr-code');
$configurator->parameters()->set('endroid_qr_code.route_prefix', $routePrefix);
// Controller and route auto-registration (only when routes are enabled)
/** @var bool $enableRoutes */
$enableRoutes = $config['route_enabled'] ?? true;
if ($enableRoutes) {
$services->set(GenerateController::class)->tag('controller.service_arguments');
// Route loader wraps the main router resource to inject the bundle's route
// This ensures routes work on all Symfony versions without user configuration
$services
->set('endroid_qr_code.route_loader', QrCodeRouteLoader::class)
->args(['', $routePrefix, $config['route_priority'] ?? 0])
->tag('routing.route_loader');
}
// Builder configurations
/** @var array<string, array<string, mixed>> $builders */
$builders = $config['builders'] ?? [];
if ($builders === []) {
$builders = [
'default' => [
'writer' => PngWriter::class,
'size' => 300,
'margin' => 10,
'encoding' => 'UTF-8',
'error_correction_level' => 'low',
'round_block_size_mode' => 'margin',
'validate_result' => false,
],
];
}
$registryDefinition = $container->findDefinition(BuilderRegistryInterface::class);
foreach ($builders as $builderName => $builderConfig) {
$builderDefinition = $this->createBuilderDefinition($builderName, $builderConfig, $container);
$registryDefinition->addMethodCall('set', [$builderName, $builderDefinition]);
}
}
/** @param array<string, mixed> $builderConfig */
private function createBuilderDefinition(
string $builderName,
array $builderConfig,
ContainerBuilder $container,
): Definition {
$id = sprintf('endroid_qr_code.%s_builder', $builderName);
$builderDefinition = new ChildDefinition(BuilderInterface::class);
/** @var array<string, Reference|Definition|ErrorCorrectionLevel|RoundBlockSizeMode|LabelAlignment|string|int|bool> $arguments */
$arguments = [];
/** @var string|int|bool|list<int> $value */
foreach ($builderConfig as $name => $value) {
$name = $this->toCamelCase($name);
switch ($name) {
case 'writer':
/** @var string $value */
$arguments[$name] = new Reference($value);
break;
case 'encoding':
$arguments[$name] = new Definition(Encoding::class, [$value]);
break;
case 'errorCorrectionLevel':
/** @var string $value */
$arguments[$name] = ErrorCorrectionLevel::from($value);
break;
case 'roundBlockSizeMode':
/** @var string $value */
$arguments[$name] = RoundBlockSizeMode::from($value);
break;
case 'foregroundColor':
case 'backgroundColor':
case 'labelTextColor':
/** @var list<int> $value */
$arguments[$name] = new Definition(Color::class, $value);
break;
case 'labelFontPath':
$labelFontSize = (int) ($builderConfig['label_font_size'] ?? 16);
$arguments['labelFont'] = new Definition(Font::class, [$value, $labelFontSize]);
break;
case 'labelFontSize':
/** @var string $labelFontPath */
$labelFontPath = $builderConfig['label_font_path'] ?? new OpenSans()->getPath();
$arguments['labelFont'] = new Definition(Font::class, [$labelFontPath, $value]);
break;
case 'labelAlignment':
/** @var string $value */
$arguments[$name] = LabelAlignment::from($value);
break;
default:
$arguments[$name] = $value;
break;
}
}
foreach ($arguments as $argumentName => $argumentValue) {
$builderDefinition->setArgument('$' . $argumentName, $argumentValue);
}
$container->setDefinition($id, $builderDefinition);
$container
->registerAliasForArgument($id, BuilderInterface::class, $builderName . 'QrCodeBuilder')
->setPublic(false);
return $builderDefinition;
}
private function toCamelCase(string $anyCase): string
{
return lcfirst(str_replace(
search: ' ',
replace: '',
subject: ucwords(str_replace(search: '_', replace: ' ', subject: $anyCase)),
));
}
}