Skip to content
Merged
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 @@ -26,7 +26,7 @@
"symfony/expression-language": "^4 || ^5 || ^6 || ^7",
"thecodingmachine/cache-utils": "^1",
"webonyx/graphql-php": "^v15.0",
"kcs/class-finder": "^0.4.0"
"kcs/class-finder": "^0.5.0"
},
"require-dev": {
"beberlei/porpaginas": "^1.2 || ^2.0",
Expand Down
5 changes: 1 addition & 4 deletions src/GlobControllerQueryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use GraphQL\Type\Definition\FieldDefinition;
use InvalidArgumentException;
use Kcs\ClassFinder\Finder\ComposerFinder;
use Kcs\ClassFinder\Finder\FinderInterface;
use Psr\Container\ContainerInterface;
use Psr\SimpleCache\CacheInterface;
Expand All @@ -33,7 +32,6 @@ final class GlobControllerQueryProvider implements QueryProviderInterface
{
/** @var array<int,string>|null */
private array|null $instancesList = null;
private FinderInterface $finder;
private AggregateControllerQueryProvider|null $aggregateControllerQueryProvider = null;
private CacheContractInterface $cacheContract;

Expand All @@ -47,11 +45,10 @@ public function __construct(
private readonly ContainerInterface $container,
private readonly AnnotationReader $annotationReader,
private readonly CacheInterface $cache,
FinderInterface|null $finder = null,
private readonly FinderInterface $finder,
int|null $cacheTtl = null,
)
{
$this->finder = $finder ?? new ComposerFinder();
$this->cacheContract = new Psr16Adapter(
$this->cache,
str_replace(['\\', '{', '}', '(', ')', '/', '@', ':'], '_', $namespace),
Expand Down
6 changes: 4 additions & 2 deletions src/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\Common\Annotations\PsrCachedReader;
use Doctrine\Common\Annotations\Reader;
use GraphQL\Type\SchemaConfig;
use Kcs\ClassFinder\Finder\ComposerFinder;
use Kcs\ClassFinder\Finder\FinderInterface;
use MyCLabs\Enum\Enum;
use PackageVersions\Versions;
Expand Down Expand Up @@ -343,8 +344,9 @@ public function createSchema(): Schema
$cachedDocBlockFactory = new CachedDocBlockFactory($namespacedCache);
$namingStrategy = $this->namingStrategy ?: new NamingStrategy();
$typeRegistry = new TypeRegistry();
$finder = $this->finder ?? new ComposerFinder();

$namespaceFactory = new NamespaceFactory($namespacedCache, $this->finder, $this->globTTL);
$namespaceFactory = new NamespaceFactory($namespacedCache, $finder, $this->globTTL);
$nsList = array_map(
static fn (string $namespace) => $namespaceFactory->createNamespace($namespace),
$this->typeNamespaces,
Expand Down Expand Up @@ -493,7 +495,7 @@ public function createSchema(): Schema
$this->container,
$annotationReader,
$namespacedCache,
$this->finder,
$finder,
$this->globTTL,
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Namespaces/NS.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function getClassList(): array
$this->classes = [];
/** @var class-string $className */
/** @var ReflectionClass<object> $reflector */
foreach ($this->finder->inNamespace($this->namespace) as $className => $reflector) {
foreach ((clone $this->finder)->inNamespace($this->namespace) as $className => $reflector) {
if (! ($reflector instanceof ReflectionClass)) {
continue;
}
Expand Down
11 changes: 5 additions & 6 deletions src/Utils/Namespaces/NamespaceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace TheCodingMachine\GraphQLite\Utils\Namespaces;

use Kcs\ClassFinder\Finder\ComposerFinder;
use Kcs\ClassFinder\Finder\FinderInterface;
use Psr\SimpleCache\CacheInterface;

Expand All @@ -15,11 +14,11 @@
*/
final class NamespaceFactory
{
private FinderInterface $finder;

public function __construct(private readonly CacheInterface $cache, FinderInterface|null $finder = null, private int|null $globTTL = 2)
{
$this->finder = $finder ?? new ComposerFinder();
public function __construct(
private readonly CacheInterface $cache,
private readonly FinderInterface $finder,
private int|null $globTTL = 2,
) {
}

/** @param string $namespace A PHP namespace */
Expand Down
3 changes: 2 additions & 1 deletion tests/AbstractQueryProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\OutputType;
use GraphQL\Type\Definition\Type;
use Kcs\ClassFinder\Finder\ComposerFinder;
use phpDocumentor\Reflection\TypeResolver as PhpDocumentorTypeResolver;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -466,7 +467,7 @@ protected function getNamespaceFactory(): NamespaceFactory
$arrayAdapter->setLogger(new ExceptionLogger());
$psr16Cache = new Psr16Cache($arrayAdapter);

$this->namespaceFactory = new NamespaceFactory($psr16Cache);
$this->namespaceFactory = new NamespaceFactory($psr16Cache, new ComposerFinder());
}
return $this->namespaceFactory;
}
Expand Down
8 changes: 0 additions & 8 deletions tests/Fixtures/BadNamespace/BadlyNamespacedClass.php

This file was deleted.

5 changes: 0 additions & 5 deletions tests/Fixtures/BadNamespace/ClassWithoutNamespace.php

This file was deleted.

9 changes: 0 additions & 9 deletions tests/Fixtures/Integration/Models/BadNamespaceClass.php

This file was deleted.

10 changes: 9 additions & 1 deletion tests/Integration/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
use GraphQL\Error\DebugFlag;
use GraphQL\Executor\ExecutionResult;
use Kcs\ClassFinder\Finder\ComposerFinder;
use Kcs\ClassFinder\Finder\FinderInterface;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use stdClass;
Expand Down Expand Up @@ -87,13 +89,15 @@ public function createContainer(array $overloadedServices = []): ContainerInterf
Schema::class => static function (ContainerInterface $container) {
return new Schema($container->get(QueryProviderInterface::class), $container->get(RecursiveTypeMapperInterface::class), $container->get(TypeResolver::class), $container->get(RootTypeMapperInterface::class));
},
FinderInterface::class => fn () => new ComposerFinder(),
QueryProviderInterface::class => static function (ContainerInterface $container) {
$queryProvider = new GlobControllerQueryProvider(
'TheCodingMachine\\GraphQLite\\Fixtures\\Integration\\Controllers',
$container->get(FieldsBuilder::class),
$container->get(BasicAutoWiringContainer::class),
$container->get(AnnotationReader::class),
new Psr16Cache(new ArrayAdapter()),
$container->get(FinderInterface::class),
);

$queryProvider = new AggregateQueryProvider([
Expand All @@ -104,6 +108,7 @@ public function createContainer(array $overloadedServices = []): ContainerInterf
$container->get(BasicAutoWiringContainer::class),
$container->get(AnnotationReader::class),
new Psr16Cache(new ArrayAdapter()),
$container->get(FinderInterface::class),
),
]);

Expand Down Expand Up @@ -201,7 +206,10 @@ public function createContainer(array $overloadedServices = []): ContainerInterf
NamespaceFactory::class => static function (ContainerInterface $container) {
$arrayAdapter = new ArrayAdapter();
$arrayAdapter->setLogger(new ExceptionLogger());
return new NamespaceFactory(new Psr16Cache($arrayAdapter));
return new NamespaceFactory(
new Psr16Cache($arrayAdapter),
$container->get(FinderInterface::class),
);
},
GlobTypeMapper::class => static function (ContainerInterface $container) {
$arrayAdapter = new ArrayAdapter();
Expand Down
20 changes: 20 additions & 0 deletions website/docs/other-frameworks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@ use TheCodingMachine\GraphQLite\Context\Context;
$result = GraphQL::executeQuery($schema, $query, null, new Context(), $variableValues);
```

### Disabling autoloading

GraphQLite uses `kcs/class-finder` to find all classes that have GraphQLite attributes. By default, it uses
autoloading under the hood. But if you have an older codebase that contains classes with incorrect or missing
namespaces, you may need to use `include_once` instead. To do so, you can overwrite the finder using `setFinder()`:

```php
use Kcs\ClassFinder\Finder\ComposerFinder;
use TheCodingMachine\GraphQLite\SchemaFactory;

$factory = new SchemaFactory($cache, $container);
$factory->addControllerNamespace('App\\Controllers\\')
->addTypeNamespace('App\\')
->setFinder(
(new ComposerFinder())->useAutoloading(false)
);

$schema = $factory->createSchema();
```

## Minimal example

The smallest working example using no framework is:
Expand Down