Skip to content

Commit 204f5aa

Browse files
committed
final fix for skeleton templates
1 parent 96d54a7 commit 204f5aa

File tree

9 files changed

+74
-123
lines changed

9 files changed

+74
-123
lines changed

src/GeneratorTwigHelper.php

+20-16
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,44 @@
1313

1414
/**
1515
* @author Sadicov Vladimir <[email protected]>
16-
*
17-
* @internal
1816
*/
19-
class GeneratorTwigHelper
17+
final class GeneratorTwigHelper
2018
{
21-
private $baseLayoutExists;
19+
private $fileManager;
2220

2321
public function __construct(FileManager $fileManager)
2422
{
25-
$this->baseLayoutExists = $fileManager->fileExists('templates/base.html.twig');
23+
$this->fileManager = $fileManager;
2624
}
2725

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

32-
if (in_array($field['type'], ['datetime'])) {
33-
$printCode = $printCode.' ? '.$printCode.'|date(\'Y-m-d H:i:s\') : \'\'';
34-
} elseif (in_array($field['type'], ['date'])) {
35-
$printCode = $printCode.' ? '.$printCode.'|date(\'Y-m-d\') : \'\'';
36-
} elseif (in_array($field['type'], ['time'])) {
37-
$printCode = $printCode.' ? '.$printCode.'|date(\'H:i:s\') : \'\'';
38-
} elseif (in_array($field['type'], ['array'])) {
39-
$printCode = $printCode.' ? '.$printCode.'|join(\', \') : \'\'';
40-
} elseif (in_array($field['type'], ['boolean'])) {
41-
$printCode = $printCode.' ? \'Yes\' : \'No\'';
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;
4246
}
4347

4448
return $printCode;
4549
}
4650

4751
public function getHeadPrintCode($title): string
4852
{
49-
if ($this->baseLayoutExists) {
53+
if ($this->fileManager->fileExists('templates/base.html.twig')) {
5054
return <<<TWIG
5155
{% extends 'base.html.twig' %}
5256

src/Resources/skeleton/crud/controller/Controller.tpl.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
class <?= $class_name ?> extends Controller
1919
{
2020
/**
21-
* @Route("/", name="<?= $route_name ?>_index", methods="GET"): Response
21+
* @Route("/", name="<?= $route_name ?>_index", methods="GET")
2222
*/
2323
<?php if ($repository_exists): ?>
24-
public function index(<?= $repository_full_class_name ?> $<?= $repository_var ?>)
24+
public function index(<?= $repository_class_name ?> $<?= $repository_var ?>): Response
2525
{
2626
return $this->render('<?= $route_name ?>/index.html.twig', ['<?= $entity_var_plural ?>' => $<?= $repository_var ?>->findAll()]);
2727
}
2828
<?php else: ?>
29-
public function index()
29+
public function index(): Response
3030
{
3131
$<?= $entity_var_plural ?> = $this->getDoctrine()
3232
->getRepository(<?= $entity_class_name ?>::class)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<form method="post" action="{{ path('<?= $route_name ?>_delete', {'<?= $entity_identifier ?>': <?= $entity_var_singular ?>.<?= $entity_identifier ?>}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
22
<input type="hidden" name="_method" value="DELETE">
33
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ <?= $entity_var_singular ?>.<?= $entity_identifier ?>) }}">
4-
<input type="submit" value="Delete">
4+
<button class="btn">Delete</button>
55
</form>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{{ form_start(form) }}
22
{{ form_widget(form) }}
3-
<div>
4-
<button>{{ button_label|default('Save') }}</button>
5-
</div>
3+
<button class="btn">{{ button_label|default('Save') }}</button>
64
{{ form_end(form) }}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
11
<?= $helper->getHeadPrintCode('Edit '.$entity_class_name) ?>
22

3-
{% block stylesheets %}
4-
<style>
5-
.example-wrapper { margin: 1em auto; max-width: 1018px; width: 95%; }
6-
.example-wrapper form div { margin: 1em 0; }
7-
.example-wrapper form label { display: block; }
8-
.example-actions form, .example-actions a { display: inline-block; margin: 0 2px; }
9-
</style>
10-
{% endblock %}
11-
123
{% block body %}
13-
<div class="example-wrapper">
14-
<h1>Edit <?= $entity_class_name ?></h1>
4+
<h1>Edit <?= $entity_class_name ?></h1>
5+
6+
{{ include('<?= $route_name ?>/_form.html.twig', {'button_label': 'Update'}) }}
157

16-
{{ include('<?= $route_name ?>/_form.html.twig', {'form': form, 'button_label': 'Edit'}) }}
8+
<a href="{{ path('<?= $route_name ?>_index') }}">back to list</a>
179

18-
<div class="example-actions">
19-
<a href="{{ path('<?= $route_name ?>_index') }}">back to list</a>
10+
{{ include('<?= $route_name ?>/_delete_form.html.twig') }}
2011

21-
{{ include('<?= $route_name ?>/_delete_form.html.twig', {'<?= $entity_var_singular ?>': <?= $entity_var_singular ?>}) }}
22-
</div>
23-
</div>
2412
{% endblock %}
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,35 @@
11
<?= $helper->getHeadPrintCode($entity_class_name.' index'); ?>
22

3-
{% block stylesheets %}
4-
<style>
5-
.example-wrapper { margin: 1em auto; max-width: 1018px; width: 95%; }
6-
.example-wrapper table { width: 100%; border-collapse: collapse; margin-bottom: 1em; }
7-
.example-wrapper thead th { border-bottom: 1px solid #999; padding: 2px 4px; text-align: left; }
8-
.example-wrapper tbody tr:nth-child(odd) { background-color: #F5F5F5; }
9-
.example-wrapper tbody td { padding: 2px 4px; }
10-
.example-actions form, .example-actions a { display: inline-block; margin: 0 2px; }
11-
</style>
12-
{% endblock %}
13-
143
{% block body %}
15-
<div class="example-wrapper">
16-
<h1><?= $entity_class_name ?> index</h1>
17-
<table>
18-
<thead>
19-
<tr>
4+
<h1><?= $entity_class_name ?> index</h1>
5+
6+
<table class="table">
7+
<thead>
8+
<tr>
209
<?php foreach ($entity_fields as $field): ?>
21-
<th><?= ucfirst($field['fieldName']) ?></th>
10+
<th><?= ucfirst($field['fieldName']) ?></th>
2211
<?php endforeach; ?>
23-
<th>actions</th>
24-
</tr>
25-
</thead>
26-
<tbody>
27-
{% for <?= $entity_var_singular ?> in <?= $entity_var_plural ?> %}
28-
<tr>
12+
<th>actions</th>
13+
</tr>
14+
</thead>
15+
<tbody>
16+
{% for <?= $entity_var_singular ?> in <?= $entity_var_plural ?> %}
17+
<tr>
2918
<?php foreach ($entity_fields as $field): ?>
30-
<td>{{ <?= $helper->getEntityFieldPrintCode($entity_var_singular, $field) ?> }}</td>
19+
<td>{{ <?= $helper->getEntityFieldPrintCode($entity_var_singular, $field) ?> }}</td>
3120
<?php endforeach; ?>
32-
<td>
33-
<a href="{{ path('<?= $route_name ?>_show', {'<?= $entity_identifier ?>': <?= $entity_var_singular ?>.<?= $entity_identifier ?>}) }}">show</a>
34-
<a href="{{ path('<?= $route_name ?>_edit', {'<?= $entity_identifier ?>': <?= $entity_var_singular ?>.<?= $entity_identifier ?>}) }}">edit</a>
35-
</td>
36-
</tr>
37-
{% else %}
38-
<tr>
39-
<td colspan="<?= (count($entity_fields) + 1) ?>">no records found</td>
40-
</tr>
41-
{% endfor %}
42-
</tbody>
43-
</table>
21+
<td>
22+
<a href="{{ path('<?= $route_name ?>_show', {'<?= $entity_identifier ?>': <?= $entity_var_singular ?>.<?= $entity_identifier ?>}) }}">show</a>
23+
<a href="{{ path('<?= $route_name ?>_edit', {'<?= $entity_identifier ?>': <?= $entity_var_singular ?>.<?= $entity_identifier ?>}) }}">edit</a>
24+
</td>
25+
</tr>
26+
{% else %}
27+
<tr>
28+
<td colspan="<?= (count($entity_fields) + 1) ?>">no records found</td>
29+
</tr>
30+
{% endfor %}
31+
</tbody>
32+
</table>
4433

45-
<div class="example-actions">
46-
<a href="{{ path('<?= $route_name ?>_new') }}">Create new</a>
47-
</div>
48-
</div>
34+
<a href="{{ path('<?= $route_name ?>_new') }}">Create new</a>
4935
{% endblock %}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
<?= $helper->getHeadPrintCode('New '.$entity_class_name) ?>
22

3-
{% block stylesheets %}
4-
<style>
5-
.example-wrapper { margin: 1em auto; max-width: 1018px; width: 95%; }
6-
.example-wrapper form div { margin: 1em 0; }
7-
.example-wrapper form label { display: block; }
8-
.example-actions form, .example-actions a { display: inline-block; margin: 0 2px; }
9-
</style>
10-
{% endblock %}
11-
123
{% block body %}
13-
<div class="example-wrapper">
14-
<h1>Create new <?= $entity_class_name ?></h1>
4+
<h1>Create new <?= $entity_class_name ?></h1>
5+
6+
{{ include('<?= $route_name ?>/_form.html.twig') }}
157

16-
{{ include('<?= $route_name ?>/_form.html.twig', {'form': form}) }}
8+
<a href="{{ path('<?= $route_name ?>_index') }}">back to list</a>
179

18-
<div class="example-actions">
19-
<a href="{{ path('<?= $route_name ?>_index') }}">back to list</a>
20-
</div>
21-
</div>
2210
{% endblock %}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
11
<?= $helper->getHeadPrintCode($entity_class_name) ?>
22

3-
{% block stylesheets %}
4-
<style>
5-
.example-wrapper { margin: 1em auto; max-width: 1018px; width: 95%; }
6-
.example-wrapper table { width: 100%; border-collapse: collapse; margin-bottom: 1em; }
7-
.example-wrapper th { border-right: 1px solid #999; padding: 2px 4px; text-align: left; width: 8em; }
8-
.example-wrapper tbody td { padding: 2px 4px; }
9-
.example-actions form, .example-actions a { display: inline-block; margin: 0 2px; }
10-
</style>
11-
{% endblock %}
12-
133
{% block body %}
14-
<div class="example-wrapper">
15-
<h1><?= $entity_class_name ?></h1>
4+
<h1><?= $entity_class_name ?></h1>
165

17-
<table>
18-
<tbody>
6+
<table class="table">
7+
<tbody>
198
<?php foreach ($entity_fields as $field): ?>
20-
<tr>
21-
<th><?= ucfirst($field['fieldName']) ?></th>
22-
<td>{{ <?= $helper->getEntityFieldPrintCode($entity_var_singular, $field) ?> }}</td>
23-
</tr>
9+
<tr>
10+
<th><?= ucfirst($field['fieldName']) ?></th>
11+
<td>{{ <?= $helper->getEntityFieldPrintCode($entity_var_singular, $field) ?> }}</td>
12+
</tr>
2413
<?php endforeach; ?>
25-
</tbody>
26-
</table>
14+
</tbody>
15+
</table>
16+
17+
<a href="{{ path('<?= $route_name ?>_index') }}">back to list</a>
2718

28-
<div class="example-actions">
29-
<a href="{{ path('<?= $route_name ?>_index') }}">back to list</a>
19+
<a href="{{ path('<?= $route_name ?>_edit', {'<?= $entity_identifier ?>': <?= $entity_var_singular ?>.<?= $entity_identifier ?>}) }}">edit</a>
3020

31-
<a href="{{ path('<?= $route_name ?>_edit', {'<?= $entity_identifier ?>': <?= $entity_var_singular ?>.<?= $entity_identifier ?>}) }}">edit</a>
21+
{{ include('<?= $route_name ?>/_delete_form.html.twig') }}
3222

33-
{{ include('<?= $route_name ?>/_delete_form.html.twig', {'<?= $entity_var_singular ?>': <?= $entity_var_singular ?>}) }}
34-
</div>
35-
</div>
3623
{% endblock %}

tests/fixtures/MakeCrud/tests/GeneratedCrudControllerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testIndexAction()
3737
$this->assertContains('Edit SweetFood', $client->getResponse()->getContent());
3838
$this->assertGreaterThan(0, $crawler->filter('input[type=text]')->count());
3939

40-
$editForm = $crawler->selectButton('Edit')->form();
40+
$editForm = $crawler->selectButton('Update')->form();
4141
$client->submit($editForm, ['sweet_food[title]' => 'Candy edited']);
4242
$this->assertTrue($client->getResponse()->isRedirect());
4343

0 commit comments

Comments
 (0)