Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add make:crud & improve make:form commands #113

Merged
merged 52 commits into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
bf9a5c1
make:crud command initial commit
sadikoff Jan 25, 2018
4c0b639
fix fabpot.ci CS
sadikoff Jan 25, 2018
6cdda8f
final fix fabpot.ci CS
sadikoff Jan 25, 2018
e9f91ef
add MakeCrud class to makers.xml
sadikoff Jan 25, 2018
8e9bd67
fix skeleton according to community recommendations
sadikoff Jan 31, 2018
08d4bc5
trying tests
sadikoff Jan 31, 2018
8c98b45
trying tests 2
sadikoff Jan 31, 2018
f189efd
fix tests
sadikoff Jan 31, 2018
9961f43
fix fabpot.io cs
sadikoff Jan 31, 2018
791241d
fix fabpot.io cs 2
sadikoff Jan 31, 2018
478b457
fix fabpot.io cs 3
sadikoff Jan 31, 2018
207d8be
add exception if entity doesn't exists
sadikoff Feb 2, 2018
1b0f17a
test remove some phpdoc comments to pass travis.ci
sadikoff Feb 2, 2018
31d9e81
fix templates generation with date fields in entity
sadikoff Feb 2, 2018
7783313
fix fobpot.io cs
sadikoff Feb 2, 2018
5157e0a
fix interception of /new url by show action
sadikoff Feb 5, 2018
42047a6
fix Token parameter name in delete action
sadikoff Feb 5, 2018
ff9a08d
some fixes
sadikoff Feb 5, 2018
422d31b
Merge branch 'master' of https://github.com/sadikoff/maker-bundle
sadikoff Feb 5, 2018
ab0a0dc
Merge remote-tracking branch 'upstream/master'
sadikoff Feb 9, 2018
96f2af2
Merge remote-tracking branch 'upstream/master'
sadikoff Feb 10, 2018
6f1c296
array style fix
sadikoff Feb 10, 2018
71d2e90
some code refactoring + testing new GeneratorHelper to simplify skele…
sadikoff Feb 11, 2018
3787739
cs fix
sadikoff Feb 11, 2018
7ed6b8b
improved tests
sadikoff Feb 12, 2018
4045695
removed unnecessary code
sadikoff Feb 12, 2018
7555a3a
improved make:form command
sadikoff Feb 13, 2018
e24b22c
fix to use with php 7.0
sadikoff Feb 13, 2018
52a20bb
improve make:crud command to use make:form logic for form generation
sadikoff Feb 13, 2018
1b95509
add csrf dependency it's necessary for delete action in crud and for …
sadikoff Feb 13, 2018
cc4b758
Merge remote-tracking branch 'upstream/master'
sadikoff Feb 21, 2018
404c38f
update all to lastest bundle code
sadikoff Feb 21, 2018
60c0005
php cs fix
sadikoff Feb 22, 2018
e8eabd6
Merge remote-tracking branch 'upstream/master'
sadikoff Feb 22, 2018
ad8a4fb
fix tests on appveyor
sadikoff Feb 22, 2018
db30634
added tests for crud testig
sadikoff Feb 23, 2018
44b7851
completed improvement of make:form command
sadikoff Feb 26, 2018
772f0db
fix make:form
sadikoff Feb 26, 2018
2ebff8c
fix cs and improvements according to review request
sadikoff Feb 27, 2018
da37780
Merge remote-tracking branch 'upstream/master'
sadikoff Feb 28, 2018
3d319bd
some skeleton improvements
sadikoff Feb 28, 2018
ba0438f
phpcs fix
sadikoff Feb 28, 2018
ee98865
making test name less specific so we can run only it
weaverryan Mar 2, 2018
38b2864
Merge remote-tracking branch 'upstream/master'
sadikoff Mar 3, 2018
96d54a7
Merge branch 'master' of https://github.com/sadikoff/maker-bundle
sadikoff Mar 3, 2018
204f5aa
final fix for skeleton templates
sadikoff Mar 3, 2018
b3bf878
some make:form improvements + experiments
sadikoff Mar 6, 2018
f740e2e
fix variables to follow official recommendations
sadikoff Mar 6, 2018
ada7db0
fix phpcs
sadikoff Mar 6, 2018
0d9e637
added validation on arguments
sadikoff Mar 7, 2018
2ec8ff0
fix form variables
sadikoff Mar 9, 2018
2683114
some improvements
sadikoff Mar 12, 2018
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
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);
}
}
93 changes: 93 additions & 0 deletions src/Doctrine/DoctrineMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?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\ClassMetadata;
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;

/**
* Simpler version of DoctrineBundle's DisconnectedMetadataFactory, to
* avoid PSR-4 issues.
*
* @internal
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Ryan Weaver <ryan@knpuniversity.com>
*/
final class DoctrineMetadataFactory
{
private $registry;

/**
* Constructor.
*
* @param ManagerRegistry $registry A ManagerRegistry instance
*/
public function __construct(ManagerRegistry $registry)
{
$this->registry = $registry;
}

/**
* @param string $namespace
*
* @return array|ClassMetadata[]
*/
public function getMetadataForNamespace($namespace)
{
$metadata = [];
foreach ($this->getAllMetadata() as $m) {
if (0 === strpos($m->name, $namespace)) {
$metadata[] = $m;
}
}

return $metadata;
}

/**
* @param string $entity
*
* @return ClassMetadata|null
*/
public function getMetadataForClass(string $entity)
{
foreach ($this->registry->getManagers() as $em) {
$cmf = new DisconnectedClassMetadataFactory();
$cmf->setEntityManager($em);

if (!$cmf->isTransient($entity)) {
return $cmf->getMetadataFor($entity);
}
}

return null;
}

/**
* @return array
*/
public function getAllMetadata()
{
$metadata = [];
foreach ($this->registry->getManagers() as $em) {
$cmf = new DisconnectedClassMetadataFactory();
$cmf->setEntityManager($em);
foreach ($cmf->getAllMetadata() as $m) {
$metadata[] = $m;
}
}

return $metadata;
}
}
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