TShopping

 找回密碼
 註冊
搜索
查看: 3158|回復: 1
打印 上一主題 下一主題

[教學] OpenCart-初學者日誌

  [複製鏈接]
跳轉到指定樓層
1#
發表於 2014-12-15 12:33:31 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook

前言

原本以為最終會用 Laravel 來開發了,無奈昨日會議結束又有了新的政策,因為某些考量決定再度切回OpenCart。無用洪只是個小小工程助理,月薪扣除健保勞保不到三萬元,身分地位卑微至極,也只能默默接受了。今天就來和大家分享 OpenCart 的首日鑽研成果。( 安裝的部份就跳過了,這邊有介紹過了)

OpenCart 採用 MVC 模式

這意思是說,入口八成就是那個index.php了。index.php 一行一行看懂基本上就大概能知道 OpenCart 大概的運作邏輯了。Let's Go~。

宣告一個常數,該常數表示目前的OpenCart版本。

  1. <?php
  2. // Version
  3. define('VERSION', '1.5.2.1');
複製代碼


引入 config.php,該檔案主要為一些路徑常數和database相關的常數。

  1. <?php
  2. //...
  3. // Configuration
  4. require_once('config.php');
複製代碼

判斷 DIR_APPLICATION 是否有宣告,若沒有則導向安裝頁。

  1. <?php
  2. // ...
  3. // Install
  4. if (!defined('DIR_APPLICATION')) {
  5.     header('Location: install/index.php');
  6.     exit;
  7. }
複製代碼


引入 startup.php,該檔案包含了各種對環境的初始化設定,以及引入一些必要的類別。

  1. <?php
  2. // ...
  3. // Startup
  4. require_once(DIR_SYSTEM . 'startup.php');
複製代碼

引入一些必要類別,我是不清楚為什們不把它們放在 startup.php裡。

  1. <?php
  2. //...
  3. // Application Classes
  4. require_once(DIR_SYSTEM . 'library/customer.php');
  5. require_once(DIR_SYSTEM . 'library/affiliate.php');
  6. require_once(DIR_SYSTEM . 'library/currency.php');
  7. require_once(DIR_SYSTEM . 'library/tax.php');
  8. require_once(DIR_SYSTEM . 'library/weight.php');
  9. require_once(DIR_SYSTEM . 'library/length.php');
  10. require_once(DIR_SYSTEM . 'library/cart.php');
複製代碼


新增一個 Registry 實體,這個物件可以說貫穿整個 OpenCart,它的任務就是把各種必要物件註冊進來。

  1. <?php
  2. //...
  3. // Registry
  4. $registry = new Registry();
複製代碼


註冊 config 物件。

  1. <?php
  2. //...
  3. // Config
  4. $config = new Config();
  5. $registry->set('config', $config);
複製代碼


註冊 DB 物件,DB 的建構子會去 新增一個 MySQL 實體, MySQL 實體的建構子會建立一個資料庫連結。

  1. <?php
  2. //...
  3. // Database
  4. $db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
  5. $registry->set('db', $db);
複製代碼


判斷有無分店,沒有個話 $config會設定 config_store_id 為 0。

  1. <?php

  2. // Store
  3. if (isset($_SERVER['HTTPS']) && (($_SERVER['HTTPS'] == 'on') || ($_SERVER['HTTPS'] == '1'))) {
  4.     $store_query = $db->query("SELECT * FROM " . DB_PREFIX . "store WHERE REPLACE(`ssl`, 'www.', '') = '" . $db->escape('https://' . str_replace('www.', '', $_SERVER['HTTP_HOST']) . rtrim(dirname($_SERVER['PHP_SELF']), '/.\\') . '/') . "'");
  5. } else {
  6.     $store_query = $db->query("SELECT * FROM " . DB_PREFIX . "store WHERE REPLACE(`url`, 'www.', '') = '" . $db->escape('http://' . str_replace('www.', '', $_SERVER['HTTP_HOST']) . rtrim(dirname($_SERVER['PHP_SELF']), '/.\\') . '/') . "'");
  7. }

  8. if ($store_query->num_rows) {
  9.     $config->set('config_store_id', $store_query->row['store_id']);
  10. } else {
  11.     $config->set('config_store_id', 0);
  12. }
複製代碼

一些類別和設定的引入,不是非常清楚用途,但不影響我們的探索。

  1. <?php
  2. // ...
  3. // Settings
  4. $query = $db->query("SELECT * FROM " . DB_PREFIX . "setting WHERE store_id = '0' OR store_id = '" . (int)$config->get('config_store_id') . "' ORDER BY store_id ASC");

  5. foreach ($query->rows as $setting) {
  6.     if (!$setting['serialized']) {
  7.         $config->set($setting['key'], $setting['value']);
  8.     } else {
  9.         $config->set($setting['key'], unserialize($setting['value']));
  10.     }
  11. }

  12. if (!$store_query->num_rows) {
  13.     $config->set('config_url', HTTP_SERVER);
  14.     $config->set('config_ssl', HTTPS_SERVER);   
  15. }

  16. // Url
  17. $url = new Url($config->get('config_url'), $config->get('config_use_ssl') ? $config->get('config_ssl') : $config->get('config_url'));
  18. $registry->set('url', $url);

  19. // Log
  20. $log = new Log($config->get('config_error_filename'));
  21. $registry->set('log', $log);
複製代碼


以下這段這樣寫不是很妥當,違反了某些撰寫原則。隨便了,總之這段就是定義一個OpenCart 自己的錯誤處理器。

  1. <?php
  2. // ...

  3. function error_handler($errno, $errstr, $errfile, $errline) {
  4.     global $log, $config;
  5.    
  6.     switch ($errno) {
  7.         case E_NOTICE:
  8.         case E_USER_NOTICE:
  9.             $error = 'Notice';
  10.             break;
  11.         case E_WARNING:
  12.         case E_USER_WARNING:
  13.             $error = 'Warning';
  14.             break;
  15.         case E_ERROR:
  16.         case E_USER_ERROR:
  17.             $error = 'Fatal Error';
  18.             break;
  19.         default:
  20.             $error = 'Unknown';
  21.             break;
  22.     }
  23.         
  24.     if ($config->get('config_error_display')) {
  25.         echo '<b>' . $error . '</b>: ' . $errstr . ' in <b>' . $errfile . '</b> on line <b>' . $errline . '</b>';
  26.     }
  27.    
  28.     if ($config->get('config_error_log')) {
  29.         $log->write('PHP ' . $error . ':  ' . $errstr . ' in ' . $errfile . ' on line ' . $errline);
  30.     }

  31.     return true;
  32. }
  33.    
  34. // Error Handler
  35. set_error_handler('error_handler');
複製代碼


將 Request 和 Response 物件註冊,至於這兩個物件在做什們就不詳加介紹了,基本上就和一般 framework 很像,但是陽春很多。

  1. <?php

  2. //...

  3. // Request
  4. $request = new Request();
  5. $registry->set('request', $request);

  6. // Response
  7. $response = new Response();
  8. $response->addHeader('Content-Type: text/html; charset=utf-8');
  9. $response->setCompression($config->get('config_compression'));
  10. $registry->set('response', $response);
複製代碼


cache類別提供了一些對cache 檔案的操作方法,也是頗陽春,session 物件 則是在建構子會做一些關於 Session 的設定。

  1. <?php
  2. //...

  3. // Cache
  4. $cache = new Cache();
  5. $registry->set('cache', $cache);

  6. // Session
  7. $session = new Session();
  8. $registry->set('session', $session);
複製代碼


檢測環境載入的語言,並讀取對應的語言檔案。

  1. <?php
  2. //...
  3. // Language Detection
  4. $languages = array();

  5. $query = $db->query("SELECT * FROM " . DB_PREFIX . "language WHERE status = '1'");

  6. foreach ($query->rows as $result) {
  7.     $languages[$result['code']] = $result;
  8. }

  9. $detect = '';

  10. if (isset($request->server['HTTP_ACCEPT_LANGUAGE']) && ($request->server['HTTP_ACCEPT_LANGUAGE'])) {
  11.     $browser_languages = explode(',', $request->server['HTTP_ACCEPT_LANGUAGE']);
  12.    
  13.     foreach ($browser_languages as $browser_language) {
  14.         foreach ($languages as $key => $value) {
  15.             if ($value['status']) {
  16.                 $locale = explode(',', $value['locale']);

  17.                 if (in_array($browser_language, $locale)) {
  18.                     $detect = $key;
  19.                 }
  20.             }
  21.         }
  22.     }
  23. }

  24. if (isset($session->data['language']) && array_key_exists($session->data['language'], $languages) && $languages[$session->data['language']]['status']) {
  25.     $code = $session->data['language'];
  26. } elseif (isset($request->cookie['language']) && array_key_exists($request->cookie['language'], $languages) && $languages[$request->cookie['language']]['status']) {
  27.     $code = $request->cookie['language'];
  28. } elseif ($detect) {
  29.     $code = $detect;
  30. } else {
  31.     $code = $config->get('config_language');
  32. }

  33. if (!isset($session->data['language']) || $session->data['language'] != $code) {
  34.     $session->data['language'] = $code;
  35. }

  36. if (!isset($request->cookie['language']) || $request->cookie['language'] != $code) {      
  37.     setcookie('language', $code, time() + 60 * 60 * 24 * 30, '/', $request->server['HTTP_HOST']);
  38. }            

  39. $config->set('config_language_id', $languages[$code]['language_id']);
  40. $config->set('config_language', $languages[$code]['code']);

  41. // Language   
  42. $language = new Language($languages[$code]['directory']);
  43. $language->load($languages[$code]['filename']);   
  44. $registry->set('language', $language);
複製代碼


一些商店設定相關物件的初始化,就不一個一個講了,都很簡單。

  1. <?php
  2. //...
  3. // Document
  4. $registry->set('document', new Document());        

  5. // Customer
  6. $registry->set('customer', new Customer($registry));

  7. // Affiliate
  8. $registry->set('affiliate', new Affiliate($registry));

  9. if (isset($request->get['tracking']) && !isset($request->cookie['tracking'])) {
  10.     setcookie('tracking', $request->get['tracking'], time() + 3600 * 24 * 1000, '/');
  11. }
  12.         
  13. // Currency
  14. $registry->set('currency', new Currency($registry));

  15. // Tax
  16. $registry->set('tax', new Tax($registry));

  17. // Weight
  18. $registry->set('weight', new Weight($registry));

  19. // Length
  20. $registry->set('length', new Length($registry));

  21. // Cart
  22. $registry->set('cart', new Cart($registry));

  23. //  Encryption
  24. $registry->set('encryption', new Encryption($config->get('config_encryption')));
複製代碼


再來是比較需要花時間分析的部份:

index.php
  1. <?php
  2. //...
  3. // Front Controller
  4. $controller = new Front($registry);

  5. // Maintenance Mode
  6. $controller->addPreAction(new Action('common/maintenance'));

  7. // SEO URL's
  8. $controller->addPreAction(new Action('common/seo_url'));   
  9.    
  10. // Router
  11. if (isset($request->get['route'])) {
  12.     $action = new Action($request->get['route']);
  13. } else {
  14.     $action = new Action('common/home');
  15. }

  16. // Dispatch
  17. $controller->dispatch($action, new Action('error/not_found'));

  18. // Output
  19. $response->output();
複製代碼


這段程式碼流程如下:

1.先新增一個 Front 實體 $controller,然後新增兩個 Action 實體並用 $controller 的addPreAction() 方法將它們儲存起來。
2.接著再根據現在的URI 新增第三個 Action 實體 $action。
3.執行 $controller 的 dispatch()方法,將$action傳入當做第一個變數。

這邊看一下 Front 類別的程式碼:

front.php
  1. <?php
  2. final class Front {
  3.     protected $registry;
  4.     protected $pre_action = array();
  5.     protected $error;
  6.    
  7.     public function __construct($registry) {
  8.         $this->registry = $registry;
  9.     }
  10.    
  11.     public function addPreAction($pre_action) {
  12.         $this->pre_action[] = $pre_action;
  13.     }
  14.    
  15.   public function dispatch($action, $error) {
  16.         $this->error = $error;
  17.             
  18.         foreach ($this->pre_action as $pre_action) {
  19.             $result = $this->execute($pre_action);
  20.                     
  21.             if ($result) {
  22.                 $action = $result;
  23.                
  24.                 break;
  25.             }
  26.         }
  27.             
  28.         while ($action) {
  29.             $action = $this->execute($action);
  30.         }
  31.   }
  32.    
  33.     private function execute($action) {
  34.         $file = $action->getFile();
  35.         $class = $action->getClass();
  36.         $method = $action->getMethod();
  37.         $args = $action->getArgs();

  38.         $action = '';

  39.         if (file_exists($file)) {
  40.             require_once($file);

  41.             $controller = new $class($this->registry);
  42.             
  43.             if (is_callable(array($controller, $method))) {
  44.                 $action = call_user_func_array(array($controller, $method), $args);
  45.             } else {
  46.                 $action = $this->error;
  47.             
  48.                 $this->error = '';
  49.             }
  50.         } else {
  51.             $action = $this->error;
  52.             
  53.             $this->error = '';
  54.         }
  55.         
  56.         return $action;
  57.     }
  58. }
複製代碼


4.我們可以看到, dispatch 會先迭代 pre_action 屬性並且透過 excute()方法執行。
5.excute() 會根據屬性引入需要的檔案並且執行對應的 controller 的方法。一般來說controller 在輸出到 view 這段會用到 ob_start() 做緩衝處理。

如果excute() 有回傳值,則 $action ( 傳入 dispatch 的實體 ) 會被回傳值取代,並且直接跳出迴圈。
如果 $action 存在,則將它傳入 excute() 方法執行。這邊這種寫法要特別小心,有機會造成無窮迴圈讓程式躺平。
整體來說不難懂,應該都能 trace 出來。就不介紹的太詳細了留給大家一點自己探索的樂趣。

小結:

OpenCart還是和Laravel 和 Symfony2 等檯面上正夯的框架架構有點落差,但還是不得不說,一個人能做到這樣真的很厲害了...

 

臉書網友討論
2#
發表於 2015-6-11 17:45:54 | 只看該作者
感謝大大分享
很實用的資訊

版主招募中

*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-24 11:43 , Processed in 0.195146 second(s), 21 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表