1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Autarky\Errors;
12:
13: use Autarky\Providers\AbstractProvider;
14:
15: 16: 17:
18: class ErrorHandlerProvider extends AbstractProvider
19: {
20: 21: 22: 23:
24: protected $register;
25:
26: 27: 28:
29: protected $manager;
30:
31: 32: 33: 34:
35: public function __construct($register = true)
36: {
37: $this->register = (bool) $register;
38: }
39:
40: 41: 42:
43: public function register()
44: {
45: $dic = $this->app->getContainer();
46: $debug = $this->app->getConfig()->get('app.debug');
47:
48: $this->manager = new ErrorHandlerManager(
49: new HandlerResolver($dic)
50: );
51:
52: $this->manager->setDefaultHandler(new DefaultErrorHandler($debug));
53:
54: $this->app->setErrorHandler($this->manager);
55:
56: if ($this->register) {
57: $this->manager->register();
58: }
59:
60: $dic->instance('Autarky\Errors\ErrorHandlerManager', $this->manager);
61: $dic->alias('Autarky\Errors\ErrorHandlerManager', 'Autarky\Errors\ErrorHandlerManagerInterface');
62:
63: $this->app->config([$this, 'configureErrorHandler']);
64: }
65:
66: public function configureErrorHandler()
67: {
68: $errorHandlers = $this->app->getConfig()->get('app.error_handlers', []);
69:
70: foreach ($errorHandlers as $errorHandler) {
71: $this->manager->appendHandler($errorHandler);
72: }
73: }
74: }
75: