Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed May 30, 2017
1 parent 80beb5a commit a915e36
Show file tree
Hide file tree
Showing 29 changed files with 761 additions and 423 deletions.
32 changes: 12 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,35 @@ $ composer require endroid/qrcode
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\SvgWriter;
use Symfony\Component\HttpFoundation\Response;

// Create a QR code
// Create a basic QR code
$qrCode = new QrCode('Life is too short to be generating QR codes');
$qrCode->setSize(300);

// Set advanced options
$qrCode
->setWriterByName('png')
->setMargin(10)
->setEncoding('UTF-8')
->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH)
->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0])
->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255])
->setLabel('Scan the code', 16, __DIR__.'/../assets/noto_sans.otf', LabelAlignment::CENTER)
->setLogoPath(__DIR__.'/../assets/symfony.png')
->setLogoSize(150)
->setLogoWidth(150)
->setValidateResult(true)
;

// Output the QR code
header('Content-Type: '.$qrCode->getContentType(PngWriter::class));
echo $qrCode->writeString(PngWriter::class);
// Directly output the QR code
header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();

// Save it to a file (guesses writer by file extension)
// Save it to a file
$qrCode->writeFile(__DIR__.'/qrcode.png');

// Create a response object
$response = new Response(
$qrCode->writeString(SvgWriter::class),
Response::HTTP_OK,
['Content-Type' => $qrCode->getContentType(SvgWriter::class)])
;

// Work via the writer
$writer = new PngWriter($qrCode);
$pngData = $writer->writeString();
$response = new Response($qrCode->writeString(), Response::HTTP_OK, ['Content-Type' => $qrCode->getContentType()]);
```

![QR Code](http://endroid.nl/qrcode/Dit%20is%20een%20test.png)
Expand All @@ -89,6 +80,7 @@ applied by the factory can optionally be overridden via the configuration.

```yaml
endroid_qr_code:
writer: 'png'
size: 300
margin: 10
foreground_color: { r: 0, g: 0, b: 0 }
Expand All @@ -100,7 +92,7 @@ endroid_qr_code:
label_alignment: left # left, center or right
label_margin: { b: 20 }
logo_path: '%kernel.root_dir%/../vendor/endroid/qrcode/assets/symfony.png'
logo_size: 150
logo_width: 150
validate_result: true # checks if the result is readable
```
Expand Down Expand Up @@ -136,8 +128,8 @@ defaults defined by the bundle or set via your configuration.

``` twig
<img src="{{ qrcode_path(message) }}" />
<img src="{{ qrcode_url(message, { extension: 'svg' }) }}" />
<img src="{{ qrcode_data_uri(message, { extension: 'svg', size: 150 }) }}" />
<img src="{{ qrcode_url(message, { writer: 'eps' }) }}" />
<img src="{{ qrcode_data_uri(message, { writer: 'svg', size: 150 }) }}" />
```

## Versioning
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"ext-gd": "*",
"symfony/options-resolver": "^2.7|^3.0",
"bacon/bacon-qr-code": "^1.0",
"myclabs/php-enum": "^1.5",
"khanamiryan/qrcode-detector-decoder": "^0.0.1",
"symfony/property-access": "^2.7|^3.0"
},
Expand Down
16 changes: 7 additions & 9 deletions src/Bundle/Controller/QrCodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Endroid\QrCode\Factory\QrCodeFactory;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -33,25 +34,22 @@ public function generateAction(Request $request, $text, $extension)
$options = $request->query->all();

$qrCode = $this->getQrCodeFactory()->create($text, $options);
$writer = $qrCode->getWriterByExtension($extension);
$qrCode->setWriterByExtension($extension);

return new Response(
$writer->writeString(),
Response::HTTP_OK,
['Content-Type' => $writer->getContentType()]
);
return new Response($qrCode->writeString(), Response::HTTP_OK, ['Content-Type' => $qrCode->getContentType()]);
}

/**
* @Route("/twig", name="endroid_qrcode_twig_functions")
* @Template()
*
* @return Response
* @return array
*/
public function twigFunctionsAction()
{
return $this->render('twig_functions.html.twig', [
return [
'message' => 'QR Code'
]);
];
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* (c) Jeroen van den Enden <info@endroid.nl>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Endroid\QrCode\Bundle\DependencyInjection\Compiler;

use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Predis\Response\Error;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class WriterRegistryCompilerPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->has('endroid.qrcode.writer_registry')) {
return;
}

$writerRegistryDefinition = $container->findDefinition('endroid.qrcode.writer_registry');

$taggedServices = $container->findTaggedServiceIds('endroid.qrcode.writer');
foreach ($taggedServices as $id => $tags) {
foreach ($tags as $attributes) {
$writerRegistryDefinition->addMethodCall('addWriter', [new Reference($id), isset($attributes['set_as_default']) && $attributes['set_as_default']]);
}
}
}
}
5 changes: 3 additions & 2 deletions src/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function getConfigTreeBuilder()
$treeBuilder
->root('endroid_qr_code')
->children()
->scalarNode('writer')->end()
->integerNode('size')->min(0)->end()
->integerNode('margin')->min(0)->end()
->scalarNode('encoding')->defaultValue('UTF-8')->end()
Expand All @@ -48,6 +49,8 @@ public function getConfigTreeBuilder()
->scalarNode('b')->isRequired()->end()
->end()
->end()
->scalarNode('logo_path')->end()
->integerNode('logo_width')->end()
->scalarNode('label')->end()
->integerNode('label_font_size')->end()
->scalarNode('label_font_path')->end()
Expand All @@ -65,8 +68,6 @@ public function getConfigTreeBuilder()
->scalarNode('l')->end()
->end()
->end()
->scalarNode('logo_path')->end()
->integerNode('logo_size')->end()
->booleanNode('validate_result')->end()
->end()
->end()
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/DependencyInjection/EndroidQrCodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services.yml');

$factoryDefinition = $container->getDefinition('endroid.qrcode.factory');
$factoryDefinition->setArguments([$config]);
$factoryDefinition->replaceArgument(0, $config);
}
}
9 changes: 9 additions & 0 deletions src/Bundle/EndroidQrCodeBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@

namespace Endroid\QrCode\Bundle;

use Endroid\QrCode\Bundle\DependencyInjection\Compiler\WriterRegistryCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class EndroidQrCodeBundle extends Bundle
{
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new WriterRegistryCompilerPass());
}
}
24 changes: 23 additions & 1 deletion src/Bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
services:
endroid.qrcode.factory:
class: Endroid\QrCode\Factory\QrCodeFactory

arguments: [ null, '@endroid.qrcode.writer_registry' ]
endroid.qrcode.twig.extension:
class: Endroid\QrCode\Twig\Extension\QrCodeExtension
arguments: [ '@endroid.qrcode.factory', '@router']
tags:
- { name: twig.extension }
endroid.qrcode.writer_registry:
class: Endroid\QrCode\WriterRegistry
endroid.qrcode.writer.binary_writer:
class: Endroid\QrCode\Writer\BinaryWriter
tags:
- { name: endroid.qrcode.writer }
endroid.qrcode.writer.debug_writer:
class: Endroid\QrCode\Writer\DebugWriter
tags:
- { name: endroid.qrcode.writer }
endroid.qrcode.writer.eps_writer:
class: Endroid\QrCode\Writer\EpsWriter
tags:
- { name: endroid.qrcode.writer }
endroid.qrcode.writer.png_writer:
class: Endroid\QrCode\Writer\PngWriter
tags:
- { name: endroid.qrcode.writer, set_as_default: true }
endroid.qrcode.writer.svg_writer:
class: Endroid\QrCode\Writer\SvgWriter
tags:
- { name: endroid.qrcode.writer }
3 changes: 3 additions & 0 deletions src/Bundle/Resources/views/QrCode/twigFunctions.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<img src="{{ qrcode_path(message) }}" />
<img src="{{ qrcode_url(message, { writer: 'svg' }) }}" />
<img src="{{ qrcode_data_uri(message, { writer: 'svg', size: 150 }) }}" />
27 changes: 21 additions & 6 deletions src/Factory/QrCodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

namespace Endroid\QrCode\Factory;

use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Symfony\Component\OptionsResolver\Options;
use Endroid\QrCode\WriterRegistryInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\PropertyAccess;

Expand All @@ -22,19 +20,20 @@ class QrCodeFactory
* @var array
*/
protected $definedOptions = [
'writer',
'size',
'margin',
'foreground_color',
'background_color',
'encoding',
'error_correction_level',
'logo_path',
'logo_width',
'label',
'label_font_size',
'label_font_path',
'label_alignment',
'label_margin',
'logo_path',
'logo_size',
'validate_result'
];

Expand All @@ -43,17 +42,24 @@ class QrCodeFactory
*/
protected $defaultOptions;

/**
* @var WriterRegistryInterface
*/
protected $writerRegistry;

/**
* @var OptionsResolver
*/
protected $optionsResolver;

/**
* @param array $defaultOptions
* @param WriterRegistryInterface $writerRegistry
*/
public function __construct(array $defaultOptions = [])
public function __construct(array $defaultOptions = [], WriterRegistryInterface $writerRegistry = null)
{
$this->defaultOptions = $defaultOptions;
$this->writerRegistry = $writerRegistry;
}

/**
Expand All @@ -67,8 +73,17 @@ public function create($text = '', array $options = [])
$accessor = PropertyAccess::createPropertyAccessor();

$qrCode = new QrCode($text);

if ($this->writerRegistry instanceof WriterRegistryInterface) {
$qrCode->setWriterRegistry($this->writerRegistry);
}

foreach ($this->definedOptions as $option) {
if (isset($options[$option])) {
if ($option === 'writer') {
$options['writer_by_name'] = $options[$option];
$option = 'writer_by_name';
}
$accessor->setValue($qrCode, $option, $options[$option]);
}
}
Expand Down
Loading

0 comments on commit a915e36

Please sign in to comment.