Skip to content

Commit

Permalink
Introduce attribute for url matching strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
shochdoerfer committed Jan 24, 2025
1 parent c272b09 commit 1cef882
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 19 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);
},
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function process(ContainerBuilder $container): void

$middlewareMethodCallArgs = $this->extractMethodCallArgs($configDefinition);
$middlewareMethodCallArgs[0] = array_merge(
$setMiddlewaresMethodCallArguments[0] ?? [],
$middlewareMethodCallArgs[0] ?? [],
[new Reference('bitexpert.sylius_force_customer_login_plugin.doctrine.middleware')],
);

Expand All @@ -37,7 +37,7 @@ public function process(ContainerBuilder $container): void
->addMethodCall('setMiddlewares', $middlewareMethodCallArgs);
}

/** @return array[] */
/** @return Reference[] */
private function extractMethodCallArgs(Definition $definition): array
{
foreach ($definition->getMethodCalls() as $methodCall) {
Expand Down
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
2 changes: 1 addition & 1 deletion src/Doctrine/DBAL/Types/Strategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function getName()
/**
* @param StrategyInterface[] $strategies
*/
public function setStrategies(array $strategies)
public function setStrategies(array $strategies): void
{
$this->strategies = $strategies;
}
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 1cef882

Please sign in to comment.