所謂老的系統(tǒng),是指沒有使用php 5.3以上命名空間(namespace)特性編碼的系統(tǒng)。
但是,只要你的系統(tǒng)運(yùn)行在 php 5.3及以上的環(huán)境,在你的老系統(tǒng)中,是可以使用這些基于新特性如命名空間編碼的庫(kù)或代碼的。
以前只是有潔癖不用而已。
比如,我是個(gè)工具控,想讓所用的禪道系統(tǒng)也像那些國(guó)際化開源 issue 項(xiàng)目一樣有一套標(biāo)準(zhǔn)開放的 api - 禪道本身是有套 html、json 自適配模式可以當(dāng)接口用的,可以用于其他客戶端或系統(tǒng)集成。這幾天在嘗試編寫的用于兼容 redmine rest 風(fēng)格接口的禪道 pms api,就有意識(shí)的用了這種混合的寫法。
由于要兼容 redmine 的 rest 風(fēng)格,首先選用了 slim 這個(gè)微服務(wù)框架,毫無疑問,它是要求運(yùn)行環(huán)境>5.3的,但我總得復(fù)用禪道已有的代碼,這樣效率才高。
原理很簡(jiǎn)單,就是一根反斜杠,或者兩根。
先用composer 初始化了slim 庫(kù)。
重點(diǎn)部位的代碼:
入口文件 index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
require __dir__ . '/vendor/autoload.php' ; require __dir__ . '/inc/zentao/nb/autoloader.php' ; \zentao\nb\autoloader::register(); $app = \zentao\core\application::app(dirname(ztnb_root)); //禪道的router $slim = new \slim\slim(); $routes = require __dir__ . '/data/config/routes.php' ; foreach ( $routes as $method => $_routes ) { if ( $_routes ) { foreach ( $_routes as $rule => $map ) { $slim -> $method ( $rule , '\\zentao\\nb\\resource\\' . $map ); } } } $slim ->run(); |
\zentao\core\application 是獨(dú)立封裝的兼容禪道原來運(yùn)行環(huán)境的類,由禪道中的 framework/router.class.php 改造而來,主要用于加載禪道中的相關(guān)資源如配置文件、模型等。精華應(yīng)該在這里面,主要是加了一些“\”來讓微服務(wù)中能跑起來禪道運(yùn)來的運(yùn)行環(huán)境,并作為一個(gè)命名空間的橋梁可以在新的代碼中調(diào)用。
再看看資源類的父類 \zentao\nb\resource,片段
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<?php namespace zentao\nb; /** * 資源類 父類 */ class resource { public function __construct() { } /** * load the model file of one module. * * @param string $methodname the method name, if empty, use current module's name. * @access public * @return object|bool if no model file, return false. else return the model object. */ protected function loadmodel( $modulename ) { $modelfile = \helper::setmodelfile( $modulename ); /* if no model file, try load config. */ if (!\helper::import( $modelfile )) { $this ->app->loadconfig( $modulename , false); $this ->app->loadlang( $modulename ); $this ->dao = new dao(); return false; } $modelclass = class_exists ( 'ext' . $modulename . 'model' ) ? 'ext' . $modulename . 'model' : $modulename . 'model' ; $modelclass = '\\' . $modelclass ; if (! class_exists ( $modelclass )) $this ->app->triggererror( " the model $modelclass not found" , __file__ , __line__ , $exit = true); $this -> $modulename = new $modelclass (); $this ->dao = $this -> $modulename ->dao; return $this -> $modulename ; } |
這樣可以在資源類中調(diào)用禪道的 model 類。
還有另外一種用法,加載語(yǔ)言包:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<?php namespace zentao\nb\resource; use zentao\nb\enum\bugtype; /** * 項(xiàng)目自行定義的問題分類 */ class issuecategory extends \zentao\nb\resource { public function fetchall( $format = 'json' ) { global $app ; $types = $app ->loadlang( 'bug' )->bug->typelist; $issue_categories = array (); foreach ( $types as $key => $name ) { $issue_categories [] = array ( 'id' => bugtype::getidbyinterid( $key ), 'name' => $name ); } echo json_encode( array ( 'issue_categories' => $issue_categories )); } /** * 根據(jù)項(xiàng)目來取其中定義的分類 * @param int $projectid * @param string $format */ public function fetchallbyprojectid( $projectid , $format = 'json' ) { $model = $this ->loadmodel( 'project' ); $project = $model ->getbyid( $projectid ); //todo 支持按項(xiàng)目代號(hào)查找 if (! $project ) { $this ->responsenotexixted(); } global $app ; $types = $app ->loadlang( 'bug' )->bug->typelist; $issue_categories = array (); foreach ( $types as $key => $name ) { $issue_categories [] = array ( 'id' => bugtype::getidbyinterid( $key ), 'project' => array ( 'id' => $projectid , 'name' => $project ->name), 'name' => $name ); } echo json_encode( array ( 'issue_categories' => $issue_categories , 'total_count' => 2 )); } } |
基本項(xiàng)目結(jié)構(gòu)如下:
項(xiàng)目只是初步成型,尚未完成。
這是在 nb 中的任務(wù)列表。
這是在 nb 中的任務(wù)詳情。
以上就是告訴大家如何在舊的php系統(tǒng)中使用php 5.3之后的庫(kù),希望對(duì)大家的學(xué)習(xí)有所幫助。