From 264de2be8bdebabdb3ee21bec3e988a38df8f9cf Mon Sep 17 00:00:00 2001 From: bartek Date: Wed, 6 Mar 2024 13:59:40 +0100 Subject: [PATCH 1/5] Extend Observer declaration in events.xml file by new attribute ifconfig. Similar to layouts xml files ifconfig is optional conditioner based on which Observer will be executed or not. --- .../Framework/Event/Config/Converter.php | 6 ++ .../Event/Invoker/InvokerDefault.php | 12 ++++ .../Test/Unit/Invoker/InvokerDefaultTest.php | 67 +++++++++++++++++++ .../Magento/Framework/Event/etc/events.xsd | 1 + 4 files changed, 86 insertions(+) diff --git a/lib/internal/Magento/Framework/Event/Config/Converter.php b/lib/internal/Magento/Framework/Event/Config/Converter.php index 35d056b61e057..258dec891958d 100644 --- a/lib/internal/Magento/Framework/Event/Config/Converter.php +++ b/lib/internal/Magento/Framework/Event/Config/Converter.php @@ -77,6 +77,12 @@ public function _convertObserverConfig($observerConfig) $output['shared'] = false; } + /** Parse condition configuration */ + $ifconfigAttribute = $observerConfig->attributes->getNamedItem('ifconfig'); + if ($ifconfigAttribute && !empty($ifconfigAttribute->nodeValue)) { + $output['ifconfig'] = $ifconfigAttribute->nodeValue; + } + return $output; } } diff --git a/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php b/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php index acd0a61633557..d370265afa5b9 100644 --- a/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php +++ b/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php @@ -31,6 +31,11 @@ class InvokerDefault implements \Magento\Framework\Event\InvokerInterface */ protected $_appState; + /** + * @var \Magento\Framework\App\Config\ScopeConfigInterface + */ + protected $scopeConfig; + /** * @var LoggerInterface */ @@ -39,15 +44,18 @@ class InvokerDefault implements \Magento\Framework\Event\InvokerInterface /** * @param \Magento\Framework\Event\ObserverFactory $observerFactory * @param State $appState + * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param LoggerInterface $logger */ public function __construct( \Magento\Framework\Event\ObserverFactory $observerFactory, State $appState, + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, LoggerInterface $logger = null ) { $this->_observerFactory = $observerFactory; $this->_appState = $appState; + $this->scopeConfig = $scopeConfig; $this->logger = $logger ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(LoggerInterface::class); } @@ -66,6 +74,10 @@ public function dispatch(array $configuration, Observer $observer) return; } + if (isset($configuration['ifconfig']) && $this->scopeConfig->isSetFlag($configuration['ifconfig']) === false) { + return; + } + if (isset($configuration['shared']) && false === $configuration['shared']) { $object = $this->_observerFactory->create($configuration['instance']); } else { diff --git a/lib/internal/Magento/Framework/Event/Test/Unit/Invoker/InvokerDefaultTest.php b/lib/internal/Magento/Framework/Event/Test/Unit/Invoker/InvokerDefaultTest.php index 09d10b17429b5..d8a86a4729001 100644 --- a/lib/internal/Magento/Framework/Event/Test/Unit/Invoker/InvokerDefaultTest.php +++ b/lib/internal/Magento/Framework/Event/Test/Unit/Invoker/InvokerDefaultTest.php @@ -40,6 +40,11 @@ class InvokerDefaultTest extends TestCase */ protected $_appStateMock; + /** + * @var MockObject + */ + protected $_scopeConfigMock; + /** * @var InvokerDefault */ @@ -59,11 +64,13 @@ protected function setUp(): void ['execute'] ); $this->_appStateMock = $this->createMock(State::class); + $this->_scopeConfigMock = $this->createMock(\Magento\Framework\App\Config::class); $this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); $this->_invokerDefault = new InvokerDefault( $this->_observerFactoryMock, $this->_appStateMock, + $this->_scopeConfigMock, $this->loggerMock ); } @@ -215,4 +222,64 @@ public function dataProviderForMethodIsNotDefined() { return ['shared' => [true], 'non shared' => [false]]; } + + public function testDispatchWithIfconfigFalse() + { + $this->_observerFactoryMock->expects($this->never())->method('get'); + $this->_observerFactoryMock->expects($this->never())->method('create'); + + $this->_scopeConfigMock->expects( + $this->any() + )->method( + 'isSetFlag' + )->with( + 'test/path/value' + )->willReturn( + false + ); + + $this->_invokerDefault->dispatch(['ifconfig' => 'test/path/value'], $this->_observerMock); + } + + public function testDispatchWithIfconfigTrue() + { + $this->_listenerMock->expects($this->once())->method('execute'); + $this->_scopeConfigMock->expects( + $this->any() + )->method( + 'isSetFlag' + )->with( + 'test/path/value' + )->willReturn( + true + ); + $this->_observerFactoryMock->expects( + $this->once() + )->method( + 'get' + )->with( + 'class_name' + )->willReturn( + $this->_listenerMock + ); + + $this->_invokerDefault->dispatch(['instance' => 'class_name', + 'ifconfig' => 'test/path/value'], $this->_observerMock); + } + + public function testDispatchWithIfconfigMissing() + { + $this->_listenerMock->expects($this->once())->method('execute'); + $this->_observerFactoryMock->expects( + $this->once() + )->method( + 'get' + )->with( + 'class_name' + )->willReturn( + $this->_listenerMock + ); + + $this->_invokerDefault->dispatch(['instance' => 'class_name'], $this->_observerMock); + } } diff --git a/lib/internal/Magento/Framework/Event/etc/events.xsd b/lib/internal/Magento/Framework/Event/etc/events.xsd index cac62af356760..86cafe4162703 100644 --- a/lib/internal/Magento/Framework/Event/etc/events.xsd +++ b/lib/internal/Magento/Framework/Event/etc/events.xsd @@ -55,6 +55,7 @@ + From 481031c5af9ee5e721d2d1d35142805823c381e9 Mon Sep 17 00:00:00 2001 From: bartek Date: Fri, 19 Apr 2024 14:20:56 +0200 Subject: [PATCH 2/5] add missing phpdoc --- .../Magento/Framework/Event/Test/Unit/CollectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Event/Test/Unit/CollectionTest.php b/lib/internal/Magento/Framework/Event/Test/Unit/CollectionTest.php index 1d65c82c200b0..211543ab5803c 100644 --- a/lib/internal/Magento/Framework/Event/Test/Unit/CollectionTest.php +++ b/lib/internal/Magento/Framework/Event/Test/Unit/CollectionTest.php @@ -22,7 +22,7 @@ class CollectionTest extends TestCase */ protected $collection; - /* + /** * Array of events in the collection * * @var array From bf1c34641161dae3e79b3a9d3432e29ae887651d Mon Sep 17 00:00:00 2001 From: bartek Date: Mon, 22 Apr 2024 12:32:44 +0200 Subject: [PATCH 3/5] update phpdoc --- lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php | 2 -- .../Magento/Framework/Event/Test/Unit/CollectionTest.php | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php b/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php index d370265afa5b9..0b8d4412a0bde 100644 --- a/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php +++ b/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php @@ -1,7 +1,5 @@ Date: Thu, 6 Jun 2024 13:02:01 +0200 Subject: [PATCH 4/5] changes due to PR request --- .../Magento/Framework/Event/Invoker/InvokerDefault.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php b/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php index 0b8d4412a0bde..a4af49160f63c 100644 --- a/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php +++ b/lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php @@ -6,6 +6,7 @@ namespace Magento\Framework\Event\Invoker; +use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Event\Observer; use Psr\Log\LoggerInterface; use Magento\Framework\App\State; @@ -48,12 +49,13 @@ class InvokerDefault implements \Magento\Framework\Event\InvokerInterface public function __construct( \Magento\Framework\Event\ObserverFactory $observerFactory, State $appState, - \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig = null, LoggerInterface $logger = null ) { $this->_observerFactory = $observerFactory; $this->_appState = $appState; - $this->scopeConfig = $scopeConfig; + $this->scopeConfig = $scopeConfig ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(ScopeConfigInterface::class); $this->logger = $logger ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(LoggerInterface::class); } From 5a428be4bb6d6f8dbe291ae8bfd5dd51d55cd0fb Mon Sep 17 00:00:00 2001 From: engcom-Dash Date: Mon, 23 Dec 2024 12:49:03 +0530 Subject: [PATCH 5/5] 38493: fix static failures --- lib/internal/Magento/Framework/Event/Config/Converter.php | 4 ++-- .../Magento/Framework/Event/Invoker/InvokerDefault.php | 4 ++-- .../Magento/Framework/Event/Test/Unit/CollectionTest.php | 5 ++--- .../Framework/Event/Test/Unit/Invoker/InvokerDefaultTest.php | 4 ++-- lib/internal/Magento/Framework/Event/etc/events.xsd | 4 ++-- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/internal/Magento/Framework/Event/Config/Converter.php b/lib/internal/Magento/Framework/Event/Config/Converter.php index 258dec891958d..2fb60880c7c49 100644 --- a/lib/internal/Magento/Framework/Event/Config/Converter.php +++ b/lib/internal/Magento/Framework/Event/Config/Converter.php @@ -1,7 +1,7 @@