Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.idea
/.phpunit.result.cache
/tools
/nbproject/*
17 changes: 6 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Trash

[![Build Status](https://img.shields.io/github/actions/workflow/status/UseMuffin/Trash/ci.yml?style=flat-square
)](https://github.com/UseMuffin/Trash/actions?query=workflow%3ACI+branch%3Amaster)
[![Coverage](https://img.shields.io/codecov/c/github/UseMuffin/Trash/master.svg?style=flat-square)](https://codecov.io/github/UseMuffin/Trash)
[![Total Downloads](https://img.shields.io/packagist/dt/muffin/trash.svg?style=flat-square)](https://packagist.org/packages/muffin/trash)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE)

Adds "soft"-delete support to CakePHP tables.
Expand All @@ -13,7 +9,7 @@ Adds "soft"-delete support to CakePHP tables.
Using [Composer][composer]:

```
composer require muffin/trash
composer require vandalorumrex/trash
```

You then need to load the plugin. You can use the shell command:
Expand Down Expand Up @@ -115,13 +111,12 @@ To ensure your PRs are considered for upstream, you MUST follow the CakePHP codi

## Bugs & Feedback

http://github.com/usemuffin/trash/issues
http://github.com/vandalorumrex/trash/issues

## License

Copyright (c) 2015-present, [Use Muffin][muffin] and licensed under [The MIT License][mit].
This library is license under the MIT License (MIT). Please see License File for more information.

[cakephp]:http://cakephp.org
[composer]:http://getcomposer.org
[mit]:http://www.opensource.org/licenses/mit-license.php
[muffin]:http://usemuffin.com
## Credits

This library was forked from [UseMuffin Trash's](https://github.com/UseMuffin) on Github https://github.com/UseMuffin/Trash.
15 changes: 10 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "muffin/trash",
"name": "vandalorumrex/trash",
"description": "Adds soft delete support to CakePHP ORM tables.",
"type": "cakephp-plugin",
"keywords": [
"cakephp",
"muffin",
"vandalorumrex",
"trash",
"orm"
],
"homepage": "https://github.com/usemuffin/trash",
"homepage": "https://github.com/vandalorumrex/trash",
"license": "MIT",
"authors": [
{
Expand All @@ -21,14 +21,19 @@
"homepage": "https://github.com/ADmad",
"role": "Author"
},
{
"name": "VandalorumRex",
"homepage": "https://github.com/VandalorumRex",
"role": "Fixer"
},
{
"name": "Others",
"homepage": "https://github.com/usemuffin/trash/graphs/contributors"
}
],
"support": {
"issues": "https://github.com/usemuffin/trash/issues",
"source": "https://github.com/usemuffin/trash"
"issues": "https://github.com/vandalorumrex/trash/issues",
"source": "https://github.com/vandalorumrex/trash"
},
"require": {
"php": ">=8.1",
Expand Down
52 changes: 28 additions & 24 deletions src/Model/Behavior/TrashBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use Cake\Core\Exception\CakeException;
use Cake\Database\Expression\FieldInterface;
use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\Database\Query\SelectQuery;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\I18n\DateTime;
use Cake\ORM\Association;
use Cake\ORM\Behavior;
use Cake\ORM\Table;
use Closure;
use InvalidArgumentException;
use function Cake\Core\pluginSplit;

Expand All @@ -23,7 +25,7 @@
*/
class TrashBehavior extends Behavior
{
public const AFTER_DELETE_EVENT_OPTION = 'trash';
public const string AFTER_DELETE_EVENT_OPTION = 'trash';

Check failure on line 28 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Class constants with native types are supported only on PHP 8.3 and later.

/**
* Default configuration.
Expand Down Expand Up @@ -137,7 +139,7 @@
* Trash given entity.
*
* @param \Cake\Datasource\EntityInterface $entity EntityInterface.
* @param array $options Trash operation options.
* @param array<string, mixed> $options Trash operation options.
* @return bool
* @throws \Cake\Core\Exception\CakeException if no primary key is set on entity.
*/
Expand All @@ -160,7 +162,11 @@
}
}

$entity->set($this->getTrashField(false), new DateTime());
if (method_exists($entity, 'patch')) {

Check failure on line 165 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Call to function method_exists() with Cake\Datasource\EntityInterface and 'patch' will always evaluate to true.
$entity = $entity->patch([$this->getTrashField(false) => new DateTime()]);
} else {
$entity->set($this->getTrashField(false), new DateTime());
}

return (bool)$this->_table->save($entity, $options);
}
Expand Down Expand Up @@ -202,17 +208,15 @@
}

if (
$expression instanceof IdentifierExpression
&& in_array($expression->getIdentifier(), $fieldIdentifiers, true)
$expression instanceof IdentifierExpression && in_array($expression->getIdentifier(), $fieldIdentifiers, true)

Check warning on line 211 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Line exceeds 120 characters; contains 126 characters
) {
$addCondition = false;

return;
}

if (
$expression instanceof FieldInterface
&& in_array($expression->getField(), $fieldIdentifiers, true)
$expression instanceof FieldInterface && in_array($expression->getField(), $fieldIdentifiers, true)
) {
$addCondition = false;
}
Expand All @@ -231,8 +235,8 @@
public function findOnlyTrashed(SelectQuery $query, array $options): SelectQuery
{
return $query
->applyOptions(['skipAddTrashCondition' => true])
->andWhere([$this->getTrashField() . ' IS NOT' => null]);
->applyOptions(['skipAddTrashCondition' => true])
->andWhere([$this->getTrashField() . ' IS NOT' => null]);
}

/**
Expand All @@ -250,15 +254,14 @@
/**
* Marks all rows matching `$conditions` as `trashed`.
*
* @param mixed $conditions Conditions to be used, accepts anything Query::where()
* can take.
* @param \Cake\Database\Expression\QueryExpression|\Closure|array|string|null $conditions Conditions to be used, accepts anything Query::where() can take.
* @return int Count Returns the affected rows.
*/
public function trashAll(mixed $conditions): int
public function trashAll(array|QueryExpression|Closure|string|null $conditions): int
{
return $this->_table->updateAll(
[$this->getTrashField(false) => new DateTime()],
$conditions
$conditions,
);
}

Expand All @@ -276,7 +279,7 @@
* Restores all (or given) trashed row(s).
*
* @param \Cake\Datasource\EntityInterface|null $entity to restore.
* @param array $options Restore operation options (only applies when restoring a specific entity).
* @param array<string, mixed> $options Restore operation options (only applies when restoring a specific entity).
* @return \Cake\Datasource\EntityInterface|int|false
*/
public function restoreTrash(?EntityInterface $entity = null, array $options = []): false|int|EntityInterface
Expand All @@ -287,7 +290,11 @@
if ($entity->isDirty()) {
throw new CakeException('Can not restore from a dirty entity.');
}
$entity->set($data, ['guard' => false]);
if (method_exists($entity, 'patch')) {

Check failure on line 293 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Call to function method_exists() with Cake\Datasource\EntityInterface and 'patch' will always evaluate to true.
$entity = $entity->patch($data, ['guard' => false]);
} else {
$entity->set($data, ['guard' => false]);
}

return $this->_table->save($entity, $options);
}
Expand All @@ -299,12 +306,12 @@
* Restore an item from trashed status and all its related data
*
* @param \Cake\Datasource\EntityInterface|null $entity Entity instance
* @param array $options Restore operation options (only applies when restoring a specific entity).
* @param array<string, mixed> $options Restore operation options (only applies when restoring a specific entity).
* @return \Cake\Datasource\EntityInterface|int|bool
*/
public function cascadingRestoreTrash(
?EntityInterface $entity = null,
array $options = []
array $options = [],
): bool|int|EntityInterface {
$result = $this->restoreTrash($entity, $options);

Expand Down Expand Up @@ -346,6 +353,7 @@
*/
public function getTrashField(bool $aliased = true): string
{
/** @var string|null $field */
$field = $this->getConfig('field');

if ($field === null) {
Expand All @@ -356,7 +364,7 @@
break;
}
}

/** @var string|null $field */
$field ??= Configure::read('Muffin/Trash.field');

if ($field === null) {
Expand All @@ -383,11 +391,7 @@
protected function _isRecursable(Association $association, Table $table): bool
{
return (
$association->getTarget()->hasBehavior('Trash')
|| $association->getTarget()->hasBehavior(static::class)
)
&& $association->isOwningSide($table)
&& $association->getDependent()
&& $association->getCascadeCallbacks();
$association->getTarget()->hasBehavior('Trash') || $association->getTarget()->hasBehavior(static::class)
) && $association->isOwningSide($table) && $association->getDependent() && $association->getCascadeCallbacks();

Check warning on line 395 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Line exceeds 120 characters; contains 123 characters
}
}
Loading