Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@
"friendsofphp/php-cs-fixer": "^2.4",
"league/html-to-markdown": "^4.0",
"psr/log": "^1.0",
"symfony/config": "^3.3 || ^4.0",
"symfony/console": "^2.7 || ^3.0 || ^4.0",
"symfony/yaml": "^2.7 || ^3.0 || ^4.0",
"symfony/config": "^3.3 || ^4.0",
"twig/twig": "^1.12 || ^2.0"
},
"require-dev": {
"api-platform/core": "^2.0",
"doctrine/orm": "^2.2",
"myclabs/php-enum": "^1.0",
"symfony/doctrine-bridge": "^2.7 || ^3.0 || ^4.0",
"symfony/filesystem": "^2.7 || ^3.0 || ^4.0",
"symfony/serializer": "^2.7 || ^3.0 || ^4.0",
"symfony/validator": "^2.7 || ^3.0 || ^4.0"
},
"suggest": {
"doctrine/collections": "For Doctrine collections",
"doctrine/orm": "For Doctrine annotations",
"myclabs/php-enum": "For enumerations",
"symfony/validator": "For constraint annotations"
},
"bin": ["bin/schema"]
}
2 changes: 1 addition & 1 deletion src/AnnotationGenerator/AbstractAnnotationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected function toPhpType(array $field, bool $adderOrRemover = false): string

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

return sprintf('%s[]', $range);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace ApiPlatform\SchemaGenerator\AnnotationGenerator;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\SchemaGenerator\TypesGenerator;

/**
Expand Down Expand Up @@ -52,6 +54,6 @@ public function generateUses(string $className): array
$subClassOf = $resource->get('rdfs:subClassOf');
$typeIsEnum = $subClassOf && $subClassOf->getUri() === TypesGenerator::SCHEMA_ORG_ENUMERATION;

return $typeIsEnum ? [] : ['ApiPlatform\Core\Annotation\ApiResource', 'ApiPlatform\Core\Annotation\ApiProperty'];
return $typeIsEnum ? [] : [ApiResource::class, ApiProperty::class];
}
}
4 changes: 3 additions & 1 deletion src/AnnotationGenerator/ConstraintAnnotationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace ApiPlatform\SchemaGenerator\AnnotationGenerator;

use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
* Constraint annotation generator.
*
Expand Down Expand Up @@ -90,7 +92,7 @@ public function generateUses(string $className): array

$uses = [];
$uses[] = 'Symfony\Component\Validator\Constraints as Assert';
$uses[] = 'Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity';
$uses[] = UniqueEntity::class;

foreach ($this->classes[$className]['fields'] as $field) {
if ($field['isEnum']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace ApiPlatform\SchemaGenerator\AnnotationGenerator;

use Symfony\Component\Serializer\Annotation\Groups;

/**
* Symfony Serializer Groups annotation generator.
*
Expand Down Expand Up @@ -43,6 +45,6 @@ public function generateFieldAnnotations(string $className, string $fieldName):
*/
public function generateUses(string $className): array
{
return ['Symfony\Component\Serializer\Annotation\Groups'];
return [Groups::class];
}
}
42 changes: 13 additions & 29 deletions src/TypesGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
namespace ApiPlatform\SchemaGenerator;

use ApiPlatform\SchemaGenerator\AnnotationGenerator\AnnotationGeneratorInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use MyCLabs\Enum\Enum;
use PhpCsFixer\Cache\NullCacheManager;
use PhpCsFixer\Differ\NullDiffer;
use PhpCsFixer\Error\ErrorsManager;
Expand All @@ -37,27 +40,6 @@ class TypesGenerator
*/
public const SCHEMA_ORG_ENUMERATION = 'http://schema.org/Enumeration';

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

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

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

/**
* @var string
*/
Expand Down Expand Up @@ -177,8 +159,8 @@ public function generate(array $config): void
$class['isEnum'] = $this->isEnum($type);
if ($class['isEnum']) {
$class['namespace'] = $typeConfig['namespace'] ?? $config['namespaces']['enum'];
$class['parent'] = self::ENUM_EXTENDS;
$class['uses'][] = self::ENUM_USE;
$class['parent'] = 'Enum';
$class['uses'][] = Enum::class;

// Constants
foreach ($this->graphs as $graph) {
Expand Down Expand Up @@ -263,9 +245,9 @@ public function generate(array $config): void

foreach ($class['fields'] as &$field) {
$field['isEnum'] = isset($classes[$field['range']]) && $classes[$field['range']]['isEnum'];
$field['typeHint'] = $this->fieldToTypeHint($field, $classes) ?? false;
$field['typeHint'] = $this->fieldToTypeHint($config, $field, $classes) ?? false;

if ('array' === $field['typeHint']) {
if ($field['isArray']) {
$field['adderRemoverTypeHint'] = $this->fieldToAdderRemoverTypeHint($field, $classes) ?? false;
}
}
Expand Down Expand Up @@ -513,10 +495,10 @@ private function isDatatype(string $type): bool
return in_array($type, ['Boolean', 'DataType', 'Date', 'DateTime', 'Float', 'Integer', 'Number', 'Text', 'Time', 'URL'], true);
}

private function fieldToTypeHint(array $field, array $classes): ?string
private function fieldToTypeHint(array $config, array $field, array $classes): ?string
{
if ($field['isArray']) {
return 'array';
return $config['doctrine']['useCollection'] ? 'Collection' : 'array';
}

return $this->fieldToAdderRemoverTypeHint($field, $classes);
Expand Down Expand Up @@ -649,11 +631,13 @@ private function generateField(array $config, array $class, \EasyRdf_Resource $t
'columnPrefix' => $columnPrefix,
'isId' => false,
];

if ($isArray) {
$class['hasConstructor'] = true;

if (isset($config['doctrine']['useCollection']) && $config['doctrine']['useCollection'] && !in_array(self::DOCTRINE_COLLECTION_USE, $class['uses'], true)) {
$class['uses'][] = self::DOCTRINE_COLLECTION_USE;
if ($config['doctrine']['useCollection'] && !in_array(ArrayCollection::class, $class['uses'], true)) {
$class['uses'][] = ArrayCollection::class;
$class['uses'][] = Collection::class;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion templates/class.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ use {{ use }};
* {{ annotation }}
{% endfor %}
*/
public function get{{ field.name|ucfirst }}(){% if field.typeHint %}: {% if field.isNullable %}?{% endif %}{{ field.typeHint }}{% endif %}
public function get{{ field.name|ucfirst }}(){% if field.typeHint %}: {% if field.isNullable and not field.isArray %}?{% endif %}{{ field.typeHint }}{% endif %}
{
return $this->{{ field.name }};
}
Expand Down
33 changes: 29 additions & 4 deletions tests/Command/GenerateTypesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public function testCommand($output, $config)
public function getArguments()
{
return [
[__DIR__.'/../../build/address-book/', __DIR__.'/../config/address-book.yaml'],
[__DIR__.'/../../build/blog/', __DIR__.'/../config/blog.yaml'],
[__DIR__.'/../../build/ecommerce/', __DIR__.'/../config/ecommerce.yaml'],
[__DIR__.'/../../build/vgo/', __DIR__.'/../config/vgo.yaml'],
Expand All @@ -56,14 +55,39 @@ public function getArguments()
];
}

public function testDoctrineCollection()
{
$outputDir = __DIR__.'/../../build/address-book';
$config = __DIR__.'/../config/address-book.yaml';

$this->fs->mkdir($outputDir);

$commandTester = new CommandTester(new GenerateTypesCommand());
$this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config]));

$person = file_get_contents($outputDir.'/AddressBook/Entity/Person.php');

$this->assertContains('use Doctrine\Common\Collections\ArrayCollection;', $person);
$this->assertContains('use Doctrine\Common\Collections\Collection;', $person);

$this->assertContains(<<<'PHP'
public function getFriends(): Collection
{
return $this->friends;
}
PHP
, $person);
}

public function testFluentMutators()
{
$outputDir = __DIR__.'/../../build/fluent-mutators';
$config = __DIR__.'/../config/fluent-mutators.yaml';
$this->fs->mkdir($outputDir);
$commandTester = new CommandTester(new GenerateTypesCommand());
$this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config]));
$organization = file_get_contents($outputDir.'/AppBundle/Entity/Person.php');
$person = file_get_contents($outputDir.'/AppBundle/Entity/Person.php');

$this->assertContains(<<<'PHP'
public function setUrl(?string $url): self
{
Expand All @@ -72,7 +96,8 @@ public function setUrl(?string $url): self
return $this;
}
PHP
, $organization);
, $person);

$this->assertContains(<<<'PHP'
public function addFriends(Person $friends): self
{
Expand All @@ -88,7 +113,7 @@ public function removeFriends(Person $friends): self
return $this;
}
PHP
, $organization);
, $person);
}

public function testDoNotGenerateAccessorMethods()
Expand Down