微擎模組機制分析2
阿新 • • 發佈:2018-11-14
2017年10月18日 17:51:06 橙虛緣 閱讀數:1626
利用微擎開發些微信公眾號還是非常方便的;模組機制分析主要從其安裝、解除安裝、使用角度,
一、安裝
安裝介面,主要是module_get_all_unistalled獲取未安裝模組
-
if ($do == 'not_installed') {
- if (empty($_W['isfounder'])) {
- itoast( '非法訪問!', referer(), 'info');
- }
-
$_W[
'page']['title'] = '安裝模組 - 模組 - 擴充套件';
- $status = $_GPC[ 'status'] == 'recycle'? 'recycle' : 'uninstalled';
-
$letters =
array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
- $title = $_GPC[ 'title'];
- $letter = $_GPC[ 'letter'];
- $pageindex = max($_GPC[ 'page'], 1);
- $pagesize = 20;
- $uninstallModules = module_get_all_unistalled($status, false);
- $total_uninstalled = $uninstallModules[ 'module_count'];
- $uninstallModules = ( array)$uninstallModules['modules'];
- if (!empty($uninstallModules)) {
- foreach($uninstallModules as $name => &$module) {
- if (!empty($letter) && strlen($letter) == 1) {
- $first_char = get_first_pinyin($module[ 'title']);
- if ($letter != $first_char) {
- unset($uninstallModules[$name]);
- continue;
- }
- }
- if (!empty($title)) {
- if (!strexists($module['title'], $title)) {
- unset($uninstallModules[$name]);
- continue;
- }
- }
- if (file_exists(IA_ROOT.'/addons/'.$module['name'].'/icon-custom.jpg')) {
- $module[ 'logo'] = tomedia(IA_ROOT.'/addons/'.$module['name'].'/icon-custom.jpg');
- } elseif (file_exists(IA_ROOT.'/addons/'.$module['name'].'/icon.jpg')) {
- $module[ 'logo'] = tomedia(IA_ROOT.'/addons/'.$module['name'].'/icon.jpg');
- } else {
- $module[ 'logo'] = tomedia($module['thumb']);
- }
- if (!empty($module['main_module'])) {
- $main_module_installed = module_fetch($module[ 'main_module']);
- if ($main_module_installed) {
- $module[ 'main_module_logo'] = $main_module_installed['logo'];
- } else {
- if ($module['from'] == 'cloud') {
- $module[ 'main_module_logo'] = tomedia($uninstallModules[$module['main_module']]['thumb']);
- } else {
- if (file_exists(IA_ROOT.'/addons/'.$module['main_module'].'/icon-custom.jpg')) {
- $module[ 'main_module_logo'] = tomedia(IA_ROOT.'/addons/'.$module['main_module'].'/icon-custom.jpg');
- } elseif (file_exists(IA_ROOT.'/addons/'.$module['main_module'].'/icon.jpg')) {
- $module[ 'main_module_logo'] = tomedia(IA_ROOT.'/addons/'.$module['main_module'].'/icon.jpg');
- }
- }
- }
- }
- }
- }
- $total = count($uninstallModules);
- $uninstallModules = array_slice($uninstallModules, ($pageindex - 1)*$pagesize, $pagesize);
- $pager = pagination($total, $pageindex, $pagesize);
- }
module_get_all_unistalled 函式原始碼,可以看到先判斷是否有快取(微擎的快取實現機制還是很清晰易懂的,資料庫、檔案、memcache、Redis),沒有再取,關鍵是cache_build_uninstalled_module;可以看到有云模組的請求,當然這部分你可以幹掉,我還是堅持用正版哈
- function module_get_all_unistalled($status, $cache = true) {
- global $_GPC;
- load()->func( 'communication');
- load()->model( 'cloud');
- load()->classs( 'cloudapi');
- $status = $status == 'recycle' ? 'recycle' : 'uninstalled';
- $uninstallModules = cache_load(cache_system_key( 'module:all_uninstall'));
- if (!$cache && $status == 'uninstalled') {
- $cloud_api = new CloudApi();
- $get_cloud_m_count = $cloud_api->get( 'site', 'stat', array('module_quantity' => 1), 'json');
- $cloud_m_count = $get_cloud_m_count[ 'module_quantity'];
- } else {
- if(is_array($uninstallModules)){
- $cloud_m_count = $uninstallModules[ 'cloud_m_count'];
- }
- }
- if (empty($uninstallModules['modules']) || intval($uninstallModules['cloud_m_count']) !== intval($cloud_m_count) || is_error($get_cloud_m_count)) {
- $uninstallModules = cache_build_uninstalled_module();
- }
- if (ACCOUNT_TYPE == ACCOUNT_TYPE_APP_NORMAL) {
- $uninstallModules[ 'modules'] = (array)$uninstallModules['modules'][$status]['wxapp'];
- $uninstallModules[ 'module_count'] = $uninstallModules['wxapp_count'];
- return $uninstallModules;
- } elseif (ACCOUNT_TYPE == ACCOUNT_TYPE_OFFCIAL_NORMAL) {
- $uninstallModules[ 'modules'] = (array)$uninstallModules['modules'][$status]['app'];
- $uninstallModules[ 'module_count'] = $uninstallModules['app_count'];
- return $uninstallModules;
- } else {
- return $uninstallModules;
- }
- }
cache_build_uninstalled_module原始碼,現在知道為什麼模組要放在addons下了,最後還把模組資訊寫入快取,以便下次直接使用
- function cache_build_uninstalled_module() {
- load()->model( 'cloud');
- load()->classs( 'cloudapi');
- load()->model( 'extension');
- load()->func( 'file');
- $cloud_api = new CloudApi();
- $cloud_m_count = $cloud_api->get( 'site', 'stat', array('module_quantity' => 1), 'json');
- $installed_module = pdo_getall( 'modules', array(), array(), 'name');
- $uninstallModules = array('recycle' => array(), 'uninstalled' => array());
- $recycle_modules = pdo_getall( 'modules_recycle', array(), array(), 'modulename');
- $recycle_modules = array_keys($recycle_modules);
- $cloud_module = cloud_m_query();
- if (!empty($cloud_module) && !is_error($cloud_module)) {
- foreach ($cloud_module as $module) {
- $upgrade_support_module = false;
- $wxapp_support = ! empty($module['site_branch']['wxapp_support']) && is_array($module['site_branch']['bought']) && in_array('wxapp', $module['site_branch']['bought']) ? $module['site_branch']['wxapp_support'] : 1;
- $app_support = ! empty($module['site_branch']['app_support']) && is_array($module['site_branch']['bought']) && in_array('app', $module['site_branch']['bought']) ? $module['site_branch']['app_support'] : 1;
- if ($wxapp_support == 1 && $app_support == 1) {
- $app_support = 2;
- }
- if (!empty($installed_module[$module['name']]) && ($installed_module[$module['name']]['app_support'] != $app_support || $installed_module[$module['name']]['wxapp_support'] != $wxapp_support)) {
- $upgrade_support_module = true;
- }
- if (!in_array($module['name'], array_keys($installed_module)) || $upgrade_support_module) {
- $status = in_array($module[ 'name'], $recycle_modules) ? 'recycle' : 'uninstalled';
- if (!empty($module['id'])) {
- $cloud_module_info = array (
- 'from' => 'cloud',
- 'name' => $module['name'],
- 'version' => $module['version'],
- 'title' => $module['title'],
- 'thumb' => $module['thumb'],
- 'wxapp_support' => $wxapp_support,
- 'app_support' => $app_support,
- 'main_module' => empty($module['main_module']) ? '' : $module['main_module'],
- 'upgrade_support' => $upgrade_support_module
- );
- if ($upgrade_support_module) {
- if ($wxapp_support == 2 && $installed_module[$module['name']]['wxapp_support'] != 2) {
- $uninstallModules[$status][ 'wxapp'][$module['name']] = $cloud_module_info;
- }
- if ($app_support == 2 && $installed_module[$module['name']]['app_support'] != 2) {
- $uninstallModules[$status][ 'app'][$module['name']] = $cloud_module_info;
- }
- } else {
- if ($wxapp_support == 2) {
- $uninstallModules[$status][ 'wxapp'][$module['name']] = $cloud_module_info;
- }
- if ($app_support == 2) {
- $uninstallModules[$status][ 'app'][$module['name']] = $cloud_module_info;
- }
- }
- }
- }
- }
- }
- $path = IA_ROOT . '/addons/';
- mkdirs($path);
- $module_file = glob($path . '*');
- if (is_array($module_file) && !empty($module_file)) {
- foreach ($module_file as $modulepath) {
- $upgrade_support_module = false;
- $modulepath = str_replace($path, '', $modulepath);
- $manifest = ext_module_manifest($modulepath);
- if (!is_array($manifest) || empty($manifest) || empty($manifest['application']['identifie'])) {
- continue;
- }
- $main_module = empty($manifest['platform']['main_module']) ? '' : $manifest['platform']['main_module'];
- $manifest = ext_module_convert($manifest);
- if (!empty($installed_module[$modulepath]) && ($manifest['app_support'] != $installed_module[$modulepath]['app_support'] || $manifest['wxapp_support'] != $installed_module[$modulepath]['wxapp_support'])) {
- $upgrade_support_module = true;
- }
- if (!in_array($manifest['name'], array_keys($installed_module)) || $upgrade_support_module) {
- $module[$manifest[ 'name']] = $manifest;
- $module_info = array(
- 'from' => 'local',
- 'name' => $manifest['name'],
- 'version' => $manifest['version'],
- 'title' => $manifest['title'],
- 'app_support' => $manifest['app_support'],
- 'wxapp_support' => $manifest['wxapp_support'],
- 'main_module' => $main_module,
- 'upgrade_support' => $upgrade_support_module
- );
- $module_type = in_array($manifest[ 'name'], $recycle_modules) ? 'recycle' : 'uninstalled';
- if ($upgrade_support_module) {
- if ($module_info['app_support'] == 2 && $installed_module[$module_info['name']]['app_support'] != 2) {
- $uninstallModules[ 'uninstalled']['app'][$manifest['name']] = $module_info;
- }
- if ($module_info['wxapp_support'] == 2 && $installed_module[$module_info['name']]['wxapp_support'] != 2) {
- $uninstallModules[ 'uninstalled']['wxapp'][$manifest['name']] = $module_info;
- }
- } else {
- if ($module_info['app_support'] == 2) {
- $uninstallModules[$module_type][ 'app'][$manifest['name']] = $module_info;
- }
- if ($module_info['wxapp_support'] == 2) {
- $uninstallModules[$module_type][ 'wxapp'][$manifest['name']] = $module_info;
- }
- }
- }
- }
- }
- $cache = array(
- 'cloud_m_count' => $cloud_m_count['module_quantity'],
- 'modules' => $uninstallModules,
- 'app_count' => count($uninstallModules['uninstalled']['app']),
- 'wxapp_count' => count($uninstallModules['uninstalled']['wxapp'])
- );
- cache_write( 'we7:module:all_uninstall', $cache, CACHE_EXPIRE_LONG);
- return $cache;
- }
還有個地方值得學習,模組的資訊定義在manifest.xml中,有點java的感覺,具體資訊讀取都在extension.mod.php中,程式碼如下
- function ext_module_manifest_parse($xml) {
- if (!strexists($xml, '<manifest')) {
- $xml = base64_decode($xml);
- }
- if (empty($xml)) {
- return array();
- }
- $dom = new DOMDocument();
- $dom->loadXML($xml);
- $root = $dom->getElementsByTagName( 'manifest')->item(0);
- if (empty($root)) {
- return array();
- }
- $vcode = explode( ',', $root->getAttribute('versionCode'));
- $manifest[ 'versions'] = array();
- if (is_array($vcode)) {
- foreach ($vcode as $v) {
- $v = trim($v);
- if (!empty($v)) {
- $manifest[ 'versions'][] = $v;
- }
- }
- $manifest[ 'versions'][] = '0.52';
- $manifest[ 'versions'][] = '