Skip to content

Commit 35f533f

Browse files
committed
Minor tweaks and fixes
* Use HTTPS everywhere (fix the generation) * Default config matching the one of the API Platform distribution * Respect the order of properties defined in the config file
1 parent 85bd370 commit 35f533f

File tree

6 files changed

+51
-26
lines changed

6 files changed

+51
-26
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## 1.2.0
4+
5+
* The default config now match the Symfony's and API Platform's Best Practices (namespaces)
6+
* The API Platform's annotation generator is enabled by default
7+
* Use HTTPS to retrieve vocabularies by default
8+
* Properties are generated in the order of the config file
9+
* Properties and constants are separated by an empty line
10+
311
## 1.1.2
412

513
* Fix a bug when generating enumerations

src/TypesGenerator.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,36 @@ class TypesGenerator
3030
* @see https://github.com/myclabs/php-enum Used enum implementation
3131
*/
3232
const ENUM_USE = 'MyCLabs\Enum\Enum';
33+
3334
/**
3435
* @var string
3536
*
3637
* @see https://github.com/doctrine/collections
3738
*/
3839
const DOCTRINE_COLLECTION_USE = 'Doctrine\Common\Collections\ArrayCollection';
40+
3941
/**
4042
* @var string
4143
*
4244
* @see https://github.com/myclabs/php-enum Used enum implementation
4345
*/
4446
const ENUM_EXTENDS = 'Enum';
47+
4548
/**
4649
* @var string
4750
*/
4851
const SCHEMA_ORG_NAMESPACE = 'http://schema.org/';
52+
4953
/**
5054
* @var string
5155
*/
5256
const SCHEMA_ORG_ENUMERATION = 'http://schema.org/Enumeration';
57+
5358
/**
5459
* @var string
5560
*/
5661
const SCHEMA_ORG_DOMAIN = 'schema:domainIncludes';
62+
5763
/**
5864
* @var string
5965
*/
@@ -147,7 +153,7 @@ public function generate(array $config)
147153
}
148154

149155
if ($resource) {
150-
$typesToGenerate[] = $resource;
156+
$typesToGenerate[$typeName] = $resource;
151157
} else {
152158
$this->logger->warning('Type "{typeName}" cannot be found. Using "{guessFrom}" type to generate entity.', ['typeName' => $typeName, 'guessFrom' => $typeConfig['guessFrom']]);
153159
$type = $graph->resource($typeConfig['vocabularyNamespace'].$typeConfig['guessFrom'], 'rdfs:Class');
@@ -226,19 +232,25 @@ public function generate(array $config)
226232
}
227233

228234
// Fields
229-
foreach ($propertiesMap[$type->getUri()] as $property) {
230-
// Ignore properties not set if using a config file
231-
if ($typeConfig['allProperties'] || (is_array($typeConfig['properties']) && array_key_exists($property->localName(), $typeConfig['properties']))) {
232-
$class = $this->generateField($config, $class, $type, $typeName, $property->localName(), $property);
233-
}
234-
}
235+
if (!$typeConfig['allProperties'] && isset($typeConfig['properties']) && is_array($typeConfig['properties'])) {
236+
foreach ($typeConfig['properties'] as $key => $value) {
237+
foreach ($propertiesMap[$type->getUri()] as $property) {
238+
if ($key !== $property->localName()) {
239+
continue;
240+
}
235241

236-
// Add custom fields (non schema.org)
237-
if (isset($typeConfig['properties']) && is_array($typeConfig['properties'])) {
238-
foreach (array_diff_key($typeConfig['properties'], $class['fields']) as $propertyName => $property) {
239-
$this->logger->info(sprintf('The property "%s" (type "%s") is a custom property.', $propertyName, $type->localName()));
242+
$class = $this->generateField($config, $class, $type, $typeName, $property->localName(), $property);
243+
continue 2;
244+
}
240245

241-
$class = $this->generateField($config, $class, $type, $typeName, $propertyName);
246+
// Add custom fields (non schema.org)
247+
$this->logger->info(sprintf('The property "%s" (type "%s") is a custom property.', $key, $type->localName()));
248+
$class = $this->generateField($config, $class, $type, $typeName, $key);
249+
}
250+
} else {
251+
// All properties
252+
foreach ($propertiesMap[$type->getUri()] as $property) {
253+
$class = $this->generateField($config, $class, $type, $typeName, $property->localName(), $property);
242254
}
243255
}
244256

src/TypesGeneratorConfiguration.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
*/
2222
class TypesGeneratorConfiguration implements ConfigurationInterface
2323
{
24-
const SCHEMA_ORG_RDFA_URL = 'http://schema.org/docs/schema_org_rdfa.html';
25-
const GOOD_RELATIONS_OWL_URL = 'http://purl.org/goodrelations/v1.owl';
24+
const SCHEMA_ORG_RDFA_URL = 'https://schema.org/docs/schema_org_rdfa.html';
25+
const GOOD_RELATIONS_OWL_URL = 'https://purl.org/goodrelations/v1.owl';
2626

2727
/**
2828
* {@inheritdoc}
@@ -70,9 +70,9 @@ function ($rdfa) {
7070
->addDefaultsIfNotSet()
7171
->info('PHP namespaces')
7272
->children()
73-
->scalarNode('entity')->defaultValue('SchemaOrg\Entity')->info('The namespace of the generated entities')->example('Acme\Entity')->end()
74-
->scalarNode('enum')->defaultValue('SchemaOrg\Enum')->info('The namespace of the generated enumerations')->example('Acme\Enum')->end()
75-
->scalarNode('interface')->defaultValue('SchemaOrg\Model')->info('The namespace of the generated interfaces')->example('Acme\Model')->end()
73+
->scalarNode('entity')->defaultValue('AppBundle\Entity')->info('The namespace of the generated entities')->example('Acme\Entity')->end()
74+
->scalarNode('enum')->defaultValue('AppBundle\Enum')->info('The namespace of the generated enumerations')->example('Acme\Enum')->end()
75+
->scalarNode('interface')->defaultValue('AppBundle\Model')->info('The namespace of the generated interfaces')->example('Acme\Model')->end()
7676
->end()
7777
->end()
7878
->arrayNode('doctrine')
@@ -161,6 +161,7 @@ function ($rdfa) {
161161
'ApiPlatform\SchemaGenerator\AnnotationGenerator\PhpDocAnnotationGenerator',
162162
'ApiPlatform\SchemaGenerator\AnnotationGenerator\ConstraintAnnotationGenerator',
163163
'ApiPlatform\SchemaGenerator\AnnotationGenerator\DoctrineOrmAnnotationGenerator',
164+
'ApiPlatform\SchemaGenerator\AnnotationGenerator\ApiPlatformCoreAnnotationGenerator',
164165
])
165166
->prototype('scalar')->end()
166167
->end()

templates/class.php.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use {{ use }};
2929
{% endfor %}
3030
*/
3131
const {{ constant.name }} = '{{ constant.value }}';
32+
3233
{% endfor %}
3334
3435
{% for field in class.fields %}
@@ -38,6 +39,7 @@ use {{ use }};
3839
{% endfor %}
3940
*/
4041
{{ config.fieldVisibility }} ${{ field.name }}{% if field.isArray and (field.isEnum or not field.typeHint or not config.doctrine.useCollection) %} = []{% endif %};
42+
4143
{% endfor %}
4244
4345
{% if config.doctrine.useCollection and class.hasConstructor %}

tests/Command/DumpConfigurationTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testDumpConfiguration()
3030
rdfa:
3131
3232
# RDFa URI to use
33-
uri: 'http://schema.org/docs/schema_org_rdfa.html' # Example: http://schema.org/docs/schema_org_rdfa.html
33+
uri: 'https://schema.org/docs/schema_org_rdfa.html' # Example: https://schema.org/docs/schema_org_rdfa.html
3434
3535
# RDFa URI data format
3636
format: null # Example: rdfxml
@@ -39,7 +39,7 @@ public function testDumpConfiguration()
3939
relations:
4040
4141
# Default:
42-
- http://purl.org/goodrelations/v1.owl
42+
- https://purl.org/goodrelations/v1.owl
4343
4444
# Debug mode
4545
debug: false
@@ -60,13 +60,13 @@ public function testDumpConfiguration()
6060
namespaces:
6161
6262
# The namespace of the generated entities
63-
entity: SchemaOrg\Entity # Example: Acme\Entity
63+
entity: AppBundle\Entity # Example: Acme\Entity
6464
6565
# The namespace of the generated enumerations
66-
enum: SchemaOrg\Enum # Example: Acme\Enum
66+
enum: AppBundle\Enum # Example: Acme\Enum
6767
6868
# The namespace of the generated interfaces
69-
interface: SchemaOrg\Model # Example: Acme\Model
69+
interface: AppBundle\Model # Example: Acme\Model
7070
7171
# Doctrine
7272
doctrine:
@@ -158,6 +158,7 @@ interface: null
158158
- ApiPlatform\SchemaGenerator\AnnotationGenerator\PhpDocAnnotationGenerator
159159
- ApiPlatform\SchemaGenerator\AnnotationGenerator\ConstraintAnnotationGenerator
160160
- ApiPlatform\SchemaGenerator\AnnotationGenerator\DoctrineOrmAnnotationGenerator
161+
- ApiPlatform\SchemaGenerator\AnnotationGenerator\ApiPlatformCoreAnnotationGenerator
161162
162163
163164
YAML

tests/config/vgo.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
21
annotationGenerators:
32
- ApiPlatform\SchemaGenerator\AnnotationGenerator\PhpDocAnnotationGenerator
43
- ApiPlatform\SchemaGenerator\AnnotationGenerator\DoctrineOrmAnnotationGenerator
54
- ApiPlatform\SchemaGenerator\AnnotationGenerator\ConstraintAnnotationGenerator
65
- ApiPlatform\SchemaGenerator\AnnotationGenerator\DunglasApiAnnotationGenerator
6+
77
namespaces:
88
entity: 'AppBundle\Entity'
9+
910
types:
1011
Session:
1112
vocabularyNamespace: http://purl.org/net/VideoGameOntology#
1213
Thing: ~
14+
1315
debug: true
16+
1417
rdfa:
15-
-
16-
uri: tests/data/vgo.rdf
17-
format: ~
18+
- tests/data/vgo.rdf
1819
- tests/data/schema.rdfa

0 commit comments

Comments
 (0)