Skip to content

Commit 3b65fb6

Browse files
committed
Merge branch 'master' into make-entity-improved
* master: (47 commits) Updating CHANGELOG some improvements fix form variables added validation on arguments fix phpcs fix variables to follow official recommendations some make:form improvements + experiments final fix for skeleton templates making test name less specific so we can run only it Update index.rst phpcs fix some skeleton improvements Fixed CS of generated entity fix cs and improvements according to review request fix make:form completed improvement of make:form command added tests for crud testig fix tests on appveyor php cs fix update all to lastest bundle code ...
2 parents e54988c + 9db7299 commit 3b65fb6

28 files changed

+1113
-23
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
1.2
2+
===
3+
4+
* New maker command! `make:crud` - thanks to @sadikoff in #113.
5+
6+
* Greatly improved `make:form` command that auto-adds fields if
7+
your form is bound to an entity class - thanks to @sadikoff in #113.
8+
19
1.1
210
===
311

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\Doctrine;
13+
14+
/**
15+
* @author Sadicov Vladimir <sadikoff@gmail.com>
16+
*
17+
* @internal
18+
*/
19+
final class DoctrineEntityDetails
20+
{
21+
private $repositoryClass;
22+
23+
private $identifier;
24+
private $displayFields;
25+
private $formFields;
26+
27+
public function __construct($repositoryClass, $identifier, $displayFields, $formFields)
28+
{
29+
$this->repositoryClass = $repositoryClass;
30+
$this->identifier = $identifier;
31+
$this->displayFields = $displayFields;
32+
$this->formFields = $formFields;
33+
}
34+
35+
public function getRepositoryClass()
36+
{
37+
return $this->repositoryClass;
38+
}
39+
40+
public function getIdentifier()
41+
{
42+
return $this->identifier;
43+
}
44+
45+
public function getDisplayFields()
46+
{
47+
return $this->displayFields;
48+
}
49+
50+
public function getFormFields()
51+
{
52+
return $this->formFields;
53+
}
54+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\Doctrine;
13+
14+
use Doctrine\Common\Persistence\ManagerRegistry;
15+
use Doctrine\ORM\Mapping\ClassMetadataInfo;
16+
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
17+
18+
/**
19+
* @author Sadicov Vladimir <sadikoff@gmail.com>
20+
*
21+
* @internal
22+
*/
23+
final class DoctrineEntityHelper
24+
{
25+
private $metadataFactory;
26+
27+
public function __construct(ManagerRegistry $registry = null)
28+
{
29+
$this->metadataFactory = null !== $registry ? new DoctrineMetadataFactory($registry) : null;
30+
}
31+
32+
private function isDoctrineInstalled(): bool
33+
{
34+
return null !== $this->metadataFactory;
35+
}
36+
37+
public function getEntitiesForAutocomplete(): array
38+
{
39+
$entities = [];
40+
41+
if ($this->isDoctrineInstalled()) {
42+
$allMetadata = $this->metadataFactory->getAllMetadata();
43+
/** @var ClassMetadataInfo $metadata */
44+
foreach ($allMetadata as $metadata) {
45+
$entityClassDetails = new ClassNameDetails($metadata->name, 'App\\Entity');
46+
$entities[] = $entityClassDetails->getRelativeName();
47+
}
48+
}
49+
50+
return $entities;
51+
}
52+
53+
/**
54+
* @param string $entityClassName
55+
*
56+
* @return null|DoctrineEntityDetails
57+
*
58+
* @throws \Exception
59+
*/
60+
public function createDoctrineDetails(string $entityClassName)
61+
{
62+
$metadata = $this->getEntityMetadata($entityClassName);
63+
64+
if (null !== $metadata) {
65+
return new DoctrineEntityDetails(
66+
$metadata->customRepositoryClassName,
67+
$metadata->identifier[0],
68+
$metadata->fieldMappings,
69+
$this->getFormFieldsFromEntity($metadata)
70+
);
71+
}
72+
73+
return null;
74+
}
75+
76+
public function getFormFieldsFromEntity(ClassMetadataInfo $metadata): array
77+
{
78+
$fields = (array) $metadata->fieldNames;
79+
// Remove the primary key field if it's not managed manually
80+
if (!$metadata->isIdentifierNatural()) {
81+
$fields = array_diff($fields, $metadata->identifier);
82+
}
83+
foreach ($metadata->associationMappings as $fieldName => $relation) {
84+
if (ClassMetadataInfo::ONE_TO_MANY !== $relation['type']) {
85+
$fields[] = $fieldName;
86+
}
87+
}
88+
89+
return $fields;
90+
}
91+
92+
public function getEntityMetadata($entityClassName)
93+
{
94+
if (null === $this->metadataFactory) {
95+
throw new \Exception('Somehow the doctrine service is missing. Is DoctrineBundle installed?');
96+
}
97+
98+
return $this->metadataFactory->getMetadataForClass($entityClassName);
99+
}
100+
}

src/Doctrine/DoctrineMetadataFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function getMappingDriverForClass(string $className): ?MappingDriver
9999
/**
100100
* @return array
101101
*/
102-
private function getAllMetadata()
102+
public function getAllMetadata()
103103
{
104104
$metadata = [];
105105
foreach ($this->registry->getManagers() as $em) {

src/Generator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
class Generator
2222
{
2323
private $fileManager;
24+
private $twigHelper;
2425
private $pendingOperations = [];
2526
private $namespacePrefix;
2627

2728
public function __construct(FileManager $fileManager, $namespacePrefix)
2829
{
2930
$this->fileManager = $fileManager;
31+
$this->twigHelper = new GeneratorTwigHelper($fileManager);
3032
$this->namespacePrefix = rtrim($namespacePrefix, '\\');
3133
}
3234

@@ -60,6 +62,10 @@ public function generateClass(string $className, string $templateName, array $va
6062
*/
6163
public function generateFile(string $targetPath, string $templateName, array $variables)
6264
{
65+
$variables = array_merge($variables, [
66+
'helper' => $this->twigHelper,
67+
]);
68+
6369
$this->addOperation($targetPath, $templateName, $variables);
6470
}
6571

src/GeneratorTwigHelper.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle;
13+
14+
/**
15+
* @author Sadicov Vladimir <sadikoff@gmail.com>
16+
*/
17+
final class GeneratorTwigHelper
18+
{
19+
private $fileManager;
20+
21+
public function __construct(FileManager $fileManager)
22+
{
23+
$this->fileManager = $fileManager;
24+
}
25+
26+
public function getEntityFieldPrintCode($entity, $field): string
27+
{
28+
$printCode = $entity.'.'.$field['fieldName'];
29+
30+
switch ($field['type']) {
31+
case 'datetime':
32+
$printCode .= ' ? '.$printCode.'|date(\'Y-m-d H:i:s\') : \'\'';
33+
break;
34+
case 'date':
35+
$printCode .= ' ? '.$printCode.'|date(\'Y-m-d\') : \'\'';
36+
break;
37+
case 'time':
38+
$printCode .= ' ? '.$printCode.'|date(\'H:i:s\') : \'\'';
39+
break;
40+
case 'array':
41+
$printCode .= ' ? '.$printCode.'|join(\', \') : \'\'';
42+
break;
43+
case 'boolean':
44+
$printCode .= ' ? \'Yes\' : \'No\'';
45+
break;
46+
}
47+
48+
return $printCode;
49+
}
50+
51+
public function getHeadPrintCode($title): string
52+
{
53+
if ($this->fileManager->fileExists('templates/base.html.twig')) {
54+
return <<<TWIG
55+
{% extends 'base.html.twig' %}
56+
57+
{% block title %}$title{% endblock %}
58+
59+
TWIG;
60+
}
61+
62+
return <<<HTML
63+
<!DOCTYPE html>
64+
65+
<title>$title</title>
66+
67+
HTML;
68+
}
69+
}

0 commit comments

Comments
 (0)