Skip to content

Commit ed3a0b7

Browse files
authored
Merge pull request #25 from marcelthole/resolve-24
Add Option to match files instead of providing each file as additiona…
2 parents 761cfdb + 3904790 commit ed3a0b7

File tree

21 files changed

+254
-37
lines changed

21 files changed

+254
-37
lines changed

.github/workflows/build.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ jobs:
6262
run: "composer show"
6363

6464
- name: "Run code style check"
65-
run: "composer run-script cs-check"
65+
run: "composer run-script cs-check"
6666
if: ${{ matrix.composer-deps == 'latest' && matrix.php-version == '7.4' }}
6767

6868
- name: "Run CI"
69-
run: "composer run-script ci"
69+
run: "composer run-script phpunit"
7070

7171
tests-with-coverage:
7272
name: "Tests with coverage and PR Comments"
@@ -124,6 +124,9 @@ jobs:
124124
git fetch --depth=1 origin $GITHUB_BASE_REF
125125
composer run-script infection -- --git-diff-filter=AM --git-diff-base=origin/$GITHUB_BASE_REF --logger-github --ignore-msi-with-no-mutations --only-covered
126126
127+
- name: Run PHPStan
128+
run: composer run-script phpstan -- --error-format=github
129+
127130
test-runs-on-docker:
128131
name: "Test if tests pass in the Docker container"
129132
runs-on: ubuntu-latest

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ Usage:
2727
2828
```
2929

30+
### Arguments
31+
| Argument | Meaning |
32+
| --- | --- |
33+
| --match[=MATCH] | Use a RegEx pattern to determine the additionalFiles. If this option is set the additionalFiles could be omitted (multiple values allowed) |
34+
| --resolve-references[=RESOLVE-REFERENCES] | Resolve the "$refs" in the given files [default: true] |
35+
| -o, --outputfile[=OUTPUTFILE] | Defines the output file for the result. Defaults the result will printed to stdout |
36+
37+
3038
## Docker
3139
Run the `openapi-merge` command within a docker container
3240
```

bin/openapi-merge

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<?php
33

44
use Mthole\OpenApiMerge\Console\Command\MergeCommand;
5+
use Mthole\OpenApiMerge\FileHandling\RegexFinder;
56
use Mthole\OpenApiMerge\Merge\PathMerger;
67
use Mthole\OpenApiMerge\OpenApiMerge;
78
use Mthole\OpenApiMerge\Merge\ReferenceNormalizer;
@@ -30,7 +31,8 @@ $application->add(new MergeCommand(
3031
new PathMerger(),
3132
new ReferenceNormalizer()
3233
),
33-
new DefinitionWriter()
34+
new DefinitionWriter(),
35+
new RegexFinder()
3436
));
3537
$application->setDefaultCommand(MergeCommand::COMMAND_NAME, true);
3638
$application->run();

src/Console/Command/MergeCommand.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Exception;
88
use Mthole\OpenApiMerge\FileHandling\File;
9+
use Mthole\OpenApiMerge\FileHandling\Finder;
910
use Mthole\OpenApiMerge\FileHandling\SpecificationFile;
1011
use Mthole\OpenApiMerge\OpenApiMergeInterface;
1112
use Mthole\OpenApiMerge\Writer\DefinitionWriterInterface;
@@ -16,8 +17,8 @@
1617
use Symfony\Component\Console\Output\OutputInterface;
1718

1819
use function array_map;
20+
use function count;
1921
use function file_put_contents;
20-
use function is_array;
2122
use function is_string;
2223
use function sprintf;
2324
use function touch;
@@ -28,14 +29,17 @@ final class MergeCommand extends Command
2829

2930
private OpenApiMergeInterface $merger;
3031
private DefinitionWriterInterface $definitionWriter;
32+
private Finder $fileFinder;
3133

3234
public function __construct(
3335
OpenApiMergeInterface $openApiMerge,
34-
DefinitionWriterInterface $definitionWriter
36+
DefinitionWriterInterface $definitionWriter,
37+
Finder $fileFinder
3538
) {
3639
parent::__construct(self::COMMAND_NAME);
3740
$this->merger = $openApiMerge;
3841
$this->definitionWriter = $definitionWriter;
42+
$this->fileFinder = $fileFinder;
3943
}
4044

4145
protected function configure(): void
@@ -53,7 +57,14 @@ protected function configure(): void
5357
HELP
5458
)
5559
->addArgument('basefile', InputArgument::REQUIRED)
56-
->addArgument('additionalFiles', InputArgument::IS_ARRAY)
60+
->addArgument('additionalFiles', InputArgument::IS_ARRAY | InputArgument::OPTIONAL)
61+
->addOption(
62+
'match',
63+
null,
64+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
65+
'Use a RegEx pattern to determine the additionalFiles. '
66+
. 'If this option is set the additionalFiles could be omitted'
67+
)
5768
->addOption(
5869
'resolve-references',
5970
null,
@@ -74,7 +85,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7485
$baseFile = $input->getArgument('basefile');
7586
$additionalFiles = $input->getArgument('additionalFiles');
7687

77-
if (! is_string($baseFile) || ! is_array($additionalFiles)) {
88+
if (count($additionalFiles) === 0) {
89+
foreach ($input->getOption('match') as $regex) {
90+
$additionalFiles = [...$additionalFiles, ...$this->fileFinder->find('.', $regex)];
91+
}
92+
}
93+
94+
if (! is_string($baseFile) || count($additionalFiles) === 0) {
7895
throw new Exception('Invalid arguments given');
7996
}
8097

src/FileHandling/Finder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Mthole\OpenApiMerge\FileHandling;
6+
7+
interface Finder
8+
{
9+
/**
10+
* @return list<string>
11+
*/
12+
public function find(string $baseDirectory, string $searchString): array;
13+
}

src/FileHandling/RegexFinder.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Mthole\OpenApiMerge\FileHandling;
6+
7+
use RecursiveCallbackFilterIterator;
8+
use RecursiveDirectoryIterator;
9+
use RecursiveIterator;
10+
use RecursiveIteratorIterator;
11+
12+
use function array_values;
13+
use function iterator_to_array;
14+
use function preg_match;
15+
use function sprintf;
16+
use function str_replace;
17+
use function strlen;
18+
use function substr;
19+
20+
class RegexFinder implements Finder
21+
{
22+
/**
23+
* @return list<string>
24+
*/
25+
public function find(string $baseDirectory, string $searchString): array
26+
{
27+
$directoryIterator = new RecursiveDirectoryIterator(
28+
$baseDirectory,
29+
RecursiveDirectoryIterator::CURRENT_AS_PATHNAME | RecursiveDirectoryIterator::SKIP_DOTS
30+
);
31+
32+
$regexIterator = new RecursiveCallbackFilterIterator(
33+
$directoryIterator,
34+
static function (
35+
string $current,
36+
string $key,
37+
RecursiveIterator $iterator
38+
) use (
39+
$baseDirectory,
40+
$searchString
41+
) {
42+
if ($iterator->hasChildren()) {
43+
return true;
44+
}
45+
46+
$relativeFileName = '.' . substr($current, strlen($baseDirectory));
47+
48+
return preg_match(
49+
sprintf('~%s~i', str_replace('~', '\~', $searchString)),
50+
$relativeFileName
51+
) === 1;
52+
}
53+
);
54+
55+
$recursiveIterator = new RecursiveIteratorIterator($regexIterator);
56+
57+
return array_values(iterator_to_array($recursiveIterator));
58+
}
59+
}

src/Merge/ReferenceNormalizer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function normalizeInlineReferences(
5555
$example,
5656
$refFileCollection
5757
);
58-
} elseif ($example !== null) {
58+
} else {
5959
$newExamples[$key] = $example;
6060
}
6161
}
@@ -77,7 +77,7 @@ public function normalizeInlineReferences(
7777
}
7878

7979
/**
80-
* @param array<int, string> $refFileCollection
80+
* @param list<string> $refFileCollection
8181
*/
8282
private function normalizeReference(Reference $reference, array &$refFileCollection): Reference
8383
{
@@ -95,9 +95,9 @@ private function normalizeReference(Reference $reference, array &$refFileCollect
9595
}
9696

9797
/**
98-
* @param array<int, string> $refFileCollection
98+
* @param list<string> $refFileCollection
9999
*
100-
* @return array<int, File>
100+
* @return list<File>
101101
*/
102102
private function normalizeFilePaths(File $openApiFile, array $refFileCollection): array
103103
{

src/Merge/ReferenceResolverResult.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
final class ReferenceResolverResult
1111
{
1212
private OpenApi $openApiSpecification;
13-
/** @var array<int, File> */
13+
/** @var list<File> */
1414
private array $foundReferenceFiles;
1515

1616
/**
17-
* @param array<int, File> $foundReferenceFiles
17+
* @param list<File> $foundReferenceFiles
1818
*/
1919
public function __construct(
2020
OpenApi $openApiSpecification,
@@ -29,7 +29,7 @@ public function getNormalizedDefinition(): OpenApi
2929
return $this->openApiSpecification;
3030
}
3131

32-
/** @return array<int, File> */
32+
/** @return list<File> */
3333
public function getFoundReferenceFiles(): array
3434
{
3535
return $this->foundReferenceFiles;

src/OpenApiMerge.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(
3232
$this->referenceNormalizer = $referenceResolver;
3333
}
3434

35-
/** @param array<int, File> $additionalFiles */
35+
/** @param list<File> $additionalFiles */
3636
public function mergeFiles(File $baseFile, array $additionalFiles, bool $resolveReference = true): SpecificationFile
3737
{
3838
$mergedOpenApiDefinition = $this->openApiReader->readFile($baseFile, $resolveReference)->getOpenApi();

src/OpenApiMergeInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
interface OpenApiMergeInterface
1111
{
12-
/** @param array<int, File> $additionalFiles */
12+
/** @param list<File> $additionalFiles */
1313
public function mergeFiles(
1414
File $baseFile,
1515
array $additionalFiles,

0 commit comments

Comments
 (0)