Skip to content

Commit db63947

Browse files
Overhauled the complete action output handling
1 parent 51e0f4a commit db63947

14 files changed

Lines changed: 803 additions & 126 deletions

File tree

src/Console/Command/Hook.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9191
$hook = new $class($io, $config, $repository);
9292
$hook->run();
9393
return 0;
94-
} catch (Exception $e) {
94+
} catch (Throwable $e) {
9595
if ($output->isDebug()) {
9696
throw $e;
9797
}
98-
return $this->handleError($output, $e);
98+
return 1;
9999
}
100100
}
101101

@@ -122,19 +122,6 @@ private function handleBootstrap(Config $config): void
122122
}
123123
}
124124

125-
/**
126-
* Handle all hook errors
127-
*
128-
* @param \Symfony\Component\Console\Output\OutputInterface $output
129-
* @param \Exception $e
130-
* @return int
131-
*/
132-
private function handleError(OutputInterface $output, Exception $e): int
133-
{
134-
$output->writeLn('<error>' . PHP_EOL . $e->getMessage() . '</error>');
135-
return 1;
136-
}
137-
138125
/**
139126
* Indicates if hooks should be skipped
140127
*

src/Console/IO/CollectorIO.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook
5+
*
6+
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\App\Console\IO;
13+
14+
use CaptainHook\App\Console\IO;
15+
use CaptainHook\App\Console\IOUtil;
16+
use SebastianFeldmann\Cli\Reader\StandardInput;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Helper\HelperSet;
19+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
20+
use Symfony\Component\Console\Output\OutputInterface;
21+
use Symfony\Component\Console\Question\ConfirmationQuestion;
22+
use Symfony\Component\Console\Question\Question;
23+
24+
/**
25+
* Class CollectorIO
26+
*
27+
* @package CaptainHook
28+
* @author Sebastian Feldmann <sf@sebastian-feldmann.info>
29+
* @link https://github.com/captainhookphp/captainhook
30+
* @since Class available since Release 5.19.0
31+
*/
32+
class CollectorIO implements IO
33+
{
34+
/**
35+
* @var \CaptainHook\App\Console\IO
36+
*/
37+
private IO $io;
38+
39+
/**
40+
* @var array<\CaptainHook\App\Console\IO\Message>
41+
*/
42+
private array $messages = [];
43+
44+
/**
45+
* Constructor
46+
*
47+
*/
48+
public function __construct(IO $io)
49+
{
50+
$this->io = $io;
51+
}
52+
53+
54+
/**
55+
*
56+
* @param string $messages
57+
* @param bool $newline
58+
* @param int $verbosity
59+
* @return void
60+
*/
61+
public function write($messages, $newline = true, $verbosity = IO::NORMAL)
62+
{
63+
$this->messages[] = new Message($messages, $newline, $verbosity);
64+
}
65+
66+
/**
67+
* @param string $messages
68+
* @param bool $newline
69+
* @param int $verbosity
70+
* @return void
71+
*/
72+
public function writeError($messages, $newline = true, $verbosity = IO::NORMAL)
73+
{
74+
$this->messages[] = new Message($messages, $newline, $verbosity);
75+
}
76+
77+
/**
78+
* @return array<\CaptainHook\App\Console\IO\Message>
79+
*/
80+
public function getMessages(): array
81+
{
82+
return $this->messages;
83+
}
84+
85+
/**
86+
* Return the original cli arguments
87+
*
88+
* @return array<string, mixed>
89+
*/
90+
public function getArguments(): array
91+
{
92+
return $this->io->getArguments();
93+
}
94+
95+
/**
96+
* Return the original cli argument or a given default
97+
*
98+
* @param string $name
99+
* @param string $default
100+
* @return string
101+
*/
102+
public function getArgument(string $name, string $default = ''): string
103+
{
104+
return $this->io->getArgument($name, $default);
105+
}
106+
107+
/**
108+
* Return the piped in standard input
109+
*
110+
* @return string[]
111+
*/
112+
public function getStandardInput(): array
113+
{
114+
return $this->io->getStandardInput();
115+
}
116+
117+
public function isInteractive()
118+
{
119+
return $this->io->isInteractive();
120+
}
121+
122+
public function isVerbose()
123+
{
124+
return $this->io->isVerbose();
125+
}
126+
127+
public function isVeryVerbose()
128+
{
129+
return $this->io->isVeryVerbose();
130+
}
131+
132+
public function isDebug()
133+
{
134+
return $this->io->isDebug();
135+
}
136+
137+
public function ask($question, $default = null)
138+
{
139+
return $this->io->ask($question, $default);
140+
}
141+
142+
public function askConfirmation($question, $default = true)
143+
{
144+
return $this->io->askConfirmation($question, $default);
145+
}
146+
147+
public function askAndValidate($question, $validator, $attempts = null, $default = null)
148+
{
149+
$this->io->askAndValidate($question, $validator, $attempts, $default);
150+
}
151+
}

src/Console/IO/Message.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook.
5+
*
6+
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\App\Console\IO;
13+
14+
/**
15+
* Class Message
16+
*
17+
* @package CaptainHook
18+
* @author Sebastian Feldmann <sf@sebastian-feldmann.info>
19+
* @link https://github.com/captainhookphp/captainhook
20+
* @since Class available since Release 5.19.0
21+
*/
22+
class Message
23+
{
24+
private string|array $message;
25+
26+
private bool $newLine;
27+
28+
private int $verbosity;
29+
30+
/**
31+
* Constructor
32+
*
33+
* @param string|array<string> $message
34+
* @param bool $newLine
35+
* @param int $verbosity
36+
*/
37+
public function __construct(string|array $message, bool $newLine, int $verbosity)
38+
{
39+
$this->message = $message;
40+
$this->newLine = $newLine;
41+
$this->verbosity = $verbosity;
42+
}
43+
44+
/**
45+
* Returns the message to print
46+
*
47+
* @return string|array
48+
*/
49+
public function message(): string|array
50+
{
51+
return $this->message;
52+
}
53+
54+
/**
55+
* If true message should end with a new line
56+
*
57+
* @return bool
58+
*/
59+
public function newLine(): bool
60+
{
61+
return $this->newLine;
62+
}
63+
64+
/**
65+
* Minimum verbosity this message should be displayed at
66+
*
67+
* @return int
68+
*/
69+
public function verbosity(): int
70+
{
71+
return $this->verbosity;
72+
}
73+
}

src/Runner/Action.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook
5+
*
6+
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\App\Runner;
13+
14+
use CaptainHook\App\Config;
15+
use CaptainHook\App\Console\IO;
16+
use SebastianFeldmann\Git\Repository;
17+
18+
/**
19+
* Interface Action
20+
*
21+
* @package CaptainHook
22+
* @author Sebastian Feldmann <sf@sebastian-feldmann.info>
23+
* @link https://github.com/captainhookphp/captainhook
24+
* @since Class available since Release 5.19.0
25+
*/
26+
interface Action
27+
{
28+
/**
29+
* Executes the action
30+
*
31+
* @param \CaptainHook\App\Config $config
32+
* @param \CaptainHook\App\Console\IO $io
33+
* @param \SebastianFeldmann\Git\Repository $repository
34+
* @param \CaptainHook\App\Config\Action $action
35+
* @return void
36+
* @throws \Exception
37+
*/
38+
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void;
39+
}

src/Runner/Action/Cli.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CaptainHook\App\Config;
1515
use CaptainHook\App\Console\IO;
1616
use CaptainHook\App\Exception;
17+
use CaptainHook\App\Runner\Action as ActionRunner;
1718
use CaptainHook\App\Runner\Action\Cli\Command\Formatter;
1819
use SebastianFeldmann\Cli\Processor\Symfony as Processor;
1920
use SebastianFeldmann\Git\Repository;
@@ -27,7 +28,7 @@
2728
* @since Class available since Release 0.9.0
2829
* @internal
2930
*/
30-
class Cli
31+
class Cli implements ActionRunner
3132
{
3233
/**
3334
* Execute the configured action

0 commit comments

Comments
 (0)