Skip to content

Commit

Permalink
4.next cake5 (#64)
Browse files Browse the repository at this point in the history
* Fixed php 8.1 deprecations

* removed deprecations

* initial migration to CakePHP 5

* upgrade-cakephp5: updated typehints

* upgrade-cakephp5: updated composer.json, updated dependencies, updated code

* upgrade-cakephp5: updated gitignore

* upgrade-cakephp5: reorganized composer dependencies

* upgrade-cakephp5: upgraded code that depends on `cakephp/elastic-search` and `friendsofcake/crud`

* updated phpunit dependency to v10

* cc, removed deprecated code

* removed deprecated code, updated composer.json

* cc

* fixing issues reported by phpcs

* fixing issues reported by phpstan

* refactored rabbitmq connection

* cc

* fixed issues reported by phpstan

* fixed tests, updated README.md, cc

* dropped RabbitMQ support

* dropped ElasticMappingCommand.php

* cc

* fixed serialization

* updated README.md, updated git push workflow, fixed test

* updated README.md

---------

Co-authored-by: Marcelo Rocha <contato@omarcelo.com.br>
  • Loading branch information
arusinowski and rochamarcelo authored Dec 21, 2023
1 parent c03812d commit 14afbcc
Show file tree
Hide file tree
Showing 53 changed files with 1,019 additions and 1,102 deletions.
26 changes: 26 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at https://editorconfig.org

root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.bat]
end_of_line = crlf

[*.yml]
indent_size = 2

[*.twig]
insert_final_newline = false

[*.neon]
indent_style = tab

[Makefile]
indent_style = tab
44 changes: 8 additions & 36 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ jobs:
strategy:
matrix:
operating-system: [ ubuntu-20.04 ]
php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
php-versions: ['8.1']

name: PHP ${{ matrix.php-versions }} Test
name: PHP ${{ matrix.php-versions }} Test
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down Expand Up @@ -44,7 +44,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
version: ['~4.0.0', '~4.1.0', '~4.2.0', '~4.3.0']
version: ['~5.0.0']

name: CakePHP ${{ matrix.version }} Test
steps:
Expand All @@ -54,35 +54,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.2'
extensions: mbstring, intl

- name: PHP Version
run: php -v

- name: CakePHP ${{matrix.version}} Compatability
run: |
composer self-update
rm -rf composer.lock
composer require cakephp/cakephp:${{matrix.version}} --no-update
composer install --prefer-dist --no-progress
composer test
compatibility_cakephp4:
runs-on: ubuntu-latest
strategy:
matrix:
version: ['~4.4.0']

name: CakePHP ${{ matrix.version }} Test
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
php-version: '8.1'
extensions: mbstring, intl

- name: PHP Version
Expand All @@ -100,11 +72,11 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
version: ['~4.4.0']
version: ['~5.0.0']

services:
elasticsearch:
image: elasticsearch:7.11.1
image: elasticsearch:7.17.6
ports:
- 9200/tcp
env:
Expand All @@ -124,7 +96,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
php-version: '8.1'
extensions: mbstring, intl

- name: PHP Version
Expand All @@ -138,4 +110,4 @@ jobs:
rm -rf composer.lock
composer require cakephp/cakephp:${{matrix.version}} --no-update
composer install --prefer-dist --no-progress
vendor/bin/phpunit tests/TestCase/Persister/ElasticSearchPersisterIntegrationTest.php
vendor/bin/phpunit tests/TestCase/Persister/ElasticSearchPersisterIntegrationTest.php
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#################################
/nbproject
.idea
/auth.json
/.ddev

# OS generated files #
######################
Expand All @@ -23,3 +25,5 @@
Icon?
ehthumbs.db
Thumbs.db
/.phpunit.cache/
/tools/
5 changes: 5 additions & 0 deletions .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpstan" version="1.10.32" installed="1.10.32" location="./tools/phpstan" copy="false"/>
<phar name="psalm" version="5.15.0" installed="5.15.0" location="./tools/psalm" copy="false"/>
</phive>
80 changes: 42 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ You now need to add the datasource configuration to your `config/app.php` file:
If you want to use a regular database, respectively an engine that can be used via the CakePHP ORM API, then you can use
the table persister that ships with this plugin.

To do so you need to configure the `AuditStash.persister` option accordingly. In your `config/app.php` file add the
To do so you need to configure the `AuditStash.persister` option accordingly. In your `config/app_local.php` file add the
following configuration:

```php
'AuditStash' => [
'persister' => 'AuditStash\Persister\TablePersister'
]
'persister' => \AuditStash\Persister\TablePersister::class,
],
```

The plugin will then by default try to store the logs in a table named `audit_logs`, via a table class with the alias
Expand All @@ -73,19 +73,19 @@ add a table named `audit_logs` with all the default columns - alternatively crea
can bake the corresponding table class.

```
bin/cake migrations migrate -p AuditStash -t 20171018185609
bin/cake migrations migrate -p AuditStash
bin/cake bake model AuditLogs
```

#### Table Persister Configuration

The table persister supports various configuration options, please refer to
[its API documentation](/src/Persister/TablePersister.php) for further information. Generally configuration can be
applied via its `config()` (or `setConfig()`) method:
applied via its `setConfig()` method:

```php
$this->addBehavior('AuditStash.AuditLog');
$this->behaviors()->get('AuditLog')->persister()->config([
$this->behaviors()->get('AuditLog')->persister()->setConfig([
'extractMetaFields' => [
'user.id' => 'user_id'
]
Expand All @@ -99,28 +99,14 @@ Enabling the Audit Log in any of your table classes is as simple as adding a beh
```php
class ArticlesTable extends Table
{
public function initialize(array $config = [])
public function initialize(array $config = []): void
{
...
$this->addBehavior('AuditStash.AuditLog');
}
}
```

When using the `Elasticserch` persister, it is recommended that you tell Elasticsearch about the schema of your table. You can do this
automatically by executing the following command:

```
bin/cake elastic_mapping Articles
```

If you are using one index per day, save yourself some time and add the `--use-templates` option. This will create a schema template so
any new index will inherit this configuration:

```
bin/cake elastic_mapping Articles --use-templates
```

Remember to execute the command line each time you change the schema of your table!

### Configuring The Behavior
Expand All @@ -130,7 +116,7 @@ The `AuditLog` behavior can be configured to ignore certain fields of your table
```php
class ArticlesTable extends Table
{
public function initialize(array $config = [])
public function initialize(array $config = []): void
{
...
$this->addBehavior('AuditStash.AuditLog', [
Expand All @@ -143,7 +129,7 @@ class ArticlesTable extends Table
If you prefer, you can use a `whitelist` instead. This means that only the fields listed in that array will be tracked by the behavior:

```php
public function initialize(array $config = [])
public function initialize(array $config = []): void
{
...
$this->addBehavior('AuditStash.AuditLog', [
Expand All @@ -164,11 +150,16 @@ use AuditStash\Meta\RequestMetadata;

class AppController extends Controller
{
public function beforeFilter(Event $event)
public function beforeFilter(EventInterface $event)
{
...
$eventManager = $this->loadModel()->eventManager();
$eventManager->on(new RequestMetadata($this->request, $this->Auth->user('id')));
$eventManager = $this->fetchTable()->getEventManager();
$eventManager->on(
new RequestMetadata(
request: $this->getRequest(),
user: $this->getRequest()->getAttribute('identity')?->getIdentifier()
)
);
}
}
```
Expand All @@ -185,10 +176,15 @@ use Cake\Event\EventManager;

class AppController extends Controller
{
public function beforeFilter(Event $event)
public function beforeFilter(EventInterface $event)
{
...
EventManager::instance()->on(new RequestMetadata($this->request, $this->Auth->user('id')));
EventManager::instance()->on(
new RequestMetadata(
request: $this->getRequest(),
user: $this->getRequest()->getAttribute('identity')?->getIdentifier()
)
);
}
}
```
Expand Down Expand Up @@ -216,7 +212,7 @@ EventManager::instance()->on(new ApplicationMetadata('my_blog_app', [
Implementing your own metadata listeners is as simple as attaching the listener to the `AuditStash.beforeLog` event. For example:

```php
EventManager::instance()->on('AuditStash.beforeLog', function ($event, array $logs) {
EventManager::instance()->on('AuditStash.beforeLog', function (EventInterface $event, array $logs): void {
foreach ($logs as $log) {
$log->setMetaInfo($log->getMetaInfo() + ['extra' => 'This is extra data to be stored']);
}
Expand All @@ -233,7 +229,7 @@ use AuditStash\PersisterInterface;

class MyPersister implements PersisterInterface
{
public function logEvents(array $auditLogs)
public function logEvents(array $auditLogs): void
{
foreach ($auditLogs as $log) {
$eventType = $log->getEventType();
Expand Down Expand Up @@ -274,27 +270,31 @@ The configuration contains the fully namespaced class name of your persister.

### Working With Transactional Queries

Occasionally, you may want to wrap a number of database changes in a transaction, so that it can be rolled back if one part of the process fails. In order to create audit logs during a transaction, some additional setup is required. First create the file `src/Model/Audit/AuditTrail.php` with the following:
Occasionally, you may want to wrap a number of database changes in a transaction, so that it can be rolled back if one
part of the process fails. In order to create audit logs during a transaction, some additional setup is required. First
create the file `src/Model/Audit/AuditTrail.php` with the following:

```php
<?php
declare(strict_types=1);

namespace App\Model\Audit;

use Cake\Utility\Text;
use SplObjectStorage;

class AuditTrail
{
protected $_auditQueue;
protected $_auditTransaction;
protected SplObjectStorage $_auditQueue;
protected string $_auditTransaction;

public function __construct()
{
$this->_auditQueue = new SplObjectStorage;
$this->_auditTransaction = Text::uuid();
}

public function toSaveOptions()
public function toSaveOptions(): array
{
return [
'_auditQueue' => $this->_auditQueue,
Expand All @@ -309,6 +309,7 @@ Anywhere you wish to use `Connection::transactional()`, you will need to first i
```php
use App\Model\Audit\AuditTrail;
use Cake\Event\Event;
use \ArrayObject
```

Your transaction should then look similar to this example of a BookmarksController:
Expand All @@ -323,14 +324,17 @@ $success = $this->Bookmarks->connection()->transactional(function () use ($trail
...
$bookmarkN = $this->Bookmarks->newEntity();
$bookmarkN->save($dataN, $trail->toSaveOptions());

return true;
});

if ($success) {
$event = new Event('Model.afterCommit', $this->Bookmarks);
$table->behaviors()->get('AuditLog')->afterCommit($event, $result, $auditTrail->toSaveOptions());
$this->Bookmarks->->behaviors()->get('AuditLog')->afterCommit(
$event,
$result,
new ArrayObject($auditTrail->toSaveOptions())
);
}
```

This will save all audit info for your objects, as well as audits for any associated data. Please note, `$result` must be an instance of an Object. Do not change the text "Model.afterCommit".
This will save all audit info for your objects, as well as audits for any associated data. Please note, `$result` must
be an instance of an Object. Do not change the text "Model.afterCommit".
Loading

0 comments on commit 14afbcc

Please sign in to comment.