定制 derywat/php-abortable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

derywat/php-abortable

最新稳定版本:0.1.1

Composer 安装命令:

composer require derywat/php-abortable

包简介

PHP library to manage aborting code execution.

README 文档

README

Overview

Library allows aborting code by use of AbortController.

Usage

Abortable method in class

Implement abortable methods by using AbortableTrait in your class.
In places where it's safe to abort methods code, check for abort and exit or throw. AbortException may be used to abort any method and catch aborted state in caller code.

use derywat\abortable\AbortableInterface;
use derywat\abortable\AbortableTrait;

class MyClass implements AbortableInterface {
	use AbortableTrait;

	protected function myAbortableMethod(){

		while($codeRuns){

			/* 
			 *  some code block that cannot be aborted
			 */

			//check conditions and abort in places where it's safe to abort
			if($this->isAborted()){
				//do some cleanup and abort execution

				//AbortException can be used to abort execution and catch in caller
				throw new AbortException('abort request received.');
			}

			/* 
			 *  some code block that cannot be aborted
			 */

			//check multiple times if needed...
			if($this->isAborted()){
				//do some cleanup and abort execution
				throw new AbortException('abort request received.');
			}

		}

	}

}

Aborting from caller code

Using AbortController with single or multiple abort closures

Any number of abort closures may be added to AbortController. Abort closures will run every time when MyClass calls isAborted method.
Closure runs in the same process as abortable code. Returning true from closure will result in isAborted returning true. Catching AbortException allows to check if method was aborted.

use derywat\abortable\AbortController;
use derywat\abortable\AbortException;

$instance = new MyClass();

$instance->registerAbortController(
	(new AbortController())
		->addClosure(
			function():bool {
				//check conditions for abort
				//return true if code should abort
				//false otherwise
			}
		)
		->addClosure(
			function():bool {
				//another closure...
			}
		)
);

//running abortable method
try {
	$instance->myAbortableMethod();
	//myAbortableMethod returns to here if not aborted
} catch(AbortException $e){
	//this code runs if myAbortableMethod was aborted
}

统计信息

  • 总下载量: 28
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-09