Skip to content

Commit 6786471

Browse files
committed
Merge branch '1.0' into 2.3
2 parents 3dc2c5f + 32b02a8 commit 6786471

File tree

6 files changed

+96
-46
lines changed

6 files changed

+96
-46
lines changed

spec/EzSystems/EzPlatformGraphQL/Schema/Domain/Content/ContentDomainIteratorSpec.php

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
<?php
2+
3+
/**
4+
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
27
namespace spec\EzSystems\EzPlatformGraphQL\Schema\Domain\Content;
38

4-
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinitionCollection;
59
use spec\EzSystems\EzPlatformGraphQL\Tools\TypeArgument;
610
use eZ\Publish\API\Repository\ContentTypeService;
711
use eZ\Publish\Core\Repository\Values\ContentType\ContentType;
812
use eZ\Publish\Core\Repository\Values\ContentType\ContentTypeGroup;
913
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition;
14+
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinitionCollection;
1015
use PhpSpec\ObjectBehavior;
1116
use Prophecy\Argument;
1217
use EzSystems\EzPlatformGraphQL\Schema\Domain;
1318
use EzSystems\EzPlatformGraphQL\Schema\Builder;
19+
use Ibexa\GraphQL\Schema\Domain\NameValidator;
1420

1521
class ContentDomainIteratorSpec extends ObjectBehavior
1622
{
17-
public function let(ContentTypeService $contentTypeService)
18-
{
19-
$this->beConstructedWith($contentTypeService);
23+
public function let(
24+
ContentTypeService $contentTypeService,
25+
NameValidator $nameValidator
26+
) {
27+
$this->beConstructedWith($contentTypeService, $nameValidator);
2028
}
2129

22-
function it_is_initializable()
30+
public function it_is_initializable()
2331
{
2432
$this->shouldHaveType(Domain\Iterator::class);
2533
}
2634

27-
function it_initializes_the_schema_with_the_Platform_root_type(Builder $schema)
35+
public function it_initializes_the_schema_with_the_Platform_root_type(Builder $schema)
2836
{
2937
$this->init($schema);
3038

@@ -36,7 +44,7 @@ function it_initializes_the_schema_with_the_Platform_root_type(Builder $schema)
3644
)->shouldHaveBeenCalled();
3745
}
3846

39-
function it_yields_content_type_groups(ContentTypeService $contentTypeService)
47+
public function it_yields_content_type_groups(ContentTypeService $contentTypeService)
4048
{
4149
$contentTypeService->loadContentTypeGroups()->willReturn([
4250
$group1 = new ContentTypeGroup(['identifier' => 'Group 1']),
@@ -54,9 +62,12 @@ function it_yields_content_type_groups(ContentTypeService $contentTypeService)
5462
);
5563
}
5664

57-
function it_yields_content_types_with_their_group_from_a_content_type_group(
58-
ContentTypeService $contentTypeService
65+
public function it_yields_content_types_with_their_group_from_a_content_type_group(
66+
ContentTypeService $contentTypeService,
67+
NameValidator $nameValidator
5968
) {
69+
$nameValidator->isValidName(Argument::any())->willReturn(true);
70+
6071
$contentTypeService->loadContentTypeGroups()->willReturn([
6172
$group = new ContentTypeGroup(['identifier' => 'Group']),
6273
]);
@@ -76,9 +87,12 @@ function it_yields_content_types_with_their_group_from_a_content_type_group(
7687
);
7788
}
7889

79-
function it_yields_fields_definitions_with_their_content_types_and_group_from_a_content_type(
80-
ContentTypeService $contentTypeService
90+
public function it_yields_fields_definitions_with_their_content_types_and_group_from_a_content_type(
91+
ContentTypeService $contentTypeService,
92+
NameValidator $nameValidator
8193
) {
94+
$nameValidator->isValidName(Argument::any())->willReturn(true);
95+
8296
$contentTypeService->loadContentTypeGroups()->willReturn([
8397
$group = new ContentTypeGroup(['identifier' => 'Group']),
8498
]);
@@ -89,7 +103,7 @@ function it_yields_fields_definitions_with_their_content_types_and_group_from_a_
89103
'field1' => $field1 = new FieldDefinition(['identifier' => 'foo']),
90104
'field2' => $field2 = new FieldDefinition(['identifier' => 'bar']),
91105
'field3' => $field3 = new FieldDefinition(['identifier' => 'faz']),
92-
])
106+
]),
93107
]),
94108
]);
95109

@@ -104,12 +118,15 @@ function it_yields_fields_definitions_with_their_content_types_and_group_from_a_
104118
);
105119
}
106120

107-
function it_only_yields_fields_definitions_from_the_current_content_type(
108-
ContentTypeService $contentTypeService
121+
public function it_only_yields_fields_definitions_from_the_current_content_type(
122+
ContentTypeService $contentTypeService,
123+
NameValidator $nameValidator
109124
) {
125+
$nameValidator->isValidName(Argument::any())->willReturn(true);
126+
110127
$contentTypeService->loadContentTypeGroups()->willReturn([
111128
$group = new ContentTypeGroup([
112-
'identifier' => 'group'
129+
'identifier' => 'group',
113130
]),
114131
]);
115132

@@ -118,13 +135,13 @@ function it_only_yields_fields_definitions_from_the_current_content_type(
118135
'identifier' => 'type1',
119136
'fieldDefinitions' => new FieldDefinitionCollection([
120137
'type1_field1' => ($type1field1 = new FieldDefinition(['identifier' => 'foo'])),
121-
])
138+
]),
122139
]),
123140
$type2 = new ContentType([
124141
'identifier' => 'type2',
125142
'fieldDefinitions' => new FieldDefinitionCollection([
126143
'type2_field1' => ($type2field1 = new FieldDefinition(['identifier' => 'bar'])),
127-
])
144+
]),
128145
]),
129146
]);
130147

src/Resources/config/services/schema.yaml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ services:
1919

2020
EzSystems\EzPlatformGraphQL\Schema\Builder\SchemaBuilder:
2121
arguments:
22-
- '@Ibexa\GraphQL\Schema\Domain\NameValidator'
23-
calls:
24-
- method: setLogger
25-
arguments:
26-
- '@logger'
22+
$nameValidator: '@Ibexa\GraphQL\Schema\Domain\NameValidator'
2723

2824
EzSystems\EzPlatformGraphQL\Schema\Domain\Content\Mapper\FieldDefinition\FieldDefinitionMapper:
2925
alias: EzSystems\EzPlatformGraphQL\Schema\Domain\Content\Mapper\FieldDefinition\DefaultFieldDefinitionMapper
@@ -87,9 +83,15 @@ services:
8783

8884
EzSystems\EzPlatformGraphQL\Schema\Domain\Content\NameHelper: ~
8985

90-
Ibexa\GraphQL\Schema\Domain\NameValidator: ~
86+
Ibexa\GraphQL\Schema\Domain\NameValidator:
87+
calls:
88+
- method: setLogger
89+
arguments:
90+
- '@logger'
9191

92-
EzSystems\EzPlatformGraphQL\Schema\Domain\ImageVariationDomain: ~
92+
EzSystems\EzPlatformGraphQL\Schema\Domain\ImageVariationDomain:
93+
arguments:
94+
$nameValidator: '@Ibexa\GraphQL\Schema\Domain\NameValidator'
9395

9496
EzSystems\EzPlatformGraphQL\Schema\Domain\Content\ContentDomainIterator: ~
9597

src/Schema/Builder/SchemaBuilder.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@
88

99
use EzSystems\EzPlatformGraphQL\Schema\Builder as SchemaBuilderInterface;
1010
use Ibexa\GraphQL\Schema\Domain\NameValidator;
11-
use Psr\Log\LoggerAwareInterface;
12-
use Psr\Log\LoggerAwareTrait;
13-
use Psr\Log\NullLogger;
1411

15-
class SchemaBuilder implements SchemaBuilderInterface, LoggerAwareInterface
12+
class SchemaBuilder implements SchemaBuilderInterface
1613
{
17-
use LoggerAwareTrait;
18-
1914
private $schema = [];
2015

2116
/** @var \Ibexa\GraphQL\Schema\Domain\NameValidator */
@@ -24,7 +19,6 @@ class SchemaBuilder implements SchemaBuilderInterface, LoggerAwareInterface
2419
public function __construct(NameValidator $nameValidator)
2520
{
2621
$this->nameValidator = $nameValidator;
27-
$this->logger = new NullLogger();
2822
}
2923

3024
public function getSchema(): array
@@ -35,7 +29,7 @@ public function getSchema(): array
3529
public function addType(Input\Type $typeInput)
3630
{
3731
if (!$this->nameValidator->isValidName($typeInput->name)) {
38-
$this->generateInvalidGraphQLNameWarning($typeInput->type, $typeInput->name);
32+
$this->nameValidator->generateInvalidNameWarning($typeInput->type, $typeInput->name);
3933

4034
return;
4135
}
@@ -65,7 +59,7 @@ public function addType(Input\Type $typeInput)
6559
public function addFieldToType($type, Input\Field $fieldInput)
6660
{
6761
if (!$this->nameValidator->isValidName($fieldInput->name)) {
68-
$this->generateInvalidGraphQLNameWarning($fieldInput->type, $fieldInput->name);
62+
$this->nameValidator->generateInvalidNameWarning($fieldInput->type, $fieldInput->name);
6963

7064
return;
7165
}
@@ -177,12 +171,4 @@ public function hasEnum($enum): bool
177171
{
178172
return $this->hasType($enum);
179173
}
180-
181-
private function generateInvalidGraphQLNameWarning(string $type, string $name): void
182-
{
183-
$message = "Skipping schema generation for %s with identifier '%s' as it stands against GraphQL specification. "
184-
. 'For more details see http://spec.graphql.org/[latest-release]/#sec-Names.';
185-
186-
$this->logger->warning(sprintf($message, $type, $name));
187-
}
188174
}

src/Schema/Domain/Content/ContentDomainIterator.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@
1111
use EzSystems\EzPlatformGraphQL\Schema\Builder\Input;
1212
use EzSystems\EzPlatformGraphQL\Schema\Domain\Iterator;
1313
use Generator;
14+
use Ibexa\GraphQL\Schema\Domain\NameValidator;
1415

1516
class ContentDomainIterator implements Iterator
1617
{
1718
/** @var \eZ\Publish\API\Repository\ContentTypeService */
1819
private $contentTypeService;
1920

20-
public function __construct(ContentTypeService $contentTypeService)
21-
{
21+
/** @var \Ibexa\GraphQL\Schema\Domain\NameValidator */
22+
private $nameValidator;
23+
24+
public function __construct(
25+
ContentTypeService $contentTypeService,
26+
NameValidator $nameValidator
27+
) {
2228
$this->contentTypeService = $contentTypeService;
29+
$this->nameValidator = $nameValidator;
2330
}
2431

2532
public function init(Builder $schema)
@@ -35,6 +42,12 @@ public function iterate(): Generator
3542
yield ['ContentTypeGroup' => $contentTypeGroup];
3643

3744
foreach ($this->contentTypeService->loadContentTypes($contentTypeGroup) as $contentType) {
45+
if (!$this->nameValidator->isValidName($contentType->identifier)) {
46+
$this->nameValidator->generateInvalidNameWarning('Content Type', $contentType->identifier);
47+
48+
continue;
49+
}
50+
3851
yield ['ContentTypeGroup' => $contentTypeGroup]
3952
+ ['ContentType' => $contentType];
4053

src/Schema/Domain/ImageVariationDomain.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use EzSystems\EzPlatformGraphQL\Schema\Builder;
1212
use EzSystems\EzPlatformGraphQL\Schema\Domain;
1313
use Generator;
14+
use Ibexa\GraphQL\Schema\Domain\NameValidator;
1415

1516
/**
1617
* Adds configured image variations to the ImageVariationIdentifier type.
@@ -23,14 +24,26 @@ class ImageVariationDomain implements Domain\Iterator, Schema\Worker
2324
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
2425
private $configResolver;
2526

26-
public function __construct(ConfigResolverInterface $configResolver)
27-
{
27+
/** @var \Ibexa\GraphQL\Schema\Domain\NameValidator */
28+
private $nameValidator;
29+
30+
public function __construct(
31+
ConfigResolverInterface $configResolver,
32+
NameValidator $nameValidator
33+
) {
2834
$this->configResolver = $configResolver;
35+
$this->nameValidator = $nameValidator;
2936
}
3037

3138
public function iterate(): Generator
3239
{
3340
foreach ($this->configResolver->getParameter('image_variations') as $identifier => $variation) {
41+
if (!$this->nameValidator->isValidName($identifier)) {
42+
$this->nameValidator->generateInvalidNameWarning('Image Variation', $identifier);
43+
44+
continue;
45+
}
46+
3447
yield [self::ARG => ['identifier' => $identifier, 'variation' => $variation]];
3548
}
3649
}

src/Schema/Domain/NameValidator.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,34 @@
88

99
namespace Ibexa\GraphQL\Schema\Domain;
1010

11+
use Psr\Log\LoggerAwareInterface;
12+
use Psr\Log\LoggerAwareTrait;
13+
use Psr\Log\NullLogger;
14+
1115
/**
1216
* Validates given name according to GraphQL specification. See http://spec.graphql.org/June2018/#sec-Names.
1317
*/
14-
class NameValidator
18+
class NameValidator implements LoggerAwareInterface
1519
{
20+
use LoggerAwareTrait;
21+
1622
private const NAME_PATTERN = '/^[_a-zA-Z][_a-zA-Z0-9]*$/';
1723

24+
public function __construct()
25+
{
26+
$this->logger = new NullLogger();
27+
}
28+
1829
public function isValidName(string $name): bool
1930
{
2031
return preg_match(self::NAME_PATTERN, $name) === 1;
2132
}
33+
34+
public function generateInvalidNameWarning(string $type, string $name): void
35+
{
36+
$message = "Skipping schema generation for %s with identifier '%s' as it stands against GraphQL specification. "
37+
. 'For more details see http://spec.graphql.org/[latest-release]/#sec-Names.';
38+
39+
$this->logger->warning(sprintf($message, $type, $name));
40+
}
2241
}

0 commit comments

Comments
 (0)