1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Gerard van Helden <[email protected]> |
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Zicht\Tool; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\Console\Helper\DialogHelper; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Zicht\Tool\Container\ExecutionAbortedException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Wrapper class for handling errors |
16
|
|
|
*/ |
17
|
|
|
class ErrorHandler |
18
|
|
|
{ |
19
|
|
|
private $input; |
20
|
|
|
private $output; |
21
|
|
|
private $dialog; |
22
|
|
|
private $repeating = array(); |
23
|
|
|
private $continueAlways = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor |
27
|
|
|
* |
28
|
|
|
* @param InputInterface $input |
29
|
|
|
* @param OutputInterface $output |
30
|
|
|
*/ |
31
|
|
|
public function __construct($input, $output) |
32
|
|
|
{ |
33
|
|
|
$this->input = $input; |
34
|
|
|
$this->output = $output; |
35
|
|
|
$this->dialog = new DialogHelper(); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Handler implementation which allows for some user interaction based on emitted user errors. |
40
|
|
|
* |
41
|
|
|
* @param int $err |
42
|
|
|
* @param string $errstr |
43
|
|
|
* @return void |
44
|
|
|
* |
45
|
|
|
* @throws Container\ExecutionAbortedException |
46
|
|
|
*/ |
47
|
|
|
public function __invoke($err, $errstr) |
48
|
|
|
{ |
49
|
|
|
if (in_array($errstr, $this->repeating)) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
$this->repeating[] = $errstr; |
53
|
|
|
if ( |
54
|
|
|
error_reporting() & E_USER_DEPRECATED |
55
|
|
|
|| error_reporting() & E_USER_NOTICE |
56
|
|
|
|| error_reporting() & E_USER_WARNING |
57
|
|
|
|| error_reporting() & E_RECOVERABLE_ERROR |
58
|
|
|
) { |
59
|
|
|
switch ($err) { |
60
|
|
|
case E_USER_WARNING: |
61
|
|
|
fprintf(STDERR, $this->output->getFormatter()->format("<comment>[WARNING]</comment> $errstr\n")); |
62
|
|
|
|
63
|
|
|
if (!$this->continueAlways) { |
64
|
|
|
do { |
65
|
|
|
if ($this->input->isInteractive()) { |
66
|
|
|
$answer = $this->dialog->ask($this->output, "Continue anyway? (y)es, (n)o, (a)lways ", false); |
|
|
|
|
67
|
|
|
} else { |
68
|
|
|
$answer = 'n'; |
69
|
|
|
} |
70
|
|
|
} while (!in_array(strtolower($answer), array('y', 'n', 'a'))); |
71
|
|
|
|
72
|
|
|
if ($answer === 'n') { |
73
|
|
|
throw new ExecutionAbortedException("Aborted by user request"); |
74
|
|
|
} elseif ($answer === 'a') { |
75
|
|
|
$this->continueAlways = true; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
break; |
81
|
|
View Code Duplication |
case E_USER_NOTICE: |
|
|
|
|
82
|
|
|
if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) { |
83
|
|
|
fprintf(STDERR, $this->output->getFormatter()->format("<comment>[NOTICE]</comment> $errstr\n")); |
84
|
|
|
} |
85
|
|
|
break; |
86
|
|
View Code Duplication |
case E_USER_DEPRECATED: |
|
|
|
|
87
|
|
|
if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) { |
88
|
|
|
fprintf(STDERR, $this->output->getFormatter()->format("<comment>[DEPRECATED]</comment> $errstr\n")); |
89
|
|
|
} |
90
|
|
|
break; |
91
|
|
|
case E_RECOVERABLE_ERROR: |
92
|
|
|
throw new \ErrorException($errstr); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.