Skip to content

Commit 685989e

Browse files
committed
Simplify approach.
1 parent 5138033 commit 685989e

9 files changed

Lines changed: 24 additions & 28 deletions

File tree

‎src/Command/ControllerCommand.php‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,15 @@ public function bake(string $controllerName, Arguments $args, ConsoleIo $io): vo
112112

113113
$currentModelName = $controllerName;
114114
$plugin = $this->plugin;
115-
if ($plugin) {
116-
$plugin .= '.';
115+
$pluginPath = $plugin;
116+
if ($pluginPath) {
117+
$pluginPath .= '.';
117118
}
118119

119-
if ($this->getTableLocator()->exists($plugin . $currentModelName)) {
120-
$modelObj = $this->getTableLocator()->get($plugin . $currentModelName);
120+
if ($this->getTableLocator()->exists($pluginPath . $currentModelName)) {
121+
$modelObj = $this->getTableLocator()->get($pluginPath . $currentModelName);
121122
} else {
122-
$modelObj = $this->getTableLocator()->get($plugin . $currentModelName, [
123+
$modelObj = $this->getTableLocator()->get($pluginPath . $currentModelName, [
123124
'connectionName' => $this->connection,
124125
]);
125126
}

‎src/Command/TemplateCommand.php‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,8 @@ public function getContent(Arguments $args, ConsoleIo $io, string $action, ?arra
408408
}
409409
$renderer->set('indexColumns', $indexColumns);
410410

411-
$useDomain = $args->getOption('use-domain') ?? false;
412-
if ($useDomain && !$this->plugin) {
413-
throw new RuntimeException('Cannot set useDomain option when not in a plugin context.');
414-
}
411+
// Always use domain translations when in plugin context
412+
$useDomain = (bool)$this->plugin;
415413
$renderer->set('useDomain', $useDomain);
416414

417415
return $renderer->generate("Bake.Template/$action");
@@ -439,9 +437,6 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
439437
'help' => 'The controller name if you have a controller that does not follow conventions.',
440438
])->addOption('prefix', [
441439
'help' => 'The routing prefix to generate views for.',
442-
])->addOption('use-domain', [
443-
'help' => 'Use __d() instead of __() for translations.',
444-
'boolean' => true,
445440
])->addOption('index-columns', [
446441
'help' => 'Limit for the number of index columns',
447442
'default' => '0',

‎templates/bake/Template/index.twig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
?>
2222
<div class="{{ pluralVar }} index content">
2323
{% set fields = Bake.filterFields(fields, schema, modelObject, indexColumns, ['binary', 'text']) %}
24-
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}{% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}{% endif %}'New {{ singularHumanName }}'), ['action' => 'add'], ['class' => 'button float-right']) ?>
24+
<?= $this->Html->link({% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'New {{ singularHumanName }}'), ['action' => 'add'], ['class' => 'button float-right']) ?>
2525
{% set done = [] %}
2626
<h3><?= {% if useDomain %}__d('{{ plugin }}', {% else %}__({% endif %}'{{ pluralHumanName }}') ?></h3>
2727
<div class="table-responsive">

‎templates/bake/element/Controller/add.twig‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
if ($this->request->is('post')) {
2929
${{ singularName }} = $this->{{ currentModelName }}->patchEntity(${{ singularName }}, $this->request->getData());
3030
if ($this->{{ currentModelName }}->save(${{ singularName }})) {
31-
$this->Flash->success(__('The {{ singularHumanName|lower }} has been saved.'));
31+
$this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} has been saved.'));
3232

3333
return $this->redirect(['action' => 'index']);
3434
}
35-
$this->Flash->error(__('The {{ singularHumanName|lower }} could not be saved. Please, try again.'));
35+
$this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} could not be saved. Please, try again.'));
3636
}
3737
{% set associations = Bake.aliasExtractor(modelObj, 'BelongsTo') %}
3838
{% set associations = associations|merge(Bake.aliasExtractor(modelObj, 'BelongsToMany')) %}

‎templates/bake/element/Controller/delete.twig‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
$this->Authorization->authorize(${{ singularName }});
2929
{% endif %}
3030
if ($this->{{ currentModelName }}->delete(${{ singularName }})) {
31-
$this->Flash->success(__('The {{ singularHumanName|lower }} has been deleted.'));
31+
$this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} has been deleted.'));
3232
} else {
33-
$this->Flash->error(__('The {{ singularHumanName|lower }} could not be deleted. Please, try again.'));
33+
$this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} could not be deleted. Please, try again.'));
3434
}
3535

3636
return $this->redirect(['action' => 'index']);

‎templates/bake/element/Controller/edit.twig‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
if ($this->request->is(['patch', 'post', 'put'])) {
3333
${{ singularName }} = $this->{{ currentModelName }}->patchEntity(${{ singularName }}, $this->request->getData());
3434
if ($this->{{ currentModelName }}->save(${{ singularName }})) {
35-
$this->Flash->success(__('The {{ singularHumanName|lower }} has been saved.'));
35+
$this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} has been saved.'));
3636

3737
return $this->redirect(['action' => 'index']);
3838
}
39-
$this->Flash->error(__('The {{ singularHumanName|lower }} could not be saved. Please, try again.'));
39+
$this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'The {{ singularHumanName|lower }} could not be saved. Please, try again.'));
4040
}
4141
{% for assoc in belongsTo|merge(belongsToMany) %}
4242
{%- set otherName = Bake.getAssociatedTableAlias(modelObj, assoc) %}

‎templates/bake/element/Controller/login.twig‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
$this->request->allowMethod(['get', 'post']);
2828
$result = $this->Authentication->getResult();
2929
if ($result->isValid()) {
30-
$this->Flash->success(__('Login successful'));
30+
$this->Flash->success({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'Login successful'));
3131
$redirect = $this->Authentication->getLoginRedirect();
3232
if ($redirect) {
3333
return $this->redirect($redirect);
@@ -36,6 +36,6 @@
3636

3737
// Display error if user submitted and authentication failed
3838
if ($this->request->is('post')) {
39-
$this->Flash->error(__('Invalid username or password'));
39+
$this->Flash->error({% if plugin %}__d('{{ plugin }}', {% else %}__({% endif %}'Invalid username or password'));
4040
}
4141
}

‎tests/TestCase/Command/TemplateCommandTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ public function testBakePluginTemplatesWithDomain()
674674
$model->belongsTo('Articles');
675675

676676
$this->generatedFile = $path . 'Comments/index.php';
677-
$this->exec('bake template BakeTest.comments index --use-domain');
677+
$this->exec('bake template BakeTest.comments index');
678678

679679
$this->assertExitCode(CommandInterface::CODE_SUCCESS);
680680
$this->assertFileExists($this->generatedFile);

‎tests/comparisons/Controller/testBakeWithPlugin.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public function add()
5050
if ($this->request->is('post')) {
5151
$bakeArticle = $this->BakeArticles->patchEntity($bakeArticle, $this->request->getData());
5252
if ($this->BakeArticles->save($bakeArticle)) {
53-
$this->Flash->success(__('The bake article has been saved.'));
53+
$this->Flash->success(__d('BakeTest', 'The bake article has been saved.'));
5454

5555
return $this->redirect(['action' => 'index']);
5656
}
57-
$this->Flash->error(__('The bake article could not be saved. Please, try again.'));
57+
$this->Flash->error(__d('BakeTest', 'The bake article could not be saved. Please, try again.'));
5858
}
5959
$bakeUsers = $this->BakeArticles->BakeUsers->find('list', limit: 200)->all();
6060
$bakeTags = $this->BakeArticles->BakeTags->find('list', limit: 200)->all();
@@ -74,11 +74,11 @@ public function edit($id = null)
7474
if ($this->request->is(['patch', 'post', 'put'])) {
7575
$bakeArticle = $this->BakeArticles->patchEntity($bakeArticle, $this->request->getData());
7676
if ($this->BakeArticles->save($bakeArticle)) {
77-
$this->Flash->success(__('The bake article has been saved.'));
77+
$this->Flash->success(__d('BakeTest', 'The bake article has been saved.'));
7878

7979
return $this->redirect(['action' => 'index']);
8080
}
81-
$this->Flash->error(__('The bake article could not be saved. Please, try again.'));
81+
$this->Flash->error(__d('BakeTest', 'The bake article could not be saved. Please, try again.'));
8282
}
8383
$bakeUsers = $this->BakeArticles->BakeUsers->find('list', limit: 200)->all();
8484
$bakeTags = $this->BakeArticles->BakeTags->find('list', limit: 200)->all();
@@ -97,9 +97,9 @@ public function delete($id = null)
9797
$this->request->allowMethod(['post', 'delete']);
9898
$bakeArticle = $this->BakeArticles->get($id);
9999
if ($this->BakeArticles->delete($bakeArticle)) {
100-
$this->Flash->success(__('The bake article has been deleted.'));
100+
$this->Flash->success(__d('BakeTest', 'The bake article has been deleted.'));
101101
} else {
102-
$this->Flash->error(__('The bake article could not be deleted. Please, try again.'));
102+
$this->Flash->error(__d('BakeTest', 'The bake article could not be deleted. Please, try again.'));
103103
}
104104

105105
return $this->redirect(['action' => 'index']);

0 commit comments

Comments
 (0)