Skip to content

Commit

Permalink
feature #15300 Provide autoconfiguration with attributes for ChannelB…
Browse files Browse the repository at this point in the history
…undle (jakubtobiasz)

This PR was merged into the 1.13 branch.

Discussion
----------

| Q               | A                                                            |
|-----------------|--------------------------------------------------------------|
| Branch?         | 1.13
| Bug fix?        | no
| New feature?    | yes
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | continuation of #14154
| License         | MIT

Thanks @ffouillet for starting this initiative 🥳!


Commits
-------

3c2f26a Provide autoconfiguration with attributes for ChannelBundle
0342759 Rename AsChannelContextRequestResolver.php to AsRequestBasedChannelResolver.php
  • Loading branch information
TheMilek committed Sep 13, 2023
2 parents 30cc6b6 + 0342759 commit b9ed312
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 1 deletion.
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 AsRequestBasedChannelResolver
{
public const SERVICE_TAG = 'sylius.context.channel.request_based.resolver';

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\AsRequestBasedChannelResolver;
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(
AsRequestBasedChannelResolver::class,
static function (ChildDefinition $definition, AsRequestBasedChannelResolver $attribute): void {
$definition->addTag(AsRequestBasedChannelResolver::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\AsRequestBasedChannelResolver;
use Sylius\Bundle\ChannelBundle\DependencyInjection\SyliusChannelExtension;
use Sylius\Bundle\ChannelBundle\Tests\Stub\ChannelContextStub;
use Sylius\Bundle\ChannelBundle\Tests\Stub\RequestBestChannelResolverStub;
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_request_based_channel_resolver_with_attribute(): void
{
$this->container->setDefinition(
'acme.channel_context_request_resolver_with_attribute',
(new Definition())
->setClass(RequestBestChannelResolverStub::class)
->setAutoconfigured(true),
);

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

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

protected function getContainerExtensions(): array
{
return [
Expand Down
28 changes: 28 additions & 0 deletions src/Sylius/Bundle/ChannelBundle/Tests/Stub/ChannelContextStub.php
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();
}
}
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\AsRequestBasedChannelResolver;
use Sylius\Component\Channel\Context\RequestBased\RequestResolverInterface;
use Sylius\Component\Channel\Model\ChannelInterface;
use Symfony\Component\HttpFoundation\Request;

#[AsRequestBasedChannelResolver(priority: 20)]
final class RequestBestChannelResolverStub implements RequestResolverInterface
{
public function findChannel(Request $request): ?ChannelInterface
{
return null;
}
}
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

0 comments on commit b9ed312

Please sign in to comment.