Skip to content

Commit

Permalink
Merge pull request #701 from FriendsOfCake/cake-5
Browse files Browse the repository at this point in the history
Cake 5
  • Loading branch information
ADmad authored Sep 28, 2023
2 parents 39a7cff + 38e9957 commit 7b2f796
Show file tree
Hide file tree
Showing 87 changed files with 917 additions and 1,368 deletions.
101 changes: 14 additions & 87 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,94 +1,21 @@
name: CI

on: [push, pull_request]
on:
push:
branches:
- master
pull_request:
branches:
- '*'

permissions:
contents: read

jobs:
testsuite:
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
php-version: ['7.4', '8.0', '8.1', '8.2']
db-type: [sqlite, mysql, pgsql]
prefer-lowest: ['']
include:
- php-version: '7.2'
db-type: 'sqlite'
prefer-lowest: 'prefer-lowest'

services:
postgres:
image: postgres
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: postgres

steps:
- uses: actions/checkout@v3

- name: Setup Service
if: matrix.db-type == 'mysql'
run: |
sudo service mysql start
mysql -h 127.0.0.1 -u root -proot -e 'CREATE DATABASE cakephp;'
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl, pdo_${{ matrix.db-type }}
coverage: pcov

- name: Composer install
run: |
if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then
composer update --prefer-lowest --prefer-stable
else
composer update
fi
- name: Run PHPUnit
run: |
if [[ ${{ matrix.db-type }} == 'sqlite' ]]; then export DB_URL='sqlite:///:memory:'; fi
if [[ ${{ matrix.db-type }} == 'mysql' ]]; then export DB_URL='mysql://root:[email protected]/cakephp'; fi
if [[ ${{ matrix.db-type }} == 'pgsql' ]]; then export DB_URL='postgres://postgres:[email protected]/postgres'; fi
if [[ ${{ matrix.php-version }} == '7.4' && ${{ matrix.db-type }} == 'sqlite' ]]; then
vendor/bin/phpunit --coverage-clover=coverage.xml
else
vendor/bin/phpunit
fi
- name: Code Coverage Report
if: success() && matrix.php-version == '7.4' && matrix.db-type == 'sqlite'
uses: codecov/codecov-action@v2
uses: ADmad/.github/.github/workflows/testsuite-with-db.yml@master
secrets: inherit

cs-stan:
name: Coding Standard & Static Analysis
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: mbstring, intl
coverage: none
tools: vimeo/psalm:4.23, phpstan:1.9, cs2pr

- name: Composer Install
run: composer require --dev cakephp/cakephp-codesniffer:^4.1

- name: Run phpcs
run: vendor/bin/phpcs --report=checkstyle --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/ | cs2pr

- name: Run psalm
if: always()
run: psalm --output-format=github

- name: Run phpstan
if: always()
run: phpstan analyse --error-format=github
uses: ADmad/.github/.github/workflows/cs-stan.yml@master
secrets: inherit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ phpunit.xml
vendor/
composer.lock
tmp
.phpunit.cache
.phpunit.result.cache
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
}
],
"require": {
"cakephp/cakephp": "^4.0"
"cakephp/cakephp": "^5.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5.23 || ^9.3",
"friendsofcake/cakephp-test-utilities": "^2.0.1",
"friendsofcake/search": "^6.0"
"friendsofcake/cakephp-test-utilities": "^3.0",
"friendsofcake/search": "^7.0",
"phpunit/phpunit": "^10.1"
},
"autoload": {
"psr-4": {
Expand Down
31 changes: 20 additions & 11 deletions docs/_code/action_index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,36 @@

class Index extends BaseAction
{

/**
* Generic handler for all HTTP verbs
*
* @return void
* @return \Cake\Http\Response|null
*/
protected function _handle()
protected function _handle(): ?Response
{
$subject = $this->_subject();
$subject->set(['success' => true, 'viewVar' => $this->viewVar()]);
[$finder, $options] = $this->_extractFinder();
$query = $this->_model()->find($finder, ...$options);
$subject = $this->_subject(['success' => true, 'query' => $query]);

$this->_trigger('beforePaginate', $subject);
try {
$items = $this->_controller()->paginate($subject->query);
} catch (NotFoundException $e) {
/** @var \Cake\Core\Exception\CakeException $previous */
$previous = $e->getPrevious();
$pagingParams = $previous->getAttributes()['pagingParams'];

$controller = $this->_controller();
$items = $controller->paginate();
$subject->set(['items' => $items]);
$url = Router::reverseToArray($this->_request());
$url['?']['page'] = $pagingParams['pageCount'];

$this->_trigger('afterPaginate', $subject);
return $this->_controller()->redirect($url);
}

$subject->set(['entities' => $items]);

$controller->set(['success' => $subject->success, $subject->viewVar => $subject->items]);
$this->_trigger('afterPaginate', $subject);
$this->_trigger('beforeRender', $subject);
}

return null;
}
}
2 changes: 1 addition & 1 deletion docs/_partials/events/before_find.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Add Conditions
public function delete($id)
{
$this->Crud->on('beforeFind', function(\Cake\Event\EventInterface $event) {
$event->getSubject()->query->where(['author' => $this->Auth->user('id')]);
$event->getSubject()->query->where(['author' => $this->Authentication->getIdentity()->id]);
});
return $this->Crud->execute();
Expand Down
2 changes: 1 addition & 1 deletion docs/_partials/events/before_paginate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Add Conditions
public function index()
{
$this->Crud->on('beforePaginate', function(\Cake\Event\EventInterface $event) {
$this->paginate['conditions']['is_active'] = true;
$event->getSubject()->query->where(['is_active' => true]);
});
return $this->Crud->execute();
Expand Down
19 changes: 7 additions & 12 deletions docs/actions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace your action class accordingly.
.. literalinclude:: _code/action_index.php
:language: php
:linenos:
:emphasize-lines: 2-4, 25-27
:emphasize-lines: 2-4

Request Methods
---------------
Expand All @@ -66,7 +66,7 @@ executed.
.. literalinclude:: _code/action_index.php
:language: php
:linenos:
:emphasize-lines: 6-11,25
:emphasize-lines: 6-10

You can treat the ``_handle()`` method as a catch-all, if your crud action
wants to process all possible HTTP verbs.
Expand All @@ -83,14 +83,14 @@ Events & Subject
----------------

All Crud actions emit a range of events, and all of these events always contain a Crud Subject. The Crud Subject can
change its state between emitted events. This object is a simple ``StdClass`` which contains the current state of the Crud request.
change its state between emitted events. This object is a ``Crud\Event\Subject`` instance which contains the current state of the Crud request.

The real beauty of Crud is the events and the flexibility they provide.

All calls to ``_trigger()`` emit an event, that you as a developer can listen to and inject your own application logic.
These events are in no way magical, they are simply normal
`CakePHP events <http://book.cakephp.org/4/en/core-libraries/events.html>`_, dispatched like all
other `events in CakePHP <http://book.cakephp.org/4/en/core-libraries/events.html>`_.
`CakePHP events <http://book.cakephp.org/5/en/core-libraries/events.html>`_, dispatched like all
other `events in CakePHP <http://book.cakephp.org/5/en/core-libraries/events.html>`_.

You can for example listen for the ``beforePaginate`` event and add conditions to your pagination query, just with a
few lines of code. Those few lines of code is what makes your application unique. The rest of the code you would
Expand All @@ -99,21 +99,16 @@ normally have is simply repeated boiler plate code.
.. literalinclude:: _code/action_index.php
:language: php
:linenos:
:emphasize-lines: 12-15,19,21,24
:emphasize-lines: 60,76-77

Boilerplate
-----------

Only the code that you would normally have in your controller is left now.

While these 3 lines seem simple, and the whole Crud implementation a bit overkill at first, the true power of this setup
While the whole Crud implementation might seem a bit overkill at first, the true power of this setup
will be clear when your application grows and the requirements increase.

.. literalinclude:: _code/action_index.php
:language: php
:linenos:
:emphasize-lines: 17,18,23

For example :doc:`adding an API layer<api>` to your application later in time will be easy because you don't need to edit
all your applications many controllers.

Expand Down
10 changes: 6 additions & 4 deletions docs/actions/custom.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ A default custom index action might be as simple as the following:
namespace App\Crud\Action;
class MyIndexAction extends \Crud\Action\BaseAction
use Crud\Action\BaseAction;
class MyIndexAction extends BaseAction
{
/**
* Default settings
*
* @var array
* @var array<string, mixed>
*/
protected $_defaultConfig = [
protected array $_defaultConfig = [
'enabled' => true,
'scope' => 'table',
'findMethod' => 'all',
Expand All @@ -47,7 +49,7 @@ A default custom index action might be as simple as the following:
*
* @return void
*/
protected function _handle()
protected function _handle(): void
{
$query = $this->_table()->find($this->findMethod());
$items = $this->_controller()->paginate($query);
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ The easiest way to achieve this is to create an ``AppController`` for the prefix
extend from that one. Then you can configure Crud in your prefixes ``AppController``.

Let's look at an example, using an ``api`` prefix. For this example, we'll assume your
`prefix routing <http://book.cakephp.org/4/en/development/routing.html#prefix-routing>`_ is already configured.
`prefix routing <http://book.cakephp.org/5/en/development/routing.html#prefix-routing>`_ is already configured.

First step is to create your new ``ApiAppController`` which should be in ``src/Controller/Api/``.

Expand Down
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ If you happen to stumble upon a bug, please `feel free to create a pull request
Features
========

If you have a good idea for a Crud feature, `please join us in #friendsofcake channel on Slack <https://cakesf.herokuapp.com/>`_ and let's discuss it.
If you have a good idea for a Crud feature, `please join us in #friendsofcake channel on Slack <https://slack-invite.cakephp.org//>`_ and let's discuss it.
Opening a `pull request <https://github.com/FriendsOfCake/crud/pulls>`_ is always more than welcome, and a great way to start a discussion.
Please check our `contribution guidelines <https://github.com/FriendsOfCake/crud/blob/master/CONTRIBUTING.md>`_.

Support / Questions
===================

You can `join us CakePHP's #support channel <https://cakesf.herokuapp.com/>`_ on Slack for any support or questions.
You can `join us CakePHP's #support channel <https://slack-invite.cakephp.org//>`_ on Slack for any support or questions.
18 changes: 4 additions & 14 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
Installation
************

Requirements
============

* CakePHP 3.2+
* PHP 5.5.9+

Using composer
==============

Expand All @@ -28,14 +22,14 @@ Run the following command
bin/cake plugin load Crud
Depending on your CakePHP version your ``src/Application.php`` or ``config/bootstrap.php``
Depending on your CakePHP version your ``src/Application.php`` or ``config/plugins.php``
will be modified to load the plugin.

Configuring the controller
==========================

The Crud plugin provides a trait which will catch a MissingActionException and then step in to provide scaffold actions
to the controllers.
The Crud plugin provides a trait which will catch a ``MissingActionException`` and
then step in to provide scaffold actions to the controllers.

To enable Crud across your whole application add the trait to your ``src/Controller/AppController.php``

Expand All @@ -53,7 +47,7 @@ To enable Crud across your whole application add the trait to your ``src/Control
To have Crud just scaffold a single controller you can just add the ``ControllerTrait`` to that specific controller.

Adding the ``ControllerTrait`` itself do not enable anything Crud, but simply installs the code to handle
the ``\Cake\Error\MissingActionException`` exception so you don't have to implement an action in your controller
the ``\Cake\Controller\Exception\MissingActionException`` exception so you don't have to implement an action in your controller
for Crud to work.

The next step is to load the Crud component in your controller. A basic example is as follows, and will enable the Crud
Expand All @@ -79,8 +73,4 @@ plugin to scaffold all your controllers index actions.
}
}
For controllers like ``ErrorController`` where you usually don't call ``parent::initialize()``
you can avoid errors due to ``CrudComponent`` not being loaded by adding
``$this->dispatchComponents['Crud'] = false`` in the controller's ``initialize()`` method.

Further configuration options are detailed on the :doc:`configuration page</configuration>`.
4 changes: 2 additions & 2 deletions docs/listeners.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The Anatomy Of A Listener
=========================

The listener system is simply the
`Events System <http://book.cakephp.org/4/en/core-libraries/events.html>`_ from
`Events System <http://book.cakephp.org/5/en/core-libraries/events.html>`_ from
CakePHP, and all the official documentation and usage also applies to Crud.

The Crud event system uses two methods ``trigger()`` and ``on()`` to interface
Expand Down Expand Up @@ -47,7 +47,7 @@ as an action creator.
Implemented Events
------------------

As documented in the `CakePHP Events System <http://book.cakephp.org/4/en/core-libraries/events.html>`_
As documented in the `CakePHP Events System <http://book.cakephp.org/5/en/core-libraries/events.html>`_
all listeners must contain a ``implementedEvents`` method.

In this example, we simply request that ``beforeRender`` in our class is executed
Expand Down
8 changes: 5 additions & 3 deletions docs/listeners/api-pagination.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ below:
],
"pagination":{
"page_count": 13,
"page_count": 5,
"current_page": 1,
"count": 25,
"count": 10,
"total_count": 45,
"page_page": 10,
"has_prev_page": false,
"has_next_page": true
}
Expand All @@ -72,7 +74,7 @@ Configuration
-------------

Configure this listener by setting the
`CakePHP Pagination <http://book.cakephp.org/4/en/controllers/components/pagination.html>`_ options directly to the
`CakePHP Pagination <http://book.cakephp.org/5/en/controllers/components/pagination.html>`_ options directly to the
query object.

.. code-block:: php
Expand Down
Loading

0 comments on commit 7b2f796

Please sign in to comment.