Skip to content
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

[10.x] Add Conditionable and Macroable traits to Sleep #47092

Closed
wants to merge 4 commits into from
Closed

[10.x] Add Conditionable and Macroable traits to Sleep #47092

wants to merge 4 commits into from

Conversation

bradietilley
Copy link
Contributor

With the new release of Sleep you can easily interface with the sleeping mechanism of PHP, which is great! A few areas we could improve this:

Conditionables

One thing it lacks is the ability to conditionally define (or override) the time to sleep (TTS?), which could be easily achieved using the Illuminate\Support\Conditionable trait. For a rudimentary example:

Sleep::for(1)->second()
    ->unless(
        $some->status === SomeStatus::PENDING,
        fn (Sleep $sleep) => $sleep->and(500)->milliseconds()
    );

Macros

Another thing it lacks is the ability to define reusable macros to override or supplement the TTS. This could be very helpful if you application sleeps in various places and you want standardised sleep logic, or wish to leverage a database setting (perhaps user-defined) to control the sleep. For a rudimentary example:

// AppServiceProvider
Sleep::macro('forConfiguredTime', static function () {
    $milliseconds = DB::table('settings')->where('key', 'sleep_after_action')->first()->value ?? 1000; // let's say 3500 ms

    return Sleep::for($milliseconds)->milliseconds();
} );

// Throughout your application:
Sleep::forConfiguredTime(); // e.g. 3500ms

Replacing durations

To supplement both of the aforementioned features, the ability to replace the previously defined duration would be helpful, so this has been added as well. For example:

$sleep = Sleep::for(1)->second();

// Replace the duration
$sleep->duration(800)->milliseconds();

// $sleep = 800ms not 1s

Happy to rename this to ->set() or ->replace() or whatever.


While these changes might not seem immediately useful or crucial to all, I foresee them being helpful to people writing elegant sleep rules.

@taylorotwell
Copy link
Member

Should be two separate PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants