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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide autoconfiguration with attributes for ChannelBundle #15300

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Sylius/Bundle/ChannelBundle/Attribute/AsChannelContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\ChannelBundle\Attribute;

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

public function __construct(private int $priority = 0)
{
}

public function getPriority(): int
{
return $this->priority;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\ChannelBundle\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsChannelContextRequestResolver
NoResponseMate marked this conversation as resolved.
Show resolved Hide resolved
{
public const SERVICE_TAG = 'sylius.context.channel.request_based.resolver';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completely besides the point, but this tag name is awful, we should think of changing it to something more concise


public function __construct(private int $priority = 0)
{
}

public function getPriority(): int
{
return $this->priority;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@

namespace Sylius\Bundle\ChannelBundle\DependencyInjection;

use Sylius\Bundle\ChannelBundle\Attribute\AsChannelContext;
use Sylius\Bundle\ChannelBundle\Attribute\AsChannelContextRequestResolver;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Extension\AbstractResourceExtension;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

Expand All @@ -38,5 +41,24 @@ public function load(array $configs, ContainerBuilder $container): void
}

$container->getDefinition('sylius.repository.channel')->setLazy(true);

$this->registerAutoconfiguration($container);
}

private function registerAutoconfiguration(ContainerBuilder $container): void
{
$container->registerAttributeForAutoconfiguration(
AsChannelContext::class,
static function (ChildDefinition $definition, AsChannelContext $attribute): void {
$definition->addTag(AsChannelContext::SERVICE_TAG, ['priority' => $attribute->getPriority()]);
},
);

$container->registerAttributeForAutoconfiguration(
AsChannelContextRequestResolver::class,
static function (ChildDefinition $definition, AsChannelContextRequestResolver $attribute): void {
$definition->addTag(AsChannelContextRequestResolver::SERVICE_TAG, ['priority' => $attribute->getPriority()]);
},
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
namespace Sylius\Bundle\ChannelBundle\Tests\DependencyInjection;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Sylius\Bundle\ChannelBundle\Attribute\AsChannelContext;
use Sylius\Bundle\ChannelBundle\Attribute\AsChannelContextRequestResolver;
use Sylius\Bundle\ChannelBundle\DependencyInjection\SyliusChannelExtension;
use Sylius\Bundle\ChannelBundle\Tests\Stub\ChannelContextRequestResolverStub;
use Sylius\Bundle\ChannelBundle\Tests\Stub\ChannelContextStub;
use Symfony\Component\DependencyInjection\Definition;

final class SyliusChannelExtensionTest extends AbstractExtensionTestCase
{
Expand Down Expand Up @@ -54,6 +59,45 @@ public function it_uses_disabled_debug_config_if_defined(): void
$this->assertContainerBuilderHasServiceDefinitionWithArgument('sylius.channel_collector', 2, false);
}

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

$this->load(['debug' => false]);
$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithTag(
'acme.channel_context_with_attribute',
AsChannelContext::SERVICE_TAG,
['priority' => 15],
);
}
/** @test */
public function it_autoconfigures_channel_context_request_resolver_with_attribute(): void
{
$this->container->setDefinition(
'acme.channel_context_request_resolver_with_attribute',
(new Definition())
->setClass(ChannelContextRequestResolverStub::class)
->setAutoconfigured(true),
);

$this->load(['debug' => false]);
$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithTag(
'acme.channel_context_request_resolver_with_attribute',
AsChannelContextRequestResolver::SERVICE_TAG,
['priority' => 20],
);
}

protected function getContainerExtensions(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\ChannelBundle\Tests\Stub;

use Sylius\Bundle\ChannelBundle\Attribute\AsChannelContextRequestResolver;
use Sylius\Component\Channel\Context\RequestBased\RequestResolverInterface;
use Sylius\Component\Channel\Model\ChannelInterface;
use Symfony\Component\HttpFoundation\Request;

#[AsChannelContextRequestResolver(priority: 20)]
final class ChannelContextRequestResolverStub implements RequestResolverInterface
{
public function findChannel(Request $request): ?ChannelInterface
{
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\ChannelBundle\Tests\Stub;

use Sylius\Bundle\ChannelBundle\Attribute\AsChannelContext;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Channel\Model\Channel;
use Sylius\Component\Channel\Model\ChannelInterface;

#[AsChannelContext(priority: 15)]
final class ChannelContextStub implements ChannelContextInterface
{
public function getChannel(): ChannelInterface
{
return new Channel();
}
}
3 changes: 2 additions & 1 deletion src/Sylius/Bundle/ChannelBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
},
"autoload-dev": {
"psr-4": {
"Sylius\\Bundle\\ChannelBundle\\spec\\": "spec/"
"Sylius\\Bundle\\ChannelBundle\\spec\\": "spec/",
"Sylius\\Bundle\\ChannelBundle\\Tests\\": "Tests/"
},
"classmap": [
"test/app/AppKernel.php"
Expand Down
Loading