\n";
$content .= " \n";
$content .= " \n";
$root_path = $compile_path . str_replace('/', DIRECTORY_SEPARATOR, 'components/diy'); // 扩展组件根目录
$file_arr = getFileMap($root_path);
if (!empty($file_arr)) {
foreach ($file_arr as $ck => $cv) {
if (strpos($cv, 'index.vue') !== false) {
$path = str_replace($root_path . '/', '', $ck);
$path = str_replace('/index.vue', '', $path);
if ($path == 'group') {
continue;
}
// 获取自定义组件 key 关键词
$name_arr = explode('-', $path);
foreach ($name_arr as $k => $v) {
// 首字母大写
$name_arr[ $k ] = strtoupper(substr($v, 0, 1)) . substr($v, 1);
}
$name = implode('', $name_arr);
$file_name = 'diy-' . $path;
$content .= " \n";
$content .= " <$file_name :component=\"component\" :index=\"index\">$file_name>\n";
$content .= " \n";
}
}
}
$content .= " \n";
$content .= " \n";
$content .= " \n";
$content .= " \n";
$content .= " \n";
$content .= " \n";
$content .= "\n";
$content .= "\n";
$content .= "\n";
$res = file_put_contents($compile_path . str_replace('/', DIRECTORY_SEPARATOR, 'components/diy/group/index.vue'), $content);
return $res;
}
/**
* 编译 fixed-group 固定模板组件代码文件
* @param $compile_path
* @param $addon_name
* @return string
*/
public function compileFixedComponentsCode($compile_path)
{
$content = "\n";
$content .= " \n";
$root_path = $compile_path . str_replace('/', DIRECTORY_SEPARATOR, 'components/fixed'); // 扩展组件根目录
$file_arr = getFileMap($root_path);
if (!empty($file_arr)) {
foreach ($file_arr as $ck => $cv) {
if (strpos($cv, 'index.vue') !== false) {
$path = str_replace($root_path . '/', '', $ck);
$path = str_replace('/index.vue', '', $path);
if ($path == 'group') {
continue;
}
// 获取自定义组件 key 关键词
$name_arr = explode('-', $path);
foreach ($name_arr as $k => $v) {
// 首字母大写
$name_arr[ $k ] = strtoupper(substr($v, 0, 1)) . substr($v, 1);
}
$name = implode('', $name_arr);
$file_name = 'fixed-' . $path;
$content .= " \n";
$content .= " <$file_name :data=\"data\">$file_name>\n";
$content .= " \n";
}
}
}
$content .= " \n";
$content .= "\n";
$content .= "\n";
$content .= "\n";
$res = file_put_contents($compile_path . str_replace('/', DIRECTORY_SEPARATOR, 'components/fixed/group/index.vue'), $content);
return $res;
}
/**
* 编译 pages.json 页面路由代码文件
* @param $compile_path
* @return bool|int|void
*/
public function installPageCode($compile_path)
{
if (!file_exists($this->geAddonPackagePath($this->addon) . 'uni-app-pages.php')) return;
$uniapp_pages = require $this->geAddonPackagePath($this->addon) . 'uni-app-pages.php';
if (empty($uniapp_pages[ 'pages' ])) {
return;
}
$addon = strtoupper($this->addon);
$content = @file_get_contents($compile_path . "pages.json");
$page_begin = $addon . '_PAGE_BEGIN';
$page_end = $addon . '_PAGE_END';
// 清除插件页面路由代码块
$pattern = "/\s+\/\/ {$page_begin}[\S\s]+\/\/ {$page_end}(\n,)?/";
$content = preg_replace($pattern, '', $content);
$uniapp_pages[ 'pages' ] = str_replace('PAGE_BEGIN', $page_begin, $uniapp_pages[ 'pages' ]);
$uniapp_pages[ 'pages' ] = str_replace('PAGE_END', $page_end, $uniapp_pages[ 'pages' ]);
$replacement = ",// {{PAGE}}\n";
$replacement .= $uniapp_pages[ 'pages' ] . "\n,";
$page_begin_matches_count = preg_match_all('/PAGE_BEGIN/', $content . $replacement, $page_begin_matches);
// 如果存在多个插件,要在插件前面加上逗号分割
if ($page_begin_matches_count > 0) {
$content = str_replace(',// {{PAGE}}', '// {{PAGE}}', $content);
}
$content = str_replace('// {{PAGE}}', $replacement, $content);
// 清除最后一个逗号
$content = preg_replace('/PAGE_END\n,\s+\],/', "PAGE_END\n],", $content);
// 找到页面路由文件 pages.json,写入内容
$res = file_put_contents($compile_path . "pages.json", $content);
return $res;
}
/**
* 编译 pages.json 页面路由代码文件
* @param $compile_path
* @return bool|int|void
*/
public function uninstallPageCode($compile_path)
{
if (!file_exists($this->geAddonPackagePath($this->addon) . 'uni-app-pages.php')) return;
$uniapp_pages = require $this->geAddonPackagePath($this->addon) . 'uni-app-pages.php';
if (empty($uniapp_pages[ 'pages' ])) {
return;
}
$addon = strtoupper($this->addon);
$content = @file_get_contents($compile_path . "pages.json");
$page_begin = $addon . '_PAGE_BEGIN';
$page_end = $addon . '_PAGE_END';
$uniapp_pages[ 'pages' ] = str_replace('PAGE_BEGIN', $page_begin, $uniapp_pages[ 'pages' ]);
$uniapp_pages[ 'pages' ] = str_replace('PAGE_END', $page_end, $uniapp_pages[ 'pages' ]);
// 清除插件页面路由代码块
$pattern = "/\s+\/\/ {$page_begin}[\S\s]+\/\/ {$page_end}(\n,)?/";
$content = preg_replace($pattern, '', $content);
$page_begin_matches_count = preg_match_all('/PAGE_BEGIN/', $content, $page_begin_matches);
// 如果没有页面,清除最后一个逗号
if ($page_begin_matches_count == 0) {
$content = str_replace(',// {{PAGE}}', '// {{PAGE}}', $content);
}
// 清除最后一个逗号
$content = preg_replace('/PAGE_END\n,\s+\],/', "PAGE_END\n],", $content);
$res = file_put_contents($compile_path . "pages.json", $content);
return $res;
}
}