PHP code example of lemax10 / simple-actions
1. Go to this page and download the library: Download lemax10/simple-actions library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lemax10 / simple-actions example snippets
declare(strict_types=1);
namespace Example/Actions;
use LeMaX10\SimpleActions\Action;
/**
* @method ExampleModel run(int $id)
*/
final class ExampleAction extends Action
{
protected function handle(int $id): ExampleModel
{
return ExampleModel::findOrFail($id);
}
}
var_dump(ExampleAction::make()->run(1)); // (ExampleModel)
declare(strict_types=1);
namespace Example/Actions;
use LeMaX10\SimpleActions\Action;
/**
* @method ExampleContactModel run(string $email, string $name)
*/
final class ExampleAction extends Action
{
protected bool $singleTransaction = true;
protected function handle(string $email, string $name): ExampleContactModel
{
$contact = new ExampleContactModel([
'name' => $name
]);
$contact->email()->associate($this->getEmailModel($email));
$contact->save();
return $contact;
}
private function getEmailModel(string $email): ExampleEmailModel
{
return ExampleEmailModel::firstOrCreate([
'email' =>
]);
}
}
var_dump(ExampleAction::make()->run('[email protected] ', 'User Name')); // (ExampleContactModel) ['name' => 'User Name', 'email' => (ExampleEmailModel) ['email' => '[email protected] ']]
declare(strict_types=1);
namespace Example/Actions;
use LeMaX10\SimpleActions\Action;
/**
* @method ExampleModel run()
*/
final class ExampleAction extends Action
{
protected function handle(): array
{
return ExampleContactModel::get()->all();
}
}
// Simple in 10 minutes
var_dump(ExampleAction::make()->remember('exampleKey', 10)->run()); // ['1', '2', 3']
// Forever
var_dump(ExampleAction::make()->rememberForever('exampleKey')->run()); // ['1', '2', 3']
// Tags cache
var_dump(ExampleAction::make()->tags(['exampleTag1', 'exampleTag2'])->remember('exampleKey')->run()); // ['1', '2', 3']
declare(strict_types=1);
namespace Example/Actions;
use LeMaX10\SimpleActions\Action;
/**
* @method ExampleModel run()
*/
final class ExampleAction extends Action
{
protected array $items = [];
protected static boot(): void
{
static::beforeRun(function(ExampleAction $action): void {
if ($action->parameters['contenxt'] === 'test') {
$action->items[] = 'attachTestContext';
}
})
}
protected function handle(string $context = []): array
{
return $this->items;
}
}
// -- And Create external listener
ExampleAction::beforeRun(function(ExampleAction $action): void {
if ($action->parameters['content'] === 'test2') {
$action->items[] = 'attachOtherTestContent';
}
})
// WithOut test context
var_dump(ExampleAction::make()->run('example')); // []
// With test context
var_dump(ExampleAction::make()->run('test')); // ['attachTestContext']
// With test2 context
var_dump(ExampleAction::make()->run('test2')); // ['attachOtherTestContent']
// Without all events
var_dump(ExampleAction::withoutEvents(fn() => ExampleAction::make()->run('test2'))) // []
app()->bind(ExampleAction::class, CustomExampleAction::class);