Skip to content

Commit 12c987e

Browse files
committed
Revert "Remove the dispatch event functions in model and view (joomla#45431)"
This reverts commit 2bd3afa.
1 parent 704aefc commit 12c987e

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

libraries/src/MVC/Model/BaseDatabaseModel.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
use Joomla\Database\Exception\DatabaseNotFoundException;
3232
use Joomla\Event\DispatcherAwareInterface;
3333
use Joomla\Event\DispatcherAwareTrait;
34+
use Joomla\Event\DispatcherInterface;
35+
use Joomla\Event\EventInterface;
3436

3537
// phpcs:disable PSR1.Files.SideEffects
3638
\defined('_JEXEC') or die;
@@ -335,6 +337,55 @@ protected function bootComponent($component): ComponentInterface
335337
return Factory::getApplication()->bootComponent($component);
336338
}
337339

340+
/**
341+
* Get the event dispatcher.
342+
*
343+
* The override was made to keep a backward compatibility for legacy component.
344+
* TODO: Remove the override in 6.0
345+
*
346+
* @return DispatcherInterface
347+
*
348+
* @since 4.4.0
349+
* @throws \UnexpectedValueException May be thrown if the dispatcher has not been set.
350+
*/
351+
public function getDispatcher()
352+
{
353+
if (!$this->dispatcher) {
354+
@trigger_error(
355+
\sprintf('Dispatcher for %s should be set through MVC factory. It will throw an exception in 6.0', __CLASS__),
356+
E_USER_DEPRECATED
357+
);
358+
359+
return Factory::getContainer()->get(DispatcherInterface::class);
360+
}
361+
362+
return $this->dispatcher;
363+
}
364+
365+
/**
366+
* Dispatches the given event on the internal dispatcher, does a fallback to the global one.
367+
*
368+
* @param EventInterface $event The event
369+
*
370+
* @return void
371+
*
372+
* @since 4.1.0
373+
*
374+
* @deprecated 4.4 will be removed in 6.0. Use $this->getDispatcher() directly.
375+
*/
376+
protected function dispatchEvent(EventInterface $event)
377+
{
378+
$this->getDispatcher()->dispatch($event->getName(), $event);
379+
380+
@trigger_error(
381+
\sprintf(
382+
'Method %s is deprecated and will be removed in 6.0. Use getDispatcher()->dispatch() directly.',
383+
__METHOD__
384+
),
385+
E_USER_DEPRECATED
386+
);
387+
}
388+
338389
/**
339390
* Get the database driver.
340391
*

libraries/src/MVC/View/AbstractView.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111

1212
use Joomla\CMS\Document\Document;
1313
use Joomla\CMS\Document\DocumentAwareInterface;
14+
use Joomla\CMS\Factory;
1415
use Joomla\CMS\Language\LanguageAwareInterface;
1516
use Joomla\CMS\Language\LanguageAwareTrait;
1617
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
1718
use Joomla\CMS\Object\LegacyErrorHandlingTrait;
1819
use Joomla\CMS\Object\LegacyPropertyManagementTrait;
1920
use Joomla\Event\DispatcherAwareInterface;
2021
use Joomla\Event\DispatcherAwareTrait;
22+
use Joomla\Event\DispatcherInterface;
23+
use Joomla\Event\EventInterface;
2124

2225
// phpcs:disable PSR1.Files.SideEffects
2326
\defined('_JEXEC') or die;
@@ -289,4 +292,53 @@ public function setDocument(Document $document): void
289292
{
290293
$this->document = $document;
291294
}
295+
296+
/**
297+
* Get the event dispatcher.
298+
*
299+
* The override was made to keep a backward compatibility for legacy component.
300+
* TODO: Remove the override in 6.0
301+
*
302+
* @return DispatcherInterface
303+
*
304+
* @since 4.4.0
305+
* @throws \UnexpectedValueException May be thrown if the dispatcher has not been set.
306+
*/
307+
public function getDispatcher()
308+
{
309+
if (!$this->dispatcher) {
310+
@trigger_error(
311+
\sprintf('Dispatcher for %s should be set through MVC factory. It will throw an exception in 6.0', __CLASS__),
312+
E_USER_DEPRECATED
313+
);
314+
315+
return Factory::getContainer()->get(DispatcherInterface::class);
316+
}
317+
318+
return $this->dispatcher;
319+
}
320+
321+
/**
322+
* Dispatches the given event on the internal dispatcher, does a fallback to the global one.
323+
*
324+
* @param EventInterface $event The event
325+
*
326+
* @return void
327+
*
328+
* @since 4.1.0
329+
*
330+
* @deprecated 4.4 will be removed in 6.0. Use $this->getDispatcher() directly.
331+
*/
332+
protected function dispatchEvent(EventInterface $event)
333+
{
334+
$this->getDispatcher()->dispatch($event->getName(), $event);
335+
336+
@trigger_error(
337+
\sprintf(
338+
'Method %s is deprecated and will be removed in 6.0. Use getDispatcher()->dispatch() directly.',
339+
__METHOD__
340+
),
341+
E_USER_DEPRECATED
342+
);
343+
}
292344
}

tests/Unit/Libraries/Cms/MVC/View/AbstractViewTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
use Joomla\CMS\MVC\Model\BaseModel;
1414
use Joomla\CMS\MVC\View\AbstractView;
15+
use Joomla\Event\DispatcherInterface;
16+
use Joomla\Event\Event;
17+
use Joomla\Event\EventInterface;
1518
use Joomla\Tests\Unit\UnitTestCase;
1619

1720
/**
@@ -217,4 +220,31 @@ public function display($tpl = null)
217220

218221
$this->assertEquals('test', $view->get('unit'));
219222
}
223+
224+
/**
225+
* @testdox can dispatch an event
226+
*
227+
* @return void
228+
*
229+
* @since 4.2.0
230+
*/
231+
public function testDispatchEvent()
232+
{
233+
$event = new Event('test');
234+
$dispatcher = $this->createMock(DispatcherInterface::class);
235+
$dispatcher->expects($this->once())->method('dispatch')->with($this->equalTo('test'), $this->equalTo($event));
236+
237+
$view = new class () extends AbstractView {
238+
public function dispatchEvent(EventInterface $event)
239+
{
240+
parent::dispatchEvent($event);
241+
}
242+
243+
public function display($tpl = null)
244+
{
245+
}
246+
};
247+
$view->setDispatcher($dispatcher);
248+
$view->dispatchEvent($event);
249+
}
220250
}

0 commit comments

Comments
 (0)