zhamao-framework/src/ZM/Command/InitCommand.php

108 lines
4.6 KiB
PHP
Raw Normal View History

2020-08-31 10:11:06 +08:00
<?php
namespace ZM\Command;
use Phar;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
2021-03-25 16:50:32 +08:00
use Symfony\Component\Console\Input\InputOption;
2020-08-31 10:11:06 +08:00
use Symfony\Component\Console\Output\OutputInterface;
class InitCommand extends Command
{
private $extract_files = [
"/config/global.php",
"/.gitignore",
"/config/file_header.json",
"/config/console_color.json",
"/config/motd.txt",
"/src/Module/Example/Hello.php",
"/src/Module/Middleware/TimerMiddleware.php",
"/src/Custom/global_function.php"
];
// the name of the command (the part after "bin/console")
protected static $defaultName = 'init';
protected function configure() {
$this->setDescription("Initialize framework starter | 初始化框架运行的基础文件");
2021-03-25 16:50:32 +08:00
$this->setDefinition([
new InputOption("force", "F", null, "强制重制,覆盖现有文件")
]);
2020-08-31 10:11:06 +08:00
$this->setHelp("此命令将会解压以下文件到项目的根目录:\n" . implode("\n", $this->getExtractFiles()));
// ...
}
protected function execute(InputInterface $input, OutputInterface $output): int {
2020-08-31 10:11:06 +08:00
if (LOAD_MODE === 1) { // 从composer依赖而来的项目模式最基本的需要初始化的模式
$output->writeln("<comment>Initializing files</comment>");
$base_path = LOAD_MODE_COMPOSER_PATH;
$args = $input->getArgument("force");
2020-08-31 10:11:06 +08:00
foreach ($this->extract_files as $file) {
if (!file_exists($base_path . $file) || $args) {
2020-08-31 10:11:06 +08:00
$info = pathinfo($file);
@mkdir($base_path . $info["dirname"], 0777, true);
echo "Copying " . $file . PHP_EOL;
$package_name = ($version = json_decode(file_get_contents(__DIR__ . "/../../../composer.json"), true)["name"]);
copy($base_path . "/vendor/" . $package_name . $file, $base_path . $file);
} else {
echo "Skipping " . $file . " , file exists." . PHP_EOL;
}
}
$autoload = [
"psr-4" => [
"Module\\" => "src/Module",
"Custom\\" => "src/Custom"
],
"files" => [
"src/Custom/global_function.php"
]
];
if (file_exists($base_path . "/composer.json")) {
$composer = json_decode(file_get_contents($base_path . "/composer.json"), true);
if (!isset($composer["autoload"])) {
$composer["autoload"] = $autoload;
} else {
foreach ($autoload["psr-4"] as $k => $v) {
if (!isset($composer["autoload"]["psr-4"][$k])) $composer["autoload"]["psr-4"][$k] = $v;
}
foreach ($autoload["files"] as $k => $v) {
if (!in_array($v, $composer["autoload"]["files"])) $composer["autoload"]["files"][] = $v;
}
}
file_put_contents($base_path . "/composer.json", json_encode($composer, 64 | 128 | 256));
$output->writeln("<info>Executing composer command: `composer dump-autoload`</info>");
exec("composer dump-autoload");
2020-08-31 10:11:06 +08:00
echo PHP_EOL;
} else {
echo("Error occurred. Please check your updates.\n");
return Command::FAILURE;
}
$output->writeln("<info>Done!</info>");
2020-08-31 10:11:06 +08:00
return Command::SUCCESS;
} elseif (LOAD_MODE === 2) { //从phar启动的框架包初始化的模式
$phar_link = new Phar(__DIR__);
$current_dir = pathinfo($phar_link->getPath())["dirname"];
chdir($current_dir);
$phar_link = "phar://" . $phar_link->getPath();
foreach ($this->extract_files as $file) {
if (!file_exists($current_dir . $file)) {
$info = pathinfo($file);
@mkdir($current_dir . $info["dirname"], 0777, true);
echo "Copying " . $file . PHP_EOL;
file_put_contents($current_dir . $file, file_get_contents($phar_link . $file));
} else {
echo "Skipping " . $file . " , file exists." . PHP_EOL;
}
}
}
$output->writeln("initialization must be started with composer-project mode!");
return Command::FAILURE;
}
private function getExtractFiles(): array {
2020-08-31 10:11:06 +08:00
return $this->extract_files;
}
}