Skip to content

Commit

Permalink
Auto Configure Tags and OtherObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed May 13, 2023
1 parent 30589a2 commit 0b7f244
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: tests
tests: vendor ## Run all tests
vendor/bin/phpunit --color
vendor/bin/phpunit --color

.PHONY: code-coverage-html
cc: vendor ## Show test coverage rates (HTML)
Expand Down
3 changes: 3 additions & 0 deletions src/CBORDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use CBOR\Decoder;
use CBOR\StringStream;

/**
* @final
*/
class CBORDecoder
{
public function __construct(
Expand Down
32 changes: 32 additions & 0 deletions src/DependencyInjection/Compiler/OtherObjectCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\CborBundle\DependencyInjection\Compiler;

use CBOR\OtherObject\OtherObjectManagerInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

final class OtherObjectCompilerPass implements CompilerPassInterface
{
public const TAG = 'cbor.other_object';

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container): void
{
if (! $container->hasDefinition(OtherObjectManagerInterface::class)) {
return;
}

$definition = $container->getDefinition(OtherObjectManagerInterface::class);

$taggedAlgorithmServices = $container->findTaggedServiceIds(self::TAG);
foreach ($taggedAlgorithmServices as $id => $tags) {
$definition->addMethodCall('add', [new Reference($id)]);
}
}
}
32 changes: 32 additions & 0 deletions src/DependencyInjection/Compiler/TagCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\CborBundle\DependencyInjection\Compiler;

use CBOR\Tag\TagManagerInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

final class TagCompilerPass implements CompilerPassInterface
{
public const TAG = 'cbor.tag';

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container): void
{
if (! $container->hasDefinition(TagManagerInterface::class)) {
return;
}

$definition = $container->getDefinition(TagManagerInterface::class);

$taggedAlgorithmServices = $container->findTaggedServiceIds(self::TAG);
foreach ($taggedAlgorithmServices as $id => $tags) {
$definition->addMethodCall('add', [new Reference($id)]);
}
}
}
9 changes: 8 additions & 1 deletion src/DependencyInjection/SpomkyLabsCborExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

namespace SpomkyLabs\CborBundle\DependencyInjection;

use CBOR\OtherObject\OtherObjectInterface;
use CBOR\Tag\TagInterface;
use SpomkyLabs\CborBundle\DependencyInjection\Compiler\OtherObjectCompilerPass;
use SpomkyLabs\CborBundle\DependencyInjection\Compiler\TagCompilerPass;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class SpomkyLabsCborExtension extends Extension
final class SpomkyLabsCborExtension extends Extension
{
private const ALIAS = 'cbor';

Expand All @@ -21,6 +25,9 @@ public function getAlias(): string

public function load(array $configs, ContainerBuilder $container): void
{
$container->registerForAutoconfiguration(TagInterface::class)->addTag(TagCompilerPass::TAG);
$container->registerForAutoconfiguration(OtherObjectInterface::class)->addTag(OtherObjectCompilerPass::TAG);

$loader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.php');
}
Expand Down
58 changes: 2 additions & 56 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,8 @@
declare(strict_types=1);

use CBOR\Decoder;
use CBOR\OtherObject\BreakObject;
use CBOR\OtherObject\DoublePrecisionFloatObject;
use CBOR\OtherObject\FalseObject;
use CBOR\OtherObject\HalfPrecisionFloatObject;
use CBOR\OtherObject\NullObject;
use CBOR\OtherObject\OtherObjectManager;
use CBOR\OtherObject\SimpleObject;
use CBOR\OtherObject\SinglePrecisionFloatObject;
use CBOR\OtherObject\TrueObject;
use CBOR\OtherObject\UndefinedObject;
use CBOR\Tag\Base16EncodingTag;
use CBOR\Tag\Base64EncodingTag;
use CBOR\Tag\Base64Tag;
use CBOR\Tag\Base64UrlEncodingTag;
use CBOR\Tag\Base64UrlTag;
use CBOR\Tag\BigFloatTag;
use CBOR\Tag\CBOREncodingTag;
use CBOR\Tag\CBORTag;
use CBOR\Tag\DatetimeTag;
use CBOR\Tag\DecimalFractionTag;
use CBOR\Tag\MimeTag;
use CBOR\Tag\NegativeBigIntegerTag;
use CBOR\Tag\TagManager;
use CBOR\Tag\TimestampTag;
use CBOR\Tag\UnsignedBigIntegerTag;
use CBOR\Tag\UriTag;
use SpomkyLabs\CborBundle\CBORDecoder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

Expand All @@ -42,36 +18,6 @@

$container->set(Decoder::class)->public();
$container->set(CBORDecoder::class)->public();

$container->set(OtherObjectManager::class)
->call('add', [BreakObject::class])
->call('add', [DoublePrecisionFloatObject::class])
->call('add', [FalseObject::class])
->call('add', [HalfPrecisionFloatObject::class])
->call('add', [NullObject::class])
->call('add', [SimpleObject::class])
->call('add', [SinglePrecisionFloatObject::class])
->call('add', [TrueObject::class])
->call('add', [UndefinedObject::class])
->private()
;

$container->set(TagManager::class)
->call('add', [Base16EncodingTag::class])
->call('add', [Base64EncodingTag::class])
->call('add', [Base64Tag::class])
->call('add', [Base64UrlEncodingTag::class])
->call('add', [Base64UrlTag::class])
->call('add', [BigFloatTag::class])
->call('add', [CBOREncodingTag::class])
->call('add', [CBORTag::class])
->call('add', [DatetimeTag::class])
->call('add', [DecimalFractionTag::class])
->call('add', [MimeTag::class])
->call('add', [NegativeBigIntegerTag::class])
->call('add', [TimestampTag::class])
->call('add', [UnsignedBigIntegerTag::class])
->call('add', [UriTag::class])
->private()
;
$container->set(OtherObjectManager::class);
$container->set(TagManager::class);
};
12 changes: 11 additions & 1 deletion src/SpomkyLabsCborBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@

namespace SpomkyLabs\CborBundle;

use SpomkyLabs\CborBundle\DependencyInjection\Compiler\OtherObjectCompilerPass;
use SpomkyLabs\CborBundle\DependencyInjection\Compiler\TagCompilerPass;
use SpomkyLabs\CborBundle\DependencyInjection\SpomkyLabsCborExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class SpomkyLabsCborBundle extends Bundle
final class SpomkyLabsCborBundle extends Bundle
{
public function getContainerExtension(): SpomkyLabsCborExtension
{
return new SpomkyLabsCborExtension();
}

public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new TagCompilerPass());
$container->addCompilerPass(new OtherObjectCompilerPass());
}
}
2 changes: 1 addition & 1 deletion tests/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\Kernel;

class AppKernel extends Kernel
final class AppKernel extends Kernel
{
public function __construct(string $environment)
{
Expand Down
77 changes: 40 additions & 37 deletions tests/Functional/DecodingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ final class DecodingTest extends KernelTestCase
/**
* @test
*/
public function theDecoderServiceIsAvailable(): void
public static function theDecoderServiceIsAvailable(): void
{
//Given
static::bootKernel();
$container = static::$kernel->getContainer();
static::assertTrue($container->has(Decoder::class));

//Then
static::assertTrue(static::getContainer()->has(Decoder::class));
}

/**
Expand All @@ -32,55 +34,56 @@ public function theDecoderServiceIsAvailable(): void
*
* @dataProvider getInputs
*/
public function theDecoderCanDecodeInputs(string $data, string $expectedNormalizedValue): void
public static function theDecoderCanDecodeInputs(string $data, string $expectedNormalizedValue): void
{
//Given
static::bootKernel();
$container = static::$kernel->getContainer();

/** @var CBORDecoder $decoder */
$decoder = $container->get(CBORDecoder::class);
$decoder = static::getContainer()->get(CBORDecoder::class);
$binary = hex2bin($data);
if (! is_string($binary)) {
throw new RuntimeException('Invalid test case');
}

// When
$result = $decoder->decode($binary);

//Then
if ($result instanceof Normalizable) {
static::assertSame($expectedNormalizedValue, $result->normalize());
}
}

public function getInputs(): array
public static function getInputs(): iterable
{
return [
['00', '0'],
['01', '1'],
['0a', '10'],
['17', '23'],
['1818', '24'],
['1819', '25'],
['1864', '100'],
['1903e8', '1000'],
['1a000f4240', '1000000'],
['1b000000e8d4a51000', '1000000000000'],
['20', '-1'],
['29', '-10'],
['3863', '-100'],
['3903e7', '-1000'],
['c349010000000000000000', '-18446744073709551617'],
['3bffffffffffffffff', '-18446744073709551616'],
['1bffffffffffffffff', '18446744073709551615'],
['c249010000000000000000', '18446744073709551616'],
['7f624865626c6c616fff', 'Hello'],
['7f612863efbda163e2979563e280bf63e2979563efbda16129ff', '(。◕‿◕。)'],
['6548656c6c6f', 'Hello'],
['7128efbda1e29795e280bfe29795efbda129', '(。◕‿◕。)'],
['781948656c6c6f48656c6c6f48656c6c6f48656c6c6f48656c6c6f', 'HelloHelloHelloHelloHello'],
['5f424865426c6c416fff', 'Hello'],
['5f412843efbda143e2979543e280bf43e2979543efbda14129ff', '(。◕‿◕。)'],
['4548656c6c6f', 'Hello'],
['5128efbda1e29795e280bfe29795efbda129', '(。◕‿◕。)'],
['581948656c6c6f48656c6c6f48656c6c6f48656c6c6f48656c6c6f', 'HelloHelloHelloHelloHello'],
];
yield ['00', '0'];
yield ['01', '1'];
yield ['0a', '10'];
yield ['17', '23'];
yield ['1818', '24'];
yield ['1819', '25'];
yield ['1864', '100'];
yield ['1903e8', '1000'];
yield ['1a000f4240', '1000000'];
yield ['1b000000e8d4a51000', '1000000000000'];
yield ['20', '-1'];
yield ['29', '-10'];
yield ['3863', '-100'];
yield ['3903e7', '-1000'];
yield ['c349010000000000000000', '-18446744073709551617'];
yield ['3bffffffffffffffff', '-18446744073709551616'];
yield ['1bffffffffffffffff', '18446744073709551615'];
yield ['c249010000000000000000', '18446744073709551616'];
yield ['7f624865626c6c616fff', 'Hello'];
yield ['7f612863efbda163e2979563e280bf63e2979563efbda16129ff', '(。◕‿◕。)'];
yield ['6548656c6c6f', 'Hello'];
yield ['7128efbda1e29795e280bfe29795efbda129', '(。◕‿◕。)'];
yield ['781948656c6c6f48656c6c6f48656c6c6f48656c6c6f48656c6c6f', 'HelloHelloHelloHelloHello'];
yield ['5f424865426c6c416fff', 'Hello'];
yield ['5f412843efbda143e2979543e280bf43e2979543efbda14129ff', '(。◕‿◕。)'];
yield ['4548656c6c6f', 'Hello'];
yield ['5128efbda1e29795e280bfe29795efbda129', '(。◕‿◕。)'];
yield ['581948656c6c6f48656c6c6f48656c6c6f48656c6c6f48656c6c6f', 'HelloHelloHelloHelloHello'];
}
}

0 comments on commit 0b7f244

Please sign in to comment.