newman/laravel-delay
Composer 安装命令:
composer require newman/laravel-delay
包简介
Laravel delay helper package
README 文档
README
Package which helps you to delay (sleep) your code for some time.
Note before continuing
As of Laravel version 10.10.0 you may not need this package anymore in favour of in-built helper - https://laravel.com/docs/10.x/helpers#sleep
Installation
Requirements
- Laravel 8.12+, 9.0+, 10.0+
- PHP 8.0
Installation
Require the package via Composer:
composer require newman/laravel-delay
📖 Usage
Using as Facade
use Newman\LaravelDelay\Facades\Delay; // ... Delay::for(3);
Using as Trait
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Newman\LaravelDelay\Traits\Delayable; class ImportTask extends Command { use Delayable; // ... }
then in code you can call the delay the code execution like this:
$this->delay()->for(5);
or
$this->delay(5);
They both will delay execution for 5 seconds.
You can include trait in any class you'd like to use it, including controllers.
Using as service container
use Newman\LaravelDelay\Contracts\DelayContract; // ... app()->make(DelayContract::class)->for(5);
Usage samples
Let's assume we're using Trait.
Delay execution for 10 seconds:
$this->delay(10); $this->delay()->for(10); $this->delay()->forSeconds(10);
Note that in case you want to delay for fractions of a second, you should use forMs function.
Delay execution for 1500 miliseconds (1.5 second):
$this->delay()->forMs(1500); $this->delay()->forMiliseconds(1500);
Delay execution for 5000 microseconds (0.005 second):
$this->delay()->forMicroseconds(5000);
Delay execution till given Carbon datetime:
$this->delay()->till(Carbon::now()->addMinutes(5)->addSeconds(15));
Delay execution for 10 seconds only on given environment/-s:
$this->delay()->for(10)->environments(['production']); // delays only on production $this->delay()->for(10)->environments(['production', 'staging']); // delays on production and staging only
Delay execution for 10 seconds except given environment/-s:
$this->delay()->for(10)->except(['prodction']); // delays on all environments, except production $this->delay()->for(10)->except(['prodction', 'staging']); // delays on all environments, except production and staging
Delay execution for 10 seconds only when callback returns false:
$this->delay()->for(10)->exceptWhen(fn () => 1 + 1 == 2); // code will not delay in this case, because callback returns true $this->delay()->for(10)->exceptWhen(fn () => 1 + 1 == 3); // code will delay in this case, because callback returns false
and we can even pass multiple callbacks.
$this->delay()->for(10)->exceptWhen(fn () => false)->exceptWhen(fn () => false); // code will delay $this->delay()->for(10)->exceptWhen(fn () => true)->exceptWhen(fn () => false); // code will not delay, because all callbacks doesn't return false
At last we can chain multiple conditions:
It will delay for 10 seconds only on production & staging environments and only when it's not 10 AM.
$this->delay() ->for(10) ->environments(['production', 'staging']) ->exceptWhen(fn () => Carbon::now()->hour == 10);
🤝 Contributing
We'll appreciate your collaboration to this package.
When making pull requests, make sure:
- All tests are passing:
composer test - Test coverage is not reduced:
composer test-coverage - There are no PHPStan errors:
composer phpstan - Coding standard is followed:
composer lintorcomposer fix-styleto automatically fix it.
newman/laravel-delay 适用场景与选型建议
newman/laravel-delay 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.01k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sleep」 「delay」 「timeout」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 newman/laravel-delay 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 newman/laravel-delay 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 newman/laravel-delay 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Emulated timeouts for synchronous operations.
Mock any function that exists in global namespace
Lightweight library that provides regular PHP methods is a non-blocking implementation.
Users are warned about expiring session.
Universal PHP library for different perposis
Translation bundle
统计信息
- 总下载量: 21.01k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-23