opencart是基於mvcl的商城系統,聽說是一個外國有人單獨開發。比較牛叉。可是又不大符合國人習慣,目前國內opencart社區也是很多。
簡單分析了下單入口,感受在國內商家用起來略微臃腫,須要再改進去除部分代碼。
由於就在index.php上寫了註釋,在此貼出來,和你們一塊兒學習。php
<?php // Version define('VERSION', '2.1.0.1'); // 引入system初始化的配置文件,包括常量和數據庫鏈接設置 if (is_file('config.php')) { require_once('config.php'); } // 若是沒有設置系統所需常量,須要先安裝 if (!defined('DIR_APPLICATION')) { header('Location: install/index.php'); exit; } // Startup引入start require_once(DIR_SYSTEM . 'startup.php'); /** * system/startup.php作了以下事情: * * 1.對比php版本,magic_quotes_gpc問題,時區,判斷https (ssl)問題,通常能夠略過。 * * 2.是否修正modifycation override,目前都沒有修改, 直接返回system/engine下的各種。 * 註冊兩個自動加載自定義函數,一個就是library下的N多個類文件, 另外一個是vendor下的scss(less 和scss是css的預處理器,不得不說php語言強大。。) * * 3.加載system/engine下的action controller event front loader model registry類,注意傳遞$register的實例作爲參數的類主要有 loader event customer front,目的是作一個備份, 以便處理其餘controller時候處理不會丟失, * * 4.最後又require了helper下的幾個函數 (包括生成token,處理json,utf8字符串,歐盟增值稅稅率扒拉扒拉, 就是那個vat文件,應該是value added tax) */ /** * 接上:簡單寫下system/engine中各個類的做用 * 1.action.php ,在構造函數中接受路由和傳遞參數, 解析爲controller目錄下的類,excute方法new這個類並調用對應函數, 做爲具體執行 * * 2.controller 和modle爲抽象類,供具體的controller和model繼承使用, 注意他們都涉及魔術方法 * __get和__set,用以方便調用一個不存在的方法後調用$register裏的方法 (看一下engine下的controller便可,比較簡單),構造函數接受$register類。 * * 3.loader.php 爲final類,不能被繼承和覆蓋, 主要是作controller文件夾 model文件夾, view helper文件夾 (strup.php引入了helper啊??) language config的一些加載,而且經過 view方法輸出extract後的$data, * * 4.front.php 也是final類,在加載執行action以前執行 * 5.registery.php,實現簡單get set,主要目的是看成參數傳遞給以上幾個類。 * 6.event.php,還沒看到具體用法,感受像鉤子,具備自定義的排序事件 */ // Registry,全局註冊類,做爲參數不斷的往裏壓入 $registry = new Registry(); // Loader,不必定須要先加載loader,須要用到$this->loader的時候會用到此類。 $loader = new Loader($registry); $registry->set('load', $loader);//此時register類裏就有一個load指向了new 的loader類 // Config 這個是關鍵,如下會從表裏把各類值都循環塞進去 $config = new Config();// 此處new的是/system/library/config.php,和register相似,可是他裏面也有個load方法,load的是system/config下的文件(默認爲空) $registry->set('config', $config);// 已經有兩個了!load&config // Database new 的是library下的db,(都是由於spl_autoload_register('libaray')起做用的)DB的常量是根下config的常量 $db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_PORT); $registry->set('db', $db); // Store 表一 store表 判斷替換 ssl和url兩個字段 if (isset($_SERVER['HTTPS']) && (($_SERVER['HTTPS'] == 'on') || ($_SERVER['HTTPS'] == '1'))) { $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']), '/.\\') . '/') . "'"); } else {// 沒有https,通常走下面 $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']), '/.\\') . '/') . "'"); } if ($store_query->num_rows) { $config->set('config_store_id', $store_query->row['store_id']); } else { $config->set('config_store_id', 0); } // Settings 表2 setting表,讀取setting表的信息, //循環全部的配置項,放在$config中,包括json格式的, $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"); foreach ($query->rows as $result) { if (!$result['serialized']) { // 開塞了 config $config->set($result['key'], $result['value']); } else { $config->set($result['key'], json_decode($result['value'], true)); } } if (!$store_query->num_rows) { $config->set('config_url', HTTP_SERVER); $config->set('config_ssl', HTTPS_SERVER); } // Url library下的url,構造url,根據上面的config,咱們常訪問的?index.php?rount=a/b/c經過此類生成,傳一個domain,是否ssl $url = new Url($config->get('config_url'), $config->get('config_secure') ? $config->get('config_ssl') : $config->get('config_url')); $registry->set('url', $url); // Log 讀取的setting表中的config_error_filename配置字段,默認爲storage/logs/error.log $log = new Log($config->get('config_error_filename')); $registry->set('log', $log); // 自定義錯誤函數 function error_handler($code, $message, $file, $line) { global $log, $config; // error suppressed with @ if (error_reporting() === 0) { return false; } switch ($code) { case E_NOTICE: case E_USER_NOTICE: $error = 'Notice'; break; case E_WARNING: case E_USER_WARNING: $error = 'Warning'; break; case E_ERROR: case E_USER_ERROR: $error = 'Fatal Error'; break; default: $error = 'Unknown'; break; } if ($config->get('config_error_display')) { echo '<b>' . $error . '</b>: ' . $message . ' in <b>' . $file . '</b> on line <b>' . $line . '</b>'; } if ($config->get('config_error_log')) { $log->write('PHP ' . $error . ': ' . $message . ' in ' . $file . ' on line ' . $line); } return true; } // Error Handler set_error_handler('error_handler'); // Request 請求對象,把get post server cookie 等過濾,保留clean方法對外 $request = new Request(); $registry->set('request', $request); // Response 處理header頭,壓縮輸出,定義跳轉函數,setoutput 輸出數據 $response = new Response(); $response->addHeader('Content-Type: text/html; charset=utf-8'); $response->setCompression($config->get('config_compression')); $registry->set('response', $response);
接上:貌似貼太多不能高亮~css
// Cache 默認有file cache,apc cache memcache ,不過要注意,5.4如下使用apc,5.5以上使用opcache吧 // 能夠參考 https://phphub.org/topics/301 $cache = new Cache('file'); $registry->set('cache', $cache); // Session 訪問api接口時候,在後臺系統設置-管理員-api中有體現,這個還不大明白 if (isset($request->get['token']) && isset($request->get['route']) && substr($request->get['route'], 0, 4) == 'api/') { $db->query("DELETE FROM `" . DB_PREFIX . "api_session` WHERE TIMESTAMPADD(HOUR, 1, date_modified) < NOW()"); $query = $db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "api` `a` LEFT JOIN `" . DB_PREFIX . "api_session` `as` ON (a.api_id = as.api_id) LEFT JOIN " . DB_PREFIX . "api_ip `ai` ON (as.api_id = ai.api_id) WHERE a.status = '1' AND as.token = '" . $db->escape($request->get['token']) . "' AND ai.ip = '" . $db->escape($request->server['REMOTE_ADDR']) . "'"); if ($query->num_rows) { // Does not seem PHP is able to handle sessions as objects properly so so wrote my own class $session = new Session($query->row['session_id'], $query->row['session_name']); $registry->set('session', $session); // keep the session alive $db->query("UPDATE `" . DB_PREFIX . "api_session` SET date_modified = NOW() WHERE api_session_id = '" . $query->row['api_session_id'] . "'"); } } else { $session = new Session(); $registry->set('session', $session); } // Language Detection 表四 language 多語言控制 $languages = array(); $query = $db->query("SELECT * FROM `" . DB_PREFIX . "language` WHERE status = '1'"); foreach ($query->rows as $result) { $languages[$result['code']] = $result; } if (isset($session->data['language']) && array_key_exists($session->data['language'], $languages)) { $code = $session->data['language']; } elseif (isset($request->cookie['language']) && array_key_exists($request->cookie['language'], $languages)) { $code = $request->cookie['language']; } else { // detect的意思是查明,發現o(∩_∩)o $detect = ''; // 查看請求頭中有沒有設置語言, if (isset($request->server['HTTP_ACCEPT_LANGUAGE']) && $request->server['HTTP_ACCEPT_LANGUAGE']) { $browser_languages = explode(',', $request->server['HTTP_ACCEPT_LANGUAGE']); foreach ($browser_languages as $browser_language) { foreach ($languages as $key => $value) { if ($value['status']) { $locale = explode(',', $value['locale']); if (in_array($browser_language, $locale)) { $detect = $key; break 2; } } } } } $code = $detect ? $detect : $config->get('config_language'); } if (!isset($session->data['language']) || $session->data['language'] != $code) { $session->data['language'] = $code; } if (!isset($request->cookie['language']) || $request->cookie['language'] != $code) { setcookie('language', $code, time() + 60 * 60 * 24 * 30, '/', $request->server['HTTP_HOST']); } $config->set('config_language_id', $languages[$code]['language_id']); $config->set('config_language', $languages[$code]['code']); // Language $language = new Language($languages[$code]['directory']); $language->load($languages[$code]['directory']); $registry->set('language', $language); // Document seo優化和獲取 資源 $registry->set('document', new Document()); // Customer 獲取用戶信息,登陸,註銷等 $customer = new Customer($registry); $registry->set('customer', $customer); // Customer Group 用戶分類 if ($customer->isLogged()) { $config->set('config_customer_group_id', $customer->getGroupId()); } elseif (isset($session->data['customer']) && isset($session->data['customer']['customer_group_id'])) { // For API calls 批量賽 $config->set('config_customer_group_id', $session->data['customer']['customer_group_id']); } elseif (isset($session->data['guest']) && isset($session->data['guest']['customer_group_id'])) { $config->set('config_customer_group_id', $session->data['guest']['customer_group_id']); } // Tracking Code if (isset($request->get['tracking'])) { setcookie('tracking', $request->get['tracking'], time() + 3600 * 24 * 1000, '/'); $db->query("UPDATE `" . DB_PREFIX . "marketing` SET clicks = (clicks + 1) WHERE code = '" . $db->escape($request->get['tracking']) . "'"); } // 如下new的這些都依託於 startup.php中的spl_autoload_register('library'); // Affiliate 隸屬於成員,也是屬於用戶的信息獲取,登陸,註銷等 $registry->set('affiliate', new Affiliate($registry)); // Currency // 貨幣 $registry->set('currency', new Currency($registry)); // Tax $registry->set('tax', new Tax($registry)); // Weight $registry->set('weight', new Weight($registry)); // Length $registry->set('length', new Length($registry)); // Cart $registry->set('cart', new Cart($registry)); // Encryption 加密,編碼 $registry->set('encryption', new Encryption($config->get('config_encryption'))); // OpenBay Pro $registry->set('openbay', new Openbay($registry)); // Event $event = new Event($registry); $registry->set('event', $event); $query = $db->query("SELECT * FROM " . DB_PREFIX . "event"); foreach ($query->rows as $result) { $event->register($result['trigger'], $result['action']); } // Front new 了個front,會先執行addPreAcion,(可理解爲先壓入的前置控制器,),而後在dispatch中具體實現,dispatch接受第二個參數爲錯誤控制器,error/not_found.php $controller = new Front($registry); /*new Action的做用:根據route判斷是否爲可用action,不可用默認爲最後一個, 若是最後一個也不可用,爲index 。 如下的兩個addPre就是上面說的的前置控制器,能夠理解爲鉤子吧,一個是seo, 另外的應該是(還不知道,maintenance爲保養的意思,用到再說), 就是在dispatch時候,先執行前置的,在依次執行傳進來的action (經過is_callable和call_user_function ) */ $controller->addPreAction(new Action('common/maintenance')); // SEO URL's $controller->addPreAction(new Action('common/seo_url')); // Router 經過request對象,接受route參數,若是沒有(默認首頁),action爲common/home就是商城首頁 if (isset($request->get['route'])) { $action = new Action($request->get['route']); } else { echo 'default action'; $action = new Action('common/hoe'); } /*Dispatch 派遣,調度,分發,傳入一個action實例, 而後根據aciton類的構造函數得知method,而後在execute每一個action */ $controller->dispatch($action, new Action('error/not_found')); // 在每一個具體的controller最後都會執行 setoutput()方法, //注意setoutput只接受一個參數,只不過在此是調用的load->view,經過ob獲得數據 // var_dump($response->getOutput()); // 是一個string類型,最後輸出 $response->output();