-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f71e30
commit 1495ab5
Showing
13 changed files
with
183 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of SyliusNoCommercePlugin corporate website. | ||
* This file is part of Monsieur Biz' No Commerce plugin for Sylius. | ||
* | ||
* (c) SyliusNoCommercePlugin <sylius+syliusnocommerceplugin@monsieurbiz.com> | ||
* (c) Monsieur Biz <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* 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\Provider; | ||
|
||
use Exception; | ||
use MonsieurBiz\SyliusSettingsPlugin\Settings\SettingsInterface; | ||
use Sylius\Component\Channel\Context\ChannelContextInterface; | ||
use Sylius\Component\Core\Model\ChannelInterface; | ||
|
@@ -22,26 +23,29 @@ final class FeaturesProvider implements FeaturesProviderInterface | |
private ChannelContextInterface $channelContext; | ||
|
||
private SettingsInterface $nocommerceSettings; | ||
|
||
public function __construct( | ||
ChannelContextInterface $channelContext, | ||
SettingsInterface $nocommerceSettings | ||
) | ||
{ | ||
) { | ||
$this->channelContext = $channelContext; | ||
$this->nocommerceSettings = $nocommerceSettings; | ||
} | ||
|
||
public function isNoCommerceEnabledForChannel(?ChannelInterface $channel = null): bool | ||
public function isNoCommerceEnabledForChannel(ChannelInterface $channel = null): bool | ||
{ | ||
if (null === $channel) { | ||
$channel = $this->channelContext->getChannel(); | ||
} | ||
// In case we are getting a channel that does not exists yet we pass null as the channel to retrieve the setting value of the global scope | ||
if (null === $channel->getId()) { | ||
$channel = null; | ||
try { | ||
if (null === $channel) { | ||
$channel = $this->channelContext->getChannel(); | ||
} | ||
// In case we are getting a channel that does not exists yet we pass null as the channel to retrieve the setting value of the global scope | ||
if (null === $channel->getId()) { | ||
$channel = null; | ||
} | ||
} catch (Exception $exception) { | ||
return false; | ||
} | ||
|
||
return $this->nocommerceSettings->getCurrentValue($channel, null, 'enabled'); | ||
return (bool) $this->nocommerceSettings->getCurrentValue($channel, null, 'enabled'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of SyliusNoCommercePlugin corporate website. | ||
* This file is part of Monsieur Biz' No Commerce plugin for Sylius. | ||
* | ||
* (c) SyliusNoCommercePlugin <sylius+syliusnocommerceplugin@monsieurbiz.com> | ||
* (c) Monsieur Biz <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* For the full copyright and license information, please view the LICENSE.txt | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
|
@@ -17,5 +17,5 @@ | |
|
||
interface FeaturesProviderInterface | ||
{ | ||
public function isNoCommerceEnabledForChannel(?ChannelInterface $channel = null): bool; | ||
public function isNoCommerceEnabledForChannel(ChannelInterface $channel = null): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Monsieur Biz' No Commerce plugin for Sylius. | ||
* | ||
* (c) Monsieur Biz <[email protected]> | ||
* | ||
* 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\Registry; | ||
|
||
use Laminas\Stdlib\SplPriorityQueue; | ||
use MonsieurBiz\SyliusNoCommercePlugin\Provider\FeaturesProviderInterface; | ||
use Sylius\Bundle\UiBundle\Registry\TemplateBlock; | ||
use Sylius\Bundle\UiBundle\Registry\TemplateBlockRegistryInterface; | ||
|
||
final class TemplateBlockRegistryDecorator implements TemplateBlockRegistryInterface | ||
{ | ||
private TemplateBlockRegistryInterface $templateBlockRegistry; | ||
|
||
private FeaturesProviderInterface $featuresProvider; | ||
|
||
/** @var array|array[] */ | ||
private array $disableEvents = [ | ||
'sylius.admin.dashboard.content' => [ | ||
'before_header_legacy', | ||
'after_header_legacy', | ||
'statistics', | ||
'after_statistics_legacy', | ||
'menu', | ||
'after_menu_legacy', | ||
'latest', | ||
'after_content_legacy', | ||
], | ||
'sylius.shop.layout.header.grid' => [ | ||
'cart', | ||
], | ||
'sylius.shop.layout.header' => [ | ||
'menu', | ||
], | ||
'sylius.shop.homepage' => [ | ||
'latest_products', | ||
'latest_products_carousel', | ||
'products_grid', | ||
], | ||
'sylius.admin.customer.show.content' => [ | ||
'statistics', | ||
], | ||
'sylius.admin.customer.show.address' => [ | ||
'header', | ||
'content', | ||
], | ||
'sylius.admin.layout.topbar_left' => [ | ||
'search', | ||
], | ||
]; | ||
|
||
public function __construct( | ||
TemplateBlockRegistryInterface $templateBlockRegistry, | ||
FeaturesProviderInterface $featuresProvider | ||
) { | ||
$this->templateBlockRegistry = $templateBlockRegistry; | ||
$this->featuresProvider = $featuresProvider; | ||
} | ||
|
||
public function findEnabledForEvents(array $eventNames): array | ||
{ | ||
// No need to sort blocks again if there's only one event because we have them sorted already | ||
if (1 === \count($eventNames)) { | ||
$eventName = reset($eventNames); | ||
|
||
$arrayBlocks = $this->all()[$eventName] ?? []; | ||
$arrayBlocks = $this->removeNoCommerceBlocks($eventName, $arrayBlocks); | ||
|
||
return array_values(array_filter( | ||
$arrayBlocks, | ||
static fn (TemplateBlock $templateBlock): bool => $templateBlock->isEnabled(), | ||
)); | ||
} | ||
|
||
$enabledFinalizedTemplateBlocks = array_filter( | ||
$this->findFinalizedForEvents($eventNames), | ||
static fn (TemplateBlock $templateBlock): bool => $templateBlock->isEnabled(), | ||
); | ||
|
||
$templateBlocksPriorityQueue = new SplPriorityQueue(); | ||
foreach ($enabledFinalizedTemplateBlocks as $templateBlock) { | ||
$templateBlocksPriorityQueue->insert($templateBlock, $templateBlock->getPriority()); | ||
} | ||
|
||
/** @phpstan-ignore-next-line */ | ||
return $templateBlocksPriorityQueue->toArray(); | ||
} | ||
|
||
public function all(): array | ||
{ | ||
return $this->templateBlockRegistry->all(); | ||
} | ||
|
||
private function findFinalizedForEvents(array $eventNames): array | ||
{ | ||
/** | ||
* @var TemplateBlock[] $finalizedTemplateBlocks | ||
* | ||
* @psalm-var array<string, TemplateBlock> $finalizedTemplateBlocks | ||
*/ | ||
$finalizedTemplateBlocks = []; | ||
$reversedEventNames = array_reverse($eventNames); | ||
|
||
foreach ($reversedEventNames as $eventName) { | ||
$templateBlocks = $this->all()[$eventName] ?? []; | ||
$templateBlocks = $this->removeNoCommerceBlocks($eventName, $templateBlocks); | ||
foreach ($templateBlocks as $blockName => $templateBlock) { | ||
if (\array_key_exists($blockName, $finalizedTemplateBlocks)) { | ||
$templateBlock = $finalizedTemplateBlocks[$blockName]->overwriteWith($templateBlock); | ||
} | ||
|
||
$finalizedTemplateBlocks[$blockName] = $templateBlock; | ||
} | ||
} | ||
|
||
return $finalizedTemplateBlocks; | ||
} | ||
|
||
/** | ||
* @return TemplateBlock[] | ||
*/ | ||
private function removeNoCommerceBlocks(string $eventName, array $arrayBlocks): array | ||
{ | ||
// Remove block from no Commerce | ||
if ($this->featuresProvider->isNoCommerceEnabledForChannel()) { | ||
if (isset($this->disableEvents[$eventName])) { | ||
foreach ($this->disableEvents[$eventName] as $block) { | ||
unset($arrayBlocks[$block]); | ||
} | ||
} | ||
} | ||
|
||
return $arrayBlocks; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
imports: | ||
- { resource: "sylius/ui.yaml" } | ||
- { resource: "sylius/grid.yaml" } | ||
- { resource: "monsieurbiz/settings.yaml" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.