Skip to content

Commit

Permalink
Merge branch 'master' into make-entity-improved
Browse files Browse the repository at this point in the history
* 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
  ...
  • Loading branch information
weaverryan committed Mar 14, 2018
2 parents e54988c + 9db7299 commit 3b65fb6
Show file tree
Hide file tree
Showing 28 changed files with 1,113 additions and 23 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
1.2
===

* New maker command! `make:crud` - thanks to @sadikoff in #113.

* Greatly improved `make:form` command that auto-adds fields if
your form is bound to an entity class - thanks to @sadikoff in #113.

1.1
===

Expand Down
54 changes: 54 additions & 0 deletions src/Doctrine/DoctrineEntityDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MakerBundle\Doctrine;

/**
* @author Sadicov Vladimir <sadikoff@gmail.com>
*
* @internal
*/
final class DoctrineEntityDetails
{
private $repositoryClass;

private $identifier;
private $displayFields;
private $formFields;

public function __construct($repositoryClass, $identifier, $displayFields, $formFields)
{
$this->repositoryClass = $repositoryClass;
$this->identifier = $identifier;
$this->displayFields = $displayFields;
$this->formFields = $formFields;
}

public function getRepositoryClass()
{
return $this->repositoryClass;
}

public function getIdentifier()
{
return $this->identifier;
}

public function getDisplayFields()
{
return $this->displayFields;
}

public function getFormFields()
{
return $this->formFields;
}
}
100 changes: 100 additions & 0 deletions src/Doctrine/DoctrineEntityHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MakerBundle\Doctrine;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;

/**
* @author Sadicov Vladimir <sadikoff@gmail.com>
*
* @internal
*/
final class DoctrineEntityHelper
{
private $metadataFactory;

public function __construct(ManagerRegistry $registry = null)
{
$this->metadataFactory = null !== $registry ? new DoctrineMetadataFactory($registry) : null;
}

private function isDoctrineInstalled(): bool
{
return null !== $this->metadataFactory;
}

public function getEntitiesForAutocomplete(): array
{
$entities = [];

if ($this->isDoctrineInstalled()) {
$allMetadata = $this->metadataFactory->getAllMetadata();
/** @var ClassMetadataInfo $metadata */
foreach ($allMetadata as $metadata) {
$entityClassDetails = new ClassNameDetails($metadata->name, 'App\\Entity');
$entities[] = $entityClassDetails->getRelativeName();
}
}

return $entities;
}

/**
* @param string $entityClassName
*
* @return null|DoctrineEntityDetails
*
* @throws \Exception
*/
public function createDoctrineDetails(string $entityClassName)
{
$metadata = $this->getEntityMetadata($entityClassName);

if (null !== $metadata) {
return new DoctrineEntityDetails(
$metadata->customRepositoryClassName,
$metadata->identifier[0],
$metadata->fieldMappings,
$this->getFormFieldsFromEntity($metadata)
);
}

return null;
}

public function getFormFieldsFromEntity(ClassMetadataInfo $metadata): array
{
$fields = (array) $metadata->fieldNames;
// Remove the primary key field if it's not managed manually
if (!$metadata->isIdentifierNatural()) {
$fields = array_diff($fields, $metadata->identifier);
}
foreach ($metadata->associationMappings as $fieldName => $relation) {
if (ClassMetadataInfo::ONE_TO_MANY !== $relation['type']) {
$fields[] = $fieldName;
}
}

return $fields;
}

public function getEntityMetadata($entityClassName)
{
if (null === $this->metadataFactory) {
throw new \Exception('Somehow the doctrine service is missing. Is DoctrineBundle installed?');
}

return $this->metadataFactory->getMetadataForClass($entityClassName);
}
}
2 changes: 1 addition & 1 deletion src/Doctrine/DoctrineMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function getMappingDriverForClass(string $className): ?MappingDriver
/**
* @return array
*/
private function getAllMetadata()
public function getAllMetadata()
{
$metadata = [];
foreach ($this->registry->getManagers() as $em) {
Expand Down
6 changes: 6 additions & 0 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
class Generator
{
private $fileManager;
private $twigHelper;
private $pendingOperations = [];
private $namespacePrefix;

public function __construct(FileManager $fileManager, $namespacePrefix)
{
$this->fileManager = $fileManager;
$this->twigHelper = new GeneratorTwigHelper($fileManager);
$this->namespacePrefix = rtrim($namespacePrefix, '\\');
}

Expand Down Expand Up @@ -60,6 +62,10 @@ public function generateClass(string $className, string $templateName, array $va
*/
public function generateFile(string $targetPath, string $templateName, array $variables)
{
$variables = array_merge($variables, [
'helper' => $this->twigHelper,
]);

$this->addOperation($targetPath, $templateName, $variables);
}

Expand Down
69 changes: 69 additions & 0 deletions src/GeneratorTwigHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MakerBundle;

/**
* @author Sadicov Vladimir <sadikoff@gmail.com>
*/
final class GeneratorTwigHelper
{
private $fileManager;

public function __construct(FileManager $fileManager)
{
$this->fileManager = $fileManager;
}

public function getEntityFieldPrintCode($entity, $field): string
{
$printCode = $entity.'.'.$field['fieldName'];

switch ($field['type']) {
case 'datetime':
$printCode .= ' ? '.$printCode.'|date(\'Y-m-d H:i:s\') : \'\'';
break;
case 'date':
$printCode .= ' ? '.$printCode.'|date(\'Y-m-d\') : \'\'';
break;
case 'time':
$printCode .= ' ? '.$printCode.'|date(\'H:i:s\') : \'\'';
break;
case 'array':
$printCode .= ' ? '.$printCode.'|join(\', \') : \'\'';
break;
case 'boolean':
$printCode .= ' ? \'Yes\' : \'No\'';
break;
}

return $printCode;
}

public function getHeadPrintCode($title): string
{
if ($this->fileManager->fileExists('templates/base.html.twig')) {
return <<<TWIG
{% extends 'base.html.twig' %}
{% block title %}$title{% endblock %}
TWIG;
}

return <<<HTML
<!DOCTYPE html>
<title>$title</title>
HTML;
}
}
Loading

0 comments on commit 3b65fb6

Please sign in to comment.