@@ -17,8 +17,9 @@ Biblioteca PHP para manejar excepciones.
17
17
18
18
- [ Instalación] ( #instalación )
19
19
- [ Requisitos] ( #requisitos )
20
- - [ Cómo empezar y ejemplos] ( #cómo-empezar-y-ejemplos )
21
- - [ Métodos disponibles] ( #métodos-disponibles )
20
+ - [ Clases disponibles] ( #clases-disponibles )
21
+ - [ Clase ExceptionHandler] ( #clase-exceptionhandler )
22
+ - [ Excepciones utilizadas] ( #excepciones-utilizadas )
22
23
- [ Uso] ( #uso )
23
24
- [ Tests] ( #tests )
24
25
- [ Tareas pendientes] ( #tareas-pendientes )
@@ -56,43 +57,44 @@ También puedes **clonar el repositorio** completo con Git:
56
57
git clone https://github.com/josantonius/php-exception-handler.git
57
58
```
58
59
59
- ## Métodos disponibles
60
+ ## Clases disponibles
60
61
61
- Métodos disponibles en esta biblioteca:
62
+ ### Clase ExceptionHandler
62
63
63
- ### Establece un manejador de excepciones
64
+ ``` php
65
+ use Josantonius\ExceptionHandler\ExceptionHandler;
66
+ ```
67
+
68
+ Establece un manejador de excepciones:
64
69
65
70
``` php
71
+ /**
72
+ * Sets a exception handler.
73
+ *
74
+ * @param callable $callback Función para manejo de excepciones.
75
+ * @param array $runBeforeCallback Métodos a llamar en la excepción antes del callback.
76
+ * @param array $runAfterCallback Métodos a llamar en la excepción después del callback.
77
+ *
78
+ * @throws NotCallableException si la llamada de retorno no es de tipo callable.
79
+ * @throws WrongMethodNameException si el nombre del método no es string o está vacío.
80
+ *
81
+ * @see https://www.php.net/manual/en/functions.first_class_callable_syntax.php
82
+ */
66
83
new ExceptionHandler(
67
84
callable $callback,
68
85
string[] $runBeforeCallback = [],
69
86
string[] $runAfterCallback = []
70
87
);
71
88
```
72
89
73
- ** @param ** callable ` $callback ` Función para manejo de excepciones.
74
-
75
- ** @param ** array ` $runBeforeCallback ` Nombres de métodos a llamar en la excepción antes de
76
- ejecutar el * callback* .
77
-
78
- ** @param ** array ` $runAfterCallback ` Nombres de métodos a llamar en la excepción después de
79
- ejecutar el * callback* .
80
-
81
- ** @throws ** ` NotCallableException ` si la llamada de retorno no es de tipo * callable* .
82
-
83
- ** @throws ** ` WrongMethodNameException ` si el nombre del método no es * string* .
84
-
85
- @see < https://www.php.net/manual/en/functions.first_class_callable_syntax.php > para más información
86
- sobre la sintaxis de las llamadas de primera clase.
87
-
88
- ## Cómo empezar
89
-
90
- Para utilizar esta biblioteca:
90
+ ## Excepciones utilizadas
91
91
92
92
``` php
93
- use Josantonius\ExceptionHandler\ExceptionHandler;
93
+ use Josantonius\ExceptionHandler\Exceptions\NotCallableException;
94
+ ```
94
95
95
- new ExceptionHandler(/*...*/);
96
+ ``` php
97
+ use Josantonius\ExceptionHandler\Exceptions\WrongMethodNameException;
96
98
```
97
99
98
100
## Uso
@@ -102,29 +104,31 @@ Ejemplo de uso para esta biblioteca:
102
104
### Establece un manejador de excepciones básico
103
105
104
106
``` php
105
- function handler(Throwable $exception) { /* hacer algo */ }
106
- ```
107
+ use Josantonius\ExceptionHandler\ExceptionHandler;
108
+
109
+ function handler(\Throwable $exception) { /* hacer algo */ }
107
110
108
- ``` php
109
111
new ExceptionHandler(
110
112
callback: handler(...)
111
113
);
112
- ```
113
-
114
- Si se lanza una excepción:
115
114
116
- - ` handler($exception) ` * callback* será ejecutado
115
+ /**
116
+ * Si se lanza una excepción, se ejecuta lo siguiente:
117
+ *
118
+ * handler($exception)
119
+ */
120
+ ```
117
121
118
122
### Establece los métodos a llamar antes de ejecutar el * callback*
119
123
120
124
``` php
125
+ use Josantonius\ExceptionHandler\ExceptionHandler;
126
+
121
127
class FooException extends \Exception
122
128
{
123
129
public function context(): void { /* hacer algo */ }
124
130
}
125
- ```
126
131
127
- ``` php
128
132
class Handler {
129
133
public function exceptions(Throwable $exception): void
130
134
{
@@ -133,32 +137,32 @@ class Handler {
133
137
}
134
138
}
135
139
}
136
- ```
137
140
138
- ``` php
139
141
new ExceptionHandler(
140
142
callback: (new Handler())->exceptions(...),
141
143
runBeforeCallback: ['context']
142
144
);
143
- ```
144
145
145
- Si se lanza ` FooException() ` :
146
-
147
- - ` FooException->context() ` será llamado
148
- - ` Handler->exceptions($exception) ` * callback* será ejecutado
146
+ /**
147
+ * Si se lanza FooException(), se ejecuta lo siguiente:
148
+ *
149
+ * FooException->context()
150
+ * Handler->exceptions($exception)
151
+ */
152
+ ```
149
153
150
- ### Sets methods to execute after calling the callback
154
+ ### Establece los métodos a ejecutar después de llamar al * callback*
151
155
152
156
``` php
157
+ use Josantonius\ExceptionHandler\ExceptionHandler;
158
+
153
159
class FooException extends \Exception
154
160
{
155
161
public function report(): void { /* hacer algo */ }
156
162
157
163
public function render(): void { /* hacer algo */ }
158
164
}
159
- ```
160
165
161
- ``` php
162
166
class Handler {
163
167
public static function exceptions(Throwable $exception): void
164
168
{
@@ -167,53 +171,54 @@ class Handler {
167
171
}
168
172
}
169
173
}
170
- ```
171
174
172
- ``` php
173
175
new ExceptionHandler(
174
176
callback: Handler::exceptions(...),
175
177
runAfterCallback: ['report', 'render']
176
178
);
177
- ```
178
-
179
- Si se lanza ` FooException() ` :
180
179
181
- - ` Handler::exceptions($exception) ` * callback* será ejecutado
182
- - ` FooException->report() ` será llamado
183
- - ` FooException->render() ` será llamado
180
+ /**
181
+ * Si se lanza FooException(), se ejecuta lo siguiente:
182
+ *
183
+ * Handler::exceptions($exception)
184
+ * FooException->report()
185
+ * FooException->render()
186
+ */
187
+ ```
184
188
185
189
### Sets methods to execute before and after calling the callback
186
190
187
191
``` php
192
+ use Josantonius\ExceptionHandler\ExceptionHandler;
193
+
188
194
class FooException extends \Exception
189
195
{
190
- public function context(): void { /* hacer algo */ }
196
+ public function context(): void { /* do something */ }
191
197
192
- public function report(): void { /* hacer algo */ }
198
+ public function report(): void { /* do something */ }
193
199
194
- public function render(): void { /* hacer algo */ }
200
+ public function render(): void { /* do something */ }
195
201
}
196
- ```
197
202
198
- ``` php
199
- function exceptionHandler(Throwable $exception) { /* hacer algo */ }
200
- ```
203
+ function exceptionHandler(Throwable $exception) { /* do something */ }
201
204
202
- ``` php
203
205
new ExceptionHandler(
204
206
callback: exceptionHandler(...),
205
207
runBeforeCallback: ['context', 'logger'],
206
208
runAfterCallback: ['report', 'render']
207
209
);
208
- ```
209
-
210
- Si se lanza ` FooException() ` :
211
210
212
- - ` FooException->context() ` será llamado
213
- - ` FooException->logger() ` será ignorado, no existe en la excepción
214
- - ` exceptionHandler($exception) ` * callback* será ejecutado
215
- - ` FooException->report() ` será llamado
216
- - ` FooException->render() ` será llamado
211
+ /**
212
+ * If FooException() is thrown, the following is executed:
213
+ *
214
+ * FooException->context()
215
+ * exceptionHandler($exception)
216
+ * FooException->report()
217
+ * FooException->render()
218
+ *
219
+ * Se ignora FooException->logger(), no existe en la excepción.
220
+ */
221
+ ```
217
222
218
223
## Tests
219
224
0 commit comments