Skip to content

Commit

Permalink
split actions into seperate files
Browse files Browse the repository at this point in the history
much less unwhieldy like that
  • Loading branch information
AD7six committed Nov 14, 2014
1 parent 2054206 commit 846982d
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 131 deletions.
45 changes: 45 additions & 0 deletions src/Template/Bake/Controller/Action/add.ctp
@@ -0,0 +1,45 @@
<%
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
%>

<% $compact = ["'" . $singularName . "'"]; %>
/**
* Add method
*
* @return void
*/
public function add() {
$<%= $singularName %> = $this-><%= $currentModelName %>->newEntity($this->request->data);
if ($this->request->is('post')) {
if ($this-><%= $currentModelName; %>->save($<%= $singularName %>)) {
$this->Flash->success('The <%= strtolower($singularHumanName) %> has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The <%= strtolower($singularHumanName) %> could not be saved. Please, try again.');
}
}
<%
foreach ($editAssociations as $assoc):
$association = $modelObj->association($assoc);
$otherName = $association->target()->alias();
$otherPlural = $this->_variableName($otherName);
%>
$<%= $otherPlural %> = $this-><%= $currentModelName %>-><%= $otherName %>->find('list');
<%
$compact[] = "'<%= $otherPlural %>'";
endforeach;
%>
$this->set(compact(" <%= join(', ', $compact) %> "));
}
33 changes: 33 additions & 0 deletions src/Template/Bake/Controller/Action/delete.ctp
@@ -0,0 +1,33 @@
<%
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
%>

/**
* Delete method
*
* @param string $id
* @return void
* @throws \Cake\Network\Exception\NotFoundException
*/
public function delete($id = null) {
$<%= $singularName %> = $this-><%= $currentModelName %>->get($id);
$this->request->allowMethod(['post', 'delete']);
if ($this-><%= $currentModelName; %>->delete($<%= $singularName %>)) {
$this->Flash->success('The <%= strtolower($singularHumanName) %> has been deleted.');
} else {
$this->Flash->error('The <%= strtolower($singularHumanName) %> could not be deleted. Please, try again.');
}
return $this->redirect(['action' => 'index']);
}
50 changes: 50 additions & 0 deletions src/Template/Bake/Controller/Action/edit.ctp
@@ -0,0 +1,50 @@
<%
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
%>

<% $compact = ["'" . $singularName . "'"]; %>
/**
* Edit method
*
* @param string $id
* @return void
* @throws \Cake\Network\Exception\NotFoundException
*/
public function edit($id = null) {
$<%= $singularName %> = $this-><%= $currentModelName %>->get($id, [
'contain' => [<%= $this->Class->stringifyList($belongsToMany, ['indent' => 4]) %>]
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$<%= $singularName %> = $this-><%= $currentModelName %>->patchEntity($<%= $singularName %>, $this->request->data);
if ($this-><%= $currentModelName; %>->save($<%= $singularName %>)) {
$this->Flash->success('The <%= strtolower($singularHumanName) %> has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The <%= strtolower($singularHumanName) %> could not be saved. Please, try again.');
}
}
<%
foreach ($editAssociations as $assoc):
$association = $modelObj->association($assoc);
$otherName = $association->target()->alias();
$otherPlural = $this->_variableName($otherName);
%>
$<%= $otherPlural %> = $this-><%= $currentModelName %>-><%= $otherName %>->find('list');
<%
$compact[] = "'<%= $otherPlural %>'";
endforeach;
%>
$this->set(compact(" <%= join(', ', $compact) %> "));
}
29 changes: 29 additions & 0 deletions src/Template/Bake/Controller/Action/index.ctp
@@ -0,0 +1,29 @@
<%
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
%>

/**
* Index method
*
* @return void
*/
public function index() {
<% if ($belongsTo): %>
$this->paginate = [
'contain' => [<%= $this->Class->stringifyList($belongsTo, ['indent' => 4]) %>]
];
<% endif; %>
$this->set('<%= $pluralName %>', $this->paginate($this-><%= $currentModelName %>));
}
29 changes: 29 additions & 0 deletions src/Template/Bake/Controller/Action/view.ctp
@@ -0,0 +1,29 @@
<%
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
%>

/**
* View method
*
* @param string $id
* @return void
* @throws \Cake\Network\Exception\NotFoundException
*/
public function view($id = null) {
$<%= $singularName%> = $this-><%= $currentModelName %>->get($id, [
'contain' => [<%= $this->Class->stringifyList($allAssociations, ['indent' => 4]) %>]
]);
$this->set('<%= $singularName %>', $<%= $singularName %>);
}
136 changes: 5 additions & 131 deletions src/Template/Bake/Controller/actions.ctp
@@ -1,26 +1,6 @@
<%
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 1.3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
$extractor = function ($val) {
return $val->target()->alias();
};
$stringifyList = function ($list) {
$wrapped = array_map(function ($v) {
return "'$v'";
}, $list);
return implode(', ', $wrapped);
};

$belongsTo = array_map($extractor, $modelObj->associations()->type('BelongsTo'));
$belongsToMany = array_map($extractor, $modelObj->associations()->type('BelongsToMany'));
Expand All @@ -33,114 +13,8 @@ $allAssociations = array_merge(
array_map($extractor, $modelObj->associations()->type('HasMany'))
);
%>

/**
* Index method
*
* @return void
*/
public function index() {
<% if ($belongsTo): %>
$this->paginate = [
'contain' => [<%= $stringifyList($belongsTo) %>]
];
<% endif; %>
$this->set('<%= $pluralName %>', $this->paginate($this-><%= $currentModelName %>));
}

/**
* View method
*
* @param string $id
* @return void
* @throws \Cake\Network\Exception\NotFoundException
*/
public function view($id = null) {
$<%= $singularName%> = $this-><%= $currentModelName %>->get($id, [
'contain' => [<%= $stringifyList($allAssociations) %>]
]);
$this->set('<%= $singularName %>', $<%= $singularName %>);
}

<% $compact = ["'" . $singularName . "'"]; %>
/**
* Add method
*
* @return void
*/
public function add() {
$<%= $singularName %> = $this-><%= $currentModelName %>->newEntity($this->request->data);
if ($this->request->is('post')) {
if ($this-><%= $currentModelName; %>->save($<%= $singularName %>)) {
$this->Flash->success('The <%= strtolower($singularHumanName) %> has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The <%= strtolower($singularHumanName) %> could not be saved. Please, try again.');
}
}
<%
foreach ($editAssociations as $assoc):
$association = $modelObj->association($assoc);
$otherName = $association->target()->alias();
$otherPlural = $this->_variableName($otherName);
%>
$<%= $otherPlural %> = $this-><%= $currentModelName %>-><%= $otherName %>->find('list');
<%
$compact[] = "'<%= $otherPlural %>'";
endforeach;
%>
$this->set(compact(" <%= join(', ', $compact) %> "));
}

<% $compact = ["'" . $singularName . "'"]; %>
/**
* Edit method
*
* @param string $id
* @return void
* @throws \Cake\Network\Exception\NotFoundException
*/
public function edit($id = null) {
$<%= $singularName %> = $this-><%= $currentModelName %>->get($id, [
'contain' => [<%= $stringifyList($belongsToMany) %>]
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$<%= $singularName %> = $this-><%= $currentModelName %>->patchEntity($<%= $singularName %>, $this->request->data);
if ($this-><%= $currentModelName; %>->save($<%= $singularName %>)) {
$this->Flash->success('The <%= strtolower($singularHumanName) %> has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The <%= strtolower($singularHumanName) %> could not be saved. Please, try again.');
}
}
<%
foreach ($editAssociations as $assoc):
$association = $modelObj->association($assoc);
$otherName = $association->target()->alias();
$otherPlural = $this->_variableName($otherName);
%>
$<%= $otherPlural %> = $this-><%= $currentModelName %>-><%= $otherName %>->find('list');
<%
$compact[] = "'<%= $otherPlural %>'";
endforeach;
%>
$this->set(compact(" <%= join(', ', $compact) %> "));
}

/**
* Delete method
*
* @param string $id
* @return void
* @throws \Cake\Network\Exception\NotFoundException
*/
public function delete($id = null) {
$<%= $singularName %> = $this-><%= $currentModelName %>->get($id);
$this->request->allowMethod(['post', 'delete']);
if ($this-><%= $currentModelName; %>->delete($<%= $singularName %>)) {
$this->Flash->success('The <%= strtolower($singularHumanName) %> has been deleted.');
} else {
$this->Flash->error('The <%= strtolower($singularHumanName) %> could not be deleted. Please, try again.');
}
return $this->redirect(['action' => 'index']);
}
<%= $this->element('/Controller/Action/index') %>
<%= $this->element('/Controller/Action/view') %>
<%= $this->element('/Controller/Action/add') %>
<%= $this->element('/Controller/Action/edit') %>
<%= $this->element('/Controller/Action/delete') %>
19 changes: 19 additions & 0 deletions src/View/Helper/ClassHelper.php
@@ -1,6 +1,7 @@
<?php
namespace Cake\View\Helper;

use Cake\Core\ConventionsTrait;
use Cake\View\Helper;
use Cake\Utility\Inflector;

Expand All @@ -9,6 +10,8 @@
*/
class ClassHelper extends Helper {

use ConventionsTrait;

/**
* Default configuration.
*
Expand Down Expand Up @@ -41,4 +44,20 @@ public function arrayProperty($name, $value, $params = []) {
return $this->_View->element('array_property', $params);
}

public function stringifyList($list, $options = []) {
$options += [
'indent' => 2,
'callback' => function ($v) {
return "'$v'";
},
];

if (!$list) {
return '';
}

$wrapped = array_map($options['callback'], $list);
$return = implode("\n" . str_repeat("\t", $indent) . ', ', $wrapped) . "\n";
}

}

0 comments on commit 846982d

Please sign in to comment.