devster / ika
PHP 命令/事务设计模式库
dev-master
2012-11-16 20:29 UTC
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-08-29 01:39:34 UTC
README
PHP 5.3 库,用于使用命令/事务设计模式
概念
Ika 依赖于与 SQL 事务相同的操作,但使用各种动作,如果发生错误,则回滚这些动作。
一个应用程序的示例将是一个安装程序,执行多个操作,如创建文件夹、复制文件、运行 SQL 查询等,如果其中一项操作失败,则清理之前的操作。
工作原理
您创建几个命令,每个命令都有一个向上操作和一个向下操作。当您开始事务时,所有命令的向上操作将按添加顺序运行。
如果发生错误,您可以回滚。它将按相反顺序运行所有成功命令的向下操作。就是这样。简单。
安装
传统方式
下载 Ika 的最新版本,并将 Ika
命名空间添加到您的 PSR-0 自动加载系统中,或者直接 require src/autoload.php
。
Composer
只需创建一个 composer.json 文件,然后运行 php composer.phar install 命令即可安装。
{ "require": { "devster/ika": ">=1.0.*" } }
用法
基本用法
这里有一个使用简单命令的示例
use Ika\Transaction; $t = new Transaction; // Register new command to the transaction and set it up $t->register('new_dir')->setUp(function($prev, $command) { // the `command` var is the new command object you just registered // $prev is the return of the previous hook/command // Here it is NULL because there is nothing before mkdir('test'); })->setDown(function($prev, $command) { rmdir('test'); }); // add a second command, this library is useless for just one command :) $t->register('create_file')->setUp(function() { // some code that throw an exception throw new Exception('Error !'); })->setDown(function(){}); // Here we go! We run the transaction try { $result = $t->begin(); } catch(TransactionException $e) { // Execute just the down action of the first command `rmdir('test')` $t->rollback(); }
使用更复杂的命令
如果您的命令更复杂,您可以扩展 Ika\Command
。
use Ika\Command; class MyCommand extends Command { public function getName() { return 'my_command'; } public function isEnabled() { // your code that decide if this command // must be run when the transaction will be executed // By default return true return true; } public function up($prev) { mkdir('test'); } public function down($prev) { rmdir(test); } }
$t = new Transaction; // add your new fresh command $t->add(new MyCommand); // add a bunch of commands //$t->addCommands(array(new Command, ...)); try { $t->begin(); } catch(TransactionException $e) { $t->rollback(); }
钩子
Ika 集成了钩子系统。以下是钩子列表
- pre // execute before any actions up or down
- post // executed after any actions up or down
- preUp // executed before up actions
- postUp // executed after up actions
- preDown // executed before down actions
- postDown // executed after down actions
$t = new Transaction; $t->addHook('preUp', function($prev, $command) { // $prev is the return of the previous hook/command // $command is the current command executed echo 'Execution of '.$command->getName(); });
如果您的钩子更复杂,您可以扩展 Ika\Transaction
。
use Ika\Transaction; class MyTransaction extends Transaction { public function initialize() { $this->addHook('pre', function($prev, $command, $direction){ echo 'Execution of '.$direction.' '.$command->getName(); }); $this->addHook('postDown', function(){}); // etc } }
技巧
您可以在任何命令处开始事务
$t->begin('my_command');
您可以在任何命令处开始回滚
$t->rollback('my_command');
关于
要求
- PHP 5.3 的任何版本都可以
作者
- Jeremy Perret jeremy@devster.org
许可
Ika 使用 MIT 许可证授权 - 有关详细信息,请参阅 LICENSE 文件