Skip to content

Commit

Permalink
ResolveTargetEntity mapping file support. Refactored Doctrine config.
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Dec 29, 2014
1 parent b3bc10f commit 92c6903
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 25 deletions.
51 changes: 32 additions & 19 deletions doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ with Doctrine Resolve Target Entity Listener option).

Example:

```yml
```yaml
namespaces:
entity: "Dunglas\EcommerceBundle\Entity"
enum: "Dunglas\EcommerceBundle\Enum"
Expand All @@ -20,7 +20,7 @@ Namespaces can also be specified for a specific type. It will take precedence ov
Example:
```yml
```yaml
types:
Thing:
namespaces:
Expand All @@ -37,7 +37,7 @@ Schema.org definition).

Example:

```yml
```yaml
types:
Brand:
properties:
Expand All @@ -61,7 +61,7 @@ Cardinalities are enforced by the class generator, the Doctrine ORM generator an

Example:

```yml
```yaml
types:
Product:
properties:
Expand All @@ -75,7 +75,7 @@ Override the guessed class hierarchy of a given type with this option.

Example:

```yml
```yaml
ImageObject:
parent: Thing # Force the parent to be Thing instead of CreativeWork > MediaObject
properties: ~
Expand All @@ -89,7 +89,7 @@ Add a `@author` PHPDoc annotation to class' DocBlock.

Example:

```yml
```yaml
author: "Kévin Dunglas <[email protected]>"
```

Expand All @@ -99,7 +99,7 @@ By default, all generators are enabled. You can specify the list of generators t

Example (enabling only the PHPDoc generator):

```yml
```yaml
annotationGenerators:
- SchemaOrgModel\AnnotationGenerator\PhpDocAnnotationGenerator
```
Expand All @@ -110,7 +110,7 @@ useful when creating your own generators.

Enabling a custom generator and the PHPDoc generator:

```yml
```yaml
annotationGenerators:
- SchemaOrgModel\AnnotationGenerator\PhpDocAnnotationGenerator
- Acme\Generators\MyGenerator
Expand All @@ -122,7 +122,7 @@ By default, the generator add a property called `id` not provided by Schema.org.
with an ORM or an ODM.
This behavior can be disabled with the following setting:

```yml
```yaml
generateId: false
```

Expand All @@ -132,8 +132,9 @@ By default, the generator use classes provided by the [Doctrine Collections](htt
to store collections of entities. This is useful (and required) when using Doctrine ORM or Doctrine ODM.
This behavior can be disabled (to fallback to standard arrays) with the following setting:

```yml
useDoctrineCollection: false
```yaml
doctrine:
useCollection: false
```

## Custom field visibility
Expand All @@ -143,7 +144,7 @@ The default visibility can be changed with the `fieldVisibility` otion.

Example:

```yml
```yaml
fieldVisibility: "protected"
```

Expand All @@ -154,7 +155,7 @@ The standard behavior of the generator is to use the `@MappedSuperclass` Doctrin

The inheritance annotation can be forced for a given type like the following:

```yml
```yaml
types:
Product:
doctrine:
Expand All @@ -172,6 +173,12 @@ mappings.
If you set the option `useInterface` to true, the generator will generate an interface corresponding to each generated
entity and will use them in relation mappings.

To let PHP Schema generating the XML mapping file usable with Symfony add the following to your config file:

```yaml
doctrine:
resolveTargetEntityConfigPath: path/to/doctrine.xml
```

## Custom schemas

Expand All @@ -181,7 +188,7 @@ to generate the PHP data model of your application.

Example:

```yml
```yaml
rdfa:
- https://raw.githubusercontent.com/rvguha/schemaorg/master/data/schema.rdfa # Experimental version of Schema.org
- http://example.com/data/myschema.rfa # Additional types
Expand All @@ -202,7 +209,7 @@ Prepend all generated PHP files with a custom comment.

Example:

```yml
```yaml
header: |
/*
* This file is part of the Ecommerce package.
Expand All @@ -222,7 +229,7 @@ header: |
rdfa:
# Default:
- https://raw.githubusercontent.com/rvguha/schemaorg/master/data/schema.rdfa
- http://schema.org/docs/schema_org_rdfa.html
# OWL relation files to use
relations:
Expand All @@ -239,9 +246,6 @@ generateId: true
# Generate interfaces and use Doctrine's Resolve Target Entity feature
useInterface: false
# Use Doctrine's ArrayCollection instead of standard arrays
useDoctrineCollection: true
# Emit a warning if a property is not derived from GoodRelations
checkIsGoodRelations: false
Expand All @@ -260,6 +264,15 @@ namespaces:
# The namespace of the generated interfaces
interface: SchemaOrg\Model # Example: Acme\Model
# Doctrine
doctrine:
# Use Doctrine's ArrayCollection instead of standard arrays
useCollection: true
# The Resolve Target Entity Listener config file pass
resolveTargetEntityConfigPath: null
# The value of the phpDoc's @author annotation
author: false # Example: Kévin Dunglas <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ protected function toPhpType(array $field, $adderOrRemover = false)
}

if ($field['isArray'] && !$adderOrRemover) {
if ($this->config['useDoctrineCollection']) {
if ($this->config['doctrine']['useCollection']) {
return sprintf('ArrayCollection<%s>', $range);
}

Expand Down
2 changes: 1 addition & 1 deletion src/SchemaOrgModel/Command/GenerateTypesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Yaml\Parser;

/**
* Generate entities command.
Expand Down
9 changes: 6 additions & 3 deletions src/SchemaOrgModel/TypesGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function generate($config)
if ($isArray) {
$class['hasConstructor'] = true;

if ($config['doctrine']['useDoctrineCollection'] && !in_array(self::DOCTRINE_COLLECTION_USE, $class['uses'])) {
if ($config['doctrine']['useCollection'] && !in_array(self::DOCTRINE_COLLECTION_USE, $class['uses'])) {
$class['uses'][] = self::DOCTRINE_COLLECTION_USE;
}
}
Expand Down Expand Up @@ -392,15 +392,18 @@ public function generate($config)
}

if (isset($interfaceMappings) && $config['doctrine']['resolveTargetEntityConfigPath']) {
$dir = dirname($config['doctrine']['resolveTargetEntityConfigPath']);
$file = $config['output'].'/'.$config['doctrine']['resolveTargetEntityConfigPath'];
$dir = dirname($file);
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}

file_put_contents(
$config['doctrine']['resolveTargetEntityConfigPath'],
$file,
$this->twig->render('doctrine.xml.twig', ['mappings' => $interfaceMappings])
);

$generatedFiles[] = $file;
}

$this->fixCs($generatedFiles);
Expand Down
2 changes: 1 addition & 1 deletion templates/class.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use {{ use }};
{{ config.fieldVisibility }} ${{ field.name }}{% if field.isArray and (field.isEnum or not field.typeHint or not config.useDoctrineCollection) %} = []{% endif %};
{% endfor %}
{% if config.doctrine.useDoctrineCollection and class.hasConstructor %}
{% if config.doctrine.useCollection and class.hasConstructor %}
public function __construct()
{
parent::__construct();
Expand Down
13 changes: 13 additions & 0 deletions templates/doctrine.xml.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:doctrine="http://symfony.com/schema/dic/doctrine"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
<doctrine:config>
<doctrine:orm>
{% for interface, class in mappings %}
<doctrine:resolve-target-entity interface="{{ interface }}">{{ class }}</doctrine:resolve-target-entity>
{% endfor %}
</doctrine:orm>
</doctrine:config>
</container>

0 comments on commit 92c6903

Please sign in to comment.