Skip to content

Commit

Permalink
Final make:crud command
Browse files Browse the repository at this point in the history
  • Loading branch information
WebBamboo committed Aug 10, 2019
1 parent 6f2116e commit d620c0c
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 63 deletions.
97 changes: 96 additions & 1 deletion Generator/Generator.php
Expand Up @@ -2,9 +2,25 @@
namespace Webbamboo\MaterialDashboard\Generator;

use Symfony\Bundle\MakerBundle\Generator as OriginalGenerator;
use Symfony\Bundle\MakerBundle\FileManager;
use Symfony\Bundle\MakerBundle\GeneratorTwigHelper;
use Symfony\Bundle\MakerBundle\Str;

class Generator extends OriginalGenerator
{
private $fileManager;
private $twigHelper;
private $pendingOperations = [];
private $namespacePrefix;

public function __construct(FileManager $fileManager, string $namespacePrefix){
parent::__construct($fileManager, $namespacePrefix);

$this->fileManager = $fileManager;
$this->twigHelper = new GeneratorTwigHelper($fileManager);
$this->namespacePrefix = trim($namespacePrefix, '\\');
}

private function addOperation(string $targetPath, string $templateName, array $variables)
{
if ($this->fileManager->fileExists($targetPath)) {
Expand All @@ -19,7 +35,6 @@ private function addOperation(string $targetPath, string $templateName, array $v
$templatePath = $templateName;
if (!file_exists($templatePath)) {
$templatePath = __DIR__.'/../Resources/skeleton/'.$templateName;

if (!file_exists($templatePath)) {
throw new \Exception(sprintf('Cannot find template "%s"', $templateName));
}
Expand All @@ -30,4 +45,84 @@ private function addOperation(string $targetPath, string $templateName, array $v
'variables' => $variables,
];
}

/**
* Generate a normal file from a template.
*
* @param string $targetPath
* @param string $templateName
* @param array $variables
*/
public function generateFile(string $targetPath, string $templateName, array $variables)
{
$variables = array_merge($variables, [
'helper' => $this->twigHelper,
]);

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

/**
* Actually writes and file changes that are pending.
*/
public function writeChanges()
{
foreach ($this->pendingOperations as $targetPath => $templateData) {
if (isset($templateData['contents'])) {
$this->fileManager->dumpFile($targetPath, $templateData['contents']);

continue;
}

$this->fileManager->dumpFile(
$targetPath,
$this->getFileContentsForPendingOperation($targetPath, $templateData)
);
}
}

public function getFileContentsForPendingOperation(string $targetPath): string
{
if (!isset($this->pendingOperations[$targetPath])) {
throw new RuntimeCommandException(sprintf('File "%s" is not in the Generator\'s pending operations', $targetPath));
}

$templatePath = $this->pendingOperations[$targetPath]['template'];
$parameters = $this->pendingOperations[$targetPath]['variables'];

$templateParameters = array_merge($parameters, [
'relative_path' => $this->fileManager->relativizePath($targetPath),
]);

return $this->fileManager->parseTemplate($templatePath, $templateParameters);
}

/**
* Generate a new file for a class from a template.
*
* @param string $className The fully-qualified class name
* @param string $templateName Template name in Resources/skeleton to use
* @param array $variables Array of variables to pass to the template
*
* @return string The path where the file will be created
*
* @throws \Exception
*/
public function generateClass(string $className, string $templateName, array $variables = []): string
{
$targetPath = $this->fileManager->getRelativePathForFutureClass($className);

if (null === $targetPath) {
throw new \LogicException(sprintf('Could not determine where to locate the new class "%s", maybe try with a full namespace like "\\My\\Full\\Namespace\\%s"', $className, Str::getShortClassName($className)));
}

$variables = array_merge($variables, [
'class_name' => Str::getShortClassName($className),
'namespace' => Str::getNamespace($className),
]);

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

return $targetPath;
}
}
5 changes: 2 additions & 3 deletions Resources/skeleton/crud/templates/_delete_form.tpl.php
@@ -1,5 +1,4 @@
<form method="post" action="{{ path('<?= $route_name ?>_delete', {'<?= $entity_identifier ?>': <?= $entity_twig_var_singular ?>.<?= $entity_identifier ?>}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<form method="post" action="{{ path('<?= $route_name ?>_delete', {'<?= $entity_identifier ?>': <?= $entity_twig_var_singular ?>.<?= $entity_identifier ?>}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');" id="deleteForm">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ <?= $entity_twig_var_singular ?>.<?= $entity_identifier ?>) }}">
<button class="btn">Delete</button>
</form>
</form>
2 changes: 1 addition & 1 deletion Resources/skeleton/crud/templates/_form.tpl.php
@@ -1,4 +1,4 @@
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_widget(form, { 'attr': {'class': 'form-horizontal'} }) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}
55 changes: 48 additions & 7 deletions Resources/skeleton/crud/templates/edit.tpl.php
@@ -1,11 +1,52 @@
<?= $helper->getHeadPrintCode('Edit '.$entity_class_name) ?>
{% extends '@MaterialDashboard/base.html.twig' %}

{% block body %}
<h1>Edit <?= $entity_class_name ?></h1>

{{ include('<?= $route_name ?>/_form.html.twig', {'button_label': 'Update'}) }}

<a href="{{ path('<?= $route_name ?>_index') }}">back to list</a>
{% block title %}Edit <?= $entity_class_name ?>{% endblock %}

{% block body %}
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12">
<div class="card ">
<div class="card-header card-header-tabs card-header-primary">
<div class="nav-tabs-navigation">
<div class="nav-tabs-wrapper">
<span class="nav-tabs-title">Edit <?= $entity_class_name ?>:</span>
<ul class="nav nav-tabs" data-tabs="tabs">
<li class="nav-item">
<a class="nav-link" href="{{ path('<?= $route_name ?>_index') }}">
<i class="material-icons">reply</i> Back to list
<div class="ripple-container"></div>
<div class="ripple-container"></div>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" id="deleteTrigger">
<i class="material-icons">delete_sweep</i> Delete
<div class="ripple-container"></div>
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="card-body ">
{{ include('<?= $route_name ?>/_form.html.twig', {'button_label': 'Update'}) }}
</div>
</div>
</div>
</div>
</div>
</div>
{{ include('<?= $route_name ?>/_delete_form.html.twig') }}
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script>
$(document).ready(function() {
$("#deleteTrigger").on('click', function() {
$("#deleteForm").submit();
});
});
</script>
{% endblock %}
68 changes: 40 additions & 28 deletions Resources/skeleton/crud/templates/index.tpl.php
@@ -1,35 +1,47 @@
<?= $helper->getHeadPrintCode($entity_class_name.' index'); ?>
{% extends "@MaterialDashboard/base.html.twig" %}

{% block body %}
<h1><?= $entity_class_name ?> index</h1>
{% block meta_title %}<?= $entity_class_name ?> index{% endblock %}

<table class="table">
<thead>
<tr>
{% block body %}
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12">
<div class="card">
<div class="card-header card-header-warning">
<h4 class="card-title"><?= $entity_class_name ?> index <a href="{{ path('<?= $route_name ?>_new') }}" class="btn btn-primary pull-right">Create new</a></h4>
</div>
<div class="card-body table-responsive">
<table class="table table-hover">
<thead class="text-warning">
<tr>
<?php foreach ($entity_fields as $field): ?>
<th><?= ucfirst($field['fieldName']) ?></th>
<th><?= ucfirst($field['fieldName']) ?></th>
<?php endforeach; ?>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for <?= $entity_twig_var_singular ?> in <?= $entity_twig_var_plural ?> %}
<tr>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for <?= $entity_twig_var_singular ?> in <?= $entity_twig_var_plural ?> %}
<tr>
<?php foreach ($entity_fields as $field): ?>
<td>{{ <?= $helper->getEntityFieldPrintCode($entity_twig_var_singular, $field) ?> }}</td>
<td>{{ <?= $helper->getEntityFieldPrintCode($entity_twig_var_singular, $field) ?> }}</td>
<?php endforeach; ?>
<td>
<a href="{{ path('<?= $route_name ?>_show', {'<?= $entity_identifier ?>': <?= $entity_twig_var_singular ?>.<?= $entity_identifier ?>}) }}">show</a>
<a href="{{ path('<?= $route_name ?>_edit', {'<?= $entity_identifier ?>': <?= $entity_twig_var_singular ?>.<?= $entity_identifier ?>}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="<?= (count($entity_fields) + 1) ?>">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>

<a href="{{ path('<?= $route_name ?>_new') }}">Create new</a>
<td>
<a href="{{ path('<?= $route_name ?>_show', {'<?= $entity_identifier ?>': <?= $entity_twig_var_singular ?>.<?= $entity_identifier ?>}) }}" class="btn btn-success">Show</a>
<a href="{{ path('<?= $route_name ?>_edit', {'<?= $entity_identifier ?>': <?= $entity_twig_var_singular ?>.<?= $entity_identifier ?>}) }}" class="btn btn-info">Edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="<?= (count($entity_fields) + 1) ?>">no records found</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
40 changes: 33 additions & 7 deletions Resources/skeleton/crud/templates/new.tpl.php
@@ -1,9 +1,35 @@
<?= $helper->getHeadPrintCode('New '.$entity_class_name) ?>
{% extends '@MaterialDashboard/base.html.twig' %}

{% block body %}
<h1>Create new <?= $entity_class_name ?></h1>

{{ include('<?= $route_name ?>/_form.html.twig') }}
{% block title %}New <?= $entity_class_name ?>{% endblock %}

<a href="{{ path('<?= $route_name ?>_index') }}">back to list</a>
{% endblock %}
{% block body %}
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12">
<div class="card ">
<div class="card-header card-header-tabs card-header-primary">
<div class="nav-tabs-navigation">
<div class="nav-tabs-wrapper">
<span class="nav-tabs-title">Create new <?= $entity_class_name ?>:</span>
<ul class="nav nav-tabs" data-tabs="tabs">
<li class="nav-item">
<a class="nav-link" href="{{ path('<?= $route_name ?>_index') }}">
<i class="material-icons">reply</i> Back to list
<div class="ripple-container"></div>
<div class="ripple-container"></div>
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="card-body ">
{{ include('<?= $route_name ?>/_form.html.twig') }}
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
76 changes: 60 additions & 16 deletions Resources/skeleton/crud/templates/show.tpl.php
@@ -1,22 +1,66 @@
<?= $helper->getHeadPrintCode($entity_class_name) ?>
{% extends "@MaterialDashboard/base.html.twig" %}

{% block body %}
<h1><?= $entity_class_name ?></h1>
{% block meta_title %}Notification{% endblock %}

<table class="table">
<tbody>
{% block body %}
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12">
<div class="card">
<div class="card-header card-header-tabs card-header-primary">
<div class="nav-tabs-navigation">
<div class="nav-tabs-wrapper">
<span class="nav-tabs-title"><?= $entity_class_name ?>:</span>
<ul class="nav nav-tabs" data-tabs="tabs">
<li class="nav-item">
<a class="nav-link" href="{{ path('<?= $route_name ?>_index') }}">
<i class="material-icons">reply</i> Back to list
<div class="ripple-container"></div>
<div class="ripple-container"></div>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ path('<?= $route_name ?>_edit', {'<?= $entity_identifier ?>': <?= $entity_twig_var_singular ?>.<?= $entity_identifier ?>}) }}">
<i class="material-icons">create</i> Edit
<div class="ripple-container"></div>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" id="deleteTrigger">
<i class="material-icons">delete_sweep</i> Delete
<div class="ripple-container"></div>
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="card-body table-responsive">
<table class="table">
<tbody>
<?php foreach ($entity_fields as $field): ?>
<tr>
<th><?= ucfirst($field['fieldName']) ?></th>
<td>{{ <?= $helper->getEntityFieldPrintCode($entity_twig_var_singular, $field) ?> }}</td>
</tr>
<tr>
<th><?= ucfirst($field['fieldName']) ?></th>
<td>{{ <?= $helper->getEntityFieldPrintCode($entity_twig_var_singular, $field) ?> }}</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

<a href="{{ path('<?= $route_name ?>_index') }}">back to list</a>

<a href="{{ path('<?= $route_name ?>_edit', {'<?= $entity_identifier ?>': <?= $entity_twig_var_singular ?>.<?= $entity_identifier ?>}) }}">edit</a>

</table>
</div>
</div>
</div>
</div>
</div>
</div>
{{ include('<?= $route_name ?>/_delete_form.html.twig') }}
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script>
$(document).ready(function() {
$("#deleteTrigger").on('click', function() {
$("#deleteForm").submit();
});
});
</script>
{% endblock %}

0 comments on commit d620c0c

Please sign in to comment.