Skip to content

Commit 46ca729

Browse files
committed
feat:模块系统增加 nuxt 的模块引导代码插入支持
1 parent bf3fabb commit 46ca729

1 file changed

Lines changed: 49 additions & 23 deletions

File tree

app/admin/library/module/Server.php

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,12 @@ public static function analysisWebBootstrap(string $uid, string $dir): array
367367
if (!file_exists($bootstrapFile)) return [];
368368
$bootstrapContent = file_get_contents($bootstrapFile);
369369
$pregArr = [
370-
'mainTsImport' => '/#main.ts import code start#([\s\S]*?)#main.ts import code end#/i',
371-
'mainTsStart' => '/#main.ts start code start#([\s\S]*?)#main.ts start code end#/i',
372-
'appVueImport' => '/#App.vue import code start#([\s\S]*?)#App.vue import code end#/i',
373-
'appVueOnMounted' => '/#App.vue onMounted code start#([\s\S]*?)#App.vue onMounted code end#/i',
370+
'mainTsImport' => '/#main.ts import code start#([\s\S]*?)#main.ts import code end#/i',
371+
'mainTsStart' => '/#main.ts start code start#([\s\S]*?)#main.ts start code end#/i',
372+
'appVueImport' => '/#App.vue import code start#([\s\S]*?)#App.vue import code end#/i',
373+
'appVueOnMounted' => '/#App.vue onMounted code start#([\s\S]*?)#App.vue onMounted code end#/i',
374+
'nuxtAppVueImport' => '/#web-nuxt\/app.vue import code start#([\s\S]*?)#web-nuxt\/app.vue import code end#/i',
375+
'nuxtAppVueStart' => '/#web-nuxt\/app.vue start code start#([\s\S]*?)#web-nuxt\/app.vue start code end#/i',
374376
];
375377
$codeStrArr = [];
376378
foreach ($pregArr as $key => $item) {
@@ -403,26 +405,38 @@ public static function analysisWebBootstrap(string $uid, string $dir): array
403405
*/
404406
public static function installWebBootstrap(string $uid, string $dir): void
405407
{
406-
$mainTsKeys = ['mainTsImport', 'mainTsStart'];
407408
$bootstrapCode = self::analysisWebBootstrap($uid, $dir);
408-
$basePath = root_path() . 'web' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
409+
if (!$bootstrapCode) {
410+
return;
411+
}
412+
413+
$webPath = root_path() . 'web' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
414+
$webNuxtPath = root_path() . 'web-nuxt' . DIRECTORY_SEPARATOR;
415+
$filePaths = [
416+
'mainTsImport' => $webPath . 'main.ts',
417+
'mainTsStart' => $webPath . 'main.ts',
418+
'appVueImport' => $webPath . 'App.vue',
419+
'appVueOnMounted' => $webPath . 'App.vue',
420+
'nuxtAppVueImport' => $webNuxtPath . 'app.vue',
421+
'nuxtAppVueStart' => $webNuxtPath . 'app.vue',
422+
];
409423

410424
$marks = [
411-
'mainTsImport' => self::buildMarkStr('import-root-mark'),
412-
'mainTsStart' => self::buildMarkStr('start-root-mark'),
413-
'appVueImport' => self::buildMarkStr('import-root-mark'),
414-
'appVueOnMounted' => self::buildMarkStr('onMounted-root-mark'),
425+
'mainTsImport' => self::buildMarkStr('import-root-mark'),
426+
'mainTsStart' => self::buildMarkStr('start-root-mark'),
427+
'appVueImport' => self::buildMarkStr('import-root-mark'),
428+
'appVueOnMounted' => self::buildMarkStr('onMounted-root-mark'),
429+
'nuxtAppVueImport' => self::buildMarkStr('import-root-mark'),
430+
'nuxtAppVueStart' => self::buildMarkStr('start-root-mark'),
415431
];
416432

417433
foreach ($bootstrapCode as $key => $item) {
418434
if ($item && isset($marks[$key])) {
419-
$filePath = $basePath . (in_array($key, $mainTsKeys) ? 'main.ts' : 'App.vue');
420-
$content = file_get_contents($filePath);
421-
435+
$content = file_get_contents($filePaths[$key]);
422436
$markPos = stripos($content, $marks[$key]);
423437
if ($markPos && strripos($content, self::buildMarkStr('module-line-mark', $uid, $key)) === false && strripos($content, self::buildMarkStr('module-multi-line-mark-start', $uid, $key)) === false) {
424438
$content = substr_replace($content, $item, $markPos + strlen($marks[$key]), 0);
425-
file_put_contents($filePath, $content);
439+
file_put_contents($filePaths[$key], $content);
426440
}
427441
}
428442
}
@@ -433,18 +447,28 @@ public static function installWebBootstrap(string $uid, string $dir): void
433447
*/
434448
public static function uninstallWebBootstrap(string $uid): void
435449
{
436-
$mainTsKeys = ['mainTsImport', 'mainTsStart'];
437-
$basePath = root_path() . 'web' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
438-
$marksKey = [
450+
$webPath = root_path() . 'web' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
451+
$webNuxtPath = root_path() . 'web-nuxt' . DIRECTORY_SEPARATOR;
452+
$filePaths = [
453+
'mainTsImport' => $webPath . 'main.ts',
454+
'mainTsStart' => $webPath . 'main.ts',
455+
'appVueImport' => $webPath . 'App.vue',
456+
'appVueOnMounted' => $webPath . 'App.vue',
457+
'nuxtAppVueImport' => $webNuxtPath . 'app.vue',
458+
'nuxtAppVueStart' => $webNuxtPath . 'app.vue',
459+
];
460+
461+
$marksKey = [
439462
'mainTsImport',
440463
'mainTsStart',
441464
'appVueImport',
442465
'appVueOnMounted',
466+
'nuxtAppVueImport',
467+
'nuxtAppVueStart',
443468
];
444469

445470
foreach ($marksKey as $item) {
446-
$filePath = $basePath . (in_array($item, $mainTsKeys) ? 'main.ts' : 'App.vue');
447-
$content = file_get_contents($filePath);
471+
$content = file_get_contents($filePaths[$item]);
448472
$moduleLineMark = self::buildMarkStr('module-line-mark', $uid, $item);
449473
$moduleMultiLineMarkStart = self::buildMarkStr('module-multi-line-mark-start', $uid, $item);
450474
$moduleMultiLineMarkEnd = self::buildMarkStr('module-multi-line-mark-end', $uid, $item);
@@ -467,7 +491,7 @@ public static function uninstallWebBootstrap(string $uid): void
467491
}
468492

469493
if ($moduleLineMarkPos || $moduleMultiLineMarkStartPos) {
470-
file_put_contents($filePath, $content);
494+
file_put_contents($filePaths[$item], $content);
471495
}
472496
}
473497
}
@@ -481,21 +505,23 @@ public static function uninstallWebBootstrap(string $uid): void
481505
*/
482506
public static function buildMarkStr(string $type, string $uid = '', string $extend = ''): string
483507
{
484-
$importKeys = ['mti', 'avi'];
508+
$nonTabKeys = ['mti', 'avi', 'navi', 'navs'];
485509
$extend = match ($extend) {
486510
'mainTsImport' => 'mti',
487511
'mainTsStart' => 'mts',
488512
'appVueImport' => 'avi',
489513
'appVueOnMounted' => 'avo',
514+
'nuxtAppVueImport' => 'navi',
515+
'nuxtAppVueStart' => 'navs',
490516
default => '',
491517
};
492518
return match ($type) {
493519
'import-root-mark' => '// modules import mark, Please do not remove.',
494520
'start-root-mark' => '// modules start mark, Please do not remove.',
495521
'onMounted-root-mark' => '// Modules onMounted mark, Please do not remove.',
496522
'module-line-mark' => ' // Code from module \'' . $uid . "'" . ($extend ? "($extend)" : ''),
497-
'module-multi-line-mark-start' => (in_array($extend, $importKeys) ? '' : Helper::tab()) . "// Code from module '$uid' start" . ($extend ? "($extend)" : '') . "\n",
498-
'module-multi-line-mark-end' => (in_array($extend, $importKeys) ? '' : Helper::tab()) . "// Code from module '$uid' end",
523+
'module-multi-line-mark-start' => (in_array($extend, $nonTabKeys) ? '' : Helper::tab()) . "// Code from module '$uid' start" . ($extend ? "($extend)" : '') . "\n",
524+
'module-multi-line-mark-end' => (in_array($extend, $nonTabKeys) ? '' : Helper::tab()) . "// Code from module '$uid' end",
499525
default => '',
500526
};
501527
}

0 commit comments

Comments
 (0)