Skip to content

Commit d6e5b67

Browse files
committed
initial
0 parents  commit d6e5b67

19 files changed

+864
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
composer.lock
2+
vendor/
3+
.idea
4+
.git
5+
.phpunit.result.cache
6+
.php-cs-fixer.cache

.php-cs-fixer.dist.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpCsFixer\Config;
6+
7+
require __DIR__ . '/vendor/autoload.php';
8+
9+
return (new Config())
10+
->setFinder(PhpCsFixer\Finder::create()->in(__DIR__ . '/src'))
11+
->setRules([
12+
'line_ending' => false,
13+
'concat_space' => [
14+
'spacing' => 'one',
15+
],
16+
'cast_spaces' => [
17+
'space' => 'none',
18+
],
19+
'not_operator_with_successor_space' => false,
20+
'simplified_null_return' => false,
21+
'explicit_string_variable' => true,
22+
'phpdoc_to_comment' => false,
23+
])
24+
->setRiskyAllowed(true);

LICENSE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2022 Edgars Neimanis
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
```

composer.json

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"name": "newman/laravel-delay",
3+
"description": "Laravel delay helper package",
4+
"keywords": [
5+
"delay",
6+
"sleep",
7+
"timeout"
8+
],
9+
"type": "library",
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Edgars Neimanis",
14+
"email": "neimanis.edgars@gmail.com"
15+
}
16+
],
17+
"require": {
18+
"php": "^8.0",
19+
"illuminate/contracts": "^8.12|^9.0",
20+
"illuminate/support": "^8.12|^9.0",
21+
"nesbot/carbon": "^2.13"
22+
},
23+
"require-dev": {
24+
"friendsofphp/php-cs-fixer": "^3.0",
25+
"nunomaduro/larastan": "^1.0",
26+
"orchestra/testbench": "^6.0|^7.0",
27+
"phpunit/phpunit": "^8.0|^9.0"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"Newman\\LaravelDelay\\": "src/"
32+
}
33+
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"Newman\\LaravelDelay\\Tests\\": "tests/"
37+
}
38+
},
39+
"scripts": {
40+
"phpstan": "phpstan analyse --memory-limit=256M",
41+
"lint": "php-cs-fixer fix --diff --dry-run",
42+
"fix-style": "php-cs-fixer fix",
43+
"test": "phpunit --colors=always --verbose"
44+
},
45+
"extra": {
46+
"laravel": {
47+
"providers": [
48+
"Newman\\LaravelDelay\\DelayServiceProvider"
49+
]
50+
}
51+
},
52+
"minimum-stability": "dev",
53+
"prefer-stable": true,
54+
"config": {
55+
"preferred-install": "dist",
56+
"sort-packages": true
57+
}
58+
}

docker/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data

docker/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM php:8.0-cli-alpine
2+
3+
COPY --from=mlocati/php-extension-installer:1.2.25 /usr/bin/install-php-extensions /usr/local/bin/
4+
5+
RUN install-php-extensions \
6+
xdebug && \
7+
rm /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
8+
9+
COPY --from=composer:2.2.4 /usr/bin/composer /usr/local/bin/
10+
11+
COPY ../ /app
12+
13+
WORKDIR /app

docker/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is only meant for development purposes.

docker/docker-compose.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: '3.4'
2+
3+
services:
4+
php:
5+
build: .
6+
volumes:
7+
- '../:/app'
8+
working_dir: '/app'
9+
tty: true

phpstan.neon.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
includes:
2+
- ./vendor/nunomaduro/larastan/extension.neon
3+
parameters:
4+
level: max
5+
paths:
6+
- src/

0 commit comments

Comments
 (0)