Completed
Push — master ( 860664...35ee95 )
by Mohamed
25s
created
src/Dispatcher/EventDispatcher.php 1 patch
Indentation   +96 added lines, -96 removed lines patch added patch discarded remove patch
@@ -29,114 +29,114 @@
 block discarded – undo
29 29
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 30
  */
31 31
 
32
- declare(strict_types=1);
32
+    declare(strict_types=1);
33 33
 
34
- namespace Mabs\Dispatcher;
34
+    namespace Mabs\Dispatcher;
35 35
 
36
- use Closure;
37
- use Psr\Container\ContainerInterface;
36
+    use Closure;
37
+    use Psr\Container\ContainerInterface;
38 38
  
39
- final class EventDispatcher implements EventDispatcherInterface
40
- {
41
-     /** @var array<string, array<array{eventName: string, callback: callable, priority: int}>> */
42
-     private array $listeners = [];
39
+    final class EventDispatcher implements EventDispatcherInterface
40
+    {
41
+        /** @var array<string, array<array{eventName: string, callback: callable, priority: int}>> */
42
+        private array $listeners = [];
43 43
  
44
-     public function __construct(
45
-         private readonly ContainerInterface $container
46
-     ) {}
44
+        public function __construct(
45
+            private readonly ContainerInterface $container
46
+        ) {}
47 47
  
48
-     /**
49
-      * Dispatch an event to all registered listeners
50
-      *
51
-      * @param string $eventName The event to dispatch
52
-      * @param mixed $data Optional data to pass to the event listeners
53
-      * @return $this
54
-      */
55
-     public function dispatch(string $eventName, mixed $data = null): self
56
-     {
57
-         foreach ($this->getListenersByEvent($eventName) as $event) {
58
-             $event['callback']($this->container, $data);
59
-         }
48
+        /**
49
+         * Dispatch an event to all registered listeners
50
+         *
51
+         * @param string $eventName The event to dispatch
52
+         * @param mixed $data Optional data to pass to the event listeners
53
+         * @return $this
54
+         */
55
+        public function dispatch(string $eventName, mixed $data = null): self
56
+        {
57
+            foreach ($this->getListenersByEvent($eventName) as $event) {
58
+                $event['callback']($this->container, $data);
59
+            }
60 60
  
61
-         return $this;
62
-     }
61
+            return $this;
62
+        }
63 63
  
64
-     /**
65
-      * Remove all listeners for a given event
66
-      *
67
-      * @param string $eventName The event name to clear
68
-      * @return $this
69
-      */
70
-     final public function detach(string $eventName): self
71
-     {
72
-         unset($this->listeners[$eventName]);
64
+        /**
65
+         * Remove all listeners for a given event
66
+         *
67
+         * @param string $eventName The event name to clear
68
+         * @return $this
69
+         */
70
+        final public function detach(string $eventName): self
71
+        {
72
+            unset($this->listeners[$eventName]);
73 73
  
74
-         return $this;
75
-     }
74
+            return $this;
75
+        }
76 76
  
77
-     /**
78
-      * Register an event listener
79
-      *
80
-      * @param string $eventName The event to listen for
81
-      * @param callable|array|string $callback The callback to execute
82
-      * @param int $priority The priority (higher numbers execute first)
83
-      * @return $this
84
-      */
85
-     final public function register(
86
-         string $eventName,
87
-         callable|array|string $callback,
88
-         int $priority = 0
89
-     ): self {
90
-         $eventName = trim($eventName);
91
-         $this->listeners[$eventName] ??= [];
77
+        /**
78
+         * Register an event listener
79
+         *
80
+         * @param string $eventName The event to listen for
81
+         * @param callable|array|string $callback The callback to execute
82
+         * @param int $priority The priority (higher numbers execute first)
83
+         * @return $this
84
+         */
85
+        final public function register(
86
+            string $eventName,
87
+            callable|array|string $callback,
88
+            int $priority = 0
89
+        ): self {
90
+            $eventName = trim($eventName);
91
+            $this->listeners[$eventName] ??= [];
92 92
  
93
-         $this->listeners[$eventName][] = [
94
-             'eventName' => $eventName,
95
-             'callback' => $this->normalizeCallback($callback),
96
-             'priority' => $priority
97
-         ];
93
+            $this->listeners[$eventName][] = [
94
+                'eventName' => $eventName,
95
+                'callback' => $this->normalizeCallback($callback),
96
+                'priority' => $priority
97
+            ];
98 98
  
99
-         if (count($this->listeners[$eventName]) > 1) {
100
-             usort(
101
-                 $this->listeners[$eventName],
102
-                 fn(array $a, array $b): int => $b['priority'] <=> $a['priority']
103
-             );
104
-         }
99
+            if (count($this->listeners[$eventName]) > 1) {
100
+                usort(
101
+                    $this->listeners[$eventName],
102
+                    fn(array $a, array $b): int => $b['priority'] <=> $a['priority']
103
+                );
104
+            }
105 105
  
106
-         return $this;
107
-     }
106
+            return $this;
107
+        }
108 108
  
109
-     /**
110
-      * Get all registered listeners
111
-      *
112
-      * @return array<string, array<array{eventName: string, callback: callable, priority: int}>>
113
-      */
114
-     public function getListeners(): array
115
-     {
116
-         return $this->listeners;
117
-     }
109
+        /**
110
+         * Get all registered listeners
111
+         *
112
+         * @return array<string, array<array{eventName: string, callback: callable, priority: int}>>
113
+         */
114
+        public function getListeners(): array
115
+        {
116
+            return $this->listeners;
117
+        }
118 118
  
119
-     /**
120
-      * Get listeners for a specific event
121
-      *
122
-      * @param string $eventName The event name
123
-      * @return array<array{eventName: string, callback: callable, priority: int}>
124
-      */
125
-     public function getListenersByEvent(string $eventName): array
126
-     {
127
-         return $this->listeners[$eventName] ?? [];
128
-     }
119
+        /**
120
+         * Get listeners for a specific event
121
+         *
122
+         * @param string $eventName The event name
123
+         * @return array<array{eventName: string, callback: callable, priority: int}>
124
+         */
125
+        public function getListenersByEvent(string $eventName): array
126
+        {
127
+            return $this->listeners[$eventName] ?? [];
128
+        }
129 129
  
130
-     /**
131
-      * Normalize different callback formats to a callable
132
-      */
133
-     private function normalizeCallback(callable|array|string $callback): callable
134
-     {
135
-         return match (true) {
136
-             is_callable($callback) => $callback,
137
-             is_string($callback) && str_contains($callback, '::') => explode('::', $callback),
138
-             is_string($callback) => fn(ContainerInterface $c) => $c->get($callback),
139
-             default => throw new \InvalidArgumentException('Invalid callback type')
140
-         };
141
-     }
142
- }
143 130
\ No newline at end of file
131
+        /**
132
+         * Normalize different callback formats to a callable
133
+         */
134
+        private function normalizeCallback(callable|array|string $callback): callable
135
+        {
136
+            return match (true) {
137
+                is_callable($callback) => $callback,
138
+                is_string($callback) && str_contains($callback, '::') => explode('::', $callback),
139
+                is_string($callback) => fn(ContainerInterface $c) => $c->get($callback),
140
+                default => throw new \InvalidArgumentException('Invalid callback type')
141
+            };
142
+        }
143
+    }
144 144
\ No newline at end of file
Please login to merge, or discard this patch.
src/Container/ContainerNotFoundException.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -29,10 +29,10 @@
 block discarded – undo
29 29
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 30
  */
31 31
 
32
- declare(strict_types=1);
32
+    declare(strict_types=1);
33 33
 
34
- namespace Mabs\Container;
34
+    namespace Mabs\Container;
35 35
 
36
- use Psr\Container\NotFoundExceptionInterface;
36
+    use Psr\Container\NotFoundExceptionInterface;
37 37
 
38 38
 final class ContainerNotFoundException extends \Exception implements NotFoundExceptionInterface {}
39 39
\ No newline at end of file
Please login to merge, or discard this patch.