Skip to content

Commit

Permalink
feat: better condition to not load routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Etienne Gutbub committed Sep 6, 2023
1 parent 457090d commit a548713
Showing 3 changed files with 71 additions and 14 deletions.
15 changes: 1 addition & 14 deletions src/Kernel/SyliusNoCommerceKernelTrait.php
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@

use MonsieurBiz\SyliusNoCommercePlugin\Model\Config;
use MonsieurBiz\SyliusNoCommercePlugin\Model\ConfigInterface;
use MonsieurBiz\SyliusNoCommercePlugin\Provider\FeaturesProviderInterface;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Routing\RouteCollection;
@@ -26,8 +25,6 @@ trait SyliusNoCommerceKernelTrait
MicroKernelTrait::loadRoutes as parentLoadRoutes;
}

private FeaturesProviderInterface $featuresProvider;

private array $routesToRemove = [
// Customers & Account & Users
'customer' => [
@@ -215,28 +212,18 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection
{
$collection = $this->parentLoadRoutes($loader);

$this->setFeatureProvider($this->container->get('monsieurbiz.no_commerce.provider.features_provider'));
if (!$this->featuresProvider->isNoCommerceEnabledForChannel()) {
return $collection;
}

$routesToRemove = $this->getRoutesToRemove();
foreach ($collection as $name => $route) {
foreach ($routesToRemove as $routeToRemove) {
if (false !== strpos($name, $routeToRemove)) {
$route->setCondition('1 == 0');
$route->setCondition("not(context.getPathInfo() matches '`^%sylius.security.new_api_route%`') and not context.checkNoCommerce()");
}
}
}

return $collection;
}

private function setFeatureProvider(FeaturesProviderInterface $featuresProvider): void
{
$this->featuresProvider = $featuresProvider;
}

/**
* Create a NoCommerce Config object.
*/
7 changes: 7 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -64,3 +64,10 @@ services:
monsieurbiz.no_commerce.provider.features_provider:
class: 'MonsieurBiz\SyliusNoCommercePlugin\Provider\FeaturesProvider'
public: true

# Routing Context
MonsieurBiz\SyliusNoCommercePlugin\Routing\NoCommerceRequestContext:
decorates: router.request_context
arguments:
- '@MonsieurBiz\SyliusNoCommercePlugin\Routing\NoCommerceRequestContext.inner'
- '@monsieurbiz.no_commerce.provider.features_provider'
63 changes: 63 additions & 0 deletions src/Routing/NoCommerceRequestContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of Monsieur Biz' No Commerce plugin for Sylius.
*
* (c) Monsieur Biz <sylius@monsieurbiz.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusNoCommercePlugin\Routing;

use Exception;
use MonsieurBiz\SyliusNoCommercePlugin\Provider\FeaturesProviderInterface;
use Symfony\Component\Routing\RequestContext as BaseRequestContext;

final class NoCommerceRequestContext extends BaseRequestContext
{
private BaseRequestContext $decorated;

private FeaturesProviderInterface $featuresProvider;

public function __construct(
BaseRequestContext $decorated,
FeaturesProviderInterface $featuresProvider
) {
parent::__construct(
$decorated->getBaseUrl(),
$decorated->getMethod(),
$decorated->getHost(),
$decorated->getScheme(),
$decorated->getHttpPort(),
$decorated->getHttpsPort(),
$decorated->getPathInfo(),
$decorated->getQueryString()
);
$this->decorated = $decorated;
$this->featuresProvider = $featuresProvider;
}

public function checkNoCommerce(): bool
{
return $this->featuresProvider->isNoCommerceEnabledForChannel();
}

/**
* @throws Exception
*
* @return mixed
*/
public function __call(string $name, array $arguments)
{
$callback = [$this->decorated, $name];
if (\is_callable($callback)) {
return \call_user_func($callback, $arguments);
}

throw new Exception(sprintf('Method %s not found for class "%s"', $name, \get_class($this->decorated)));
}
}

0 comments on commit a548713

Please sign in to comment.