中国青基会
RSS
热门关键字:  None  linux+moodle安装  mac  rhel5  199
当前位置 : Nixsky>程序设计>PHP>列表

PHP 中的批处理

来源:IBM DW中国 作者:Jack D. Herrington 时间:2007-03-03 点击:

为了演示这种功能,我们看看在命令行上发生了什么:

% php generic_test_add.php 
% php generic_process.php 
Printing: foo
%

输出并不多,但是您能够看出要点。通过这种机制,可以将任何 PHP 函数的处理推迟。

现在,如果您不喜欢将 PHP 函数名和参数放进数据库中,那么另一种方法是在 PHP 代码中建立数据库中的 “处理作业类型” 名称和实际 PHP 处理函数之间的映射。按照这种方式,如果以后决定修改 PHP 后端,那么只要 “处理作业类型” 字符串匹配,系统就仍然可以工作。

放弃数据库

最后,我演示另一种稍有不同的解决方案,它使用一个目录中的文件来存储批作业,而不是使用数据库。在这里提供这个思路并不是建议您 “采用这种方式,而不使用数据库”,这只是一种可供选择的方式,是否采用它由您决定。

显然,这个解决方案中没有模式,因为我们不使用数据库。所以先编写一个类,它包含与前面示例中相似的 add()get_all()delete() 方法。


清单 9. batch_by_file.php

				
<?php
define( 'BATCH_DIRECTORY', 'batch_items/' );
class BatchFiles
{
  public static function delete( $id )
  {
    unlink( $id );
    return true;
  }
  public static function add( $function, $args )
  {
    $path = '';
    while( true )
    {
        $path = BATCH_DIRECTORY.time();
        if ( file_exists( $path ) == false )
            break;
    }

    $fh = fopen( $path, "w" );
    fprintf( $fh, $function."\n" );
    foreach( $args as $k => $v )
    {
        fprintf( $fh, $k.":".$v."\n" );
    }
    fclose( $fh );

    return true;
  }
  public static function get_all()
  {
    $rows = array();
    if (is_dir(BATCH_DIRECTORY)) {
        if ($dh = opendir(BATCH_DIRECTORY)) {
            while (($file = readdir($dh)) !== false) {
                $path = BATCH_DIRECTORY.$file;
                if ( is_dir( $path ) == false )
                {
                    $item = array();
                    $item['id'] = $path;
                    $fh = fopen( $path, 'r' );
                    if ( $fh )
                    {
                        $item['function'] = trim(fgets( $fh ));
                        $item['args'] = array();
                        while( ( $line = fgets( $fh ) ) != null )
                        {
                            $args = split( ':', trim($line) );
                            $item['args'][$args[0]] = $args[1];
                        }
                        $rows []= $item;
                        fclose( $fh );
                    }
                }
            }
            closedir($dh);
        }
    }
    return $rows;
  }
}
?>

BatchFiles 类有三个主要方法:add()get_all()delete()。这个类不访问数据库,而是读写 batch_items 目录中的文件。

使用以下测试代码添加新的批处理条目。


清单 10. batch_by_file_test_add.php

				
<?php
require_once 'batch_by_file.php';

BatchFiles::add( "printvalue", array( 'value' => 'foo' ) );
?>

有一点需要注意:除了类名(BatchFiles)之外,实际上没有任何迹象能够说明作业是如何存储的。所以,以后很容易将它改为数据库风格的存储方式,而不需要修改接口。

上一篇:没有了
下一篇:php分页处理
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册