MDL-79336 core: add core\hook\stoppable_trait
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
namespace core\hook\navigation;
|
||||
|
||||
use core\hook\described_hook;
|
||||
use core\hook\stoppable_trait;
|
||||
use core\navigation\views\primary;
|
||||
|
||||
/**
|
||||
@@ -28,9 +29,7 @@ use core\navigation\views\primary;
|
||||
*/
|
||||
class primary_extend implements described_hook,
|
||||
\Psr\EventDispatcher\StoppableEventInterface {
|
||||
|
||||
/** @var bool $stopped propagation is stopped */
|
||||
protected $stopped = false;
|
||||
use stoppable_trait;
|
||||
|
||||
/**
|
||||
* Creates new hook.
|
||||
@@ -66,23 +65,4 @@ class primary_extend implements described_hook,
|
||||
public static function get_hook_tags(): array {
|
||||
return ['navigation'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows plugins to notify the hook dispatcher that hook propagation should be stopped
|
||||
*/
|
||||
public function stop(): void {
|
||||
$this->stopped = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is propagation stopped?
|
||||
*
|
||||
* This will typically only be used by the Dispatcher to determine if the
|
||||
* previous listener halted propagation.
|
||||
* @return bool True if the Event is complete and no further listeners should be called.
|
||||
* False to continue calling listeners.
|
||||
*/
|
||||
public function isPropagationStopped(): bool {
|
||||
return $this->stopped;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
// This file is part of Moodle - https://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
namespace core\hook;
|
||||
|
||||
/**
|
||||
* Moodle specific implementation of
|
||||
* \Psr\EventDispatcher\StoppableEventInterface
|
||||
* interface from PSR-14.
|
||||
*
|
||||
* @package core
|
||||
* @author Petr Skoda
|
||||
* @copyright 2023 Open LMS
|
||||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
trait stoppable_trait {
|
||||
/** @var bool $propagationstopped propagation is stopped */
|
||||
private $propagationstopped = false;
|
||||
|
||||
/**
|
||||
* Allows plugins to notify the hook dispatcher that hook propagation should be stopped
|
||||
*/
|
||||
public function stop_propagation(): void {
|
||||
$this->propagationstopped = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is propagation stopped?
|
||||
*
|
||||
* This will typically only be used by the Dispatcher to determine if the
|
||||
* previous listener halted propagation.
|
||||
* @return bool True if the Event is complete and no further listeners should be called.
|
||||
* False to continue calling listeners.
|
||||
*/
|
||||
public function isPropagationStopped(): bool {
|
||||
return $this->propagationstopped;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
// This file is part of Moodle - https://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
namespace core\hook\navigation;
|
||||
|
||||
/**
|
||||
* Test hook for primary navigation.
|
||||
*
|
||||
* @coversDefaultClass \core\hook\navigation\primary_extend
|
||||
*
|
||||
* @package core
|
||||
* @author Petr Skoda
|
||||
* @copyright 2023 Open LMS
|
||||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class primary_extend_test extends \advanced_testcase {
|
||||
/**
|
||||
* Test stoppable_trait.
|
||||
* @covers ::stop_propagation
|
||||
*/
|
||||
public function test_stop_propagation() {
|
||||
global $PAGE;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$PAGE = new \moodle_page();
|
||||
$PAGE->set_url('/');
|
||||
$primarynav = new \core\navigation\views\primary($PAGE);
|
||||
|
||||
$hook = new primary_extend($primarynav);
|
||||
$this->assertInstanceOf('Psr\EventDispatcher\StoppableEventInterface', $hook);
|
||||
$this->assertFalse($hook->isPropagationStopped());
|
||||
|
||||
$hook->stop_propagation();
|
||||
$this->assertTrue($hook->isPropagationStopped());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user