Skip to content

Commit

Permalink
Allow customizing how documents are found
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Mar 10, 2022
1 parent 854853c commit 75258f5
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 76 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## v0.19.0

### Added

- Allow customizing how documents are found

## v0.18.2

### Fixed
Expand Down
47 changes: 47 additions & 0 deletions src/Codegen/DirectoryFinder.php
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);
}
}
46 changes: 3 additions & 43 deletions src/Codegen/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,12 @@

namespace Spawnia\Sailor\Codegen;

class Finder
interface Finder
{
protected string $rootPath;

public function __construct(string $rootPath)
{
$this->rootPath = $rootPath;
}

/**
* Finds all GraphQL documents in a given path.
*
* Returns a map from the paths of the .graphql files to their contents.
* Return a map from the paths/names of GraphQL documents to their contents.
*
* @return array<string, string>
*/
public function documents(): array
{
$contents = [];
/** @var \SplFileInfo $fileInfo */
foreach ($this->fileIterator() as $fileInfo) {
/** @var string $path We know this file exists, since it was found in search. */
$path = $fileInfo->getRealPath();

// 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,
// Look for all .graphql files
'/^.+\.graphql$/',
\RecursiveRegexIterator::MATCH
);
}
public function documents(): array;
}
11 changes: 3 additions & 8 deletions src/Codegen/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use GraphQL\Utils\BuildSchema;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\PsrPrinter;
use Nette\Utils\FileSystem;
use Spawnia\Sailor\EndpointConfig;

class Generator
Expand Down Expand Up @@ -181,17 +180,13 @@ protected function schema(): Schema
*/
protected function parsedDocuments(): array
{
$finder = new Finder($this->endpointConfig->searchPath());
$documents = $finder->documents();
$documents = $this->endpointConfig
->finder()
->documents();

$parsed = static::parseDocuments($documents);
static::ensureOperationsAreNamed($parsed);

return $parsed;
}

protected function deleteGeneratedFiles(): void
{
FileSystem::delete($this->endpointConfig->targetPath());
}
}
2 changes: 1 addition & 1 deletion src/Codegen/OperationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function generate(): iterable
'enter' => function (VariableDefinitionNode $variableDefinition) use ($typeInfo): void {
$name = $variableDefinition->variable->name->value;

/** @var Type & InputType $type */
/** @var Type&InputType $type */
$type = $typeInfo->getInputType();

/** @var Type&NamedType $namedType */
Expand Down
10 changes: 10 additions & 0 deletions src/EndpointConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Type\Schema;
use Nette\PhpGenerator\ClassType;
use Spawnia\Sailor\Codegen\DirectoryFinder;
use Spawnia\Sailor\Codegen\Finder;
use Spawnia\Sailor\Error\Error;
use Spawnia\Sailor\Type\BooleanTypeConfig;
use Spawnia\Sailor\Type\EnumTypeConfig;
Expand Down Expand Up @@ -47,6 +49,14 @@ abstract public function searchPath(): string;
*/
abstract public function schemaPath(): string;

/**
* Instantiate a class to find GraphQL documents.
*/
public function finder(): Finder
{
return new DirectoryFinder($this->searchPath());
}

/**
* Instantiate an Error class from a plain GraphQL error.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ abstract public static function document(): string;
abstract protected static function converters(): array;

/**
* @param mixed ...$args
* @param mixed ...$args type depends on the subclass
*
* @return TResult
*/
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/Codegen/DirectoryFinderTest.php
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());
}
}
23 changes: 0 additions & 23 deletions tests/Unit/Codegen/FinderTest.php

This file was deleted.

Empty file.

0 comments on commit 75258f5

Please sign in to comment.