在老的 PHP 系統中使用 PHP 5.3以后的庫
所謂老的系統,是指沒有使用PHP 5.3以上命名空間(namespace)特性編碼的系統。
但是,只要你的系統運行在 PHP 5.3及以上的環境,在你的老系統中,是可以使用這些基於新特性如命名空間編碼的庫或代碼的。
以前只是有潔癖不用而已。
比如,我是個工具控,想讓所用的禪道系統也像那些國際化開源 Issue 項目一樣有一套標准開放的 API - 禪道本身是有套 html、json 自適配模式可以當接口用的,可以用於其他客戶端或系統集成。這幾天在嘗試編寫的用於兼容 Redmine REST 風格接口的禪道 PMS API,就有意識的用了這種混合的寫法。
由於要兼容 Redmine 的 REST 風格,首先選用了 Slim 這個微服務框架,毫無疑問,它是要求運行環境>5.3的,但我總得復用禪道已有的代碼,這樣效率才高。
原理很簡單,就是一根反斜杠,或者兩根。
先用 composer 初始化了 slim 庫。
重點部位的代碼:
入口文件 index.php
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 是獨立封裝的兼容禪道原來運行環境的類,由禪道中的 framework/router.class.php 改造而來,主要用於加載禪道中的相關資源如配置文件、模型等。精華應該在這里面,主要是加了一些“\”來讓微服務中能跑起來禪道運來的運行環境,並作為一個命名空間的橋梁可以在新的代碼中調用。
再看看資源類的父類 \zentao\nb\Resource,片段
<?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;
}
這樣可以在資源類中調用禪道的 model 類。
還有另外一種用法,加載語言包:
<?php
namespace zentao\nb\resource;
use zentao\nb\enum\BugType;
/**
* 項目自行定義的問題分類
*/
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));
}
/**
* 根據項目來取其中定義的分類
* @param int $projectId
* @param string $format
*/
public function fetchAllByProjectId($projectId, $format = 'json') {
$model = $this->loadModel('project');
$project = $model->getById($projectId);//TODO 支持按項目代號查找
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
));
}
}
基本項目結構如下:

代碼:https://github.com/web3d/zentao-rest-api
項目只是初步成型,尚未完成。
這是在 NB 中的任務列表。
這是在 NB 中的任務詳情。