PHP code example of bradietilley / laravel-actions
1. Go to this page and download the library: Download bradietilley/laravel-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/ */
bradietilley / laravel-actions example snippets
/**
* Using the Action class
*/
class AssignDefaultRole extends \BradieTilley\Actions\Action
{
public function __construct(public readonly User $user)
{}
public function handle(): User
{
if ($this->user->role) {
return $this->user->role;
}
$this->user->update([
'role' => $default => Role::DEFAULT,
]);
return $default;
}
}
/**
* Using the Actionable interface and Dispatchable trait.
*/
class AssignDefaultRole implements \BradieTilley\Actions\Contracts\Actionable
{
use \BradieTilley\Actions\Dispatchable;
public function __construct(public readonly User $user)
{}
public function handle(): User
{
if ($this->user->role) {
return $this->user->role;
}
$this->user->update([
'role' => $default => Role::DEFAULT,
]);
return $default;
}
}
Action::replace(ExampleActionA::class, ExampleActionB::class);
ExampleActionA::dispatch($user, '123');
// Runs ExampleActionB with $user and '123' constructor arguments instead of ExampleActionA
Action::replace([
// replace an action from a package and use your own action
DownloadAvatarFromUrl::class => CustomDownloadAvatarFromUrl::class,
// temporarily disable an integration
SynchroniseMemberToMailchimp::class => CustomEmptyAction::class,
]);
use BradieTilley\Actions\Facades\Action;
// your test
Action::fake();
// your app
AssignDefaultRole::dispatch($user);
// your test
Action::assertDispatched(AssignDefaultRole::class); // pass
Action::assertNotDispatched(AssignAdminRole::class); // pass
use BradieTilley\Actions\Facades\Action;
// your test
Action::fake([
RecordAuditLog::class,
]);
// your app
AssignDefaultRole::dispatch($user); // still runs
RecordAuditLog::dispatch($user); // doesn't run
// your test
Action::assertDispatched(RecordAuditLog::class); // pass
use BradieTilley\Actions\Facades\Action;
// your test
Action::fake()->except([
AssignDefaultRole::class,
]);
// your app
AssignDefaultRole::dispatch($user); // still runs
RecordAuditLog::dispatch($user); // doesn't run
// your test
Action::assertDispatched(RecordAuditLog::class); // pass
Action::fake([
ExampleA::class,
]);
ExampleA::dispatch(); // will skip
ExampleB::dispatch(); // will run
Action::addFake(ExampleB::class);
ExampleA::dispatch(); // will skip
ExampleB::dispatch(); // will skip
Action::removeFake(ExampleA::class);
ExampleA::dispatch(); // will run
ExampleB::dispatch(); // will skip
use BradieTilley\Actions\Facades\Action;
// your test
Action::fake()->allowExecution();
// your app
AssignDefaultRole::dispatch($user); // still runs
RecordAuditLog::dispatch($user); // still runs
// your test
Action::assertDispatched(RecordAuditLog::class); // pass
use BradieTilley\Actions\Facades\Action;
// your test
Action::fake()->disallowExecution();
// your app
AssignDefaultRole::dispatch($user); // doesn't run
RecordAuditLog::dispatch($user); // doesn't run
// your test
Action::assertDispatched(RecordAuditLog::class); // pass
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
Event::listen(function (ActionDispatching $event) {
Log::channel('actions')->debug(sprintf(
'Running action %s',
$event->action::class,
));
});
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
Event::listen(function (ActionDispatched $event) {
Log::channel('actions')->debug(sprintf(
'Successfuly ran action %s in %s milliseconds',
$event->action::class,
$event->duration->asMilliseconds(),
));
});
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
Event::listen(function (ActionFailed $event) {
Log::channel('actions')->debug(sprintf(
'Failed to run action %s with error %s (see sentry)',
$event->action::class,
$event->error->getMessage(),
));
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.