Skip to content

Commit

Permalink
Merge pull request #41 from bitExpert/feature/attribute_config
Browse files Browse the repository at this point in the history
Introduce attribute for url matching strategy
  • Loading branch information
shochdoerfer authored Jan 24, 2025
2 parents e8014f1 + 1cef882 commit e2e5e2c
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ Based on the RegEx strategy, but negates the result. Helpful if only a few pages
### Add custom strategy

The strategy configuration is meant to be extensible. You can create an own strategy by creating a new class that
implements the `\BitExpert\SyliusForceCustomerLoginPlugin\Model\StrategyInterface` interface and is tagged with
`force_customer_login.url_strategy` in your service configuration.
implements the `\BitExpert\SyliusForceCustomerLoginPlugin\Model\StrategyInterface` interface. Your custom class needs
to be registered as a service and be annotated with `#[AsUrlStrategy]`.

## Tests

Expand Down
19 changes: 19 additions & 0 deletions src/Attribute/AsUrlStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Sylius Force Customer Login package.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);

namespace BitExpert\SyliusForceCustomerLoginPlugin\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsUrlStrategy
{
public const SERVICE_TAG = 'force_customer_login.url_strategy';
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

namespace BitExpert\SyliusForceCustomerLoginPlugin\DependencyInjection;

use BitExpert\SyliusForceCustomerLoginPlugin\Attribute\AsUrlStrategy;
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 @@ -27,5 +29,17 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('services.xml');

$this->registerResources('bitexpert_sylius_forcelogin', $config['driver'], $config['resources'], $container);

$this->registerAutoconfiguration($container);
}

private function registerAutoconfiguration(ContainerBuilder $container): void
{
$container->registerAttributeForAutoconfiguration(
AsUrlStrategy::class,
static function (ChildDefinition $definition): void {
$definition->addTag(AsUrlStrategy::SERVICE_TAG);
},
);
}
}
1 change: 0 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ private function addResourcesSection(ArrayNodeDefinition $node): void
->scalarNode('form')->defaultValue(WhitelistEntryType::class)->cannotBeEmpty()->end()
->end()
->end()

->end()
->end()
->end()
Expand Down
4 changes: 0 additions & 4 deletions src/Doctrine/ORM/WhitelistEntryRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@

namespace BitExpert\SyliusForceCustomerLoginPlugin\Doctrine\ORM;

use BitExpert\SyliusForceCustomerLoginPlugin\Model\WhitelistEntry;
use BitExpert\SyliusForceCustomerLoginPlugin\Model\WhitelistEntryInterface;
use Sylius\Component\Channel\Model\ChannelInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

/**
* @extends RepositoryInterface<WhitelistEntry>
*/
interface WhitelistEntryRepositoryInterface extends RepositoryInterface
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Model/NegatedRegexMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

namespace BitExpert\SyliusForceCustomerLoginPlugin\Model;

use BitExpert\SyliusForceCustomerLoginPlugin\Attribute\AsUrlStrategy;

#[AsUrlStrategy]
class NegatedRegexMatcher extends RegexMatcher
{
public function getType(): string
Expand Down
3 changes: 3 additions & 0 deletions src/Model/RegexMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

namespace BitExpert\SyliusForceCustomerLoginPlugin\Model;

use BitExpert\SyliusForceCustomerLoginPlugin\Attribute\AsUrlStrategy;

#[AsUrlStrategy]
class RegexMatcher implements StrategyInterface
{
public function getType(): string
Expand Down
3 changes: 3 additions & 0 deletions src/Model/StaticMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

namespace BitExpert\SyliusForceCustomerLoginPlugin\Model;

use BitExpert\SyliusForceCustomerLoginPlugin\Attribute\AsUrlStrategy;

#[AsUrlStrategy]
class StaticMatcher implements StrategyInterface
{
public function getType(): string
Expand Down
14 changes: 5 additions & 9 deletions src/Resources/config/services/strategies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="BitExpert\SyliusForceCustomerLoginPlugin\Model\NegatedRegexMatcher">
<tag name="force_customer_login.url_strategy" />
</service>
<defaults autoconfigure="true"/>

<service id="BitExpert\SyliusForceCustomerLoginPlugin\Model\RegexMatcher">
<tag name="force_customer_login.url_strategy" />
</service>
<service id="BitExpert\SyliusForceCustomerLoginPlugin\Model\NegatedRegexMatcher" />

<service id="BitExpert\SyliusForceCustomerLoginPlugin\Model\StaticMatcher">
<tag name="force_customer_login.url_strategy" />
</service>
<service id="BitExpert\SyliusForceCustomerLoginPlugin\Model\RegexMatcher" />

<service id="BitExpert\SyliusForceCustomerLoginPlugin\Model\StaticMatcher" />
</services>
</container>

0 comments on commit e2e5e2c

Please sign in to comment.