Skip to content

Commit 23d6c9d

Browse files
authored
Use ComposerFinder's useAutoloading method (#667)
* Use ComposerFinder's useAutoloading method * Drop useAutoloading() method * Add "Disabling autoloading" section to the docs * Fix code style * Change docs wording
1 parent 8ae940e commit 23d6c9d

File tree

11 files changed

+43
-38
lines changed

11 files changed

+43
-38
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"symfony/expression-language": "^4 || ^5 || ^6 || ^7",
2727
"thecodingmachine/cache-utils": "^1",
2828
"webonyx/graphql-php": "^v15.0",
29-
"kcs/class-finder": "^0.4.0"
29+
"kcs/class-finder": "^0.5.0"
3030
},
3131
"require-dev": {
3232
"beberlei/porpaginas": "^1.2 || ^2.0",

src/GlobControllerQueryProvider.php

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

77
use GraphQL\Type\Definition\FieldDefinition;
88
use InvalidArgumentException;
9-
use Kcs\ClassFinder\Finder\ComposerFinder;
109
use Kcs\ClassFinder\Finder\FinderInterface;
1110
use Psr\Container\ContainerInterface;
1211
use Psr\SimpleCache\CacheInterface;
@@ -33,7 +32,6 @@ final class GlobControllerQueryProvider implements QueryProviderInterface
3332
{
3433
/** @var array<int,string>|null */
3534
private array|null $instancesList = null;
36-
private FinderInterface $finder;
3735
private AggregateControllerQueryProvider|null $aggregateControllerQueryProvider = null;
3836
private CacheContractInterface $cacheContract;
3937

@@ -47,11 +45,10 @@ public function __construct(
4745
private readonly ContainerInterface $container,
4846
private readonly AnnotationReader $annotationReader,
4947
private readonly CacheInterface $cache,
50-
FinderInterface|null $finder = null,
48+
private readonly FinderInterface $finder,
5149
int|null $cacheTtl = null,
5250
)
5351
{
54-
$this->finder = $finder ?? new ComposerFinder();
5552
$this->cacheContract = new Psr16Adapter(
5653
$this->cache,
5754
str_replace(['\\', '{', '}', '(', ')', '/', '@', ':'], '_', $namespace),

src/SchemaFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\Common\Annotations\PsrCachedReader;
99
use Doctrine\Common\Annotations\Reader;
1010
use GraphQL\Type\SchemaConfig;
11+
use Kcs\ClassFinder\Finder\ComposerFinder;
1112
use Kcs\ClassFinder\Finder\FinderInterface;
1213
use MyCLabs\Enum\Enum;
1314
use PackageVersions\Versions;
@@ -343,8 +344,9 @@ public function createSchema(): Schema
343344
$cachedDocBlockFactory = new CachedDocBlockFactory($namespacedCache);
344345
$namingStrategy = $this->namingStrategy ?: new NamingStrategy();
345346
$typeRegistry = new TypeRegistry();
347+
$finder = $this->finder ?? new ComposerFinder();
346348

347-
$namespaceFactory = new NamespaceFactory($namespacedCache, $this->finder, $this->globTTL);
349+
$namespaceFactory = new NamespaceFactory($namespacedCache, $finder, $this->globTTL);
348350
$nsList = array_map(
349351
static fn (string $namespace) => $namespaceFactory->createNamespace($namespace),
350352
$this->typeNamespaces,
@@ -493,7 +495,7 @@ public function createSchema(): Schema
493495
$this->container,
494496
$annotationReader,
495497
$namespacedCache,
496-
$this->finder,
498+
$finder,
497499
$this->globTTL,
498500
);
499501
}

src/Utils/Namespaces/NS.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function getClassList(): array
7979
$this->classes = [];
8080
/** @var class-string $className */
8181
/** @var ReflectionClass<object> $reflector */
82-
foreach ($this->finder->inNamespace($this->namespace) as $className => $reflector) {
82+
foreach ((clone $this->finder)->inNamespace($this->namespace) as $className => $reflector) {
8383
if (! ($reflector instanceof ReflectionClass)) {
8484
continue;
8585
}

src/Utils/Namespaces/NamespaceFactory.php

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

55
namespace TheCodingMachine\GraphQLite\Utils\Namespaces;
66

7-
use Kcs\ClassFinder\Finder\ComposerFinder;
87
use Kcs\ClassFinder\Finder\FinderInterface;
98
use Psr\SimpleCache\CacheInterface;
109

@@ -15,11 +14,11 @@
1514
*/
1615
final class NamespaceFactory
1716
{
18-
private FinderInterface $finder;
19-
20-
public function __construct(private readonly CacheInterface $cache, FinderInterface|null $finder = null, private int|null $globTTL = 2)
21-
{
22-
$this->finder = $finder ?? new ComposerFinder();
17+
public function __construct(
18+
private readonly CacheInterface $cache,
19+
private readonly FinderInterface $finder,
20+
private int|null $globTTL = 2,
21+
) {
2322
}
2423

2524
/** @param string $namespace A PHP namespace */

tests/AbstractQueryProviderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use GraphQL\Type\Definition\ObjectType;
1010
use GraphQL\Type\Definition\OutputType;
1111
use GraphQL\Type\Definition\Type;
12+
use Kcs\ClassFinder\Finder\ComposerFinder;
1213
use phpDocumentor\Reflection\TypeResolver as PhpDocumentorTypeResolver;
1314
use PHPUnit\Framework\TestCase;
1415
use Psr\Container\ContainerInterface;
@@ -466,7 +467,7 @@ protected function getNamespaceFactory(): NamespaceFactory
466467
$arrayAdapter->setLogger(new ExceptionLogger());
467468
$psr16Cache = new Psr16Cache($arrayAdapter);
468469

469-
$this->namespaceFactory = new NamespaceFactory($psr16Cache);
470+
$this->namespaceFactory = new NamespaceFactory($psr16Cache, new ComposerFinder());
470471
}
471472
return $this->namespaceFactory;
472473
}

tests/Fixtures/BadNamespace/BadlyNamespacedClass.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/Fixtures/BadNamespace/ClassWithoutNamespace.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/Fixtures/Integration/Models/BadNamespaceClass.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/Integration/IntegrationTestCase.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
66
use GraphQL\Error\DebugFlag;
77
use GraphQL\Executor\ExecutionResult;
8+
use Kcs\ClassFinder\Finder\ComposerFinder;
9+
use Kcs\ClassFinder\Finder\FinderInterface;
810
use PHPUnit\Framework\TestCase;
911
use Psr\Container\ContainerInterface;
1012
use stdClass;
@@ -87,13 +89,15 @@ public function createContainer(array $overloadedServices = []): ContainerInterf
8789
Schema::class => static function (ContainerInterface $container) {
8890
return new Schema($container->get(QueryProviderInterface::class), $container->get(RecursiveTypeMapperInterface::class), $container->get(TypeResolver::class), $container->get(RootTypeMapperInterface::class));
8991
},
92+
FinderInterface::class => fn () => new ComposerFinder(),
9093
QueryProviderInterface::class => static function (ContainerInterface $container) {
9194
$queryProvider = new GlobControllerQueryProvider(
9295
'TheCodingMachine\\GraphQLite\\Fixtures\\Integration\\Controllers',
9396
$container->get(FieldsBuilder::class),
9497
$container->get(BasicAutoWiringContainer::class),
9598
$container->get(AnnotationReader::class),
9699
new Psr16Cache(new ArrayAdapter()),
100+
$container->get(FinderInterface::class),
97101
);
98102

99103
$queryProvider = new AggregateQueryProvider([
@@ -104,6 +108,7 @@ public function createContainer(array $overloadedServices = []): ContainerInterf
104108
$container->get(BasicAutoWiringContainer::class),
105109
$container->get(AnnotationReader::class),
106110
new Psr16Cache(new ArrayAdapter()),
111+
$container->get(FinderInterface::class),
107112
),
108113
]);
109114

@@ -201,7 +206,10 @@ public function createContainer(array $overloadedServices = []): ContainerInterf
201206
NamespaceFactory::class => static function (ContainerInterface $container) {
202207
$arrayAdapter = new ArrayAdapter();
203208
$arrayAdapter->setLogger(new ExceptionLogger());
204-
return new NamespaceFactory(new Psr16Cache($arrayAdapter));
209+
return new NamespaceFactory(
210+
new Psr16Cache($arrayAdapter),
211+
$container->get(FinderInterface::class),
212+
);
205213
},
206214
GlobTypeMapper::class => static function (ContainerInterface $container) {
207215
$arrayAdapter = new ArrayAdapter();

0 commit comments

Comments
 (0)