Skip to content

Commit 847fd3c

Browse files
authored
feat: add resolveTypes and allTypes parameters (#374)
The allTypes parameter can be set on a specific vocabulary to generate all its types on top of the explicit types you have provided. If the allTypes parameter is enabled globally, you can also use it to disable it on a specific vocabulary (but you can explicitly define types for this vocabulary). If resolveTypes is enabled, if a type is present in a vocabulary but not explicitly imported or if the vocabulary is not totally imported (allTypes), it will be generated.
1 parent fd77c34 commit 847fd3c

23 files changed

+422
-217
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
"symfony/serializer": "^5.2 || ^6.0",
6060
"symfony/validator": "^5.2 || ^6.0",
6161
"phpspec/prophecy-phpunit": "^2.0",
62-
"phpstan/phpstan": "^1.2.0"
62+
"phpstan/phpstan": "^1.2.0",
63+
"symfony/finder": "^5.2 || ^6.0"
6364
},
6465
"bin": [
6566
"bin/schema"

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ parameters:
4141
'''
4242
Configuration: '''
4343
array{
44-
vocabularies: array{uri: string, format: string}[],
44+
vocabularies: array{uri: string, format: string, allTypes: ?boolean}[],
4545
vocabularyNamespace: string,
4646
relations: string[],
4747
debug: boolean,
@@ -58,6 +58,7 @@ parameters:
5858
fluentMutatorMethods: boolean,
5959
rangeMapping: array<string, string>,
6060
allTypes: boolean,
61+
resolveTypes: boolean,
6162
types: array<string, TypeConfiguration>,
6263
annotationGenerators: class-string<ApiPlatform\SchemaGenerator\AnnotationGenerator\AnnotationGeneratorInterface>[],
6364
attributeGenerators: class-string<ApiPlatform\SchemaGenerator\AttributeGenerator\AttributeGeneratorInterface>[],

src/CardinalitiesExtractor.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,25 @@ class CardinalitiesExtractor
3232
public const CARDINALITY_N_N = '(*..*)';
3333
public const CARDINALITY_UNKNOWN = 'unknown';
3434

35-
/**
36-
* @var RdfGraph[]
37-
*/
38-
private array $graphs;
3935
private GoodRelationsBridge $goodRelationsBridge;
4036

41-
/**
42-
* @param RdfGraph[] $graphs
43-
*/
44-
public function __construct(array $graphs, GoodRelationsBridge $goodRelationsBridge)
37+
public function __construct(GoodRelationsBridge $goodRelationsBridge)
4538
{
46-
$this->graphs = $graphs;
4739
$this->goodRelationsBridge = $goodRelationsBridge;
4840
}
4941

5042
/**
5143
* Extracts cardinality of properties.
5244
*
45+
* @param RdfGraph[] $graphs
46+
*
5347
* @return array<string, string>
5448
*/
55-
public function extract(): array
49+
public function extract(array $graphs): array
5650
{
5751
$properties = [];
5852

59-
foreach ($this->graphs as $graph) {
53+
foreach ($graphs as $graph) {
6054
foreach (TypesGenerator::$propertyTypes as $propertyType) {
6155
/** @var RdfResource $property */
6256
foreach ($graph->allOfType($propertyType) as $property) {

src/ClassMutator/AnnotationsAppender.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public function __construct(array $classes, array $annotationGenerators, array $
3939
$this->typesToGenerate = $typesToGenerate;
4040
}
4141

42-
public function __invoke(Class_ $class): void
42+
/**
43+
* @param array{} $context
44+
*/
45+
public function __invoke(Class_ $class, array $context): void
4346
{
4447
$this->generateClassUses($class);
4548
$this->generateClassAnnotations($class);

src/ClassMutator/AttributeAppender.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public function __construct(array $classes, array $attributeGenerators)
3434
$this->classes = $classes;
3535
}
3636

37-
public function __invoke(Class_ $class): void
37+
/**
38+
* @param array{} $context
39+
*/
40+
public function __invoke(Class_ $class, array $context): void
3841
{
3942
$this->generateClassUses($class);
4043
$this->generateClassAttributes($class);

src/ClassMutator/ClassIdAppender.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ public function __construct(IdPropertyGeneratorInterface $idPropertyGenerator, a
3131
$this->config = $config;
3232
}
3333

34-
public function __invoke(Class_ $class): void
34+
/**
35+
* @param array{} $context
36+
*/
37+
public function __invoke(Class_ $class, array $context): void
3538
{
3639
if (
3740
$class->isEnum()

src/ClassMutator/ClassInterfaceMutator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ public function __construct(string $desiredNamespace)
2525
$this->desiredNamespace = $desiredNamespace;
2626
}
2727

28-
public function __invoke(Class_ $class): void
28+
/**
29+
* @param array{} $context
30+
*/
31+
public function __invoke(Class_ $class, array $context): void
2932
{
3033
$class->interface = new Interface_(sprintf('%sInterface', $class->name()), $this->desiredNamespace);
3134
}

src/ClassMutator/ClassMutatorInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717

1818
interface ClassMutatorInterface
1919
{
20-
public function __invoke(Class_ $class): void;
20+
// @phpstan-ignore-next-line
21+
public function __invoke(Class_ $class, array $context): void;
2122
}

src/ClassMutator/ClassParentMutator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public function __construct(array $config, PhpTypeConverterInterface $phpTypeCon
3636
$this->config = $config;
3737
}
3838

39-
public function __invoke(Class_ $class): void
39+
/**
40+
* @param array{} $context
41+
*/
42+
public function __invoke(Class_ $class, array $context): void
4043
{
4144
if (!$class instanceof SchemaClass) {
4245
return;
@@ -47,7 +50,7 @@ public function __invoke(Class_ $class): void
4750

4851
if (null === $class->parent() && $subclassOf = $class->getSubClassOf()) {
4952
if (\count($subclassOf) > 1) {
50-
$this->logger ? $this->logger->warning(sprintf('The type "%s" has several supertypes. Using the first one.', $class->rdfType())) : null;
53+
$this->logger ? $this->logger->info(sprintf('The type "%s" has several supertypes. Using the first one.', $class->rdfType())) : null;
5154
}
5255

5356
$class->withParent($this->phpTypeConverter->escapeIdentifier($subclassOf[0]->localName()));

0 commit comments

Comments
 (0)