Skip to content

Commit 7b2f796

Browse files
authored
Merge pull request #701 from FriendsOfCake/cake-5
Cake 5
2 parents 39a7cff + 38e9957 commit 7b2f796

File tree

87 files changed

+917
-1368
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+917
-1368
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,21 @@
11
name: CI
22

3-
on: [push, pull_request]
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- '*'
10+
11+
permissions:
12+
contents: read
413

514
jobs:
615
testsuite:
7-
runs-on: ubuntu-22.04
8-
strategy:
9-
fail-fast: false
10-
matrix:
11-
php-version: ['7.4', '8.0', '8.1', '8.2']
12-
db-type: [sqlite, mysql, pgsql]
13-
prefer-lowest: ['']
14-
include:
15-
- php-version: '7.2'
16-
db-type: 'sqlite'
17-
prefer-lowest: 'prefer-lowest'
18-
19-
services:
20-
postgres:
21-
image: postgres
22-
ports:
23-
- 5432:5432
24-
env:
25-
POSTGRES_PASSWORD: postgres
26-
27-
steps:
28-
- uses: actions/checkout@v3
29-
30-
- name: Setup Service
31-
if: matrix.db-type == 'mysql'
32-
run: |
33-
sudo service mysql start
34-
mysql -h 127.0.0.1 -u root -proot -e 'CREATE DATABASE cakephp;'
35-
36-
- name: Setup PHP
37-
uses: shivammathur/setup-php@v2
38-
with:
39-
php-version: ${{ matrix.php-version }}
40-
extensions: mbstring, intl, pdo_${{ matrix.db-type }}
41-
coverage: pcov
42-
43-
- name: Composer install
44-
run: |
45-
if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then
46-
composer update --prefer-lowest --prefer-stable
47-
else
48-
composer update
49-
fi
50-
51-
- name: Run PHPUnit
52-
run: |
53-
if [[ ${{ matrix.db-type }} == 'sqlite' ]]; then export DB_URL='sqlite:///:memory:'; fi
54-
if [[ ${{ matrix.db-type }} == 'mysql' ]]; then export DB_URL='mysql://root:[email protected]/cakephp'; fi
55-
if [[ ${{ matrix.db-type }} == 'pgsql' ]]; then export DB_URL='postgres://postgres:[email protected]/postgres'; fi
56-
57-
if [[ ${{ matrix.php-version }} == '7.4' && ${{ matrix.db-type }} == 'sqlite' ]]; then
58-
vendor/bin/phpunit --coverage-clover=coverage.xml
59-
else
60-
vendor/bin/phpunit
61-
fi
62-
63-
- name: Code Coverage Report
64-
if: success() && matrix.php-version == '7.4' && matrix.db-type == 'sqlite'
65-
uses: codecov/codecov-action@v2
16+
uses: ADmad/.github/.github/workflows/testsuite-with-db.yml@master
17+
secrets: inherit
6618

6719
cs-stan:
68-
name: Coding Standard & Static Analysis
69-
runs-on: ubuntu-22.04
70-
71-
steps:
72-
- uses: actions/checkout@v3
73-
74-
- name: Setup PHP
75-
uses: shivammathur/setup-php@v2
76-
with:
77-
php-version: '7.4'
78-
extensions: mbstring, intl
79-
coverage: none
80-
tools: vimeo/psalm:4.23, phpstan:1.9, cs2pr
81-
82-
- name: Composer Install
83-
run: composer require --dev cakephp/cakephp-codesniffer:^4.1
84-
85-
- name: Run phpcs
86-
run: vendor/bin/phpcs --report=checkstyle --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/ | cs2pr
87-
88-
- name: Run psalm
89-
if: always()
90-
run: psalm --output-format=github
91-
92-
- name: Run phpstan
93-
if: always()
94-
run: phpstan analyse --error-format=github
20+
uses: ADmad/.github/.github/workflows/cs-stan.yml@master
21+
secrets: inherit

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ phpunit.xml
44
vendor/
55
composer.lock
66
tmp
7+
.phpunit.cache
78
.phpunit.result.cache

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@
3939
}
4040
],
4141
"require": {
42-
"cakephp/cakephp": "^4.0"
42+
"cakephp/cakephp": "^5.0"
4343
},
4444
"require-dev": {
45-
"phpunit/phpunit": "^8.5.23 || ^9.3",
46-
"friendsofcake/cakephp-test-utilities": "^2.0.1",
47-
"friendsofcake/search": "^6.0"
45+
"friendsofcake/cakephp-test-utilities": "^3.0",
46+
"friendsofcake/search": "^7.0",
47+
"phpunit/phpunit": "^10.1"
4848
},
4949
"autoload": {
5050
"psr-4": {

docs/_code/action_index.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,36 @@
33

44
class Index extends BaseAction
55
{
6-
76
/**
87
* Generic handler for all HTTP verbs
98
*
10-
* @return void
9+
* @return \Cake\Http\Response|null
1110
*/
12-
protected function _handle()
11+
protected function _handle(): ?Response
1312
{
14-
$subject = $this->_subject();
15-
$subject->set(['success' => true, 'viewVar' => $this->viewVar()]);
13+
[$finder, $options] = $this->_extractFinder();
14+
$query = $this->_model()->find($finder, ...$options);
15+
$subject = $this->_subject(['success' => true, 'query' => $query]);
1616

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

19-
$controller = $this->_controller();
20-
$items = $controller->paginate();
21-
$subject->set(['items' => $items]);
25+
$url = Router::reverseToArray($this->_request());
26+
$url['?']['page'] = $pagingParams['pageCount'];
2227

23-
$this->_trigger('afterPaginate', $subject);
28+
return $this->_controller()->redirect($url);
29+
}
30+
31+
$subject->set(['entities' => $items]);
2432

25-
$controller->set(['success' => $subject->success, $subject->viewVar => $subject->items]);
33+
$this->_trigger('afterPaginate', $subject);
2634
$this->_trigger('beforeRender', $subject);
27-
}
2835

36+
return null;
37+
}
2938
}

docs/_partials/events/before_find.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Add Conditions
3434
public function delete($id)
3535
{
3636
$this->Crud->on('beforeFind', function(\Cake\Event\EventInterface $event) {
37-
$event->getSubject()->query->where(['author' => $this->Auth->user('id')]);
37+
$event->getSubject()->query->where(['author' => $this->Authentication->getIdentity()->id]);
3838
});
3939
4040
return $this->Crud->execute();

docs/_partials/events/before_paginate.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Add Conditions
1111
public function index()
1212
{
1313
$this->Crud->on('beforePaginate', function(\Cake\Event\EventInterface $event) {
14-
$this->paginate['conditions']['is_active'] = true;
14+
$event->getSubject()->query->where(['is_active' => true]);
1515
});
1616
1717
return $this->Crud->execute();

docs/actions.rst

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace your action class accordingly.
5050
.. literalinclude:: _code/action_index.php
5151
:language: php
5252
:linenos:
53-
:emphasize-lines: 2-4, 25-27
53+
:emphasize-lines: 2-4
5454

5555
Request Methods
5656
---------------
@@ -66,7 +66,7 @@ executed.
6666
.. literalinclude:: _code/action_index.php
6767
:language: php
6868
:linenos:
69-
:emphasize-lines: 6-11,25
69+
:emphasize-lines: 6-10
7070

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

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

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

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

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

104104
Boilerplate
105105
-----------
106106

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

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

112-
.. literalinclude:: _code/action_index.php
113-
:language: php
114-
:linenos:
115-
:emphasize-lines: 17,18,23
116-
117112
For example :doc:`adding an API layer<api>` to your application later in time will be easy because you don't need to edit
118113
all your applications many controllers.
119114

docs/actions/custom.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ A default custom index action might be as simple as the following:
1818
1919
namespace App\Crud\Action;
2020
21-
class MyIndexAction extends \Crud\Action\BaseAction
21+
use Crud\Action\BaseAction;
22+
23+
class MyIndexAction extends BaseAction
2224
{
2325
/**
2426
* Default settings
2527
*
26-
* @var array
28+
* @var array<string, mixed>
2729
*/
28-
protected $_defaultConfig = [
30+
protected array $_defaultConfig = [
2931
'enabled' => true,
3032
'scope' => 'table',
3133
'findMethod' => 'all',
@@ -47,7 +49,7 @@ A default custom index action might be as simple as the following:
4749
*
4850
* @return void
4951
*/
50-
protected function _handle()
52+
protected function _handle(): void
5153
{
5254
$query = $this->_table()->find($this->findMethod());
5355
$items = $this->_controller()->paginate($query);

docs/configuration.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ The easiest way to achieve this is to create an ``AppController`` for the prefix
236236
extend from that one. Then you can configure Crud in your prefixes ``AppController``.
237237

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

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

docs/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ If you happen to stumble upon a bug, please `feel free to create a pull request
3939
Features
4040
========
4141

42-
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.
42+
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.
4343
Opening a `pull request <https://github.com/FriendsOfCake/crud/pulls>`_ is always more than welcome, and a great way to start a discussion.
4444
Please check our `contribution guidelines <https://github.com/FriendsOfCake/crud/blob/master/CONTRIBUTING.md>`_.
4545

4646
Support / Questions
4747
===================
4848

49-
You can `join us CakePHP's #support channel <https://cakesf.herokuapp.com/>`_ on Slack for any support or questions.
49+
You can `join us CakePHP's #support channel <https://slack-invite.cakephp.org//>`_ on Slack for any support or questions.

0 commit comments

Comments
 (0)