基于CodeIgniter的事件驱动扩展和开发规范 步骤 1.数据库设计和基本结构 1.1我们用一张表来表示文件和文件夹,主要字段和意义如下表所示。 id 文件的唯一标识id name 文件名 uid 文件创建者的id created 文件创建的时间戳 modified 文件修改的时间戳 size 文
基于CodeIgniter的事件驱动扩展和开发规范
步骤
1.数据库设计和基本结构
1.1我们用一张表来表示文件和文件夹,主要字段和意义如下表所示。
id | 文件的唯一标识id |
name | 文件名 |
uid | 文件创建者的id |
created | 文件创建的时间戳 |
modified | 文件修改的时间戳 |
size | 文件大小 |
type | 文件类型 |
pid | 文件父目录的id |
path | 文件的物理位置 |
route | 文件从父目录到根节点的所有祖先id |
version | 文件版本号 |
文件系统是一种简单的树形结构,特点是:文件夹节点有子节点,文件节点没有子节点。
2.类设计和定义
2.1要考虑的问题
从要实现的操作“增、删、改、查”来看,要考虑的主要问题只有一个,就是在对文件夹节点进行删改操作时,如何让上层目录和下层文件或文件夹进行相应的更改。
2.2希望的写法
1.对文件类(也可以用来表示文件夹)有简单的save , set , delete 等方法。
2.希望有一个类来表示文件的所有祖先节点,这个类可以是一个简单的文件类的集合。它在必要时可以将所有的祖先文件夹实例化。它能提供一些简单的集合操作方法。
3.希望有一个类来表示文件的所有子文件和子文件夹,他能提供一些方法让我批量操作子文件或文件夹。
2.3类定义
这里的实体类,和实体集合类参考了前端框架backbone。
我先定义了一个实体类,这个类有基本增删改查操作。
<span>class</span><span> Entity { </span><span>private</span> <span>$attributes</span> = <span>array</span><span>(); </span><span>private</span> <span>$is_deleted</span> = <span>false</span><span>; </span><span>public</span> <span>function</span><span> __construct() { } </span><span>public</span> <span>function</span> <span>reset</span>( <span>$data</span><span> ) { </span><span>$this</span> -><span> chunk(); </span><span>return</span> <span>$this</span> -> set( <span>$data</span><span> ); } </span><span>public</span> <span>function</span> get( <span>$attr_name</span><span> ) { </span><span>return</span> <span>isset</span>( <span>$this</span> -> attributes[<span>$attr_name</span>] ) ? <span>$this</span> -> attributes[<span>$attr_name</span>]->value : <span>false</span><span>; } </span><span>public</span> <span>function</span> get_attr_detail( <span>$attr_name</span><span> ){ </span><span>return</span> <span>isset</span>( <span>$this</span> -> attributes[<span>$attr_name</span>] ) ? <span>$this</span> -> attributes[<span>$attr_name</span>] : <span>false</span><span>; } </span><span>public</span> <span>function</span><span> set() { </span><span>if</span>( <span>$this</span> -><span> is_deleted ){ </span><span>return</span> <span>false</span><span>; } </span><span>$args</span> = <span>func_get_args</span><span>(); </span><span>if</span>( <span>count</span>( <span>$args</span>) == 1<span> ){ </span><span>$attr_array</span> = ( <span>array</span> ) <span>$args</span>[0<span>]; }</span><span>else</span> <span>if</span>( <span>count</span>( <span>$args</span>) == 2<span> ){ </span><span>$attr_array</span> = <span>array</span>(<span>$args</span>[0] => <span>$args</span>[1<span>] ); } </span><span>foreach</span>( <span>$attr_array</span> <span>as</span> <span>$key</span> => <span>$value</span><span> ) { </span><span>$value_obj</span> = <span>new</span><span> stdClass(); </span><span>$value_obj</span> -> changed = <span>false</span><span>; </span><span>$value_obj</span> -> <span>new</span> = <span>true</span><span>; </span><span>if</span>( <span>isset</span>(<span>$this</span> -> attributes[<span>$key</span><span>])){ </span><span>$value_obj</span> -> <span>new</span> = <span>false</span><span>; </span><span>if</span>( <span>$this</span> -> attributes[<span>$key</span>] -> value !== <span>$value</span><span> ){ </span><span>$value_obj</span> -> changed = <span>true</span><span>; </span><span>$value_obj</span> -> last = <span>$this</span> -> attributes[<span>$key</span>] -><span> value; } } </span><span>$value_obj</span> -> value = <span>$value</span><span>; </span><span>$this</span> -> attributes[<span>$key</span>] = <span>$value_obj</span><span>; } </span><span>return</span> <span>$this</span><span>; } </span><span>public</span> <span>function</span><span> chunk() { </span><span>$this</span> -> attributes = <span>array</span><span>( ); </span><span>return</span> <span>$this</span><span>; } </span><span>public</span> <span>function</span><span> save() { </span><span>return</span> <span>false</span><span>; } </span><span>public</span> <span>function</span><span> delete(){ </span><span>$this</span> -> deleted = <span>true</span><span>; } </span><span>public</span> <span>function</span><span> to_object() { </span><span>$object</span> = <span>new</span><span> stdClass(); </span><span>foreach</span>( <span>$this</span> -> attributes <span>as</span> <span>$key</span> => <span>$value_obj</span><span> ) { </span><span>$object</span> -> <span>$key</span> = <span>$value_obj</span> -><span>value; } </span><span>return</span> <span>$object</span><span>; } </span><span>public</span> <span>function</span><span> to_array() { </span><span>return</span> ( <span>array</span> ) <span>$this</span> -><span> to_object(); } </span><span>public</span> <span>function</span><span> is_empty(){ </span><span>return</span> <span>empty</span>( <span>$this</span> -><span> attributes ); } </span><span>public</span> <span>function</span><span> is_delete(){ </span><span>return</span> <span>$this</span> -><span> deleted; } }</span>
然后定义了一个集合类,这个类实现了spl的IteratorAggregate 接口,使得php可以对它进行foreach操作。
<span>class</span> Entity_collection <span>implements</span><span> IteratorAggregate{ </span><span>public</span> <span>$length</span> = 0<span>; </span><span>public</span> <span>$options</span> = <span>array</span><span>( </span>'child_class' => "Entity"<span> ); </span><span>public</span> <span>$children</span> = <span>array</span><span>( ); </span><span>public</span> <span>function</span> __construct( <span>$options</span><span> ) { </span><span>$this</span> -> options = <span>array_merge</span>( <span>$this</span> -> options, <span>$options</span><span> ); </span><span>$this</span> -> load_children( <span>$this</span> -><span> options ); } </span><span>private</span> <span>function</span> load_children( &<span>$options</span><span> ){ </span><span>if</span>( !<span>empty</span>( <span>$options</span>['children_data'<span>] ) ) { </span><span>$this</span> -> reset_from_data( <span>$options</span>['children_data'<span>] ); } </span><span>else</span> <span>if</span>( !<span>empty</span>( <span>$options</span>['children'<span>] ) ) { </span><span>$this</span> -> <span>reset</span>( <span>$options</span>['children'<span>] ); } } </span><span>public</span> <span>function</span><span> to_array() { </span><span>$output</span> = <span>array</span><span>( ); </span><span>foreach</span>( <span>$this</span> -> children <span>as</span> <span>$child</span><span> ) { </span><span>$output</span>[] = <span>$child</span> -><span> to_object(); } </span><span>return</span> <span>$output</span><span>; } </span><span>public</span> <span>function</span> <span>reset</span>( &<span>$children</span><span> ) { </span><span>$this</span> -> children = <span>$children</span><span>; </span><span>$this</span> -> length = <span>count</span>( <span>$children</span><span> ); } </span><span>public</span> <span>function</span> reset_from_data( <span>$children_data</span><span> ) { </span><span>foreach</span>( <span>$children_data</span> <span>as</span> <span>$child_data</span><span> ) { </span><span>$class_name</span> = <span>$this</span> -> options['child_class'<span>]; </span><span>$children_obj</span> = <span>new</span> <span>$class_name</span>( <span>$child_data</span><span> ); </span><span>if</span>( !<span>$children_obj</span> -><span> is_empty() ){ </span><span>$this</span> -> children[] = <span>$children_obj</span><span>; } } </span><span>$this</span> -> length = <span>count</span>( <span>$this</span> -><span> children ); } </span><span>public</span> <span>function</span> get( <span>$id</span><span> ){ </span><span>foreach</span>( <span>$this</span> -> children <span>as</span> &<span>$child</span><span> ){ </span><span>if</span>( <span>$child</span> -> get("id") == <span>$id</span><span> ){ </span><span>return</span> <span>$child</span><span>; } } </span><span>return</span> <span>false</span><span>; } </span><span>public</span> <span>function</span><span> getIterator(){ </span><span>return</span> <span>new</span> ArrayIterator( <span>$this</span> -><span> children ); } }</span>
定义了文件(文件夹)类,继承自实体类。这里注意里面使用另一个叫做file_database的单例,这个单例是用来进行具体数据库操作。单独提出来是为了提高扩展性。用户可以通过修改file_database类来应对自己的需求。
<span>class</span> <span>File</span> <span>extends</span><span> Entity{ </span><span>private</span> <span>$file_database</span><span>; </span><span>public</span> <span>function</span> __construct( <span>$fid</span> = <span>false</span><span> ){ parent</span>::<span>__construct(); </span><span>$this</span> -> file_database = File_database::<span>get_instance(); </span><span>if</span>( <span>$fid</span><span> ){ </span><span>if</span>( <span>is_object</span>( <span>$fid</span> ) || <span>is_array</span>( <span>$fid</span><span> ) ){ </span><span>$data</span> = <span>$fid</span><span>; }</span><span>else</span><span>{ </span><span>$data</span> = <span>$this</span> -> file_database -> load_file( <span>$fid</span><span> ); } </span><span>$this</span> -> <span>reset</span>( <span>$data</span><span> ); } } </span><span>public</span> <span>function</span><span> save(){ </span><span>$data</span> = <span>$this</span> -><span> to_object(); </span><span>$id</span> = <span>$this</span> -> get( 'id'<span> ); </span><span>if</span>( <span>$id</span><span> ){ </span><span>$new_data</span> = <span>$this</span> -> file_database -> update_file( <span>$data</span><span> ); }</span><span>else</span><span>{ </span><span>$new_data</span> = <span>$this</span> -> file_database -> insert_file( <span>$data</span><span> ); } </span><span>if</span>( <span>$new_data</span><span> ){ </span><span>$this</span> -> <span>reset</span>( <span>$new_data</span><span> ); }</span><span>else</span><span>{ </span><span>//</span><span>错误处理</span> <span> } </span><span>return</span> <span>$this</span><span>; } </span><span>public</span> <span>function</span><span> delete(){ } }</span>
文件祖先类,这里注意它不是直接集成自实体集合类。
<span>class</span> File_ancenstors <span>implements</span><span> IteratorAggregate{ </span><span>public</span> <span>$file</span> = <span>false</span><span>; </span><span>public</span> <span>$ancestors</span><span>; </span><span>public</span> <span>function</span> __construct( <span>$fid</span><span> ){ </span><span>$this</span> -> <span>file</span> = <span>new</span> <span>File</span>( <span>$fid</span><span> ); </span><span>$this</span> -> load_ancestor_entities( <span>$this</span> -> <span>file</span> -> get( 'route'<span> ) ); } </span><span>private</span> <span>function</span> load_ancestor_entities( <span>$route</span><span> ){ </span><span>$ancestor_ids</span> = <span>explode</span>( '/', <span>$route</span><span> ); </span><span>$collection_options</span> = <span>array</span><span>( </span>'child_class' => "File", 'children_data' => <span>$ancestor_ids</span><span> ); </span><span>$this</span> -> ancestors = <span>new</span> Entity_collection( <span>$collection_options</span><span> ); </span><span>return</span> <span>$this</span><span>; } </span><span>public</span> <span>function</span><span> getIterator(){ </span><span>return</span> <span>$this</span> -><span> ancestors; } }</span>
子文件(不包括后代文件)集合类
<span>class</span> File_children <span>extends</span><span> Entity_collection{ </span><span>private</span> <span>$file_database</span><span>; </span><span>public</span> <span>function</span> __construct( <span>$fid</span><span> ){ </span><span>$this</span> -> file_database = File_database::<span>get_instance(); </span><span>$options</span> = <span>array</span><span>( </span>'child_class' => 'File',<span> ); parent</span>::__construct( <span>$options</span><span> ); </span><span>$this</span> -> load_children_entities( <span>$fid</span><span> ); } </span><span>public</span> <span>function</span> load_children_entities( <span>$fid</span><span> ){ </span><span>$children_data</span> = <span>$this</span> -> file_database -> load_children_files( <span>$fid</span><span> ); </span><span>$this</span> -> reset_from_data( <span>$children_data</span><span> ); </span><span>return</span> <span>$this</span><span>; } }</span>
最后数据库操作类
<span>class</span><span> File_database{ </span><span>const</span> table = 'files'<span>; </span><span>static</span> <span>$instance</span> = <span>false</span><span>; </span><span>private</span> <span>$CI</span> = <span>false</span><span>; </span><span>static</span> <span>function</span><span> get_instance(){ </span><span>if</span>( !File_database::<span>$instance</span><span> ){ </span><span>new</span><span> File_database(); } </span><span>return</span> File_database::<span>$instance</span><span>; } </span><span>private</span> <span>$db</span><span>; </span><span>public</span> <span>function</span><span> __construct(){ </span><span>$this</span> -> CI = &<span>get_instance(); </span><span>$this</span> -> db = <span>$this</span> -> CI -><span> db; self</span>::<span>$instance</span> = &<span>$this</span><span>; } </span><span>public</span> <span>function</span> load_file( <span>$fid</span><span> ){ </span><span>$mid</span> = <span>$this</span> -> CI -> user -> id % var_get("user_mod"<span>); </span><span>$table_name</span> = self::table ."_{<span>$mid</span>}"<span>; </span><span>return</span> <span>$this</span> -> db -> get_where( <span>$table_name</span>, <span>array</span>( "id" => <span>$fid</span> ) ) -><span> row(); } </span><span>public</span> <span>function</span> update_file( <span>$file</span><span> ){ </span><span>if</span>( !<span>isset</span>( <span>$file</span> -><span> id ) ){ </span><span>return</span> <span>false</span><span>; } </span><span>$update_data</span> = ( <span>array</span> ) <span>$file</span><span>; </span><span>$mid</span> = <span>$update_data</span>['uid'] % var_get("user_mod"<span>); </span><span>$table_name</span> = self::table."_{<span>$mid</span>}"<span>; </span><span>$this</span> -> db -> where( "id", <span>$update_data</span>['id'<span>] ); </span><span>unset</span>( <span>$update_data</span>['id'<span>] ); </span><span>$update_result</span> = <span>$this</span> -> db -> update( <span>$table_name</span>, <span>$update_data</span><span> ); </span><span>return</span> <span>$update_result</span> ? <span>$file</span> : <span>false</span><span>; } </span><span>public</span> <span>function</span> insert_file( <span>$file</span><span> ){ </span><span>if</span>( <span>isset</span>( <span>$file</span> -><span> id ) ){ </span><span>return</span> <span>false</span><span>; } </span><span>$insert_data</span> = ( <span>array</span> ) <span>$file</span><span>; </span><span>$insert_data</span>['id'] = <span>$this</span> -><span> generate_file_id(); </span><span>$mid</span> = <span>$insert_data</span>['uid'] % var_get("user_mod"<span>); </span><span>$table_name</span> = self::table."_{<span>$mid</span>}"<span>; </span><span>$insert_result</span> = <span>$this</span> -> db -> insert( <span>$table_name</span>, <span>$insert_data</span><span> ); </span><span>if</span>( <span>$insert_result</span><span> ){ </span><span>$this</span> -> db -> where( 'id', <span>$insert_data</span>['id'<span>] ); </span><span>$new_data</span> = <span>$this</span> -> db -> get( <span>$table_name</span> ) -><span> row(); } </span><span>return</span> <span>$insert_result</span> ? <span>$new_data</span> : <span>false</span><span>; } </span><span>private</span> <span>function</span><span> generate_file_id(){ </span><span>do</span><span>{ </span><span>$fid</span> = <span>$this</span> -> salt( 16<span> ); }</span><span>while</span>( <span>$this</span> -> file_id_exist( <span>$fid</span><span> ) ); </span><span>return</span> <span>$fid</span><span>; } </span><span>private</span> <span>function</span> salt( <span>$length</span> = 16<span> ){ </span><span>return</span> <span>substr</span>( <span>md5</span>( <span>uniqid</span>( <span>rand</span>(), <span>true</span> ) ), 0, <span>$length</span><span> ); } </span><span>public</span> <span>function</span> file_id_exist( <span>$fid</span><span> ){ </span><span>if</span>( <span>$this</span>-> db -> query( "SELECT id FROM files WHERE id = '{<span>$fid</span>}'" ) -><span> num_rows() ){ </span><span>return</span> <span>true</span><span>; } </span><span>return</span> <span>false</span><span>; } </span><span>public</span> <span>function</span> load_children_files( <span>$fid</span><span> ){ </span><span>$mid</span> = <span>$this</span> -> CI -> user -> id % var_get("user_mod"<span>); </span><span>$table_name</span> = self::table ."_{<span>$mid</span>}"<span>; </span><span>$this</span> -> db -> where( 'pid', <span>$fid</span><span> ); </span><span>$mysql_result</span> = <span>$this</span> -> db -> get( <span>$table_name</span><span> ); </span><span>return</span> <span>$mysql_result</span> ? <span>$mysql_result</span> -> result() : <span>array</span><span>(); } }</span>
3.模块定义
3.1希望的写法
1.希望在写对任何一个节点的逻辑操作时,无论增删改查,我都可以先只考虑考虑当前节点。通过抛出事件让自己的模块,或者其他扩展模块来决定要不要进行相应的其他操作。
2.在写任何一个针对文件某一变化(如修改、删除)时,代码不关心只对自己模块内的变化负责。
3.2模块实现
这里我使用CodeIgniter作为基础框架,读者也可以直接自己移植。具体的内容请看注释。
<span>php </span><span>/*</span><span>* * @author 侯振宇 * @date 2012-6-26 * @encode UTF-8 </span><span>*/</span> <span>class</span> File_model <span>extends</span><span> CI_Model{ </span><span>public</span> <span>function</span><span> __construct( ){ parent</span>::<span>__construct( ); </span><span>//</span><span>file factory 包含了和文件相关的类</span> <span>$this</span> -> load -> library("File_factory"<span>); } </span><span>//</span><span>声明本模块需要进行权限验证的接口</span> <span>public</span> <span>function</span><span> auth(){ </span><span>return</span> <span>array</span><span>( </span>'main/file_list' => <span>array</span><span>( </span>'file_owner_validate' => <span>array</span><span>( </span>'validate' => 'is_operator_file_owner'<span> ) )</span>, 'main/file_update' => <span>array</span><span>( </span>'file_owner_validate' => <span>array</span><span>( </span>'validate' => 'is_operator_file_owner'<span> ) ) ); } </span><span>//</span><span>声明模块要监听的事件,可以是内部事件,也可以是外部事件。 //这里监听的都是内部的文件操作的事件,如果修改,增加。为的是分离祖先后者后台的连带操作。</span> <span>public</span> <span>function</span><span> listen(){ </span><span>return</span> <span>array</span><span>( </span>"file_update" => "react_file_update", "file_insert" => "react_file_insert", "file_remove" => "react_file_remove",<span> ); } </span><span>//</span><span>文件更新后 更新父目录的size等属性。</span> <span>public</span> <span>function</span> react_file_update( <span>$file</span><span> ){ </span><span>//</span><span>size</span> <span>if</span>( <span>$file</span> -> get_attr_detail("size") -><span> changed ){ </span><span>$size_change_value</span> = <span>$file</span> -> get("size") - <span>$file</span> -> get_attr_detail("size") -><span> last; </span><span>$ancestors</span> = <span>new</span> File_ancenstors( <span>$file</span>->get("id"<span>) ); </span><span>foreach</span>( <span>$ancestors</span> <span>as</span> &<span>$ancestor</span><span> ){ </span><span>$ancestor</span> -> set( "size", <span>$ancestor</span> -> get("size") + <span>$size_change_value</span><span> ); </span><span>$ancestor</span> -><span> save(); } } } </span><span>//</span><span>文件插入后,更新文件route。当然这个操作你可以在文件新建的时候就写好,这里只是示例。</span> <span>public</span> <span>function</span> react_file_insert( <span>$file</span><span> ){ </span><span>$pfile</span> = <span>new</span> <span>File</span>( <span>$file</span> -> get("pid"<span>) ); </span><span>$file_route</span> = <span>explode</span>('/', <span>$pfile</span> -> get('route'<span>) ); </span><span>array_push</span>( <span>$file_route</span>, <span>$file</span> -> get("pid"<span>) ); </span><span>$file_route_str</span> = <span>implode</span>('/', <span>array_filter</span>( <span>$file_route</span><span>) ); </span><span>$file</span> -> set("route" , <span>$file_route_str</span>) -><span> save(); } </span><span>public</span> <span>function</span><span> is_operator_file_owner(){ </span><span>//</span><span>根据自己情况进行判断</span> <span>return</span> <span>true</span><span>; } </span><span>//</span><span>文件的一个列表</span> <span>public</span> <span>function</span> file_list( <span>$fid</span><span> ){ </span><span>$files</span> = <span>new</span> File_children( <span>$fid</span><span> ); </span><span>return</span> <span>$files</span> -><span> to_array(); } </span><span>//</span><span>单个文件载入</span> <span>public</span> <span>function</span> file_load(<span>$fid</span><span>){ </span><span>$file</span> = <span>new</span> <span>File</span>( <span>$fid</span><span> ); </span><span>return</span> <span>$file</span> -><span> to_object(); } </span><span>//</span><span>某一文件的祖先</span> <span>public</span> <span>function</span> file_ancestors( <span>$fid</span><span> ){ </span><span>$file_ancenstors</span> = <span>new</span> File_ancenstors( <span>$fid</span><span> ); </span><span>return</span> <span>$file_ancenstors</span> -> ancestors -><span> to_array() ; } </span><span>//</span><span>创建文件</span> <span>public</span> <span>function</span><span> file_new(){ </span><span>//</span><span>测试例子</span> <span>$file_attrs</span> = <span>array</span><span>( </span>'name' => "new_test", 'uid' => 151, 'created' => now(), 'pid' => '8a0341838c007cf4',<span> ); </span><span>$file</span> = <span>new</span> <span>File</span>( <span>$file_attrs</span><span> ); </span><span>$file</span> -><span> save(); </span><span>$this</span> -> event -> trigger("file_insert", <span>$file</span><span> ); </span><span>return</span> <span>$file</span><span>; } </span><span>//</span><span>创建文件夹</span> <span>public</span> <span>function</span> folder_new( <span>$pid</span>, <span>$name</span><span> ){ </span><span>$file_attrs</span> = <span>array</span><span>( </span>'name' => <span>$name</span>, 'uid' => <span>$this</span> -> user -> id, 'created' => now(), 'pid' => <span>$pid</span>,<span> ); </span><span>$file</span> = <span>new</span> <span>File</span>( <span>$file_attrs</span><span> ); </span><span>$file</span> -><span> save(); </span><span>$this</span> -> event -> trigger("file_insert", <span>$file</span><span> ); </span><span>return</span> <span>$file</span> -><span> to_object() ; } </span><span>//</span><span>文件更新</span> <span>public</span> <span>function</span> file_update( <span>$fid</span>, <span>$data</span><span> ){ </span><span>$file</span> = <span>new</span> <span>File</span>( <span>$fid</span><span> ); </span><span>$file</span> -> set( <span>$data</span><span> ); </span><span>$file</span> -><span> save(); </span><span>$this</span> -> event -> trigger( "file_update", <span>$file</span><span> ); } } </span>?>
总结
事件驱动 相比 模块间通过接口来沟通 的最大好处应该是进一步降低了耦合,同时使系统的逻辑更容易阅读。
希望读者能够仔细阅读代码,能给我提出一些您的宝贵建议。毕竟我不希望我的博客只是放出一个现成的东西,编程让大家测试一下而已。贵在交流。
我有两个项目会基于事件驱动来做,非商业的,开源的,个人觉得还比较好玩。有兴趣参与的请与我联系。留言或者email都可以。谢谢。