Skip to content

Commit 0153f4f

Browse files
authored
Merge pull request #6000 from kenjis/deprecate-const-EVENT_PRIORITY
refactor: deprecate const `EVENT_PRIORITY_*`
2 parents 8ddc566 + 98b3ad2 commit 0153f4f

File tree

7 files changed

+39
-11
lines changed

7 files changed

+39
-11
lines changed

app/Config/Constants.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,18 @@
7777
defined('EXIT_DATABASE') || define('EXIT_DATABASE', 8); // database error
7878
defined('EXIT__AUTO_MIN') || define('EXIT__AUTO_MIN', 9); // lowest automatically-assigned error code
7979
defined('EXIT__AUTO_MAX') || define('EXIT__AUTO_MAX', 125); // highest automatically-assigned error code
80+
81+
/**
82+
* @deprecated Use \CodeIgniter\Events\Events::PRIORITY_LOW instead.
83+
*/
84+
define('EVENT_PRIORITY_LOW', 200);
85+
86+
/**
87+
* @deprecated Use \CodeIgniter\Events\Events::PRIORITY_NORMAL instead.
88+
*/
89+
define('EVENT_PRIORITY_NORMAL', 100);
90+
91+
/**
92+
* @deprecated Use \CodeIgniter\Events\Events::PRIORITY_HIGH instead.
93+
*/
94+
define('EVENT_PRIORITY_HIGH', 10);

system/Events/Events.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
use Config\Modules;
1515
use Config\Services;
1616

17-
define('EVENT_PRIORITY_LOW', 200);
18-
define('EVENT_PRIORITY_NORMAL', 100);
19-
define('EVENT_PRIORITY_HIGH', 10);
20-
2117
/**
2218
* Events
2319
*/
2420
class Events
2521
{
22+
public const PRIORITY_LOW = 200;
23+
public const PRIORITY_NORMAL = 100;
24+
public const PRIORITY_HIGH = 10;
25+
2626
/**
2727
* The list of listeners.
2828
*

tests/system/Events/EventsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,15 @@ public function testPriorityWithMultiple()
157157

158158
Events::on('foo', static function () use (&$result) {
159159
$result[] = 'a';
160-
}, EVENT_PRIORITY_NORMAL);
160+
}, Events::PRIORITY_NORMAL);
161161

162162
Events::on('foo', static function () use (&$result) {
163163
$result[] = 'b';
164-
}, EVENT_PRIORITY_LOW);
164+
}, Events::PRIORITY_LOW);
165165

166166
Events::on('foo', static function () use (&$result) {
167167
$result[] = 'c';
168-
}, EVENT_PRIORITY_HIGH);
168+
}, Events::PRIORITY_HIGH);
169169

170170
Events::on('foo', static function () use (&$result) {
171171
$result[] = 'd';

user_guide_src/source/changelogs/v4.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Deprecations
103103
- ``CodeIgniter\Router\Router::setDefaultController()`` is deprecated.
104104
- The constant ``SPARKED`` in **spark** is deprecated. Use the ``$context`` property in ``CodeIgniter\CodeIgniter`` instead.
105105
- ``CodeIgniter\Autoloader\Autoloader::discoverComposerNamespaces()`` is deprecated, and no longer used.
106+
- 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.
106107

107108
Bugs Fixed
108109
**********

user_guide_src/source/extending/events.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ are executed first, with a value of 1 having the highest priority, and there bei
4646

4747
Any subscribers with the same priority will be executed in the order they were defined.
4848

49-
Three constants are defined for your use, that set some helpful ranges on the values. You are not required to use these
49+
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
5050
but you might find they aid readability:
5151

5252
.. literalinclude:: events/004.php
5353

54+
.. 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``.
55+
5456
Once sorted, all subscribers are executed in order. If any subscriber returns a boolean false value, then execution of
5557
the subscribers will stop.
5658

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3-
define('EVENT_PRIORITY_LOW', 200);
4-
define('EVENT_PRIORITY_NORMAL', 100);
5-
define('EVENT_PRIORITY_HIGH', 10);
3+
use CodeIgniter\Events\Events;
4+
5+
Events::PRIORITY_LOW; // 200
6+
Events::PRIORITY_NORMAL; // 100
7+
Events::PRIORITY_HIGH; // 10

user_guide_src/source/installation/upgrade_420.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@ Please refer to the upgrade instructions corresponding to your installation meth
1515
Mandatory File Changes
1616
**********************
1717

18+
index.php and spark
19+
===================
20+
1821
The following files received significant changes and
1922
**you must merge the updated versions** with your application:
2023

2124
* ``public/index.php``
2225
* ``spark``
2326

27+
Config/Constants.php
28+
====================
29+
30+
The constants ``EVENT_PRIORITY_LOW``, ``EVENT_PRIORITY_NORMAL`` and ``EVENT_PRIORITY_HIGH`` are deprecated, and the definitions are moved to ``app/Config/Constants.php``. If you use these constants, define them in ``app/Config/Constants.php``. Or use new class constants ``CodeIgniter\Events\Events::PRIORITY_LOW``, ``CodeIgniter\Events\Events::PRIORITY_NORMAL`` and ``CodeIgniter\Events\Events::PRIORITY_HIGH``.
31+
2432
Breaking Changes
2533
****************
2634

0 commit comments

Comments
 (0)