Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use composer/class-map-generator to find directive classes #2245

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"require": {
"php": ">= 7.2",
"ext-json": "*",
"haydenpierce/class-finder": "^0.4",
"composer/class-map-generator": "^1",
"illuminate/auth": "5.6.* || 5.7.* || 5.8.* || ^6 || ^7 || ^8 || ^9",
"illuminate/bus": "5.6.* || 5.7.* || 5.8.* || ^6 || ^7 || ^8 || ^9",
"illuminate/contracts": "5.6.* || 5.7.* || 5.8.* || ^6 || ^7 || ^8 || ^9",
Expand Down
46 changes: 1 addition & 45 deletions src/Console/IdeHelperCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\Type;
use GraphQL\Utils\SchemaPrinter;
use HaydenPierce\ClassFinder\ClassFinder;
use Illuminate\Console\Command;
use Nuwave\Lighthouse\Schema\AST\ASTCache;
use Nuwave\Lighthouse\Schema\AST\ASTHelper;
use Nuwave\Lighthouse\Schema\DirectiveLocator;
use Nuwave\Lighthouse\Schema\SchemaBuilder;
use Nuwave\Lighthouse\Schema\Source\SchemaSourceProvider;
use Nuwave\Lighthouse\Support\Contracts\Directive;

class IdeHelperCommand extends Command
{
Expand Down Expand Up @@ -56,11 +54,7 @@ public function schemaDirectiveDefinitions(DirectiveLocator $directiveLocator):

GRAPHQL;

$directiveClasses = $this->scanForDirectives(
$directiveLocator->namespaces()
);

foreach ($directiveClasses as $directiveClass) {
foreach ($directiveLocator->classes() as $directiveClass) {
$definition = $this->define($directiveClass);

$schema .= /** @lang GraphQL */ <<<GRAPHQL
Expand All @@ -77,44 +71,6 @@ public function schemaDirectiveDefinitions(DirectiveLocator $directiveLocator):
$this->info("Wrote schema directive definitions to {$filePath}.");
}

/**
* Scan the given namespaces for directive classes.
*
* @param array<string> $directiveNamespaces
*
* @return array<string, class-string<\Nuwave\Lighthouse\Support\Contracts\Directive>>
*/
protected function scanForDirectives(array $directiveNamespaces): array
{
$directives = [];

foreach ($directiveNamespaces as $directiveNamespace) {
/** @var array<class-string> $classesInNamespace */
$classesInNamespace = ClassFinder::getClassesInNamespace($directiveNamespace);

foreach ($classesInNamespace as $class) {
$reflection = new \ReflectionClass($class);
if (! $reflection->isInstantiable()) {
continue;
}

if (! is_a($class, Directive::class, true)) {
continue;
}
$name = DirectiveLocator::directiveName($class);

// The directive was already found, so we do not add it twice
if (isset($directives[$name])) {
continue;
}

$directives[$name] = $class;
}
}

return $directives;
}

/**
* @param class-string<\Nuwave\Lighthouse\Support\Contracts\Directive> $directiveClass
*
Expand Down
20 changes: 15 additions & 5 deletions src/Schema/DirectiveLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Nuwave\Lighthouse\Schema;

use Composer\ClassMapGenerator\ClassMapGenerator;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\Node;
use HaydenPierce\ClassFinder\ClassFinder;
use Illuminate\Container\Container;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -90,11 +90,21 @@ public function classes(): array
{
$directives = [];

foreach ($this->namespaces() as $directiveNamespace) {
/** @var array<class-string> $classesInNamespace */
$classesInNamespace = ClassFinder::getClassesInNamespace($directiveNamespace);
$basePath = base_path();
dump($basePath);

foreach ($classesInNamespace as $class) {
foreach ($this->namespaces() as $directiveNamespace) {
dump($directiveNamespace);
$generator = new ClassMapGenerator();
dump('generator');
// TODO any of those hang indefinitely
// $generator->scanPaths($basePath, null, 'psr-4', '');
$generator->scanPaths($basePath);
dump('scanPaths');
$classesInNamespace = $generator->getClassMap()->getMap();
dump($classesInNamespace);

foreach ($classesInNamespace as $class => $_) {
$reflection = new \ReflectionClass($class);
if (! $reflection->isInstantiable()) {
continue;
Expand Down