Skip to content

Commit

Permalink
Merge 1781777 into e1bb334
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Jun 26, 2019
2 parents e1bb334 + 1781777 commit 9be51da
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 249 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -33,7 +33,7 @@ This *service provider* expects the following configuration / services to be ava
| `graphqlite.namespace.controllers` | *yes* | An array containing the namespaces where GraphQL controllers are stored |
| `graphqlite.namespace.types` | *yes* | An array containing the namespaces where GraphQL types are stored |
| `Psr\SimpleCache\CacheInterface` | *yes* | A PSR-16 cache service |
| `Doctrine\Common\Annotations\Reader` | *yes* | A Doctrine annotation reader |
| `Doctrine\Common\Annotations\Reader` | *no* | A Doctrine annotation reader |
| `TheCodingMachine\GraphQLite\Security\AuthenticationServiceInterface` | *no* | A service to plug authentication to GraphQLite. If not passed, the `FailAuthenticationService` is used instead. |
| `TheCodingMachine\GraphQLite\Security\AuthorizationServiceInterface` | *no* | A service to plug authorization to GraphQLite. If not passed, the `FailAuthorizationService` is used instead. |

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -11,7 +11,7 @@
"require": {
"php": ">=7.1",
"thecodingmachine/funky": "^1",
"thecodingmachine/graphqlite": "^3",
"thecodingmachine/graphqlite": "~4.0.0",
"thecodingmachine/doctrine-annotations-universal-module": "^1.1"
},
"require-dev": {
Expand All @@ -30,7 +30,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.0.x-dev"
"dev-master": "4.0.x-dev"
}
},
"suggest": {
Expand Down
284 changes: 38 additions & 246 deletions src/GraphQLiteServiceProvider.php
Expand Up @@ -15,6 +15,7 @@
use TheCodingMachine\Funky\ServiceProvider;
use TheCodingMachine\GraphQLite\AggregateQueryProvider;
use TheCodingMachine\GraphQLite\AnnotationReader;
use TheCodingMachine\GraphQLite\FieldsBuilder;
use TheCodingMachine\GraphQLite\FieldsBuilderFactory;
use TheCodingMachine\GraphQLite\GlobControllerQueryProvider;
use TheCodingMachine\GraphQLite\Hydrators\FactoryHydrator;
Expand All @@ -26,12 +27,18 @@
use TheCodingMachine\GraphQLite\Mappers\PorpaginasTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface;
use TheCodingMachine\GraphQLite\Mappers\Root\BaseTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\Root\CompositeRootTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\Root\MyCLabsEnumTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\Root\RootTypeMapperInterface;
use TheCodingMachine\GraphQLite\Mappers\TypeMapperInterface;
use TheCodingMachine\GraphQLite\NamingStrategy;
use TheCodingMachine\GraphQLite\NamingStrategyInterface;
use TheCodingMachine\GraphQLite\QueryProviderInterface;
use TheCodingMachine\GraphQLite\Reflection\CachedDocBlockFactory;
use TheCodingMachine\GraphQLite\Schema;
use GraphQL\Type\Schema as WebonyxSchema;
use TheCodingMachine\GraphQLite\SchemaFactory;
use TheCodingMachine\GraphQLite\Security\AuthenticationServiceInterface;
use TheCodingMachine\GraphQLite\Security\AuthorizationServiceInterface;
use TheCodingMachine\GraphQLite\Security\FailAuthenticationService;
Expand All @@ -44,265 +51,50 @@
class GraphQLiteServiceProvider extends ServiceProvider
{
/**
* @Factory()
* @Factory(aliases={WebonyxSchema::class})
*/
public static function getSchema(
QueryProviderInterface $queryProvider,
RecursiveTypeMapperInterface $recursiveTypeMapper,
TypeResolver $typeResolver
SchemaFactory $schemaFactory
): Schema {
return new Schema($queryProvider, $recursiveTypeMapper, $typeResolver);
}

/**
* @Factory(aliases={QueryProviderInterface::class})
* @param QueryProviderInterface[] $queryProviders
*/
public static function getQueryProvider(array $queryProviders): AggregateQueryProvider
{
return new AggregateQueryProvider($queryProviders);
}

/**
* @Factory(name="queryProviders")
* @return QueryProviderInterface[]
*/
public static function getQueryProviders(
FieldsBuilderFactory $fieldsBuilderFactory,
RecursiveTypeMapperInterface $recursiveTypeMapper,
ContainerInterface $container,
CacheInterface $cache,
LockFactory $lockFactory
): array {
$namespaces = $container->get('graphqlite.namespace.controllers');
$queryProviders = [];

foreach ($namespaces as $namespace) {
$queryProviders[] = new GlobControllerQueryProvider(
$namespace,
$fieldsBuilderFactory,
$recursiveTypeMapper,
$container,
$lockFactory,
$cache
);
}

return $queryProviders;
}

/**
* @Factory()
*/
public static function getLockFactory(StoreInterface $store): LockFactory
{
return new LockFactory($store);
}

/**
* @Factory()
*/
public static function getStore(): StoreInterface
{
if (extension_loaded('sysvsem')) {
return new SemaphoreStore();
} else {
return new FlockStore(sys_get_temp_dir());
}
}

/**
* @Factory()
*/
public static function getFieldsBuilderFactory(
AnnotationReader $annotationReader,
HydratorInterface $hydrator,
AuthenticationServiceInterface $authenticationService,
AuthorizationServiceInterface $authorizationService,
TypeResolver $typeResolver,
CachedDocBlockFactory $cachedDocBlockFactory,
NamingStrategyInterface $namingStrategy
): FieldsBuilderFactory {
return new FieldsBuilderFactory(
$annotationReader,
$hydrator,
$authenticationService,
$authorizationService,
$typeResolver,
$cachedDocBlockFactory,
$namingStrategy
);
return $schemaFactory->createSchema();
}

/**
* @Factory()
* @param CacheInterface $cache
* @param ContainerInterface $container
* @param Reader|null $annotationReader
* @return SchemaFactory
*/
public static function getTypeResolver(): TypeResolver
{
return new TypeResolver();
}

/**
* @Factory(aliases={AuthenticationServiceInterface::class})
*/
public static function getAuthenticationService(): FailAuthenticationService
{
return new FailAuthenticationService();
}

/**
* @Factory(aliases={AuthorizationServiceInterface::class})
*/
public static function getAuthorizationService(): FailAuthorizationService
{
return new FailAuthorizationService();
}

/**
* @Factory(aliases={RecursiveTypeMapperInterface::class})
*/
public static function getRecursiveTypeMapperInterface(
TypeMapperInterface $typeMapper,
NamingStrategyInterface $namingStrategy,
public static function getSchemaFactory(
CacheInterface $cache,
TypeRegistry $typeRegistry
): RecursiveTypeMapper {
return new RecursiveTypeMapper($typeMapper, $namingStrategy, $cache, $typeRegistry);
}

/**
* @Factory(aliases={TypeMapperInterface::class})
* @param TypeMapperInterface[] $typeMappers
*/
public static function getTypeMapper(array $typeMappers): CompositeTypeMapper
{
return new CompositeTypeMapper($typeMappers);
}

/**
* @Factory(name="typeMappers")
* @return TypeMapperInterface[]
*/
public static function getTypeMappers(
ContainerInterface $container,
TypeGenerator $typeGenerator,
InputTypeGenerator $inputTypeGenerator,
InputTypeUtils $inputTypeUtils,
AnnotationReader $annotationReader,
NamingStrategyInterface $namingStrategy,
CacheInterface $cache,
PorpaginasTypeMapper $porpaginasTypeMapper,
LockFactory $lockFactory
): array {
$namespaces = $container->get('graphqlite.namespace.types');
$typeMappers = [];

foreach ($namespaces as $namespace) {
$typeMappers[] = new GlobTypeMapper(
$namespace,
$typeGenerator,
$inputTypeGenerator,
$inputTypeUtils,
$container,
$annotationReader,
$namingStrategy,
$lockFactory,
$cache
);
}

$typeMappers[] = $porpaginasTypeMapper;

return $typeMappers;
}

/**
* @Factory()
*/
public static function getPorpaginasTypeMapper(): PorpaginasTypeMapper
{
return new PorpaginasTypeMapper();
}

/**
* @Factory()
*/
public static function getTypeGenerator(
AnnotationReader $annotationReader,
FieldsBuilderFactory $fieldsBuilderFactory,
NamingStrategyInterface $namingStrategy,
TypeRegistry $typeRegistry,
ContainerInterface $container
): TypeGenerator {
return new TypeGenerator($annotationReader, $fieldsBuilderFactory, $namingStrategy, $typeRegistry, $container);
}

/**
* @Factory()
*/
public static function getTypeRegistry(): TypeRegistry
{
return new TypeRegistry();
}

/**
* @Factory()
*/
public static function getInputTypeGenerator(
InputTypeUtils $inputTypeUtils,
FieldsBuilderFactory $fieldsBuilderFactory,
ArgumentResolver $argumentResolver
): InputTypeGenerator {
return new InputTypeGenerator($inputTypeUtils, $fieldsBuilderFactory, $argumentResolver);
}

/**
* @Factory()
*/
public static function getInputTypeUtils(
AnnotationReader $annotationReader,
NamingStrategyInterface $namingStrategy
): InputTypeUtils {
return new InputTypeUtils($annotationReader, $namingStrategy);
}
?Reader $annotationReader = null,
?AuthenticationServiceInterface $authenticationService = null,
?AuthorizationServiceInterface $authorizationService = null
): SchemaFactory {
$schemaFactory = new SchemaFactory($cache, $container);

/**
* @Factory()
*/
public static function getAnnotationReader(Reader $annotationReader): AnnotationReader
{
return new AnnotationReader($annotationReader);
}
$controllerNamespaces = $container->get('graphqlite.namespace.controllers');
$typeNamespaces = $container->get('graphqlite.namespace.types');

/**
* @Factory(aliases={HydratorInterface::class})
*/
public static function getFactoryHydrator(): FactoryHydrator
{
return new FactoryHydrator();
}

/**
* @Factory()
*/
public static function getArgumentResolver(HydratorInterface $hydrator): ArgumentResolver
{
return new ArgumentResolver($hydrator);
}
foreach ($controllerNamespaces as $controllerNamespace) {
$schemaFactory->addControllerNamespace($controllerNamespace);
}
foreach ($typeNamespaces as $typeNamespace) {
$schemaFactory->addTypeNamespace($typeNamespace);
}

/**
* @Factory(aliases={NamingStrategyInterface::class})
*/
public static function getNamingStrategy(): NamingStrategy
{
return new NamingStrategy();
}
if ($annotationReader !== null) {
$schemaFactory->setDoctrineAnnotationReader($annotationReader);
}
if ($authenticationService !== null) {
$schemaFactory->setAuthenticationService($authenticationService);
}
if ($authorizationService !== null) {
$schemaFactory->setAuthorizationService($authorizationService);
}

/**
* @Factory()
*/
public static function getCachedDocBlockFactory(CacheInterface $cache): CachedDocBlockFactory
{
return new CachedDocBlockFactory($cache);
return $schemaFactory;
}
}
6 changes: 6 additions & 0 deletions tests/GraphQLiteServiceProviderTest.php
Expand Up @@ -5,6 +5,10 @@
use PHPUnit\Framework\TestCase;
use Simplex\Container;
use TheCodingMachine\GraphQLite\Schema;
use TheCodingMachine\GraphQLite\Security\AuthenticationServiceInterface;
use TheCodingMachine\GraphQLite\Security\AuthorizationServiceInterface;
use TheCodingMachine\GraphQLite\Security\FailAuthenticationService;
use TheCodingMachine\GraphQLite\Security\FailAuthorizationService;

class GraphQLiteServiceProviderTest extends TestCase
{
Expand All @@ -16,6 +20,8 @@ public function testServiceProvider(): void
new GraphQLiteServiceProvider()]);
$container->set('graphqlite.namespace.types', ['App\\Types']);
$container->set('graphqlite.namespace.controllers', ['App\\Controllers']);
$container->set(AuthenticationServiceInterface::class, function() { return new FailAuthenticationService(); });
$container->set(AuthorizationServiceInterface::class, function() { return new FailAuthorizationService(); });

$schema = $container->get(Schema::class);
$this->assertInstanceOf(Schema::class, $schema);
Expand Down

0 comments on commit 9be51da

Please sign in to comment.