generated from spawnia/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow customizing how documents are found
- Loading branch information
Showing
10 changed files
with
101 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Spawnia\Sailor\Codegen; | ||
|
||
class DirectoryFinder implements Finder | ||
{ | ||
protected string $rootPath; | ||
|
||
protected string $pattern; | ||
|
||
public function __construct( | ||
string $rootPath, | ||
string $pattern = '/^.+\.graphql$/' | ||
) { | ||
$this->rootPath = $rootPath; | ||
$this->pattern = $pattern; | ||
} | ||
|
||
public function documents(): array | ||
{ | ||
$contents = []; | ||
foreach ($this->fileIterator() as $fileInfo) { | ||
assert($fileInfo instanceof \SplFileInfo); | ||
|
||
$path = $fileInfo->getRealPath(); | ||
assert(is_string($path), 'We know this file exists, since it was found in search'); | ||
|
||
// When installing from source, the examples might end up in the critical path, | ||
// so we exclude them from the search. | ||
if (false !== mb_strpos($path, 'vendor/spawnia/sailor/')) { | ||
continue; | ||
} | ||
|
||
$contents[$path] = \Safe\file_get_contents($path); | ||
} | ||
|
||
return $contents; | ||
} | ||
|
||
protected function fileIterator(): \RegexIterator | ||
{ | ||
$directory = new \RecursiveDirectoryIterator($this->rootPath); | ||
$iterator = new \RecursiveIteratorIterator($directory); | ||
|
||
return new \RegexIterator($iterator, $this->pattern, \RecursiveRegexIterator::MATCH); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Spawnia\Sailor\Tests\Unit\Codegen; | ||
|
||
use PHPUnit\Framework; | ||
use Spawnia\Sailor\Codegen\DirectoryFinder; | ||
|
||
class DirectoryFinderTest extends Framework\TestCase | ||
{ | ||
public function testFindsFiles(): void | ||
{ | ||
$finder = new DirectoryFinder(__DIR__ . '/finder'); | ||
|
||
self::assertCount(3, $finder->documents()); | ||
} | ||
|
||
public function testNoFiles(): void | ||
{ | ||
$finder = new DirectoryFinder(__DIR__ . '/finder/empty'); | ||
|
||
self::assertCount(0, $finder->documents()); | ||
} | ||
|
||
public function testPattern(): void | ||
{ | ||
$finder = new DirectoryFinder(__DIR__ . '/finder', '/^.+\.suffix\.graphql$/'); | ||
|
||
self::assertCount(1, $finder->documents()); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Empty file.