Skip to content

Commit

Permalink
Merge pull request #373 from cakephp/twig
Browse files Browse the repository at this point in the history
Twig support
  • Loading branch information
markstory authored Nov 22, 2017
2 parents 1457d59 + 12bb04b commit 08d775b
Show file tree
Hide file tree
Showing 60 changed files with 1,256 additions and 1,166 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ trim_trailing_whitespace = true
[*.yml]
indent_style = space
indent_size = 2

[*.twig]
insert_final_newline = false
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ php:
- 5.6
- 7.0
- 7.1

sudo: false
- 7.2

cache:
directories:
Expand Down Expand Up @@ -33,9 +32,8 @@ before_install:
- if [[ $DB = 'mysql' ]]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi
- if [[ $DB = 'pgsql' ]]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi

- phpenv rehash
- set +H
- composer update
before_script:
- composer install --prefer-dist --no-interaction

script:
- if [[ $RUN_TESTS = 1 && $TRAVIS_PHP_VERSION != 7.* ]]; then vendor/bin/phpunit; fi
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
},
"require": {
"php": ">=5.6.0",
"cakephp/cakephp": ">=3.5.0,<4.0.0",
"cakephp/plugin-installer": "~1.0"
"cakephp/cakephp": "^3.5",
"cakephp/plugin-installer": "^1.0"
},
"require-dev": {
"cakephp/cakephp-codesniffer": "^3.0",
"phpunit/phpunit": "~5.7 | ~6.0"
"phpunit/phpunit": "^5.7 | ^6.0",
"wyrihaximus/twig-view": "^4.1"
},
"autoload": {
"psr-4": {
Expand All @@ -39,6 +40,5 @@
"Bake\\Test\\App\\": "tests/test_app/App",
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests"
}
},
"minimum-stability": "beta"
}
}
1 change: 1 addition & 0 deletions src/Shell/Task/BakeTemplateTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function getView()
],
'theme' => $theme
];

$view = new BakeView(new Request(), new Response(), null, $viewOptions);
$event = new Event('Bake.initialize', $view);
EventManager::instance()->dispatch($event);
Expand Down
3 changes: 2 additions & 1 deletion src/Shell/Task/PluginTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,13 @@ protected function _generateFiles($pluginName, $path)
do {
$templatesPath = array_shift($paths) . 'Bake/Plugin';
$templatesDir = new Folder($templatesPath);
$templates = $templatesDir->findRecursive('.*\.ctp');
$templates = $templatesDir->findRecursive('.*\.(twig|ctp)');
} while (!$templates);

sort($templates);
foreach ($templates as $template) {
$template = substr($template, strrpos($template, 'Plugin') + 7, -4);
$template = rtrim($template, '.');
$this->_generateFile($template, $root);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%
{#
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
Expand All @@ -9,20 +9,20 @@
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 0.1.0
* @since 2.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
%>
#}
<?php
namespace <%= $namespace %>\Controller\Component;
namespace {{ namespace }}\Controller\Component;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
/**
* <%= $name %> component
* {{ name }} component
*/
class <%= $name %>Component extends Component
class {{ name }}Component extends Component
{
/**
Expand Down
52 changes: 0 additions & 52 deletions src/Template/Bake/Controller/controller.ctp

This file was deleted.

57 changes: 57 additions & 0 deletions src/Template/Bake/Controller/controller.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{#
/**
* Controller bake template file
*
* Allows templating of Controllers generated from bake.
*
* 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 2.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
#}
<?php
namespace {{ namespace }}\Controller{{ prefix }};
use {{ namespace }}\Controller\AppController;
/**
* {{ name }} Controller
*
{% if defaultModel %}
* @property \{{ defaultModel }} ${{ name }}
{% endif %}
{%- for component in components %}
{% set classInfo = Bake.classInfo(component, 'Controller/Component', 'Component') %}
* @property {{ classInfo.fqn }} ${{ classInfo.name }}
{% endfor %}
{%- if 'index' in actions %}
*
* @method \{{ namespace }}\Model\Entity\{{ entityClassName }}[] paginate($object = null, array $settings = [])
{% endif %}
*/
class {{ name }}Controller extends AppController
{
{% set helpers = Bake.arrayProperty('helpers', helpers, {'indent': false})|raw %}
{% if helpers|trim %}
{{- helpers|raw }}
{% endif %}
{%- set components = Bake.arrayProperty('components', components, {'indent': false})|raw %}
{% if components|trim %}
{{- components|raw }}
{% endif %}
{%- for action in actions %}
{%- element 'Controller/' ~ action %}
{% endfor %}
}
52 changes: 0 additions & 52 deletions src/Template/Bake/Element/Controller/add.ctp

This file was deleted.

47 changes: 47 additions & 0 deletions src/Template/Bake/Element/Controller/add.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{#
/**
* 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 2.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
#}
{% set compact = ["'#{singularName}'"] %}

/**
* Add method
*
* @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
*/
public function add()
{
${{ singularName }} = $this->{{ currentModelName }}->newEntity();
if ($this->request->is('post')) {
${{ singularName }} = $this->{{ currentModelName }}->patchEntity(${{ singularName }}, $this->request->getData());
if ($this->{{ currentModelName }}->save(${{ singularName }})) {
$this->Flash->success(__('The {{ singularHumanName|lower }} has been saved.'));

return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The {{ singularHumanName|lower }} could not be saved. Please, try again.'));
}
{% set associations = Bake.aliasExtractor(modelObj, 'BelongsTo') %}
{% set associations = associations|merge(Bake.aliasExtractor(modelObj, 'BelongsToMany')) %}

{%- for assoc in associations %}
{%- set otherName = Bake.getAssociatedTableAlias(modelObj, assoc) %}
{%- set otherPlural = otherName|variable %}
${{ otherPlural }} = $this->{{ currentModelName }}->{{ otherName }}->find('list', ['limit' => 200]);
{{- "\n" }}
{%- set compact = compact|merge(["'#{otherPlural}'"]) %}
{% endfor %}
$this->set(compact({{ compact|join(', ')|raw }}));
$this->set('_serialize', ['{{ singularName }}']);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%
{#
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
Expand All @@ -9,26 +9,26 @@
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 0.1.0
* @since 2.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
%>
#}

/**
* Delete method
*
* @param string|null $id <%= $singularHumanName %> id.
* @param string|null $id {{ singularHumanName }} id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$<%= $singularName %> = $this-><%= $currentModelName %>->get($id);
if ($this-><%= $currentModelName; %>->delete($<%= $singularName %>)) {
$this->Flash->success(__('The <%= strtolower($singularHumanName) %> has been deleted.'));
${{ singularName }} = $this->{{ currentModelName }}->get($id);
if ($this->{{ currentModelName }}->delete(${{ singularName }})) {
$this->Flash->success(__('The {{ singularHumanName|lower }} has been deleted.'));
} else {
$this->Flash->error(__('The <%= strtolower($singularHumanName) %> could not be deleted. Please, try again.'));
$this->Flash->error(__('The {{ singularHumanName|lower }} could not be deleted. Please, try again.'));
}

return $this->redirect(['action' => 'index']);
Expand Down
Loading

0 comments on commit 08d775b

Please sign in to comment.