Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cake5 #48

Merged
merged 6 commits into from
Oct 12, 2023
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
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['7.4', '8.2']
php-version: ['8.1', '8.2']
db-type: [sqlite, mysql, pgsql]
prefer-lowest: ['']
include:
- php-version: '7.4'
- php-version: '8.1'
db-type: 'sqlite'
prefer-lowest: 'prefer-lowest'

Expand Down Expand Up @@ -69,7 +69,7 @@ jobs:
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' ]]
if [[ ${{ matrix.php-version }} == '8.1' && ${{ matrix.db-type }} == 'sqlite' ]]
then
vendor/bin/phpunit --coverage-clover=coverage.xml
else
Expand All @@ -79,7 +79,7 @@ jobs:
run: if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then vendor/bin/validate-prefer-lowest -m; fi

- name: Code Coverage Report
if: success() && matrix.php-version == '7.4' && matrix.db-type == 'sqlite'
if: success() && matrix.php-version == '8.1' && matrix.db-type == 'sqlite'
uses: codecov/codecov-action@v3

validation:
Expand All @@ -92,7 +92,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
php-version: '8.1'
extensions: mbstring, intl
coverage: none

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
phpunit.phar
.phpunit.result.cache
.phpunit.cache
composer.lock
vendor/
plugins/
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
[![CI](https://github.com/dereuromark/cakephp-ajax/workflows/CI/badge.svg?branch=master)](https://github.com/dereuromark/cakephp-ajax/actions?query=workflow%3ACI+branch%3Amaster)
[![Coverage Status](https://codecov.io/gh/dereuromark/cakephp-ajax/branch/master/graph/badge.svg)](https://codecov.io/gh/dereuromark/cakephp-ajax)
[![Latest Stable Version](https://poser.pugx.org/dereuromark/cakephp-ajax/v/stable.svg)](https://packagist.org/packages/dereuromark/cakephp-ajax)
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.4-8892BF.svg)](https://php.net/)
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.1-8892BF.svg)](https://php.net/)
[![License](https://poser.pugx.org/dereuromark/cakephp-ajax/license.svg)](https://packagist.org/packages/dereuromark/cakephp-ajax)
[![Total Downloads](https://poser.pugx.org/dereuromark/cakephp-ajax/d/total.svg)](https://packagist.org/packages/dereuromark/cakephp-ajax)
[![Coding Standards](https://img.shields.io/badge/cs-PSR--2--R-yellow.svg)](https://github.com/php-fig-rectified/fig-rectified-standards)

A CakePHP plugin that makes working with AJAX a piece of cake..

This branch is for **CakePHP 4.2+**. For details see [version map](https://github.com/dereuromark/cakephp-ajax/wiki#cakephp-version-map).
This branch is for **CakePHP 5.0+**. For details see [version map](https://github.com/dereuromark/cakephp-ajax/wiki#cakephp-version-map).

## What is this plugin for?
Basically DRY (Don't repeat yourself) and easy AJAX handling.
Expand Down
14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
}
],
"require": {
"php": ">=7.4",
"cakephp/cakephp": "^4.2.0"
"php": ">=8.1",
"cakephp/cakephp": "^5.0.0"
},
"require-dev": {
"dereuromark/cakephp-tools": "^2.0.0",
"phpunit/phpunit": "^9.5",
"fig-r/psr2r-sniffer": "dev-master"
},
"dereuromark/cakephp-tools": "^3.0.0",
"phpunit/phpunit": "^10.2.1",
"fig-r/psr2r-sniffer": "dev-next",
"ext-mbstring": "*"
},
"autoload": {
"psr-4": {
"Ajax\\": "src/"
Expand All @@ -44,6 +45,7 @@
"issues": "https://github.com/dereuromark/cakephp-ajax/issues"
},
"prefer-stable": true,
"minimum-stability": "dev",
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --log-junit tmp/coverage/unitreport.xml --coverage-html tmp/coverage --coverage-clover tmp/coverage/coverage.xml",
Expand Down
14 changes: 4 additions & 10 deletions docs/Component/Ajax.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ follow those redirects and return the content via requestAction(). This might no

This plugin prevents this internal request, and instead returns the URL and status code inside the JSON response.

### Disable internal requests
Make sure you disabled the deprecated `enableBeforeRedirect` option:
```php
$this->loadComponent('RequestHandler', ['enableBeforeRedirect' => false]);
```

## Setup
Load the Ajax component inside `Controller::initialize()`:
```php
Expand Down Expand Up @@ -63,12 +57,12 @@ are not reserved:
```php
$content = ['id' => 1, 'title' => 'title'];
$this->set(compact('content'));
$this->set('_serialize', ['content']);
$this->set('serialize', ['content']);
```
results in

"content":{...}, ...

### AJAX Delete

For usability reasons you might want to delete a row in a paginated table, without the need to refresh the whole page.
Expand All @@ -84,7 +78,7 @@ public function delete($id = null) {
$this->request->allowMethod(['post', 'delete']);
$group = $this->Groups->get($id);

$this->Groups->delete($group);
$this->Groups->deleteOrFail($group);
$this->Flash->success(__('The group has been deleted.'));

return $this->redirect(['action' => 'index']);
Expand Down Expand Up @@ -116,7 +110,7 @@ public function delete($id = null) {
return $this->redirect($this->referer(['action' => 'index'], true));
}

$this->Groups->delete($group);
$this->Groups->deleteOrFail($group);
$this->Flash->success(__('The group has been deleted.'));

return $this->redirect(['action' => 'index']);
Expand Down
1 change: 0 additions & 1 deletion docs/Contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ There are a few guidelines that I need contributors to follow:

Tip: You can use the composer commands to set up everything:
* `composer install`
* `composer test-setup`

Now you can run the tests via `composer test` and get coverage via `composer test-coverage` commands.

Expand Down
3 changes: 0 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
## Installation
* [Installation](Install.md)

## Upgrade Guide
* [Upgrade guide from 2.x to 3.x](Upgrade.md)

## Documentation
* [Ajax plugin](Ajax.md)
* [View/Ajax](View/Ajax.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/View/Ajax.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The result can be this, for example:
"error": ''
}
```
You can add more data to the response object via `_serialize`.
You can add more data to the response object via `serialize`.


### Drop down selections
Expand Down
26 changes: 13 additions & 13 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="tests/bootstrap.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
cacheDirectory=".phpunit.cache">
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
<!-- E_ALL => 32767 -->
<!-- E_ALL & ~E_USER_DEPRECATED => 16383 -->
<!-- E_ALL & ~E_USER_DEPRECATED (16383)-->
<!-- E_ALL (32767) -->
<ini name="error_reporting" value="16383"/>
</php>
<testsuites>
<testsuite name="ajax">
<directory>tests/</directory>
<directory>tests/TestCase/</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="Cake\TestSuite\Fixture\FixtureInjector">
<arguments>
<object class="Cake\TestSuite\Fixture\FixtureManager"/>
</arguments>
</listener>
</listeners>
<coverage>
<extensions>
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
</source>
</phpunit>
28 changes: 14 additions & 14 deletions src/Controller/Component/AjaxComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ class AjaxComponent extends Component {
/**
* @var bool
*/
protected $respondAsAjax = false;
public bool $respondAsAjax = false;

/**
* @var array<string, mixed>
*/
protected $_defaultConfig = [
protected array $_defaultConfig = [
'viewClass' => 'Ajax.Ajax',
'autoDetect' => true,
'resolveRedirect' => true,
Expand Down Expand Up @@ -78,7 +78,7 @@ public function beforeRender(EventInterface $event): void {
/**
* @return void
*/
protected function _respondAsAjax() {
protected function _respondAsAjax(): void {
$this->getController()->viewBuilder()->setClassName($this->_config['viewClass']);

// Set flash messages to the view
Expand All @@ -87,16 +87,16 @@ protected function _respondAsAjax() {
$this->getController()->set('_message', $message);
}

// If _serialize is true, *all* viewVars will be serialized; no need to add _message.
// If `serialize` is true, *all* viewVars will be serialized; no need to add _message.
if ($this->_isControllerSerializeTrue()) {
return;
}

$serializeKeys = ['_message'];
if (!empty($this->getController()->viewBuilder()->getVar('_serialize'))) {
$serializeKeys = array_merge($serializeKeys, (array)$this->getController()->viewBuilder()->getVar('_serialize'));
if (!empty($this->getController()->viewBuilder()->getVar('serialize'))) {
$serializeKeys = array_merge($serializeKeys, (array)$this->getController()->viewBuilder()->getVar('serialize'));
}
$this->getController()->set('_serialize', $serializeKeys);
$this->getController()->set('serialize', $serializeKeys);
}

/**
Expand Down Expand Up @@ -129,23 +129,23 @@ public function beforeRedirect(EventInterface $event, $url, Response $response):
}

$serializeKeys = ['_redirect'];
if ($this->getController()->viewBuilder()->getVar('_serialize')) {
$serializeKeys = array_merge($serializeKeys, (array)$this->getController()->viewBuilder()->getVar('_serialize'));
if ($this->getController()->viewBuilder()->getVar('serialize')) {
$serializeKeys = array_merge($serializeKeys, (array)$this->getController()->viewBuilder()->getVar('serialize'));
}
$this->getController()->set('_serialize', $serializeKeys);
$this->getController()->set('serialize', $serializeKeys);
// Further changes will be required here when the change to immutable response objects is completed
$response = $this->getController()->render();

return $response;
}

/**
* Checks to see if the Controller->viewVar labeled _serialize is set to boolean true.
* Checks to see if the Controller->viewVar labeled `serialize` is set to boolean true.
*
* @return bool
*/
protected function _isControllerSerializeTrue() {
if ($this->getController()->viewBuilder()->getVar('_serialize') === true) {
protected function _isControllerSerializeTrue(): bool {
if ($this->getController()->viewBuilder()->getVar('serialize') === true) {
return true;
}

Expand All @@ -157,7 +157,7 @@ protected function _isControllerSerializeTrue() {
*
* @return bool
*/
protected function _isActionEnabled() {
protected function _isActionEnabled(): bool {
$actions = $this->getConfig('actions');
if (!$actions) {
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ class Plugin extends BasePlugin {
/**
* @var bool
*/
protected $middlewareEnabled = false;
protected bool $middlewareEnabled = false;

/**
* @var bool
*/
protected $consoleEnabled = false;
protected bool $consoleEnabled = false;

/**
* @var bool
*/
protected $routesEnabled = false;
protected bool $routesEnabled = false;

}
20 changes: 10 additions & 10 deletions src/View/AjaxView.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* A response to an invalid request may be just HTTP status "code" and error "message"
* (e.g, on 4xx or 5xx).
* A response to a valid request will always contain at least "content" and "error" keys.
* You can add more data using _serialize.
* You can add more data using `serialize`.
*
* @author Mark Scherer
* @license http://opensource.org/licenses/mit-license.php MIT
Expand All @@ -30,7 +30,7 @@ class AjaxView extends AppView {
*
* @var array<string>
*/
protected $_passedVars = [
protected array $_passedVars = [
'viewVars', 'autoLayout', 'ext', 'helpers', 'view', 'layout', 'name', 'theme',
'layoutPath', 'plugin', 'passedArgs', 'subDir', 'template', 'templatePath',
];
Expand All @@ -40,14 +40,14 @@ class AjaxView extends AppView {
*
* @var string
*/
protected $subDir = '';
protected string $subDir = '';

/**
* List of special view vars.
*
* @var array<string>
*/
protected $_specialVars = ['_serialize', '_jsonOptions', '_jsonp'];
protected array $_specialVars = ['serialize', '_jsonOptions', '_jsonp'];

/**
* Constructor
Expand All @@ -62,7 +62,7 @@ public function __construct(
?ServerRequest $request = null,
?Response $response = null,
?EventManager $eventManager = null,
array $viewOptions = []
array $viewOptions = [],
) {
parent::__construct($request, $response, $eventManager, $viewOptions);

Expand All @@ -83,7 +83,7 @@ public function __construct(
/**
* Renders an AJAX view.
* The rendered content will be part of the JSON response object and
* can be accessed via response.content in JavaScript.
* can be accessed via `response.content` in JavaScript.
*
* If an error or success has been set, the rendering will be skipped.
*
Expand Down Expand Up @@ -119,8 +119,8 @@ public function render(?string $view = null, $layout = null): string {
}

$this->viewVars = Hash::merge($dataToSerialize, $this->viewVars);
if (isset($this->viewVars['_serialize'])) {
$dataToSerialize = $this->_dataToSerialize($this->viewVars['_serialize'], $dataToSerialize);
if (isset($this->viewVars['serialize'])) {
$dataToSerialize = $this->_dataToSerialize($this->viewVars['serialize'], $dataToSerialize);
}

return $this->_serialize($dataToSerialize);
Expand All @@ -133,7 +133,7 @@ public function render(?string $view = null, $layout = null): string {
* @param array<mixed> $dataToSerialize Array of data that is to be serialzed.
* @return string The serialized data.
*/
protected function _serialize(array $dataToSerialize = []) {
protected function _serialize(array $dataToSerialize = []): string {
return JsonEncoder::encode($dataToSerialize);
}

Expand All @@ -146,7 +146,7 @@ protected function _serialize(array $dataToSerialize = []) {
* render method.
* @return array<mixed> The data to serialize.
*/
protected function _dataToSerialize($serialize, $additionalData = []) {
protected function _dataToSerialize(array|bool|string $serialize, array $additionalData = []): array {
if ($serialize === true) {
$data = array_diff_key(
$this->viewVars,
Expand Down
Loading