Skip to content

Commit 7e30555

Browse files
Grant placeholders full IO access
1 parent ae14546 commit 7e30555

File tree

11 files changed

+163
-99
lines changed

11 files changed

+163
-99
lines changed

src/Runner/Action/Cli.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function execute(Config $config, IO $io, Repository $repository, Config\A
4242
{
4343
$processor = new Processor();
4444
$cmdOriginal = $action->getAction();
45-
$cmdFormatted = $this->formatCommand($config, $repository, $cmdOriginal, $io->getArguments());
45+
$cmdFormatted = $this->formatCommand($io, $config, $repository, $cmdOriginal);
4646

4747
// if any placeholders got replaced display the finally executed command
4848
if ($cmdFormatted !== $cmdOriginal) {
@@ -87,15 +87,15 @@ public function execute(Config $config, IO $io, Repository $repository, Config\A
8787
* - post-checkout => PREVIOUSHEAD, NEWHEAD, MODE
8888
* - post-merge => SQUASH
8989
*
90+
* @param \CaptainHook\App\Console\IO $io
9091
* @param \CaptainHook\App\Config $config
9192
* @param \SebastianFeldmann\Git\Repository $repository
9293
* @param string $command
93-
* @param array<string> $args
9494
* @return string
9595
*/
96-
protected function formatCommand(Config $config, Repository $repository, string $command, array $args): string
96+
protected function formatCommand(IO $io, Config $config, Repository $repository, string $command): string
9797
{
98-
$formatter = new Formatter($config, $repository, $args);
98+
$formatter = new Formatter($io, $config, $repository);
9999
return $formatter->format($command);
100100
}
101101
}

src/Runner/Action/Cli/Command/Formatter.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CaptainHook\App\Runner\Action\Cli\Command;
1313

1414
use CaptainHook\App\Config;
15+
use CaptainHook\App\Console\IO;
1516
use SebastianFeldmann\Git\Repository;
1617

1718
/**
@@ -29,51 +30,53 @@ class Formatter
2930
*
3031
* @var array<string, string>
3132
*/
32-
private static $cache = [];
33+
private static array $cache = [];
34+
35+
/**
36+
* Input output handler
37+
*
38+
* @var \CaptainHook\App\Console\IO
39+
*/
40+
private IO $io;
3341

3442
/**
3543
* CaptainHook configuration
3644
*
3745
* @var \CaptainHook\App\Config
3846
*/
39-
private $config;
47+
private Config $config;
4048

4149
/**
4250
* List of available placeholders
4351
*
4452
* @var array<string, string>
4553
*/
46-
private static $placeholders = [
47-
'config' => '\\CaptainHook\\App\\Runner\\Action\\Cli\\Command\\Placeholder\\Config',
48-
'env' => '\\CaptainHook\\App\\Runner\\Action\\Cli\\Command\\Placeholder\\Env',
49-
'staged_files' => '\\CaptainHook\\App\\Runner\\Action\\Cli\\Command\\Placeholder\\StagedFiles'
54+
private static array $placeholders = [
55+
'config' => '\\CaptainHook\\App\\Runner\\Action\\Cli\\Command\\Placeholder\\Config',
56+
'env' => '\\CaptainHook\\App\\Runner\\Action\\Cli\\Command\\Placeholder\\Env',
57+
'staged_files' => '\\CaptainHook\\App\\Runner\\Action\\Cli\\Command\\Placeholder\\StagedFiles',
58+
'changed_files' => '\\CaptainHook\\App\\Runner\\Action\\Cli\\Command\\Placeholder\\ChangedFiles'
5059
];
5160

5261
/**
5362
* Git repository
5463
*
5564
* @var \SebastianFeldmann\Git\Repository
5665
*/
57-
private $repository;
58-
59-
/**
60-
* Original hook arguments
61-
*
62-
* @var array<string, string>
63-
*/
64-
private $arguments;
66+
private Repository $repository;
6567

6668
/**
6769
* Formatter constructor
6870
*
71+
* @param \CaptainHook\App\Console\IO $io
72+
* @param \CaptainHook\App\Config $config
6973
* @param \SebastianFeldmann\Git\Repository $repository
70-
* @param array<string, string> $arguments
7174
*/
72-
public function __construct(Config $config, Repository $repository, array $arguments)
75+
public function __construct(IO $io, Config $config, Repository $repository)
7376
{
77+
$this->io = $io;
7478
$this->config = $config;
7579
$this->repository = $repository;
76-
$this->arguments = $arguments;
7780
}
7881

7982
/**
@@ -123,7 +126,7 @@ private function replace(string $placeholder): string
123126
{
124127
// if placeholder references an original hook argument return the argument
125128
// otherwise compute the placeholder
126-
return $this->arguments[strtolower($placeholder)] ?? $this->computedPlaceholder($placeholder);
129+
return $this->io->getArguments()[strtolower($placeholder)] ?? $this->computedPlaceholder($placeholder);
127130
}
128131

129132
/**
@@ -161,7 +164,7 @@ private function createPlaceholder(string $placeholder): Placeholder
161164
{
162165
/** @var class-string<\CaptainHook\App\Runner\Action\Cli\Command\Placeholder> $class */
163166
$class = self::$placeholders[$placeholder];
164-
return new $class($this->config, $this->repository);
167+
return new $class($this->io, $this->config, $this->repository);
165168
}
166169

167170
/**

src/Runner/Action/Cli/Command/Placeholder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CaptainHook\App\Runner\Action\Cli\Command;
1313

1414
use CaptainHook\App\Config;
15+
use CaptainHook\App\Console\IO;
1516
use SebastianFeldmann\Git\Repository;
1617

1718
/**
@@ -27,10 +28,11 @@ interface Placeholder
2728
/**
2829
* Placeholder constructor
2930
*
31+
* @param \CaptainHook\App\Console\IO $io
3032
* @param \CaptainHook\App\Config $config
3133
* @param \SebastianFeldmann\Git\Repository $repository
3234
*/
33-
public function __construct(Config $config, Repository $repository);
35+
public function __construct(IO $io, Config $config, Repository $repository);
3436

3537
/**
3638
* Return the replacement value for this placeholder
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook.
5+
*
6+
* (c) Sebastian Feldmann <[email protected]>
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\Action\Cli\Command\Placeholder;
13+
14+
/**
15+
* Class CheckFiles
16+
*
17+
* @package CaptainHook
18+
* @author Sebastian Feldmann <[email protected]>
19+
* @link https://github.com/captainhookphp/captainhook
20+
* @since Class available since Release 5.0.0
21+
*/
22+
abstract class CheckFiles extends Foundation
23+
{
24+
/**
25+
* Filter staged files by directory
26+
*
27+
* @param array<string> $files
28+
* @param array<string, string> $options
29+
* @return array<string>
30+
*/
31+
protected function filterByDirectory(array $files, array $options): array
32+
{
33+
if (!isset($options['in-dir'])) {
34+
return $files;
35+
}
36+
37+
$directory = $options['in-dir'];
38+
$filtered = [];
39+
foreach ($files as $file) {
40+
if (strpos($file, $directory, 0) === 0) {
41+
$filtered[] = $file;
42+
}
43+
}
44+
45+
return $filtered;
46+
}
47+
48+
/**
49+
* Run search replace for all files
50+
*
51+
* @param array<string> $files
52+
* @param array<string, string> $options
53+
* @return array<string>
54+
*/
55+
protected function replaceInAll(array $files, array $options): array
56+
{
57+
if (!isset($options['replace'])) {
58+
return $files;
59+
}
60+
61+
$search = $options['replace'];
62+
$replace = $options['with'] ?? '';
63+
64+
foreach ($files as $index => $file) {
65+
$files[$index] = str_replace($search, $replace, $file);
66+
}
67+
return $files;
68+
}
69+
}

src/Runner/Action/Cli/Command/Placeholder/Foundation.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CaptainHook\App\Runner\Action\Cli\Command\Placeholder;
1313

1414
use CaptainHook\App\Config;
15+
use CaptainHook\App\Console\IO;
1516
use CaptainHook\App\Runner\Action\Cli\Command\Placeholder as PlaceholderInterface;
1617
use SebastianFeldmann\Git\Repository;
1718

@@ -25,28 +26,37 @@
2526
*/
2627
abstract class Foundation implements PlaceholderInterface
2728
{
29+
/**
30+
* Input Output handler
31+
*
32+
* @var \CaptainHook\App\Console\IO
33+
*/
34+
protected IO $io;
35+
2836
/**
2937
* CaptainHook configuration
3038
*
3139
* @var \CaptainHook\App\Config
3240
*/
33-
protected $config;
41+
protected Config $config;
3442

3543
/**
3644
* Git repository
3745
*
3846
* @var \SebastianFeldmann\Git\Repository
3947
*/
40-
protected $repository;
48+
protected Repository $repository;
4149

4250
/**
4351
* StagedFile constructor
4452
*
53+
* @param \CaptainHook\App\Console\IO $io
4554
* @param \CaptainHook\App\Config $config
4655
* @param \SebastianFeldmann\Git\Repository $repository
4756
*/
48-
public function __construct(Config $config, Repository $repository)
57+
public function __construct(IO $io, Config $config, Repository $repository)
4958
{
59+
$this->io = $io;
5060
$this->config = $config;
5161
$this->repository = $repository;
5262
}

src/Runner/Action/Cli/Command/Placeholder/StagedFiles.php

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @link https://github.com/captainhookphp/captainhook
2020
* @since Class available since Release 5.0.0
2121
*/
22-
class StagedFiles extends Foundation
22+
class StagedFiles extends CheckFiles
2323
{
2424
/**
2525
* @param array<string, string> $options
@@ -37,50 +37,4 @@ public function replacement(array $options): string
3737

3838
return implode(($options['separated-by'] ?? ' '), $files);
3939
}
40-
41-
/**
42-
* Filter staged files by directory
43-
*
44-
* @param array<string> $files
45-
* @param array<string, string> $options
46-
* @return array<string>
47-
*/
48-
private function filterByDirectory(array $files, array $options): array
49-
{
50-
if (!isset($options['in-dir'])) {
51-
return $files;
52-
}
53-
54-
$directory = $options['in-dir'];
55-
$filtered = [];
56-
foreach ($files as $file) {
57-
if (strpos($file, $directory, 0) === 0) {
58-
$filtered[] = $file;
59-
}
60-
}
61-
62-
return $filtered;
63-
}
64-
65-
/**
66-
* Run search replace for all files
67-
*
68-
* @param array<string> $files
69-
* @param array<string, string> $options
70-
* @return array<string>
71-
*/
72-
private function replaceInAll(array $files, array $options): array
73-
{
74-
if (!isset($options['replace'])) {
75-
return $files;
76-
}
77-
78-
$search = $options['replace'];
79-
$replace = $options['with'] ?? '';
80-
81-
foreach ($files as $index => $file) {
82-
$files[$index] = str_replace($search, $replace, $file);
83-
}
84-
return $files;
85-
}
8640
}

0 commit comments

Comments
 (0)