Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ Thumbs.db
# Composer
/vendor/*
composer.lock

/tmp
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# CakePHP-LoadsysTheme
# Loadsys Bake Theme for CakePHP

[![Latest Version](https://img.shields.io/github/release/loadsys/CakePHP-LoadsysTheme.svg?style=flat-square)](https://github.com/loadsys/CakePHP-LoadsysTheme/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
Expand All @@ -9,7 +9,7 @@
[![Coverage Status](https://coveralls.io/repos/loadsys/CakePHP-LoadsysTheme/badge.svg)](https://coveralls.io/r/loadsys/CakePHP-LoadsysTheme)
-->

CakePHP 3.x bake generation theme that matches Loadsys' code sniffer standards.
A CakePHP 3.x bake generation theme that matches Loadsys' code sniffer standards. It's designed to dovetail with our [CakePHP App Skeleton](https://github.com/loadsys/CakePHP-Skeleton).


## Requirements
Expand All @@ -23,13 +23,15 @@ CakePHP 3.x bake generation theme that matches Loadsys' code sniffer standards.
$ composer require loadsys/cakephp-loadsys-theme:~1.0
````

_This plugin includes Bake in its own composer dependencies, so when using this theme you do not need to include it separately in your projects._


## Usage

* Add this plugin to your application by adding this line to your bootstrap.php

````php
CakePlugin::load('LoadsysTheme');
CakePlugin::load('LoadsysTheme', ['bootstrap' => true, 'routes' => false]);
````

* To use when baking, use the CLI option `--theme LoadsysTheme` like so
Expand Down Expand Up @@ -77,6 +79,11 @@ Please use [GitHub Isuses](https://github.com/loadsys/CakePHP-LoadsysTheme/issue

When developing this plugin, please fork and issue a PR for any new development.

### Running Tests

* `vendor/bin/phpunit --coverage-html=tmp/coverage/`
* `vendor/bin/phpcs`


## License ##

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"require": {
"php": ">=5.4.16",
"cakephp/cakephp": "~3.0",
"cakephp/bake": "~1.1",
"loadsys/loadsys_codesniffer": "~3.0"
},
"require-dev": {
Expand Down
26 changes: 26 additions & 0 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Hook into the normal baking process to install our own Helper(s) and
* modify the view Vars used for any generated file.
*
* @link http://book.cakephp.org/3.0/en/bake/development.html
*/

use Cake\Event\Event;
use Cake\Event\EventManager;

/**
* Default to the LoadsysTheme and install our own BakeHelper.
*/
EventManager::instance()->on('Bake.initialize', function (Event $event) {
$view = $event->subject;

// Use the LoadsysTheme if none was explicitly named.
if (empty($view->theme())) {
$view->theme('LoadsysTheme');
}

// Swap in our overridden BakeHelper class.
$view->helpers()->unload('Bake');
$view->loadHelper('LoadsysTheme.Bake');
});
8 changes: 4 additions & 4 deletions config/routes.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
use Cake\Routing\Router;
/**
* Nothing to do currently.
*/

Router::plugin('LoadsysTheme', function ($routes) {
$routes->fallbacks('InflectedRoute');
});
use Cake\Routing\Router;
17 changes: 17 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<ruleset name="LoadsysTheme">
<description>Import rules from Loadsys standard.</description>

<arg value="sp"/>
<arg name="colors"/>

<!-- Include CakePHP rules (but don't USE them) and Loadsys rules. -->
<rule ref="vendor/cakephp/cakephp-codesniffer/CakePHP/ruleset.xml">
<exclude name="CakePHP"/>
</rule>
<rule ref="vendor/loadsys/loadsys_codesniffer/Loadsys/ruleset.xml" />

<file>./config</file>
<file>./src</file>
<file>./tests</file>
</ruleset>
11 changes: 2 additions & 9 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php"
bootstrap="tests/bootstrap.php"
>
<php>
<ini name="memory_limit" value="-1"/>
Expand All @@ -18,15 +18,8 @@
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<!-- There is NO fixture listener! -->
<listeners>
<listener
class="\Cake\TestSuite\Fixture\FixtureInjector"
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
<arguments>
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
</arguments>
</listener>
</listeners>

<!-- Prevent coverage reports from looking in tests and vendors -->
Expand Down
4 changes: 1 addition & 3 deletions src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@

use App\Controller\AppController as BaseController;

class AppController extends BaseController
{

class AppController extends BaseController {
}
5 changes: 4 additions & 1 deletion src/Template/Bake/Controller/component.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
*/
%>
<?php
/**
* <%= $name %> Component
*/
namespace <%= $namespace %>\Controller\Component;

use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;

/**
* <%= $name %> component
* \<%= $namespace %>\Controller\Component\<%= $name %>
*/
class <%= $name %>Component extends Component {

Expand Down
5 changes: 4 additions & 1 deletion src/Template/Bake/Controller/controller.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ $defaultModel = $name;
$ignoreAssociations = ['Creators', 'Modifiers'];
%>
<?php
/**
* <%= $name %> Controller
*/
namespace <%= $namespace %>\Controller<%= $prefix %>;

use <%= $namespace %>\Controller\AppController;

/**
* <%= $name %> Controller
* \<%= $namespace %>\Controller<%= $prefix %>\<%= $name %>
*
* @property \<%= $namespace %>\Model\Table\<%= $defaultModel %>Table $<%= $defaultModel %>
<%
Expand Down
1 change: 1 addition & 0 deletions src/Template/Bake/Element/Controller/delete.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
} else {
$this->Flash->error(__('The <%= strtolower($singularHumanName) %> could not be deleted. Please, try again.'));
}

return $this->redirect(['action' => 'index']);
}
2 changes: 1 addition & 1 deletion src/Template/Bake/Element/Controller/edit.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ $compact = ["'" . $singularName . "'"];
*/
public function edit($id = null) {
$<%= $singularName %> = $this-><%= $currentModelName %>->get($id, [
'contain' => [<%= $this->Bake->stringifyList($belongsToMany, ['indent' => false]) %>]
'contain' => [<%= $this->Bake->stringifyList($belongsToMany, ['indent' => false]) %>],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$<%= $singularName %> = $this-><%= $currentModelName %>->patchEntity($<%= $singularName %>, $this->request->data);
Expand Down
2 changes: 1 addition & 1 deletion src/Template/Bake/Element/Controller/index.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<% if ($belongsTo):
$belongsTo = array_diff($belongsTo, $ignoreAssociations); %>
$this->paginate = [
'contain' => [<%= $this->Bake->stringifyList($belongsTo, ['indent' => false]) %>]
'contain' => [<%= $this->Bake->stringifyList($belongsTo, ['indent' => false]) %>],
];
<% endif; %>
$this->set('<%= $pluralName %>', $this->paginate($this-><%= $currentModelName %>));
Expand Down
2 changes: 1 addition & 1 deletion src/Template/Bake/Element/Controller/view.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $allAssociations = array_merge(
*/
public function view($id = null) {
$<%= $singularName%> = $this-><%= $currentModelName %>->get($id, [
'contain' => [<%= $this->Bake->stringifyList($allAssociations, ['indent' => false]) %>]
'contain' => [<%= $this->Bake->stringifyList($allAssociations, ['indent' => false]) %>],
]);
$this->set('<%= $singularName %>', $<%= $singularName %>);
$this->set('_serialize', ['<%= $singularName %>']);
Expand Down
12 changes: 12 additions & 0 deletions src/Template/Bake/Element/add-columns.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% foreach ($columns as $columnName => $columnAttributes):
$type = $columnAttributes['type'];
unset($columnAttributes['type']);

$columnAttributes = $this->Migration->getColumnOption($columnAttributes);
$columnAttributes = $this->Migration->stringifyList($columnAttributes, ['indent' => 4]);
if (!empty($columnAttributes)): %>
->addColumn('<%= $columnName %>', '<%= $type %>', [<%= $columnAttributes %>])
<% else: %>
->addColumn('<%= $columnName %>', '<%= $type %>')
<% endif; %>
<% endforeach; %>
3 changes: 3 additions & 0 deletions src/Template/Bake/Element/add-foreign-keys-from-create.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% foreach ($constraints as $table => $tableConstraints):
echo $this->element('LoadsysTheme.add-foreign-keys', ['constraints' => $tableConstraints, 'table' => $table]);
endforeach; %>
44 changes: 44 additions & 0 deletions src/Template/Bake/Element/add-foreign-keys.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<%
$statement = $this->Migration->tableStatement($table, true);
$hasProcessedConstraint = false;
%>
<% foreach ($constraints as $constraint):
$constraintColumns = $constraint['columns'];
sort($constraintColumns);
if ($constraint['type'] !== 'unique'):
$hasProcessedConstraint = true;
$columnsList = '\'' . $constraint['columns'][0] . '\'';
if (count($constraint['columns']) > 1):
$columnsList = '[' . $this->Migration->stringifyList($constraint['columns'], ['indent' => 5]) . ']';
endif;
$this->Migration->returnedData['dropForeignKeys'][$table][] = $columnsList;

if (is_array($constraint['references'][1])):
$columnsReference = '[' . $this->Migration->stringifyList($constraint['references'][1], ['indent' => 5]) . ']';
else:
$columnsReference = '\'' . $constraint['references'][1] . '\'';
endif;

if (!isset($statement)):
$statement = $this->Migration->tableStatement($table);
endif;

if (!empty($statement)): %>

<%= $statement %>
<% unset($statement);
endif; %>
->addForeignKey(
<%= $columnsList %>,
'<%= $constraint['references'][0] %>',
<%= $columnsReference %>,
[
'update' => '<%= strtoupper($constraint['update']) %>',
'delete' => '<%= strtoupper($constraint['delete']) %>'
]
)
<% endif; %>
<% endforeach; %>
<% if (isset($this->Migration->tableStatements[$table]) && $hasProcessedConstraint): %>
->update();
<% endif; %>
9 changes: 9 additions & 0 deletions src/Template/Bake/Element/add-indexes.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% foreach ($indexes as $indexName => $index): %>
->addIndex([<% echo $this->Migration->stringifyList($index['columns'], ['indent' => false]); %>], [<%
$params = ['name' => $indexName];
if ($index['type'] === 'unique'):
$params['unique'] = true;
endif;
echo $this->Migration->stringifyList($params, ['indent' => 4]);
%>])
<% endforeach; %>
32 changes: 16 additions & 16 deletions src/Template/Bake/Element/breadcrumbs.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ $display = "\$$singularVar->{$displayField}";
$keyedActions = ['view', 'edit'];

switch ($action) {
case 'view':
$lastCrumb = "h({$display})";
break;
default:
$humanAction = Inflector::humanize($action);
$lastCrumb = "__('{$humanAction} {$singularHumanName}')";
break;
case 'view':
$lastCrumb = "h({$display})";
break;
default:
$humanAction = Inflector::humanize($action);
$lastCrumb = "__('{$humanAction} {$singularHumanName}')";
break;
}
%>
<?php
$this->set('breadcrumbs', [
__('<%= $pluralHumanName %>') => [
'prefix' => $this->request->params['prefix'],
'controller' => '<%= Inflector::camelize($pluralVar) %>',
'action' => 'index',
],
__('<%= $pluralHumanName %>') => [
'prefix' => $this->request->params['prefix'],
'controller' => '<%= Inflector::camelize($pluralVar) %>',
'action' => 'index',
],
<% if ($action != "index"): %>
<%= $lastCrumb %> => [
'prefix' => $this->request->params['prefix'],
'controller' => '<%= Inflector::camelize($pluralVar) %>',
'action' => '<%= $action %>',
<%= $lastCrumb %> => [
'prefix' => $this->request->params['prefix'],
'controller' => '<%= Inflector::camelize($pluralVar) %>',
'action' => '<%= $action %>',
<%= (in_array($action, $keyedActions) ? "\t\t{$pk},\n" : '') %> ],
<% endif; %>
]);
Expand Down
Loading