1 | <?php |
||
11 | abstract class AbstractCommand extends Command |
||
12 | { |
||
13 | /** |
||
14 | * @var InputInterface |
||
15 | */ |
||
16 | protected $input; |
||
17 | |||
18 | /** |
||
19 | * @var OutputInterface |
||
20 | */ |
||
21 | protected $output; |
||
22 | |||
23 | /** |
||
24 | * Configures the current command. |
||
25 | * |
||
26 | * @param string $message |
||
27 | */ |
||
28 | protected function abort($message = '') |
||
29 | { |
||
30 | if ($message) { |
||
31 | $this->error($message); |
||
32 | } |
||
33 | |||
34 | $this->error('Abort!'); |
||
35 | |||
36 | throw new DomainException(); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Executes the current command. |
||
41 | * |
||
42 | * @param InputInterface $input An InputInterface instance |
||
43 | * @param OutputInterface $output An OutputInterface instance |
||
44 | * |
||
45 | * @return null|int null or 0 if everything went fine, or an error code. |
||
46 | */ |
||
47 | protected function execute(InputInterface $input, OutputInterface $output) |
||
48 | { |
||
49 | $this->input = $input; |
||
50 | $this->output = $output; |
||
51 | |||
52 | try { |
||
53 | return $this->fire(); |
||
54 | } catch (DomainException $e) { |
||
55 | return 1; |
||
56 | } catch (Exception $e) { |
||
57 | $this->error($e->getMessage()); |
||
58 | $this->error('Abort!'); |
||
59 | |||
60 | return $e->getCode(); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Echo an error message. |
||
66 | * |
||
67 | * @param string$message |
||
68 | */ |
||
69 | protected function error($message) |
||
70 | { |
||
71 | $this->output->writeln("<error>{$message}</error>"); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Echo an info. |
||
76 | * |
||
77 | * @param string $message |
||
78 | */ |
||
79 | protected function info($message) |
||
80 | { |
||
81 | $this->output->writeln("<info>{$message}</info>"); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Echo a message. |
||
86 | * |
||
87 | * @param string $message |
||
88 | */ |
||
89 | protected function message($message) |
||
90 | { |
||
91 | $this->output->writeln("{$message}"); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Execute the console command. |
||
96 | * |
||
97 | * @return null|int |
||
98 | */ |
||
99 | abstract protected function fire(); |
||
100 | } |
||
101 |