Skip to content

Commit

Permalink
Merge pull request #39 from maximehuran/feature/route-to-enable
Browse files Browse the repository at this point in the history
Route to enable are now matching with the full array routes
  • Loading branch information
maximehuran authored Sep 3, 2024
2 parents 323922a + 5b0ccda commit 1ca4068
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 353 deletions.
1 change: 0 additions & 1 deletion docker-compose.yaml.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.8'
services:
database:
image: mysql:8.0
Expand Down
42 changes: 26 additions & 16 deletions src/Form/Type/Settings/NoCommerceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace MonsieurBiz\SyliusNoCommercePlugin\Form\Type\Settings;

use MonsieurBiz\SyliusNoCommercePlugin\Firewall\RegistryInterface;
use MonsieurBiz\SyliusNoCommercePlugin\Provider\FeaturesProvider;
use MonsieurBiz\SyliusNoCommercePlugin\Model\ConfigInterface;
use MonsieurBiz\SyliusSettingsPlugin\Form\AbstractSettingsType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
Expand All @@ -35,6 +35,7 @@ public function __construct(RegistryInterface $firewallRegistry)

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
Expand All @@ -61,20 +62,29 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'multiple' => true,
'choices' => $choices,
]);
$this->addWithDefaultCheckbox($builder, 're_enabled_admin_routes', ChoiceType::class, [
'label' => 'monsieurbiz.nocommerce.ui.form.field.re_enabled_admin_routes.label',
'required' => false,
'multiple' => true,
'choices' => [
'sylius.ui.countries' => FeaturesProvider::COUNTRIES_KEY,
'sylius.ui.currencies' => FeaturesProvider::CURRENCIES_KEY,
'sylius.ui.inventory' => FeaturesProvider::INVENTORY_KEY,
'sylius.ui.payment' => FeaturesProvider::PAYMENT_KEY,
'sylius.menu.admin.main.catalog.header' => FeaturesProvider::CATALOG_KEY,
'sylius.ui.shipping' => FeaturesProvider::SHIPPING_KEY,
'sylius.ui.tax' => FeaturesProvider::TAX_KEY,
'sylius.ui.zones' => FeaturesProvider::ZONES_KEY,
],
]);

if ($this->isDefaultForm($builder)) {
$this->addWithDefaultCheckbox($builder, 'routes_to_enable', ChoiceType::class, [
'label' => 'monsieurbiz.nocommerce.ui.form.field.routes_to_enable.label',
'required' => false,
'multiple' => true,
'expanded' => false,
'choices' => $this->getEnabledRouteChoices(),
]);
}
}

private function getEnabledRouteChoices(): array
{
$allRoutes = ConfigInterface::ROUTES_BY_GROUP;
$choices = [];

foreach ($allRoutes as $group => $routes) {
foreach ($routes as $route) {
$choices[$group][$route] = $route;
}
}

return $choices;
}
}
217 changes: 35 additions & 182 deletions src/Kernel/SyliusNoCommerceKernelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

namespace MonsieurBiz\SyliusNoCommercePlugin\Kernel;

use Exception;
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;
Expand All @@ -25,186 +27,6 @@ trait SyliusNoCommerceKernelTrait
MicroKernelTrait::loadRoutes as parentLoadRoutes;
}

private array $routesToRemove = [
// Customers & Account & Users
'customer' => [
'sylius_admin_partial_customer',
'sylius_admin_customer',
'api_customer',
'sylius_shop_log',
'sylius_shop_register',
'sylius_shop_request_password_reset_token',
'sylius_shop_password_reset',
'sylius_shop_user_request_verification_token',
'sylius_shop_user_verification',
'sylius_shop_account',
'api_register_shop_users_post_collection',
'sylius_api_shop_authentication_token',
'sylius_shop_ajax_user_check_action',
],

// Products
'product' => [
'sylius_admin_product',
'sylius_admin_api_product',
'sylius_admin_ajax_product',
'sylius_shop_partial_product',
'sylius_shop_product',
'sylius_admin_partial_product',
'sylius_admin_ajax_generate_product_slug',
'api_product',
],

// Taxons
'taxon' => [
'sylius_admin_partial_taxon',
'sylius_admin_ajax_taxon',
'sylius_admin_taxon',
'sylius_admin_api_taxon',
'sylius_shop_partial_taxon',
'sylius_admin_ajax_generate_taxon_slug',
'sylius_shop_partial_channel_menu_taxon_index',
'api_taxon',
],

// Checkout
'checkout' => [
'sylius_admin_api_checkout',
'sylius_shop_checkout',
'sylius_shop_register_after_checkout',
],

// Addresses
'address' => [
'sylius_shop_account_address',
'sylius_admin_partial_address',
],

// Orders
'order' => [
'sylius_admin_order',
'sylius_admin_api_order',
'sylius_shop_account_order',
'sylius_shop_order',
'sylius_admin_partial_order',
'sylius_admin_customer_order',
'sylius_admin_api_customer_order',
'api_order',
],

// Adjustments
'adjustment' => [
'sylius_admin_api_adjustment',
'sylius_shop_ajax_render_province_form',
'api_adjustment',
],

// Promotions
'promotion' => [
'sylius_admin_partial_promotion',
'sylius_admin_promotion',
'sylius_admin_api_promotion',
'api_promo',
],

// Shipping and Shipments
'shipment' => [
'sylius_admin_partial_shipment',
'sylius_admin_ship',
'sylius_admin_api_ship',
'api_ship',
],

// Inventory
'inventory' => [
'sylius_admin_inventory',
],

// Attributes
'attribute' => [
'sylius_admin_get_attribute_types',
'sylius_admin_get_product_attributes',
'sylius_admin_render_attribute_forms',
],

// Payments
'payment' => [
'sylius_admin_payment',
'sylius_admin_get_payment',
'payum_',
'sylius_admin_api_payment',
'api_pay',
],

// PayPal
'paypal' => [
'sylius_paypal',
],

// Taxes
'tax' => [
'sylius_admin_tax_',
'sylius_admin_api_tax_',
'api_tax',
],

// Currencies
'currency' => [
'sylius_admin_currency',
'sylius_admin_api_currency',
'sylius_shop_switch_currency',
'api_currencies',
],

// Exchange rates
'exchange' => [
'sylius_admin_exchange',
'sylius_admin_api_exchange',
'api_exchange',
],

// Zones
'zone' => [
'sylius_admin_zone',
'sylius_admin_api_zone',
'api_zone',
],

// Countries
'country' => [
'sylius_admin_country',
'sylius_admin_api_country',
'api_countries',
],

// Provinces
'province' => [
'sylius_admin_api_province',
'sylius_admin_ajax_render_province_form',
'api_province',
],

// Carts
'cart' => [
'sylius_admin_api_cart',
'sylius_shop_ajax_cart',
'sylius_shop_partial_cart',
'sylius_shop_cart',
'api_cart',
],

// Dashboard
'dashboard' => [
'sylius_admin_dashboard_statistics',
],

// Others
'other' => [
'api_shop_billing',
'api_channels_shop',
],
];

/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
Expand All @@ -216,7 +38,7 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection
foreach ($collection as $name => $route) {
foreach ($routesToRemove as $routeToRemove) {
if (false !== strpos($name, $routeToRemove)) {
$route->setCondition('not context.checkNoCommerce(params)');
$route->setCondition('not context.checkNoCommerce()');
}
}
}
Expand All @@ -239,27 +61,58 @@ private function getConfig(): ConfigInterface
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function getRoutesToRemove(): array
public function getRoutesToRemove(): array
{
$config = $this->getConfig();
$routesToRemove = [];
$this->routesToRemove = ConfigInterface::ROUTES_BY_GROUP;

/** @deprecated */
if ($config->areCustomersAllowed()) {
unset($this->routesToRemove['customer']);
}

/** @deprecated */
if ($config->areZonesAllowed()) {
unset($this->routesToRemove['zone']);
}

/** @deprecated */
if ($config->areZonesAllowed() || $config->areCountriesAllowed()) {
unset($this->routesToRemove['country']);
}

// Loop on settings to add routes
/** @var FeaturesProviderInterface $featuresProvider */
$featuresProvider = $this->container->get('monsieurbiz.no_commerce.provider.features_provider');

try {
$routesToEnable = $featuresProvider->getRoutesToEnable();
} catch (Exception $e) {
$routesToEnable = [];
}

foreach ($routesToEnable as $route) {
$this->enableRoute($route);
}

foreach ($this->routesToRemove as $routes) {
$routesToRemove = array_merge($routesToRemove, $routes);
}

return $routesToRemove;
}

private function enableRoute(string $route): void
{
foreach ($this->routesToRemove as $group => $routes) {
if (false !== ($key = array_search($route, $routes, true))) {
unset($this->routesToRemove[$group][$key]);
}

if (empty($this->routesToRemove[$group])) {
unset($this->routesToRemove[$group]);
}
}
}
}
Loading

0 comments on commit 1ca4068

Please sign in to comment.