What is APF
APF是某網(wǎng)站的后端php架構(gòu)
APF特點
- Intercepetor
- Config
- Component
- Compressed JS/CSS
- Multi Application
- Debugger
- Logger
工作流程
以訪問一個頁面為例
瀏覽器發(fā)起一次頁面請求,后臺服務(wù)器新開一個線程抬闷,處理這次請求粉臊。在當前線程中,只維護一個實例锐峭,用于處理接下去的所有工作,如請求資源文件等鳄袍。
system/classes/APF.php是APF框架的核心淌哟,線程會調(diào)用run方法執(zhí)行工作流程
- run APF主入口,實例化請求類惠豺、響應(yīng)類
function run() {
$this->prepare();
$this->dispatch();
}
- prepare 實例化后面流程需要用到的請求類银还、響應(yīng)類、路由類等
function prepare(){
$this->request = new $this->request_class();
$this->response = new $this->response_class();
$this->router = new $this->router_class();
}
- dispatch 真正框架執(zhí)行方法洁墙,依次執(zhí)行各個攔截器before方法蛹疯、控制器方法、頁面方法热监、攔截器逆序after方法以及清理工作
基本流程:
1.取得controller類 --> $controller
2.取得inteceptor類 --> $inteceptor(可能有多個攔截器)
3.執(zhí)行各個攔截器before方法 --> $step (存儲攔截器的結(jié)果) 攔截器的作用用于訪問頁面前的權(quán)限校驗以及分配城市等工作
4.If $step != EXIT 執(zhí)行控制器方法和頁面方法
1.執(zhí)行$controller->handle_request()
(handle_request作用是對頁面屬性的設(shè)置捺弦,一般返回頁面類字符串,供后面頁面類實例化以及執(zhí)行頁面類方法)
2.執(zhí)行$this->page()方法 實例化頁面現(xiàn)實邏輯
1>$this->register_resources()
2>$page = $this->load_component()
3>$page->execute()
5.執(zhí)行攔截器逆序after方法
dispatch方法偽代碼
function dispatch() {
$class = $this->router->mapping(); // 讀取路由配置文件
$controller = $this->get_controller($class);
$interceptor = $this->get_config($class, "interceptor");
$step = $interceptor->before();
if($step != STEP_EXIT) {
$result = $controller->handle_request();
$this->page($result);
}
$interceptor->after()
}
Intecerptor類
攔截器的作用是在頁面訪問前校驗其權(quán)限或預(yù)處理任務(wù)孝扛。每個攔截器類都會提供before
方法列吼,返回一些特定的標志位。
const STEP_CONTINUE = 1;
const STEP_BREAK = 2;
const STEP_EXIT = 3; // 退出標志苦始,表示校驗不通過等寞钥,那么就不再執(zhí)行controller
Controller類
每個頁面都配有一個controller類,一般每個頁面controller類都繼承自BrokerBaseController
每個頁面controller必須實現(xiàn)handle_request_internal
方法盈简,該方法返回頁面類名
abstract class BrokerBaseController extends BrokerController{
// APF中dispatch方法會調(diào)用
function handle_request(){
...
return $this->handle_request_internal();
}
abstract function handle_request_internal(); // 該方法子類必須實現(xiàn)凑耻,返回頁面類名
}
Component類
如何定義
1.在component文件夾內(nèi)定義組件,一般一個組件由以下四個文件組成:
A.js
A.css
A.php
A.phtml
2.每一個組件類都繼承自APF組件類(APF_Component
)柠贤,在APF_Component
類中定義了execute()
方法,作用載入組件顯示頁面
如何調(diào)用
1.在使用組件的頁面中調(diào)用
// 實例化組件類类缤,并執(zhí)行set_params() 和 execute()方法
$this->component('Broker_NavTabGJ', 參數(shù)list)
2.資源載入
APF.php中的方法register_resources()
會在$this->page()
中被調(diào)用
register_resources()
會依次調(diào)用use系方法臼勉,這樣會把頁面需要使用的組件資源文件和頁面資源文件進行打包或輸出在html文件中。這也是APF能夠打包資源特定的原因餐弱。
Page類
我們可以將page看作是特殊的Component宴霸,每個page類可能有以下幾個方法(俗稱use系方法):
1.use_boundable_styles()
2.use_boundable_javascripts()
3.use_component()
4....
每個page類都繼承一些基類,基類中定義一些基本方法膏蚓,這些基本方法都可以在子類中覆寫瓢谢,以實現(xiàn)自定義功能。
在最后的基類中(APF_Component)中定義了execute()
方法驮瞧,加載裝飾器以及渲染頁面(所謂渲染氓扛,其實就是include(xxx.phtml))
至于頁面一些變量的設(shè)置,自有方法去完成,如下:
每個頁面類對應(yīng)一定有一個同名的controller類采郎,例如page/Set.php
,在controller文件夾中一定能找到同名的controller/Set.php
文件千所。如上面controller類的解釋,不光是返回頁面類名蒜埋,同時會獲取到單例中的$request
屬性淫痰。$request
是一個對象,PHP中對象是引用的,所以可以在$request
中調(diào)用$request->set_attribute()
方法添加變量整份。由于是單例模式待错,所以頁面類中獲取$request
對象時,就可以得到controller中定義的變量烈评。這就是頁面添加變量的原理朗鸠。