Skip to content

Commit 1ca4068

Browse files
authored
Merge pull request #39 from maximehuran/feature/route-to-enable
Route to enable are now matching with the full array routes
2 parents 323922a + 5b0ccda commit 1ca4068

File tree

10 files changed

+288
-353
lines changed

10 files changed

+288
-353
lines changed

docker-compose.yaml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
version: '3.8'
21
services:
32
database:
43
image: mysql:8.0

src/Form/Type/Settings/NoCommerceType.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace MonsieurBiz\SyliusNoCommercePlugin\Form\Type\Settings;
1515

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

3636
/**
3737
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
38+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
3839
*/
3940
public function buildForm(FormBuilderInterface $builder, array $options): void
4041
{
@@ -61,20 +62,29 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
6162
'multiple' => true,
6263
'choices' => $choices,
6364
]);
64-
$this->addWithDefaultCheckbox($builder, 're_enabled_admin_routes', ChoiceType::class, [
65-
'label' => 'monsieurbiz.nocommerce.ui.form.field.re_enabled_admin_routes.label',
66-
'required' => false,
67-
'multiple' => true,
68-
'choices' => [
69-
'sylius.ui.countries' => FeaturesProvider::COUNTRIES_KEY,
70-
'sylius.ui.currencies' => FeaturesProvider::CURRENCIES_KEY,
71-
'sylius.ui.inventory' => FeaturesProvider::INVENTORY_KEY,
72-
'sylius.ui.payment' => FeaturesProvider::PAYMENT_KEY,
73-
'sylius.menu.admin.main.catalog.header' => FeaturesProvider::CATALOG_KEY,
74-
'sylius.ui.shipping' => FeaturesProvider::SHIPPING_KEY,
75-
'sylius.ui.tax' => FeaturesProvider::TAX_KEY,
76-
'sylius.ui.zones' => FeaturesProvider::ZONES_KEY,
77-
],
78-
]);
65+
66+
if ($this->isDefaultForm($builder)) {
67+
$this->addWithDefaultCheckbox($builder, 'routes_to_enable', ChoiceType::class, [
68+
'label' => 'monsieurbiz.nocommerce.ui.form.field.routes_to_enable.label',
69+
'required' => false,
70+
'multiple' => true,
71+
'expanded' => false,
72+
'choices' => $this->getEnabledRouteChoices(),
73+
]);
74+
}
75+
}
76+
77+
private function getEnabledRouteChoices(): array
78+
{
79+
$allRoutes = ConfigInterface::ROUTES_BY_GROUP;
80+
$choices = [];
81+
82+
foreach ($allRoutes as $group => $routes) {
83+
foreach ($routes as $route) {
84+
$choices[$group][$route] = $route;
85+
}
86+
}
87+
88+
return $choices;
7989
}
8090
}

src/Kernel/SyliusNoCommerceKernelTrait.php

Lines changed: 35 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
namespace MonsieurBiz\SyliusNoCommercePlugin\Kernel;
1515

16+
use Exception;
1617
use MonsieurBiz\SyliusNoCommercePlugin\Model\Config;
1718
use MonsieurBiz\SyliusNoCommercePlugin\Model\ConfigInterface;
19+
use MonsieurBiz\SyliusNoCommercePlugin\Provider\FeaturesProviderInterface;
1820
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
1921
use Symfony\Component\Config\Loader\LoaderInterface;
2022
use Symfony\Component\Routing\RouteCollection;
@@ -25,186 +27,6 @@ trait SyliusNoCommerceKernelTrait
2527
MicroKernelTrait::loadRoutes as parentLoadRoutes;
2628
}
2729

28-
private array $routesToRemove = [
29-
// Customers & Account & Users
30-
'customer' => [
31-
'sylius_admin_partial_customer',
32-
'sylius_admin_customer',
33-
'api_customer',
34-
'sylius_shop_log',
35-
'sylius_shop_register',
36-
'sylius_shop_request_password_reset_token',
37-
'sylius_shop_password_reset',
38-
'sylius_shop_user_request_verification_token',
39-
'sylius_shop_user_verification',
40-
'sylius_shop_account',
41-
'api_register_shop_users_post_collection',
42-
'sylius_api_shop_authentication_token',
43-
'sylius_shop_ajax_user_check_action',
44-
],
45-
46-
// Products
47-
'product' => [
48-
'sylius_admin_product',
49-
'sylius_admin_api_product',
50-
'sylius_admin_ajax_product',
51-
'sylius_shop_partial_product',
52-
'sylius_shop_product',
53-
'sylius_admin_partial_product',
54-
'sylius_admin_ajax_generate_product_slug',
55-
'api_product',
56-
],
57-
58-
// Taxons
59-
'taxon' => [
60-
'sylius_admin_partial_taxon',
61-
'sylius_admin_ajax_taxon',
62-
'sylius_admin_taxon',
63-
'sylius_admin_api_taxon',
64-
'sylius_shop_partial_taxon',
65-
'sylius_admin_ajax_generate_taxon_slug',
66-
'sylius_shop_partial_channel_menu_taxon_index',
67-
'api_taxon',
68-
],
69-
70-
// Checkout
71-
'checkout' => [
72-
'sylius_admin_api_checkout',
73-
'sylius_shop_checkout',
74-
'sylius_shop_register_after_checkout',
75-
],
76-
77-
// Addresses
78-
'address' => [
79-
'sylius_shop_account_address',
80-
'sylius_admin_partial_address',
81-
],
82-
83-
// Orders
84-
'order' => [
85-
'sylius_admin_order',
86-
'sylius_admin_api_order',
87-
'sylius_shop_account_order',
88-
'sylius_shop_order',
89-
'sylius_admin_partial_order',
90-
'sylius_admin_customer_order',
91-
'sylius_admin_api_customer_order',
92-
'api_order',
93-
],
94-
95-
// Adjustments
96-
'adjustment' => [
97-
'sylius_admin_api_adjustment',
98-
'sylius_shop_ajax_render_province_form',
99-
'api_adjustment',
100-
],
101-
102-
// Promotions
103-
'promotion' => [
104-
'sylius_admin_partial_promotion',
105-
'sylius_admin_promotion',
106-
'sylius_admin_api_promotion',
107-
'api_promo',
108-
],
109-
110-
// Shipping and Shipments
111-
'shipment' => [
112-
'sylius_admin_partial_shipment',
113-
'sylius_admin_ship',
114-
'sylius_admin_api_ship',
115-
'api_ship',
116-
],
117-
118-
// Inventory
119-
'inventory' => [
120-
'sylius_admin_inventory',
121-
],
122-
123-
// Attributes
124-
'attribute' => [
125-
'sylius_admin_get_attribute_types',
126-
'sylius_admin_get_product_attributes',
127-
'sylius_admin_render_attribute_forms',
128-
],
129-
130-
// Payments
131-
'payment' => [
132-
'sylius_admin_payment',
133-
'sylius_admin_get_payment',
134-
'payum_',
135-
'sylius_admin_api_payment',
136-
'api_pay',
137-
],
138-
139-
// PayPal
140-
'paypal' => [
141-
'sylius_paypal',
142-
],
143-
144-
// Taxes
145-
'tax' => [
146-
'sylius_admin_tax_',
147-
'sylius_admin_api_tax_',
148-
'api_tax',
149-
],
150-
151-
// Currencies
152-
'currency' => [
153-
'sylius_admin_currency',
154-
'sylius_admin_api_currency',
155-
'sylius_shop_switch_currency',
156-
'api_currencies',
157-
],
158-
159-
// Exchange rates
160-
'exchange' => [
161-
'sylius_admin_exchange',
162-
'sylius_admin_api_exchange',
163-
'api_exchange',
164-
],
165-
166-
// Zones
167-
'zone' => [
168-
'sylius_admin_zone',
169-
'sylius_admin_api_zone',
170-
'api_zone',
171-
],
172-
173-
// Countries
174-
'country' => [
175-
'sylius_admin_country',
176-
'sylius_admin_api_country',
177-
'api_countries',
178-
],
179-
180-
// Provinces
181-
'province' => [
182-
'sylius_admin_api_province',
183-
'sylius_admin_ajax_render_province_form',
184-
'api_province',
185-
],
186-
187-
// Carts
188-
'cart' => [
189-
'sylius_admin_api_cart',
190-
'sylius_shop_ajax_cart',
191-
'sylius_shop_partial_cart',
192-
'sylius_shop_cart',
193-
'api_cart',
194-
],
195-
196-
// Dashboard
197-
'dashboard' => [
198-
'sylius_admin_dashboard_statistics',
199-
],
200-
201-
// Others
202-
'other' => [
203-
'api_shop_billing',
204-
'api_channels_shop',
205-
],
206-
];
207-
20830
/**
20931
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
21032
*/
@@ -216,7 +38,7 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection
21638
foreach ($collection as $name => $route) {
21739
foreach ($routesToRemove as $routeToRemove) {
21840
if (false !== strpos($name, $routeToRemove)) {
219-
$route->setCondition('not context.checkNoCommerce(params)');
41+
$route->setCondition('not context.checkNoCommerce()');
22042
}
22143
}
22244
}
@@ -239,27 +61,58 @@ private function getConfig(): ConfigInterface
23961
*
24062
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
24163
*/
242-
private function getRoutesToRemove(): array
64+
public function getRoutesToRemove(): array
24365
{
24466
$config = $this->getConfig();
24567
$routesToRemove = [];
68+
$this->routesToRemove = ConfigInterface::ROUTES_BY_GROUP;
24669

70+
/** @deprecated */
24771
if ($config->areCustomersAllowed()) {
24872
unset($this->routesToRemove['customer']);
24973
}
25074

75+
/** @deprecated */
25176
if ($config->areZonesAllowed()) {
25277
unset($this->routesToRemove['zone']);
25378
}
25479

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

85+
// Loop on settings to add routes
86+
/** @var FeaturesProviderInterface $featuresProvider */
87+
$featuresProvider = $this->container->get('monsieurbiz.no_commerce.provider.features_provider');
88+
89+
try {
90+
$routesToEnable = $featuresProvider->getRoutesToEnable();
91+
} catch (Exception $e) {
92+
$routesToEnable = [];
93+
}
94+
95+
foreach ($routesToEnable as $route) {
96+
$this->enableRoute($route);
97+
}
98+
25999
foreach ($this->routesToRemove as $routes) {
260100
$routesToRemove = array_merge($routesToRemove, $routes);
261101
}
262102

263103
return $routesToRemove;
264104
}
105+
106+
private function enableRoute(string $route): void
107+
{
108+
foreach ($this->routesToRemove as $group => $routes) {
109+
if (false !== ($key = array_search($route, $routes, true))) {
110+
unset($this->routesToRemove[$group][$key]);
111+
}
112+
113+
if (empty($this->routesToRemove[$group])) {
114+
unset($this->routesToRemove[$group]);
115+
}
116+
}
117+
}
265118
}

0 commit comments

Comments
 (0)