Skip to content

Commit b1a7764

Browse files
authored
Merge pull request #17030 from phalcon/T17019-ev-enhacements
T17006 ev enhacements
2 parents fae94f9 + a464b09 commit b1a7764

24 files changed

Lines changed: 1850 additions & 249 deletions

CHANGELOG-5.0.md

Lines changed: 20 additions & 0 deletions
Large diffs are not rendered by default.

phalcon/Contracts/Events/Event.zep

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
/**
3+
* This file is part of the Phalcon Framework.
4+
*
5+
* (c) Phalcon Team <team@phalcon.io>
6+
*
7+
* For the full copyright and license information, please view the LICENSE.txt
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Phalcon\Contracts\Events;
12+
13+
/**
14+
* Canonical contract for Phalcon\Events\Event.
15+
*/
16+
interface Event
17+
{
18+
/**
19+
* Gets event data
20+
*/
21+
public function getData() -> var;
22+
23+
/**
24+
* Gets event type
25+
*/
26+
public function getType() -> var;
27+
28+
/**
29+
* Check whether the event is cancelable
30+
*/
31+
public function isCancelable() -> bool;
32+
33+
/**
34+
* Check whether the event is currently stopped
35+
*/
36+
public function isStopped() -> bool;
37+
38+
/**
39+
* Sets event data
40+
*/
41+
public function setData(var data = null) -> <Event>;
42+
43+
/**
44+
* Sets event type
45+
*/
46+
public function setType(string type) -> <Event>;
47+
48+
/**
49+
* Stops the event preventing propagation
50+
*/
51+
public function stop() -> <Event>;
52+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
/**
3+
* This file is part of the Phalcon Framework.
4+
*
5+
* (c) Phalcon Team <team@phalcon.io>
6+
*
7+
* For the full copyright and license information, please view the LICENSE.txt
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Phalcon\Contracts\Events;
12+
13+
use Phalcon\Events\ManagerInterface;
14+
15+
/**
16+
* Canonical contract for Phalcon\Events\EventsAwareInterface. Implemented by
17+
* components that accept an events manager and dispatch through it.
18+
*
19+
* Cross-references the legacy ManagerInterface (not the canonical Manager
20+
* contract) to preserve LSP for the many AbstractEventsAware subclasses that
21+
* already type-hint ManagerInterface. ManagerInterface extends Manager, so
22+
* this remains type-compatible with any code that needs the canonical surface.
23+
*/
24+
interface EventsAware
25+
{
26+
/**
27+
* Returns the internal events manager
28+
*/
29+
public function getEventsManager() -> <ManagerInterface> | null;
30+
31+
/**
32+
* Sets the events manager
33+
*/
34+
public function setEventsManager(<ManagerInterface> eventsManager) -> void;
35+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
2+
/**
3+
* This file is part of the Phalcon Framework.
4+
*
5+
* (c) Phalcon Team <team@phalcon.io>
6+
*
7+
* For the full copyright and license information, please view the LICENSE.txt
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Phalcon\Contracts\Events;
12+
13+
/**
14+
* Canonical contract for Phalcon\Events\Manager.
15+
*/
16+
interface Manager
17+
{
18+
/**
19+
* @var int
20+
*/
21+
const DEFAULT_PRIORITY = 100;
22+
23+
/**
24+
* Registers an event subscriber. The subscriber's getSubscribedEvents()
25+
* map is parsed and each entry is attached through the regular listener
26+
* pipeline.
27+
*/
28+
public function addSubscriber(<Subscriber> subscriber) -> void;
29+
30+
/**
31+
* Returns whether priority ordering is currently enabled.
32+
*/
33+
public function arePrioritiesEnabled() -> bool;
34+
35+
/**
36+
* Attach a listener to the events manager.
37+
*
38+
* @param object|callable handler
39+
*/
40+
public function attach(
41+
string eventType,
42+
handler,
43+
int priority = self::DEFAULT_PRIORITY
44+
) -> void;
45+
46+
/**
47+
* Removes every registered subscriber and detaches each listener they
48+
* contributed. Listeners attached via attach() are untouched.
49+
*/
50+
public function clearSubscribers() -> void;
51+
52+
/**
53+
* Toggle response collection on/off.
54+
*/
55+
public function collectResponses(bool collect) -> void;
56+
57+
/**
58+
* Detach a listener from the events manager.
59+
*
60+
* @param object|callable handler
61+
*/
62+
public function detach(string eventType, handler) -> void;
63+
64+
/**
65+
* Removes all listeners — globally or for a single event type.
66+
*/
67+
public function detachAll(string type = null) -> void;
68+
69+
/**
70+
* Toggle priority ordering on/off.
71+
*/
72+
public function enablePriorities(bool enablePriorities) -> void;
73+
74+
/**
75+
* Fires an event, notifying the active listeners.
76+
*
77+
* @param object source
78+
* @param mixed data
79+
* @return mixed
80+
*/
81+
public function fire(
82+
string eventType,
83+
object source,
84+
var data = null,
85+
bool cancelable = true
86+
);
87+
88+
/**
89+
* Returns all listeners attached to the given event type.
90+
*/
91+
public function getListeners(string type) -> array;
92+
93+
/**
94+
* Returns the responses recorded during the last fire (when collecting).
95+
*/
96+
public function getResponses() -> array;
97+
98+
/**
99+
* Returns the list of registered subscriber instances.
100+
*/
101+
public function getSubscribers() -> array;
102+
103+
/**
104+
* Check whether the given event type has any listeners.
105+
*/
106+
public function hasListeners(string type) -> bool;
107+
108+
/**
109+
* Check whether the manager is currently collecting responses.
110+
*/
111+
public function isCollecting() -> bool;
112+
113+
/**
114+
* Returns true when the given handler is an object or callable.
115+
*/
116+
public function isValidHandler(var handler) -> bool;
117+
118+
/**
119+
* Removes a previously registered subscriber. Detaches every listener the
120+
* subscriber declared via getSubscribedEvents(). Idempotent.
121+
*/
122+
public function removeSubscriber(<Subscriber> subscriber) -> void;
123+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
/**
3+
* This file is part of the Phalcon Framework.
4+
*
5+
* (c) Phalcon Team <team@phalcon.io>
6+
*
7+
* For the full copyright and license information, please view the LICENSE.txt
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Phalcon\Contracts\Events;
12+
13+
/**
14+
* Phalcon's local mirror of PSR-14 StoppableEventInterface. Identical shape;
15+
* not extended from the PSR interface because the Zephir extension cannot
16+
* reference Composer-loaded interfaces at build time. A separate bridge
17+
* package exposes a PSR-14 adapter.
18+
*/
19+
interface Stoppable
20+
{
21+
/**
22+
* Returns true when the event must stop propagating to subsequent
23+
* listeners.
24+
*/
25+
public function isPropagationStopped() -> bool;
26+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
/**
3+
* This file is part of the Phalcon Framework.
4+
*
5+
* (c) Phalcon Team <team@phalcon.io>
6+
*
7+
* For the full copyright and license information, please view the LICENSE.txt
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Phalcon\Contracts\Events;
12+
13+
/**
14+
* Contract for event subscriber classes. A subscriber declares the events it
15+
* wants to listen to via a static map; Events\Manager parses the map and
16+
* attaches each entry as a regular listener.
17+
*
18+
* Accepted value shapes per event key:
19+
*
20+
* 'event:name' => 'methodName'
21+
* 'event:name' => ['methodName', priority]
22+
* 'event:name' => [
23+
* ['methodName1'],
24+
* ['methodName2', priority],
25+
* ]
26+
*
27+
* Keys can be either a Phalcon event string (e.g. "db:beforeQuery") or a
28+
* fully qualified event class name.
29+
*
30+
* Wildcard subscriptions: Phalcon's manager fires both the prefix queue and
31+
* the full-name queue (e.g. "db" is fired before "db:beforeQuery"). To
32+
* subscribe to every event of a component, use the prefix as the key:
33+
*
34+
* 'db' => 'onAnyDbEvent' // fires for db:beforeQuery, db:afterQuery, ...
35+
*/
36+
interface Subscriber
37+
{
38+
/**
39+
* Returns a map of event name => listener config. Called once per
40+
* Manager::addSubscriber() / removeSubscriber() call.
41+
*/
42+
public static function getSubscribedEvents() -> array;
43+
}

phalcon/Events/Event.zep

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace Phalcon\Events;
1212

13+
use Phalcon\Contracts\Events\Stoppable;
14+
1315
/**
1416
* This class offers contextual information of a fired event in the
1517
* EventsManager
@@ -23,7 +25,7 @@ namespace Phalcon\Events;
2325
* }
2426
* ```
2527
*/
26-
class Event implements EventInterface
28+
final class Event implements EventInterface, Stoppable
2729
{
2830
/**
2931
* Is event cancelable?
@@ -65,13 +67,18 @@ class Event implements EventInterface
6567
*
6668
* @param object source
6769
*/
68-
public function __construct(string! type, var source = null, var data = null, bool cancelable = true)
69-
{
70+
public function __construct(
71+
string type,
72+
var source = null,
73+
var data = null,
74+
bool cancelable = true
75+
) {
7076
if unlikely null !== source && typeof source !== "object" {
7177
throw new Exception(
7278
"The source of " . type . " event must be an object, got " . (typeof source)
7379
);
7480
}
81+
7582
let this->type = type,
7683
this->source = source,
7784
this->data = data,
@@ -107,6 +114,15 @@ class Event implements EventInterface
107114
return this->cancelable;
108115
}
109116

117+
/**
118+
* Returns whether propagation must stop. PSR-14 alias backed by the same
119+
* `stopped` flag as `isStopped()`; calling `stop()` flips both.
120+
*/
121+
public function isPropagationStopped() -> bool
122+
{
123+
return this->stopped;
124+
}
125+
110126
/**
111127
* Check whether the event is currently stopped.
112128
*/

phalcon/Events/EventInterface.zep

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,15 @@
1010

1111
namespace Phalcon\Events;
1212

13+
use Phalcon\Contracts\Events\Event as EventContract;
14+
1315
/**
14-
* Interface for Phalcon\Events\Event class
16+
* Phalcon\Events\EventInterface
17+
*
18+
* @psalm-suppress DeprecatedInterface
19+
* @deprecated Will be removed in a future major release.
20+
* Use {@see \Phalcon\Contracts\Events\Event} instead.
1521
*/
16-
interface EventInterface
22+
interface EventInterface extends EventContract
1723
{
18-
/**
19-
* Gets event data
20-
*/
21-
public function getData() -> var;
22-
23-
/**
24-
* Gets event type
25-
*/
26-
public function getType() -> var;
27-
28-
/**
29-
* Check whether the event is cancelable
30-
*/
31-
public function isCancelable() -> bool;
32-
33-
/**
34-
* Check whether the event is currently stopped
35-
*/
36-
public function isStopped() -> bool;
37-
38-
/**
39-
* Sets event data
40-
*/
41-
public function setData(var data = null) -> <EventInterface>;
42-
43-
/**
44-
* Sets event type
45-
*/
46-
public function setType(string! type) -> <EventInterface>;
47-
48-
/**
49-
* Stops the event preventing propagation
50-
*/
51-
public function stop() -> <EventInterface>;
5224
}

0 commit comments

Comments
 (0)