Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide autoconfiguration with attributes for ShippingBundle #15303

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ShippingBundle\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsShippingCalculator
{
public const SERVICE_TAG = 'sylius.shipping_calculator';

public function __construct(
private string $calculator,
private string $label,
private string $formType,
private int $priority = 0,
) {
}

public function getCalculator(): string
{
return $this->calculator;
}

public function getLabel(): string
{
return $this->label;
}

public function getFormType(): string
{
return $this->formType;
}

public function getPriority(): int
{
return $this->priority;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ShippingBundle\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsShippingMethodResolver
{
public const SERVICE_TAG = 'sylius.shipping_method_resolver';

public function __construct(
private string $type,
private string $label,
private int $priority = 0,
) {
}

public function getType(): string
{
return $this->type;
}

public function getLabel(): string
{
return $this->label;
}

public function getPriority(): int
{
return $this->priority;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ShippingBundle\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsShippingMethodRuleChecker
{
public const SERVICE_TAG = 'sylius.shipping_method_rule_checker';

public function __construct(
private string $type,
private string $label,
private string $formType,
private int $priority = 0,
) {
}

public function getType(): string
{
return $this->type;
}

public function getLabel(): string
{
return $this->label;
}

public function getFormType(): string
{
return $this->formType;
}

public function getPriority(): int
{
return $this->priority;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
namespace Sylius\Bundle\ShippingBundle\DependencyInjection;

use Sylius\Bundle\ResourceBundle\DependencyInjection\Extension\AbstractResourceExtension;
use Sylius\Bundle\ShippingBundle\Attribute\AsShippingCalculator;
use Sylius\Bundle\ShippingBundle\Attribute\AsShippingMethodResolver;
use Sylius\Bundle\ShippingBundle\Attribute\AsShippingMethodRuleChecker;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

Expand All @@ -30,5 +34,44 @@ public function load(array $configs, ContainerBuilder $container): void
$this->registerResources('sylius', $config['driver'], $config['resources'], $container);

$loader->load('services.xml');
$this->registerAutoconfiguration($container);
}

private function registerAutoconfiguration(ContainerBuilder $container): void
{
$container->registerAttributeForAutoconfiguration(
AsShippingCalculator::class,
static function (ChildDefinition $definition, AsShippingCalculator $attribute): void {
$definition->addTag(AsShippingCalculator::SERVICE_TAG, [
'calculator' => $attribute->getCalculator(),
'label' => $attribute->getLabel(),
'form-type' => $attribute->getFormType(),
'priority' => $attribute->getPriority(),
]);
},
);

$container->registerAttributeForAutoconfiguration(
AsShippingMethodResolver::class,
static function (ChildDefinition $definition, AsShippingMethodResolver $attribute): void {
$definition->addTag(AsShippingMethodResolver::SERVICE_TAG, [
'type' => $attribute->getType(),
'label' => $attribute->getLabel(),
'priority' => $attribute->getPriority(),
]);
},
);

$container->registerAttributeForAutoconfiguration(
AsShippingMethodRuleChecker::class,
static function (ChildDefinition $definition, AsShippingMethodRuleChecker $attribute): void {
$definition->addTag(AsShippingMethodRuleChecker::SERVICE_TAG, [
'type' => $attribute->getType(),
'label' => $attribute->getLabel(),
'form-type' => $attribute->getFormType(),
'priority' => $attribute->getPriority(),
]);
},
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ShippingBundle\Tests\DependencyInjection;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Sylius\Bundle\ShippingBundle\Attribute\AsShippingCalculator;
use Sylius\Bundle\ShippingBundle\Attribute\AsShippingMethodResolver;
use Sylius\Bundle\ShippingBundle\Attribute\AsShippingMethodRuleChecker;
use Sylius\Bundle\ShippingBundle\DependencyInjection\SyliusShippingExtension;
use Sylius\Bundle\ShippingBundle\Tests\Stub\ShippingCalculatorStub;
use Sylius\Bundle\ShippingBundle\Tests\Stub\ShippingMethodResolverStub;
use Sylius\Bundle\ShippingBundle\Tests\Stub\ShippingMethodRuleCheckerStub;
use Symfony\Component\DependencyInjection\Definition;

final class SyliusShippingExtensionTest extends AbstractExtensionTestCase
{
/** @test */
public function it_autoconfigures_shipping_calculator_with_attribute(): void
{
$this->container->setDefinition(
'acme.shipping_calculator_autoconfigured',
(new Definition())
->setClass(ShippingCalculatorStub::class)
->setAutoconfigured(true),
);

$this->load();
$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithTag(
'acme.shipping_calculator_autoconfigured',
AsShippingCalculator::SERVICE_TAG,
[
'calculator' => 'test',
'label' => 'Test',
'form-type' => 'SomeFormType',
'priority' => 0,
],
);
}

/** @test */
public function it_autoconfigures_shipping_method_resolver_with_attribute(): void
{
$this->container->setDefinition(
'acme.shipping_method_resolver_autoconfigured',
(new Definition())
->setClass(ShippingMethodResolverStub::class)
->setAutoconfigured(true),
);

$this->load();
$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithTag(
'acme.shipping_method_resolver_autoconfigured',
AsShippingMethodResolver::SERVICE_TAG,
[
'type' => 'test',
'label' => 'Test',
'priority' => 10,
],
);
}

/** @test */
public function it_autoconfigures_shipping_method_rule_checker_with_attribute(): void
{
$this->container->setDefinition(
'acme.shipping_method_rule_checker_autoconfigured',
(new Definition())
->setClass(ShippingMethodRuleCheckerStub::class)
->setAutoconfigured(true),
);

$this->load();
$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithTag(
'acme.shipping_method_rule_checker_autoconfigured',
AsShippingMethodRuleChecker::SERVICE_TAG,
[
'type' => 'test',
'label' => 'Test',
'form-type' => 'SomeFormType',
'priority' => 20,
],
);
}

protected function getContainerExtensions(): array
{
return [new SyliusShippingExtension()];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ShippingBundle\Tests\Stub;

use Sylius\Bundle\ShippingBundle\Attribute\AsShippingCalculator;
use Sylius\Component\Shipping\Calculator\CalculatorInterface;
use Sylius\Component\Shipping\Model\ShipmentInterface;

#[AsShippingCalculator(calculator: 'test', label: 'Test', formType: 'SomeFormType')]
final class ShippingCalculatorStub implements CalculatorInterface
{
public function calculate(ShipmentInterface $subject, array $configuration): int
{
return 0;
}

public function getType(): string
{
return '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ShippingBundle\Tests\Stub;

use Sylius\Bundle\ShippingBundle\Attribute\AsShippingMethodResolver;
use Sylius\Component\Shipping\Model\ShippingSubjectInterface;
use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface;

#[AsShippingMethodResolver(type: 'test', label: 'Test', priority: 10)]
final class ShippingMethodResolverStub implements ShippingMethodsResolverInterface
{
public function getSupportedMethods(ShippingSubjectInterface $subject): array
{
return [];
}

public function supports(ShippingSubjectInterface $subject): bool
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ShippingBundle\Tests\Stub;

use Sylius\Bundle\ShippingBundle\Attribute\AsShippingMethodRuleChecker;
use Sylius\Component\Shipping\Checker\Rule\RuleCheckerInterface;
use Sylius\Component\Shipping\Model\ShippingSubjectInterface;

#[AsShippingMethodRuleChecker(type: 'test', label: 'Test', formType: 'SomeFormType', priority: 20)]
final class ShippingMethodRuleCheckerStub implements RuleCheckerInterface
{
public function isEligible(ShippingSubjectInterface $subject, array $configuration): bool
{
return true;
}
}