diff --git a/app/Config/Constants.php b/app/Config/Constants.php index 2290758cebee..0c1b53487cd9 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -77,3 +77,18 @@ defined('EXIT_DATABASE') || define('EXIT_DATABASE', 8); // database error defined('EXIT__AUTO_MIN') || define('EXIT__AUTO_MIN', 9); // lowest automatically-assigned error code defined('EXIT__AUTO_MAX') || define('EXIT__AUTO_MAX', 125); // highest automatically-assigned error code + +/** + * @deprecated Use \CodeIgniter\Events\Events::PRIORITY_LOW instead. + */ +define('EVENT_PRIORITY_LOW', 200); + +/** + * @deprecated Use \CodeIgniter\Events\Events::PRIORITY_NORMAL instead. + */ +define('EVENT_PRIORITY_NORMAL', 100); + +/** + * @deprecated Use \CodeIgniter\Events\Events::PRIORITY_HIGH instead. + */ +define('EVENT_PRIORITY_HIGH', 10); diff --git a/system/Events/Events.php b/system/Events/Events.php index ac08107459ca..f3d1c4d67128 100644 --- a/system/Events/Events.php +++ b/system/Events/Events.php @@ -14,15 +14,15 @@ use Config\Modules; use Config\Services; -define('EVENT_PRIORITY_LOW', 200); -define('EVENT_PRIORITY_NORMAL', 100); -define('EVENT_PRIORITY_HIGH', 10); - /** * Events */ class Events { + public const PRIORITY_LOW = 200; + public const PRIORITY_NORMAL = 100; + public const PRIORITY_HIGH = 10; + /** * The list of listeners. * diff --git a/tests/system/Events/EventsTest.php b/tests/system/Events/EventsTest.php index 571118a6d534..77b233f9c1d5 100644 --- a/tests/system/Events/EventsTest.php +++ b/tests/system/Events/EventsTest.php @@ -157,15 +157,15 @@ public function testPriorityWithMultiple() Events::on('foo', static function () use (&$result) { $result[] = 'a'; - }, EVENT_PRIORITY_NORMAL); + }, Events::PRIORITY_NORMAL); Events::on('foo', static function () use (&$result) { $result[] = 'b'; - }, EVENT_PRIORITY_LOW); + }, Events::PRIORITY_LOW); Events::on('foo', static function () use (&$result) { $result[] = 'c'; - }, EVENT_PRIORITY_HIGH); + }, Events::PRIORITY_HIGH); Events::on('foo', static function () use (&$result) { $result[] = 'd'; diff --git a/user_guide_src/source/changelogs/v4.2.0.rst b/user_guide_src/source/changelogs/v4.2.0.rst index a75a88281025..8fa41aa1dd8c 100644 --- a/user_guide_src/source/changelogs/v4.2.0.rst +++ b/user_guide_src/source/changelogs/v4.2.0.rst @@ -102,6 +102,7 @@ Deprecations - ``CodeIgniter\Router\Router::setDefaultController()`` is deprecated. - The constant ``SPARKED`` in **spark** is deprecated. Use the ``$context`` property in ``CodeIgniter\CodeIgniter`` instead. - ``CodeIgniter\Autoloader\Autoloader::discoverComposerNamespaces()`` is deprecated, and no longer used. +- The constants ``EVENT_PRIORITY_LOW``, ``EVENT_PRIORITY_NORMAL`` and ``EVENT_PRIORITY_HIGH`` are deprecated. Use the class constants ``CodeIgniter\Events\Events::PRIORITY_LOW``, ``CodeIgniter\Events\Events::PRIORITY_NORMAL`` and ``CodeIgniter\Events\Events::PRIORITY_HIGH`` instead. Bugs Fixed ********** diff --git a/user_guide_src/source/extending/events.rst b/user_guide_src/source/extending/events.rst index 1b2c53a918cd..aa4f99e797c8 100644 --- a/user_guide_src/source/extending/events.rst +++ b/user_guide_src/source/extending/events.rst @@ -46,11 +46,13 @@ are executed first, with a value of 1 having the highest priority, and there bei Any subscribers with the same priority will be executed in the order they were defined. -Three constants are defined for your use, that set some helpful ranges on the values. You are not required to use these +Since v4.2.0, three class constants are defined for your use, that set some helpful ranges on the values. You are not required to use these but you might find they aid readability: .. literalinclude:: events/004.php +.. note:: The constants ``EVENT_PRIORITY_LOW``, ``EVENT_PRIORITY_NORMAL`` and ``EVENT_PRIORITY_HIGH`` are deprecated, and the definitions are moved to ``app/Config/Constants.php``. + Once sorted, all subscribers are executed in order. If any subscriber returns a boolean false value, then execution of the subscribers will stop. diff --git a/user_guide_src/source/extending/events/004.php b/user_guide_src/source/extending/events/004.php index df0ce3b16958..c211495e3e3c 100644 --- a/user_guide_src/source/extending/events/004.php +++ b/user_guide_src/source/extending/events/004.php @@ -1,5 +1,7 @@