Skip to content

Commit

Permalink
Prevent throwing in lighthouse:ide-helper when no custom directives…
Browse files Browse the repository at this point in the history
… are defined (#948)
  • Loading branch information
spawnia authored Sep 3, 2019
1 parent 387079f commit eaf4556
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `@softDeletes` and `@trashed` directives to enable
filtering soft deleted models https://github.com/nuwave/lighthouse/pull/937

### Fixed

- Prevent throwing in `lighthouse:ide-helper` when no custom directives are defined https://github.com/nuwave/lighthouse/pull/948

## [4.2.1](https://github.com/nuwave/lighthouse/compare/v4.2.0...v4.2.1)

### Fixed
Expand Down
12 changes: 11 additions & 1 deletion src/Console/IdeHelperCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Nuwave\Lighthouse\Schema\DirectiveNamespaces;
use Nuwave\Lighthouse\Support\Contracts\Directive;
use Nuwave\Lighthouse\Support\Contracts\DefinedDirective;
use HaydenPierce\ClassFinder\Exception\ClassFinderException;

class IdeHelperCommand extends Command
{
Expand Down Expand Up @@ -75,7 +76,16 @@ protected function scanForDirectives(array $directiveNamespaces): array
$directives = [];

foreach ($directiveNamespaces as $directiveNamespace) {
foreach (ClassFinder::getClassesInNamespace($directiveNamespace) as $class) {
try {
$classesInNamespace = ClassFinder::getClassesInNamespace($directiveNamespace);
} catch (ClassFinderException $classFinderException) {
// TODO remove if https://gitlab.com/hpierce1102/ClassFinder/merge_requests/16 is merged
// The ClassFinder throws if no classes are found. Since we can not know
// in advance if the user has defined custom directives, this behaviour is problematic.
continue;
}

foreach ($classesInNamespace as $class) {
$reflection = new \ReflectionClass($class);
if (! $reflection->isInstantiable()) {
continue;
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Console/IdeHelperCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,21 @@ public function itGeneratesSchemaDirectives(): void
);
$this->assertContains(UnionDirective::class, $generated);
}

/**
* @test
*/
public function itDoesNotRequireCustomDirectives(): void
{
/** @var \Illuminate\Contracts\Config\Repository $config */
$config = $this->app['config'];

$config->set('lighthouse.namespaces.directives', [
'Empty\\Because\\The\\User\\Has\\Not\\Created\\Custom\\Directives\\Yet',
]);

$this->artisan('lighthouse:ide-helper');

$this->assertFileExists(IdeHelperCommand::filePath());
}
}

0 comments on commit eaf4556

Please sign in to comment.