Skip to content

Commit 98c0026

Browse files
authored
Merge pull request #9 from maximehuran/feature/add-config
Add config to enable customers (users)
2 parents 34afde3 + f6d2b32 commit 98c0026

File tree

13 files changed

+290
-103
lines changed

13 files changed

+290
-103
lines changed

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ COMPOSER=symfony composer
66
CONSOLE=${SYMFONY} console
77
export COMPOSE_PROJECT_NAME=no-commerce
88
COMPOSE=docker-compose
9-
YARN=cd ${APP_DIR} && yarn
9+
YARN=yarn
1010
PHPSTAN=symfony php vendor/bin/phpstan
1111
PHPUNIT=symfony php vendor/bin/phpunit
1212
PHPSPEC=symfony php vendor/bin/phpspec
@@ -51,9 +51,8 @@ endif
5151
yarn.install: ${APP_DIR}/yarn.lock
5252

5353
${APP_DIR}/yarn.lock:
54-
${YARN} install
5554
ln -sf ${APP_DIR}/node_modules node_modules
56-
${YARN} encore dev
55+
cd ${APP_DIR} && ${YARN} install && ${YARN} build
5756

5857
node_modules: ${APP_DIR}/node_modules ## Install the Node dependencies using yarn
5958

@@ -63,6 +62,8 @@ ${APP_DIR}/node_modules: yarn.install
6362
### TESTS
6463
### ¯¯¯¯¯
6564

65+
test.all: test.composer test.phpstan test.phpunit test.phpspec test.phpcs test.yaml test.schema test.twig ## Run all tests in once
66+
6667
test.composer: ## Validate composer.json
6768
${COMPOSER} validate --strict
6869

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ Then create the config file in `config/packages/monsieurbiz_sylius_nocommerce_pl
4242
```yaml
4343
imports:
4444
- { resource: "@MonsieurBizSyliusNoCommercePlugin/Resources/config/config.yaml" }
45+
46+
monsieurbiz_sylius_nocommerce:
47+
config:
48+
allow_customers: false
4549
```
4650
51+
You can allow customers by changing the `allow_customers` parameters to `true`.
52+
4753
Add some annotations to your `src/Entity/Channel/Channel.php` entity to prevent error during Channel saving:
4854

4955
```diff
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
imports:
22
- { resource: "@MonsieurBizSyliusNoCommercePlugin/Resources/config/config.yaml" }
3+
4+
monsieurbiz_sylius_nocommerce:
5+
config:
6+
allow_customers: false

src/DependencyInjection/Configuration.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ final class Configuration implements ConfigurationInterface
2323
*/
2424
public function getConfigTreeBuilder(): TreeBuilder
2525
{
26-
return new TreeBuilder('monsieurbiz_sylius_nocommerce');
26+
$treeBuilder = new TreeBuilder('monsieurbiz_sylius_nocommerce');
27+
$rootNode = $treeBuilder->getRootNode();
28+
29+
$rootNode
30+
->children()
31+
// Config
32+
->arrayNode('config')
33+
->children()
34+
->booleanNode('allow_customers')->isRequired()->end()
35+
->end()
36+
->end()
37+
;
38+
39+
return $treeBuilder;
2740
}
2841
}

src/DependencyInjection/MonsieurBizSyliusNoCommerceExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ final class MonsieurBizSyliusNoCommerceExtension extends Extension
2626
public function load(array $config, ContainerBuilder $container): void
2727
{
2828
$configuration = $this->getConfiguration([], $container);
29-
$this->processConfiguration(/** @scrutinizer ignore-type */ $configuration, $config);
29+
$config = $this->processConfiguration(/** @scrutinizer ignore-type */ $configuration, $config);
30+
foreach ($config as $name => $value) {
31+
$container->setParameter($this->getAlias() . '.' . $name, $value);
32+
}
3033
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
3134
$loader->load('services.yaml');
3235
}

src/Kernel/SyliusNoCommerceKernelTrait.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace MonsieurBiz\SyliusNoCommercePlugin\Kernel;
1515

16+
use MonsieurBiz\SyliusNoCommercePlugin\Model\Config;
17+
use MonsieurBiz\SyliusNoCommercePlugin\Model\ConfigInterface;
1618
use MonsieurBiz\SyliusNoCommercePlugin\Routing\RouteCollectionBuilder;
1719
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
1820
use Symfony\Component\Config\Loader\LoaderInterface;
@@ -29,9 +31,21 @@ trait SyliusNoCommerceKernelTrait
2931
*/
3032
public function loadRoutes(LoaderInterface $loader)
3133
{
32-
$routes = new RouteCollectionBuilder($loader);
34+
$routes = new RouteCollectionBuilder($this->getConfig(), $loader);
3335
$this->configureRoutes($routes);
3436

3537
return $routes->build();
3638
}
39+
40+
/**
41+
* Create a NoCommerce Config object.
42+
*
43+
* @return ConfigInterface
44+
*/
45+
private function getConfig(): ConfigInterface
46+
{
47+
return new Config(
48+
(array) $this->container->getParameter('monsieurbiz_sylius_nocommerce.config') ?? []
49+
);
50+
}
3751
}

src/Menu/AdminMenuListener.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,29 @@
1313

1414
namespace MonsieurBiz\SyliusNoCommercePlugin\Menu;
1515

16+
use MonsieurBiz\SyliusNoCommercePlugin\Model\ConfigInterface;
1617
use Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent;
1718

1819
final class AdminMenuListener
1920
{
21+
private ConfigInterface $config;
22+
23+
public function __construct(ConfigInterface $config)
24+
{
25+
$this->config = $config;
26+
}
27+
2028
public function __invoke(MenuBuilderEvent $event): void
2129
{
2230
$menu = $event->getMenu();
2331

2432
$menu->removeChild('sales');
2533
$menu->removeChild('catalog');
2634
$menu->removeChild('marketing');
27-
$menu->removeChild('customers');
35+
36+
if (!$this->config->areCustomersAllowed()) {
37+
$menu->removeChild('customers');
38+
}
2839

2940
if (null !== $configuration = $menu->getChild('configuration')) {
3041
$configuration->removeChild('currencies');

src/Model/Config.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Monsieur Biz' No Commerce plugin for Sylius.
5+
*
6+
* (c) Monsieur Biz <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace MonsieurBiz\SyliusNoCommercePlugin\Model;
15+
16+
final class Config implements ConfigInterface
17+
{
18+
private array $config = [];
19+
20+
public function __construct(array $config)
21+
{
22+
$this->config = $config;
23+
}
24+
25+
public function areCustomersAllowed(): bool
26+
{
27+
return (bool) $this->config['allow_customers'] ?: false;
28+
}
29+
}

src/Model/ConfigInterface.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Monsieur Biz' No Commerce plugin for Sylius.
5+
*
6+
* (c) Monsieur Biz <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace MonsieurBiz\SyliusNoCommercePlugin\Model;
15+
16+
interface ConfigInterface
17+
{
18+
/**
19+
* Return true if customers are allowed on the website.
20+
*
21+
* @return bool
22+
*/
23+
public function areCustomersAllowed(): bool;
24+
}

src/Resources/config/services.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ services:
2323
MonsieurBiz\SyliusNoCommercePlugin\Menu\ShopAccountMenuListener:
2424
tags:
2525
- { name: kernel.event_listener, event: sylius.menu.shop.account, priority: -10000 }
26+
27+
MonsieurBiz\SyliusNoCommercePlugin\Model\ConfigInterface: '@MonsieurBiz\SyliusNoCommercePlugin\Model\Config'
28+
MonsieurBiz\SyliusNoCommercePlugin\Model\Config:
29+
arguments:
30+
- '%monsieurbiz_sylius_nocommerce.config%'

0 commit comments

Comments
 (0)