|
| 1 | +# Laravel Delay |
| 2 | + |
| 3 | +Package which helps you to delay (sleep) your code for some time. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +### Requirements |
| 8 | +- Laravel 8.12+ / 9.0+ |
| 9 | +- PHP 8.0 |
| 10 | + |
| 11 | +### Installation |
| 12 | +Require the package via Composer: |
| 13 | + |
| 14 | +```bash |
| 15 | +composer require newman/laravel-delay |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +### Using as Trait |
| 21 | + |
| 22 | +```php |
| 23 | +<?php |
| 24 | + |
| 25 | +namespace App\Console\Commands; |
| 26 | + |
| 27 | +use Illuminate\Console\Command; |
| 28 | +use Newman\LaravelDelay\Traits\Delayable; |
| 29 | + |
| 30 | +class ImportTask extends Command { |
| 31 | + use Delayable; |
| 32 | + |
| 33 | + // ... |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +then in code you can call the delay the code execution like this: |
| 38 | + |
| 39 | +```php |
| 40 | +$this->delay()->for(5); |
| 41 | +``` |
| 42 | + |
| 43 | +or |
| 44 | + |
| 45 | +```php |
| 46 | +$this->delay(5); |
| 47 | +``` |
| 48 | + |
| 49 | +They both will delay execution for 5 seconds. |
| 50 | + |
| 51 | +You can include trait in any class you'd like to use it, including controllers. |
| 52 | + |
| 53 | +### Using as service container |
| 54 | + |
| 55 | +```php |
| 56 | +use Newman\LaravelDelay\Contracts\DelayContract; |
| 57 | + |
| 58 | +// ... |
| 59 | + |
| 60 | +app()->make(DelayContract::class)->for(5); |
| 61 | +``` |
| 62 | + |
| 63 | +### Usage samples |
| 64 | + |
| 65 | +Let's assume we're using Trait. |
| 66 | + |
| 67 | +**Delay execution for 10 seconds:** |
| 68 | + |
| 69 | +```php |
| 70 | +$this->delay(10); |
| 71 | +$this->delay()->for(10); |
| 72 | +$this->delay()->forSeconds(10); |
| 73 | +``` |
| 74 | + |
| 75 | +Note that in case you want to delay for fractions of a second, you should use `forMs` function. |
| 76 | + |
| 77 | +**Delay execution for 1500 miliseconds (1.5 second):** |
| 78 | + |
| 79 | +```php |
| 80 | +$this->delay()->forMs(1500); |
| 81 | +$this->delay()->forMiliseconds(1500); |
| 82 | +``` |
| 83 | + |
| 84 | +**Delay execution for 5000 microseconds (0.005 second):** |
| 85 | + |
| 86 | +```php |
| 87 | +$this->delay()->forMicroseconds(5000); |
| 88 | +``` |
| 89 | + |
| 90 | +**Delay execution till given Carbon datetime:** |
| 91 | + |
| 92 | +```php |
| 93 | +$this->delay()->till(Carbon::now()->addMinutes(5)->addSeconds(15)); |
| 94 | +``` |
| 95 | + |
| 96 | +**Delay execution for 10 seconds only on given environment/-s:** |
| 97 | + |
| 98 | +```php |
| 99 | +$this->delay()->for(10)->environments(['production']); // delays only on production |
| 100 | +$this->delay()->for(10)->environments(['production', 'staging']); // delays on production and staging only |
| 101 | +``` |
| 102 | + |
| 103 | +**Delay execution for 10 seconds except given environment/-s:** |
| 104 | + |
| 105 | +```php |
| 106 | +$this->delay()->for(10)->except(['prodction']); // delays on all environments, except production |
| 107 | +$this->delay()->for(10)->except(['prodction', 'staging']); // delays on all environments, except production and staging |
| 108 | +``` |
| 109 | + |
| 110 | +**Delay execution for 10 seconds only when callback returns false:** |
| 111 | + |
| 112 | +```php |
| 113 | +$this->delay()->for(10)->exceptWhen(fn () => 1 + 1 == 2); // code will not delay in this case, because callback returns true |
| 114 | +$this->delay()->for(10)->exceptWhen(fn () => 1 + 1 == 3); // code will delay in this case, because callback returns false |
| 115 | +``` |
| 116 | + |
| 117 | +and we can even pass multiple callbacks. |
| 118 | + |
| 119 | +```php |
| 120 | +$this->delay()->for(10)->exceptWhen(fn () => false)->exceptWhen(fn () => false); // code will delay |
| 121 | +$this->delay()->for(10)->exceptWhen(fn () => true)->exceptWhen(fn () => false); // code will not delay, because all callbacks doesn't return false |
| 122 | +``` |
| 123 | + |
| 124 | +**At last we can chain multiple conditions:** |
| 125 | + |
| 126 | +It will delay for 10 seconds only on production & staging environments and only when it's not 10 AM. |
| 127 | + |
| 128 | +```php |
| 129 | +$this->delay() |
| 130 | + ->for(10) |
| 131 | + ->environments(['production', 'staging']) |
| 132 | + ->exceptWhen(fn () => Carbon::now()->hour == 10); |
| 133 | +``` |
0 commit comments