Skip to content
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

Magento should create a log entry if an observer does not implement ObserverInterface #21767

Merged
merged 4 commits into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 26 additions & 4 deletions lib/internal/Magento/Framework/Event/Invoker/InvokerDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
namespace Magento\Framework\Event\Invoker;

use Magento\Framework\Event\Observer;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\State;

/**
* Default Invoker.
*/
class InvokerDefault implements \Magento\Framework\Event\InvokerInterface
{
/**
Expand All @@ -22,20 +27,29 @@ class InvokerDefault implements \Magento\Framework\Event\InvokerInterface
/**
* Application state
*
* @var \Magento\Framework\App\State
* @var State
*/
protected $_appState;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param \Magento\Framework\Event\ObserverFactory $observerFactory
* @param \Magento\Framework\App\State $appState
* @param State $appState
* @param LoggerInterface $logger
*/
public function __construct(
\Magento\Framework\Event\ObserverFactory $observerFactory,
\Magento\Framework\App\State $appState
State $appState,
LoggerInterface $logger = null
) {
$this->_observerFactory = $observerFactory;
$this->_appState = $appState;
$this->logger = $logger ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(LoggerInterface::class);
}

/**
Expand All @@ -61,6 +75,8 @@ public function dispatch(array $configuration, Observer $observer)
}

/**
* Execute Observer.
*
* @param \Magento\Framework\Event\ObserverInterface $object
* @param Observer $observer
* @return $this
Expand All @@ -70,14 +86,20 @@ protected function _callObserverMethod($object, $observer)
{
if ($object instanceof \Magento\Framework\Event\ObserverInterface) {
$object->execute($observer);
} elseif ($this->_appState->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that we should delete this condition for the developer mode, also such changes are backward incompatible because method signature is changed (LogicException is not throwing anymore).

} elseif ($this->_appState->getMode() == State::MODE_DEVELOPER) {
throw new \LogicException(
sprintf(
'Observer "%s" must implement interface "%s"',
get_class($object),
\Magento\Framework\Event\ObserverInterface::class
)
);
} else {
$this->logger->warning(sprintf(
'Observer "%s" must implement interface "%s"',
get_class($object),
\Magento\Framework\Event\ObserverInterface::class
));
}
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class InvokerDefaultTest extends \PHPUnit\Framework\TestCase
*/
protected $_invokerDefault;

/**
* @var |Psr\Log|LoggerInterface
*/
private $loggerMock;

protected function setUp()
{
$this->_observerFactoryMock = $this->createMock(\Magento\Framework\Event\ObserverFactory::class);
Expand All @@ -41,10 +46,12 @@ protected function setUp()
['execute']
);
$this->_appStateMock = $this->createMock(\Magento\Framework\App\State::class);
$this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);

$this->_invokerDefault = new \Magento\Framework\Event\Invoker\InvokerDefault(
$this->_observerFactoryMock,
$this->_appStateMock
$this->_appStateMock,
$this->loggerMock
);
}

Expand Down Expand Up @@ -166,13 +173,15 @@ public function testWrongInterfaceCallWithDisabledDeveloperMode($shared)
$this->returnValue($notObserver)
);
$this->_appStateMock->expects(
$this->once()
$this->exactly(1)
)->method(
'getMode'
)->will(
$this->returnValue(\Magento\Framework\App\State::MODE_PRODUCTION)
);

$this->loggerMock->expects($this->once())->method('warning');

$this->_invokerDefault->dispatch(
[
'shared' => $shared,
Expand Down