Skip to content

Extend Observer declaration in events.xml by new attribute ifconfig #38493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: 2.4-develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/internal/Magento/Framework/Event/Config/Converter.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2013 Adobe
* All Rights Reserved.
*/
namespace Magento\Framework\Event\Config;

@@ -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;
}
}
20 changes: 16 additions & 4 deletions lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php
/**
* Default event invoker
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2013 Adobe
* All Rights Reserved.
*/

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;
@@ -31,6 +30,11 @@ class InvokerDefault implements \Magento\Framework\Event\InvokerInterface
*/
protected $_appState;

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $scopeConfig;

/**
* @var LoggerInterface
*/
@@ -39,15 +43,19 @@ 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 = null,
LoggerInterface $logger = null
) {
$this->_observerFactory = $observerFactory;
$this->_appState = $appState;
$this->scopeConfig = $scopeConfig ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(ScopeConfigInterface::class);
$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 {
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php
/**
* @category Magento
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2014 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

@@ -22,7 +21,7 @@ class CollectionTest extends TestCase
*/
protected $collection;

/*
/**
* Array of events in the collection
*
* @var array
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2013 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

@@ -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 static 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);
}
}
5 changes: 3 additions & 2 deletions lib/internal/Magento/Framework/Event/etc/events.xsd
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2013 Adobe
* All Rights Reserved.
*/
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
@@ -55,6 +55,7 @@
<xs:attribute name="instance" type="xs:string" use="optional" />
<xs:attribute name="disabled" type="xs:boolean" use="optional" />
<xs:attribute name="shared" type="xs:boolean" use="optional" />
<xs:attribute name="ifconfig" type="xs:string" use="optional" />
</xs:complexType>

<xs:simpleType name="eventName">