-
-
Notifications
You must be signed in to change notification settings - Fork 33
Add Events #428
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
Merged
+1,193
−63
Merged
Add Events #428
Changes from 33 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
895ae13
Add Events
Tigrov 1a6d192
Apply fixes from StyleCI
StyleCIBot 5b0bd2e
Apply Rector changes (CI)
Tigrov dfe978b
Fix psalm
Tigrov a8b557b
Merge branch 'master' into add-events
Tigrov 00d8a5e
Merge branch 'master' into add-events
vjik 325876c
Improve and add `SoftDelete`, `AfterUpsert`, `BeforeUpsert`
Tigrov 295191c
Fix
Tigrov f08df59
Fix
Tigrov 8109d02
Replace `ActiveRecordInterface` with `object` in `EventDispatcher::ad…
Tigrov e69c1bc
Replace with `EventDispatcherProvider`
Tigrov f93fd17
Replace with `AttributeHandlerProvider`
Tigrov 7ba4059
Add doc [skip ci]
Tigrov 7635f3e
Update doc [skip ci]
Tigrov 8e2d33e
Improve `EventDispatcherProvider`
Tigrov 5d129a3
Improve doc
Tigrov 827447b
Merge branch 'master' into add-events
Tigrov 935078c
Improve
Tigrov 3f9e104
Merge branch 'master' into add-events
Tigrov d1bd615
Merge branch 'master' into add-events
Tigrov 2256687
Merge branch 'master' into add-events
Tigrov 8b73636
Apply fixes from StyleCI
StyleCIBot a01d9e1
Fix tests
Tigrov 19bdf90
Fix composer-require-checker
Tigrov f1ec06d
Fix composer-require-checker
Tigrov 7463fe5
Fix composer-require-checker
Tigrov e294a86
Merge branch 'master' into add-events
Tigrov 863d710
Merge branch 'master' into add-events
vjik 7975b89
Fix psalm
Tigrov 7650a1e
Update doc
Tigrov f5c4484
Merge branch 'master' into add-events
Tigrov 86598c6
Merge branch 'master' into add-events
Tigrov da0ceae
Merge branch 'master' into add-events
vjik 3aadcdc
Cleanup (#453)
vjik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
{ | ||
"symbol-whitelist": [ | ||
"Psr\\EventDispatcher\\EventDispatcherInterface", | ||
"Psr\\EventDispatcher\\StoppableEventInterface", | ||
"Psr\\Http\\Message\\ResponseInterface", | ||
"Psr\\Http\\Message\\ServerRequestInterface", | ||
"Psr\\Http\\Server\\MiddlewareInterface", | ||
"Psr\\Http\\Server\\RequestHandlerInterface", | ||
"Yiisoft\\Arrays\\ArrayableTrait", | ||
"Yiisoft\\EventDispatcher\\Dispatcher\\Dispatcher", | ||
"Yiisoft\\EventDispatcher\\Provider\\ListenerCollection", | ||
"Yiisoft\\EventDispatcher\\Provider\\Provider", | ||
"Yiisoft\\Factory\\Factory" | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ActiveRecord\Event; | ||
|
||
use Psr\EventDispatcher\StoppableEventInterface; | ||
use Yiisoft\ActiveRecord\ActiveRecordInterface; | ||
|
||
/** | ||
* Base class for events in Active Record models. | ||
*/ | ||
abstract class AbstractEvent implements StoppableEventInterface | ||
{ | ||
/** @var bool Whether the default action of the event should be prevented. */ | ||
private bool $isDefaultPrevented = false; | ||
/** @var bool Whether the propagation of the event should be stopped. */ | ||
private bool $isPropagationStopped = false; | ||
/** @var mixed The return value if the default action is prevented. */ | ||
private mixed $returnValue = null; | ||
|
||
/** | ||
* @param ActiveRecordInterface $model The target model associated with this event. | ||
Tigrov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public function __construct(private readonly ActiveRecordInterface $model) | ||
Tigrov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
} | ||
|
||
/** | ||
* @return ActiveRecordInterface The target model associated with this event. | ||
*/ | ||
public function getModel(): ActiveRecordInterface | ||
{ | ||
return $this->model; | ||
} | ||
|
||
/** | ||
* Returns the value that will be returned by the method that triggered this event | ||
* if the {@see isDefaultPrevented() default action is prevented}. | ||
*/ | ||
public function getReturnValue(): mixed | ||
{ | ||
return $this->returnValue; | ||
} | ||
|
||
/** | ||
* Checks if the default action associated with this event has been prevented. | ||
*/ | ||
public function isDefaultPrevented(): bool | ||
{ | ||
return $this->isDefaultPrevented; | ||
} | ||
|
||
public function isPropagationStopped(): bool | ||
{ | ||
return $this->isPropagationStopped; | ||
} | ||
|
||
/** | ||
* Prevents the default action associated with this event from being executed. | ||
* | ||
* @see returnValue() | ||
*/ | ||
public function preventDefault(): void | ||
{ | ||
$this->isDefaultPrevented = true; | ||
} | ||
|
||
/** | ||
* Sets the return value which will be returned by the method that triggered this event | ||
* if the {@see isDefaultPrevented() default action is prevented}. | ||
Tigrov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @see preventDefault() | ||
*/ | ||
public function returnValue(mixed $returnValue): void | ||
{ | ||
$this->returnValue = $returnValue; | ||
} | ||
|
||
/** | ||
* Stops the propagation of the event to further listeners. | ||
* No further listeners will be notified after this method is called. | ||
*/ | ||
public function stopPropagation(): void | ||
{ | ||
$this->isPropagationStopped = true; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ActiveRecord\Event; | ||
|
||
use Yiisoft\ActiveRecord\ActiveQueryInterface; | ||
use Yiisoft\ActiveRecord\ActiveRecordInterface; | ||
|
||
/** | ||
* Event triggered after the query has been created for the {@see ActiveRecordInterface} model. | ||
* | ||
* @see ActiveRecordInterface::query() | ||
*/ | ||
final class AfterCreateQuery extends AbstractEvent | ||
{ | ||
public function __construct( | ||
ActiveRecordInterface $model, | ||
public ActiveQueryInterface &$query, | ||
) { | ||
parent::__construct($model); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ActiveRecord\Event; | ||
|
||
use Yiisoft\ActiveRecord\ActiveRecordInterface; | ||
|
||
/** | ||
* Event triggered after the record has been deleted from the database. | ||
* | ||
* @see ActiveRecordInterface::delete() | ||
*/ | ||
final class AfterDelete extends AbstractEvent | ||
{ | ||
/** | ||
* @param ActiveRecordInterface $model The model that was deleted. | ||
* @param int $count Number of deleted rows. | ||
*/ | ||
public function __construct(ActiveRecordInterface $model, public int &$count) | ||
{ | ||
parent::__construct($model); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ActiveRecord\Event; | ||
|
||
use Yiisoft\ActiveRecord\ActiveRecordInterface; | ||
|
||
/** | ||
* Event triggered after the record has been inserted into the database. | ||
* | ||
* @see ActiveRecordInterface::insert | ||
*/ | ||
final class AfterInsert extends AbstractEvent | ||
{ | ||
/** | ||
* @param ActiveRecordInterface $model The model that has been inserted. | ||
* @param bool $isSuccessful Whether the insert operation is successful. | ||
*/ | ||
public function __construct(ActiveRecordInterface $model, public bool &$isSuccessful) | ||
{ | ||
parent::__construct($model); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ActiveRecord\Event; | ||
|
||
use Yiisoft\ActiveRecord\ActiveRecordInterface; | ||
|
||
/** | ||
* Event triggered after the model has been populated with data. | ||
* | ||
* @see ActiveRecordInterface::populate() | ||
*/ | ||
final class AfterPopulate extends AbstractEvent | ||
{ | ||
/** | ||
* @param ActiveRecordInterface $model The model that has been populated. | ||
* @param array $data The data used to populate the model. | ||
*/ | ||
public function __construct(ActiveRecordInterface $model, public readonly array $data) | ||
{ | ||
parent::__construct($model); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ActiveRecord\Event; | ||
|
||
use Yiisoft\ActiveRecord\ActiveRecordInterface; | ||
|
||
/** | ||
* Event triggered after the model has been saved to the database. | ||
* | ||
* @see ActiveRecordInterface::afterSave() | ||
*/ | ||
final class AfterSave extends AbstractEvent | ||
{ | ||
/** | ||
* @param ActiveRecordInterface $model The model that was saved. | ||
* @param bool $isSuccessful Whether the save operation was successful. | ||
*/ | ||
public function __construct(ActiveRecordInterface $model, public bool &$isSuccessful) | ||
{ | ||
parent::__construct($model); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ActiveRecord\Event; | ||
|
||
use Yiisoft\ActiveRecord\ActiveRecordInterface; | ||
|
||
/** | ||
* Event triggered after the record has been updated in the database. | ||
* | ||
* @see ActiveRecordInterface::update() | ||
*/ | ||
final class AfterUpdate extends AbstractEvent | ||
{ | ||
/** | ||
* @param ActiveRecordInterface $model The model that was updated. | ||
* @param int $count The number of rows that were updated. | ||
*/ | ||
public function __construct(ActiveRecordInterface $model, public int &$count) | ||
{ | ||
parent::__construct($model); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ActiveRecord\Event; | ||
|
||
use Yiisoft\ActiveRecord\ActiveRecordInterface; | ||
|
||
/** | ||
* Event triggered after the model has been upserted (inserted or updated). | ||
* | ||
* @see ActiveRecordInterface::upsert() | ||
*/ | ||
final class AfterUpsert extends AbstractEvent | ||
{ | ||
/** | ||
* @param ActiveRecordInterface $model The model that was upserted. | ||
* @param bool $isSuccessful Whether the upsert operation was successful. | ||
*/ | ||
public function __construct(ActiveRecordInterface $model, public bool &$isSuccessful) | ||
{ | ||
parent::__construct($model); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ActiveRecord\Event; | ||
|
||
use Yiisoft\ActiveRecord\ActiveRecordInterface; | ||
|
||
/** | ||
* Event triggered before creating the query for the {@see ActiveRecordInterface} model. | ||
* | ||
* @see ActiveRecordInterface::query() | ||
*/ | ||
final class BeforeCreateQuery extends AbstractEvent | ||
{ | ||
public function __construct( | ||
ActiveRecordInterface $model, | ||
) { | ||
parent::__construct($model); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ActiveRecord\Event; | ||
|
||
/** | ||
* Event triggered before the record is deleted from the database. | ||
* | ||
* @see ActiveRecordInterface::delete() | ||
*/ | ||
final class BeforeDelete extends AbstractEvent | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
Tigrov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ActiveRecord\Event; | ||
|
||
use Yiisoft\ActiveRecord\ActiveRecordInterface; | ||
|
||
/** | ||
* Event triggered before a new record is inserted into the database. | ||
* It allows to modify properties that will be used for {@see ActiveRecordInterface::insert()} operation. | ||
* | ||
* @see ActiveRecordInterface::insert() | ||
*/ | ||
final class BeforeInsert extends AbstractEvent | ||
{ | ||
/** | ||
* @param ActiveRecordInterface $model The model that is being inserted. | ||
* @param array|null &$properties The properties that will be used for the insert operation. | ||
*/ | ||
public function __construct(ActiveRecordInterface $model, public array|null &$properties) | ||
{ | ||
parent::__construct($model); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ActiveRecord\Event; | ||
|
||
use Yiisoft\ActiveRecord\ActiveRecordInterface; | ||
|
||
/** | ||
* Event triggered before the model is populated with data. | ||
* It allows to modify the data that will be used for {@see ActiveRecordInterface::populateRecord()} operation. | ||
* | ||
* @see ActiveRecordInterface::populateRecord() | ||
*/ | ||
final class BeforePopulate extends AbstractEvent | ||
{ | ||
/** | ||
* @param ActiveRecordInterface $model The model that will be populated. | ||
* @param array &$data The data that will be used to populate the model. | ||
*/ | ||
public function __construct(ActiveRecordInterface $model, public array &$data) | ||
{ | ||
parent::__construct($model); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.