feature
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class ChannelProjectService
|
||||
{
|
||||
/**
|
||||
* Run tools/new_project.py to materialize public/web/{channelId}/.
|
||||
* Domains come from config (系统设置 / env); script rebuilds sync only when domains changed.
|
||||
*/
|
||||
public function generate(string $channelId): void
|
||||
{
|
||||
$channelId = strtolower(trim($channelId));
|
||||
if (! preg_match('/^[a-z0-9]{32}$/', $channelId)) {
|
||||
throw new RuntimeException('Invalid channel id');
|
||||
}
|
||||
|
||||
$domains = array_values(array_filter(config('coruna.channel_domains', [])));
|
||||
if ($domains === []) {
|
||||
throw new RuntimeException('投放域名未配置(系统设置 → 投放域名)');
|
||||
}
|
||||
|
||||
$labRoot = dirname(base_path()); // coruna-lab/
|
||||
$script = $labRoot.'/tools/new_project.py';
|
||||
if (! is_file($script)) {
|
||||
throw new RuntimeException('new_project.py not found');
|
||||
}
|
||||
|
||||
$joined = implode(',', $domains);
|
||||
$cmd = [
|
||||
'python3',
|
||||
$script,
|
||||
'--channel-id',
|
||||
$channelId,
|
||||
'--deployment-domains',
|
||||
$joined,
|
||||
'--reporting-domains',
|
||||
$joined,
|
||||
];
|
||||
|
||||
$process = new Process($cmd, $labRoot);
|
||||
$process->setTimeout(600);
|
||||
$process->run();
|
||||
|
||||
if (! $process->isSuccessful()) {
|
||||
Log::error('new_project failed', [
|
||||
'channel_id' => $channelId,
|
||||
'out' => $process->getOutput(),
|
||||
'err' => $process->getErrorOutput(),
|
||||
]);
|
||||
$this->deleteWebTree($channelId);
|
||||
throw new RuntimeException(
|
||||
'生成渠道资源失败: '.trim($process->getErrorOutput() ?: $process->getOutput() ?: 'unknown')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteWebTree(string $channelId): void
|
||||
{
|
||||
$channelId = strtolower(trim($channelId));
|
||||
if (! preg_match('/^[a-z0-9]{32}$/', $channelId)) {
|
||||
return;
|
||||
}
|
||||
$dir = public_path('web/'.$channelId);
|
||||
if (is_dir($dir)) {
|
||||
File::deleteDirectory($dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user