Skip to content

Commit 92c6903

Browse files
committed
ResolveTargetEntity mapping file support. Refactored Doctrine config.
1 parent b3bc10f commit 92c6903

File tree

6 files changed

+54
-25
lines changed

6 files changed

+54
-25
lines changed

doc/configuration.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ with Doctrine Resolve Target Entity Listener option).
99

1010
Example:
1111

12-
```yml
12+
```yaml
1313
namespaces:
1414
entity: "Dunglas\EcommerceBundle\Entity"
1515
enum: "Dunglas\EcommerceBundle\Enum"
@@ -20,7 +20,7 @@ Namespaces can also be specified for a specific type. It will take precedence ov
2020
2121
Example:
2222
23-
```yml
23+
```yaml
2424
types:
2525
Thing:
2626
namespaces:
@@ -37,7 +37,7 @@ Schema.org definition).
3737

3838
Example:
3939

40-
```yml
40+
```yaml
4141
types:
4242
Brand:
4343
properties:
@@ -61,7 +61,7 @@ Cardinalities are enforced by the class generator, the Doctrine ORM generator an
6161

6262
Example:
6363

64-
```yml
64+
```yaml
6565
types:
6666
Product:
6767
properties:
@@ -75,7 +75,7 @@ Override the guessed class hierarchy of a given type with this option.
7575

7676
Example:
7777

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

9090
Example:
9191

92-
```yml
92+
```yaml
9393
author: "Kévin Dunglas <[email protected]>"
9494
```
9595

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

100100
Example (enabling only the PHPDoc generator):
101101

102-
```yml
102+
```yaml
103103
annotationGenerators:
104104
- SchemaOrgModel\AnnotationGenerator\PhpDocAnnotationGenerator
105105
```
@@ -110,7 +110,7 @@ useful when creating your own generators.
110110

111111
Enabling a custom generator and the PHPDoc generator:
112112

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

125-
```yml
125+
```yaml
126126
generateId: false
127127
```
128128

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

135-
```yml
136-
useDoctrineCollection: false
135+
```yaml
136+
doctrine:
137+
useCollection: false
137138
```
138139

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

144145
Example:
145146

146-
```yml
147+
```yaml
147148
fieldVisibility: "protected"
148149
```
149150

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

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

157-
```yml
158+
```yaml
158159
types:
159160
Product:
160161
doctrine:
@@ -172,6 +173,12 @@ mappings.
172173
If you set the option `useInterface` to true, the generator will generate an interface corresponding to each generated
173174
entity and will use them in relation mappings.
174175

176+
To let PHP Schema generating the XML mapping file usable with Symfony add the following to your config file:
177+
178+
```yaml
179+
doctrine:
180+
resolveTargetEntityConfigPath: path/to/doctrine.xml
181+
```
175182

176183
## Custom schemas
177184

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

182189
Example:
183190

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

203210
Example:
204211

205-
```yml
212+
```yaml
206213
header: |
207214
/*
208215
* This file is part of the Ecommerce package.
@@ -222,7 +229,7 @@ header: |
222229
rdfa:
223230
224231
# Default:
225-
- https://raw.githubusercontent.com/rvguha/schemaorg/master/data/schema.rdfa
232+
- http://schema.org/docs/schema_org_rdfa.html
226233
227234
# OWL relation files to use
228235
relations:
@@ -239,9 +246,6 @@ generateId: true
239246
# Generate interfaces and use Doctrine's Resolve Target Entity feature
240247
useInterface: false
241248
242-
# Use Doctrine's ArrayCollection instead of standard arrays
243-
useDoctrineCollection: true
244-
245249
# Emit a warning if a property is not derived from GoodRelations
246250
checkIsGoodRelations: false
247251
@@ -260,6 +264,15 @@ namespaces:
260264
# The namespace of the generated interfaces
261265
interface: SchemaOrg\Model # Example: Acme\Model
262266
267+
# Doctrine
268+
doctrine:
269+
270+
# Use Doctrine's ArrayCollection instead of standard arrays
271+
useCollection: true
272+
273+
# The Resolve Target Entity Listener config file pass
274+
resolveTargetEntityConfigPath: null
275+
263276
# The value of the phpDoc's @author annotation
264277
author: false # Example: Kévin Dunglas <[email protected]>
265278

src/SchemaOrgModel/AnnotationGenerator/AbstractAnnotationGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ protected function toPhpType(array $field, $adderOrRemover = false)
187187
}
188188

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

src/SchemaOrgModel/Command/GenerateTypesCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
use Symfony\Component\Console\Input\InputInterface;
1919
use Symfony\Component\Console\Logger\ConsoleLogger;
2020
use Symfony\Component\Console\Output\OutputInterface;
21-
use Symfony\Component\Yaml\Yaml;
2221
use Symfony\Component\Config\Definition\Processor;
22+
use Symfony\Component\Yaml\Parser;
2323

2424
/**
2525
* Generate entities command.

src/SchemaOrgModel/TypesGenerator.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public function generate($config)
265265
if ($isArray) {
266266
$class['hasConstructor'] = true;
267267

268-
if ($config['doctrine']['useDoctrineCollection'] && !in_array(self::DOCTRINE_COLLECTION_USE, $class['uses'])) {
268+
if ($config['doctrine']['useCollection'] && !in_array(self::DOCTRINE_COLLECTION_USE, $class['uses'])) {
269269
$class['uses'][] = self::DOCTRINE_COLLECTION_USE;
270270
}
271271
}
@@ -392,15 +392,18 @@ public function generate($config)
392392
}
393393

394394
if (isset($interfaceMappings) && $config['doctrine']['resolveTargetEntityConfigPath']) {
395-
$dir = dirname($config['doctrine']['resolveTargetEntityConfigPath']);
395+
$file = $config['output'].'/'.$config['doctrine']['resolveTargetEntityConfigPath'];
396+
$dir = dirname($file);
396397
if (!file_exists($dir)) {
397398
mkdir($dir, 0777, true);
398399
}
399400

400401
file_put_contents(
401-
$config['doctrine']['resolveTargetEntityConfigPath'],
402+
$file,
402403
$this->twig->render('doctrine.xml.twig', ['mappings' => $interfaceMappings])
403404
);
405+
406+
$generatedFiles[] = $file;
404407
}
405408

406409
$this->fixCs($generatedFiles);

templates/class.php.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use {{ use }};
3939
{{ config.fieldVisibility }} ${{ field.name }}{% if field.isArray and (field.isEnum or not field.typeHint or not config.useDoctrineCollection) %} = []{% endif %};
4040
{% endfor %}
4141
42-
{% if config.doctrine.useDoctrineCollection and class.hasConstructor %}
42+
{% if config.doctrine.useCollection and class.hasConstructor %}
4343
public function __construct()
4444
{
4545
parent::__construct();

templates/doctrine.xml.twig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<container xmlns="http://symfony.com/schema/dic/services"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns:doctrine="http://symfony.com/schema/dic/doctrine"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
5+
http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
6+
<doctrine:config>
7+
<doctrine:orm>
8+
{% for interface, class in mappings %}
9+
<doctrine:resolve-target-entity interface="{{ interface }}">{{ class }}</doctrine:resolve-target-entity>
10+
{% endfor %}
11+
</doctrine:orm>
12+
</doctrine:config>
13+
</container>

0 commit comments

Comments
 (0)