Skip to content

Commit

Permalink
Merge pull request #66 from dunglas/improvements
Browse files Browse the repository at this point in the history
Minor tweaks and fixes
  • Loading branch information
dunglas committed Nov 24, 2016
2 parents 85bd370 + 35f533f commit 6866df7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 1.2.0

* The default config now match the Symfony's and API Platform's Best Practices (namespaces)
* The API Platform's annotation generator is enabled by default
* Use HTTPS to retrieve vocabularies by default
* Properties are generated in the order of the config file
* Properties and constants are separated by an empty line

## 1.1.2

* Fix a bug when generating enumerations
Expand Down
36 changes: 24 additions & 12 deletions src/TypesGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,36 @@ class TypesGenerator
* @see https://github.com/myclabs/php-enum Used enum implementation
*/
const ENUM_USE = 'MyCLabs\Enum\Enum';

/**
* @var string
*
* @see https://github.com/doctrine/collections
*/
const DOCTRINE_COLLECTION_USE = 'Doctrine\Common\Collections\ArrayCollection';

/**
* @var string
*
* @see https://github.com/myclabs/php-enum Used enum implementation
*/
const ENUM_EXTENDS = 'Enum';

/**
* @var string
*/
const SCHEMA_ORG_NAMESPACE = 'http://schema.org/';

/**
* @var string
*/
const SCHEMA_ORG_ENUMERATION = 'http://schema.org/Enumeration';

/**
* @var string
*/
const SCHEMA_ORG_DOMAIN = 'schema:domainIncludes';

/**
* @var string
*/
Expand Down Expand Up @@ -147,7 +153,7 @@ public function generate(array $config)
}

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

// Fields
foreach ($propertiesMap[$type->getUri()] as $property) {
// Ignore properties not set if using a config file
if ($typeConfig['allProperties'] || (is_array($typeConfig['properties']) && array_key_exists($property->localName(), $typeConfig['properties']))) {
$class = $this->generateField($config, $class, $type, $typeName, $property->localName(), $property);
}
}
if (!$typeConfig['allProperties'] && isset($typeConfig['properties']) && is_array($typeConfig['properties'])) {
foreach ($typeConfig['properties'] as $key => $value) {
foreach ($propertiesMap[$type->getUri()] as $property) {
if ($key !== $property->localName()) {
continue;
}

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

$class = $this->generateField($config, $class, $type, $typeName, $propertyName);
// Add custom fields (non schema.org)
$this->logger->info(sprintf('The property "%s" (type "%s") is a custom property.', $key, $type->localName()));
$class = $this->generateField($config, $class, $type, $typeName, $key);
}
} else {
// All properties
foreach ($propertiesMap[$type->getUri()] as $property) {
$class = $this->generateField($config, $class, $type, $typeName, $property->localName(), $property);
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/TypesGeneratorConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/
class TypesGeneratorConfiguration implements ConfigurationInterface
{
const SCHEMA_ORG_RDFA_URL = 'http://schema.org/docs/schema_org_rdfa.html';
const GOOD_RELATIONS_OWL_URL = 'http://purl.org/goodrelations/v1.owl';
const SCHEMA_ORG_RDFA_URL = 'https://schema.org/docs/schema_org_rdfa.html';
const GOOD_RELATIONS_OWL_URL = 'https://purl.org/goodrelations/v1.owl';

/**
* {@inheritdoc}
Expand Down Expand Up @@ -70,9 +70,9 @@ function ($rdfa) {
->addDefaultsIfNotSet()
->info('PHP namespaces')
->children()
->scalarNode('entity')->defaultValue('SchemaOrg\Entity')->info('The namespace of the generated entities')->example('Acme\Entity')->end()
->scalarNode('enum')->defaultValue('SchemaOrg\Enum')->info('The namespace of the generated enumerations')->example('Acme\Enum')->end()
->scalarNode('interface')->defaultValue('SchemaOrg\Model')->info('The namespace of the generated interfaces')->example('Acme\Model')->end()
->scalarNode('entity')->defaultValue('AppBundle\Entity')->info('The namespace of the generated entities')->example('Acme\Entity')->end()
->scalarNode('enum')->defaultValue('AppBundle\Enum')->info('The namespace of the generated enumerations')->example('Acme\Enum')->end()
->scalarNode('interface')->defaultValue('AppBundle\Model')->info('The namespace of the generated interfaces')->example('Acme\Model')->end()
->end()
->end()
->arrayNode('doctrine')
Expand Down Expand Up @@ -161,6 +161,7 @@ function ($rdfa) {
'ApiPlatform\SchemaGenerator\AnnotationGenerator\PhpDocAnnotationGenerator',
'ApiPlatform\SchemaGenerator\AnnotationGenerator\ConstraintAnnotationGenerator',
'ApiPlatform\SchemaGenerator\AnnotationGenerator\DoctrineOrmAnnotationGenerator',
'ApiPlatform\SchemaGenerator\AnnotationGenerator\ApiPlatformCoreAnnotationGenerator',
])
->prototype('scalar')->end()
->end()
Expand Down
2 changes: 2 additions & 0 deletions templates/class.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use {{ use }};
{% endfor %}
*/
const {{ constant.name }} = '{{ constant.value }}';
{% endfor %}
{% for field in class.fields %}
Expand All @@ -38,6 +39,7 @@ use {{ use }};
{% endfor %}
*/
{{ config.fieldVisibility }} ${{ field.name }}{% if field.isArray and (field.isEnum or not field.typeHint or not config.doctrine.useCollection) %} = []{% endif %};
{% endfor %}
{% if config.doctrine.useCollection and class.hasConstructor %}
Expand Down
11 changes: 6 additions & 5 deletions tests/Command/DumpConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testDumpConfiguration()
rdfa:
# RDFa URI to use
uri: 'http://schema.org/docs/schema_org_rdfa.html' # Example: http://schema.org/docs/schema_org_rdfa.html
uri: 'https://schema.org/docs/schema_org_rdfa.html' # Example: https://schema.org/docs/schema_org_rdfa.html
# RDFa URI data format
format: null # Example: rdfxml
Expand All @@ -39,7 +39,7 @@ public function testDumpConfiguration()
relations:
# Default:
- http://purl.org/goodrelations/v1.owl
- https://purl.org/goodrelations/v1.owl
# Debug mode
debug: false
Expand All @@ -60,13 +60,13 @@ public function testDumpConfiguration()
namespaces:
# The namespace of the generated entities
entity: SchemaOrg\Entity # Example: Acme\Entity
entity: AppBundle\Entity # Example: Acme\Entity
# The namespace of the generated enumerations
enum: SchemaOrg\Enum # Example: Acme\Enum
enum: AppBundle\Enum # Example: Acme\Enum
# The namespace of the generated interfaces
interface: SchemaOrg\Model # Example: Acme\Model
interface: AppBundle\Model # Example: Acme\Model
# Doctrine
doctrine:
Expand Down Expand Up @@ -158,6 +158,7 @@ interface: null
- ApiPlatform\SchemaGenerator\AnnotationGenerator\PhpDocAnnotationGenerator
- ApiPlatform\SchemaGenerator\AnnotationGenerator\ConstraintAnnotationGenerator
- ApiPlatform\SchemaGenerator\AnnotationGenerator\DoctrineOrmAnnotationGenerator
- ApiPlatform\SchemaGenerator\AnnotationGenerator\ApiPlatformCoreAnnotationGenerator
YAML
Expand Down
9 changes: 5 additions & 4 deletions tests/config/vgo.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@

annotationGenerators:
- ApiPlatform\SchemaGenerator\AnnotationGenerator\PhpDocAnnotationGenerator
- ApiPlatform\SchemaGenerator\AnnotationGenerator\DoctrineOrmAnnotationGenerator
- ApiPlatform\SchemaGenerator\AnnotationGenerator\ConstraintAnnotationGenerator
- ApiPlatform\SchemaGenerator\AnnotationGenerator\DunglasApiAnnotationGenerator

namespaces:
entity: 'AppBundle\Entity'

types:
Session:
vocabularyNamespace: http://purl.org/net/VideoGameOntology#
Thing: ~

debug: true

rdfa:
-
uri: tests/data/vgo.rdf
format: ~
- tests/data/vgo.rdf
- tests/data/schema.rdfa

0 comments on commit 6866df7

Please sign in to comment.