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

118 lines
4.8 KiB
PHP
Raw Normal View History

2020-08-31 10:11:06 +08:00
<?php
declare(strict_types=1);
2020-08-31 10:11:06 +08:00
namespace ZM\Command;
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
{
// the name of the command (the part after "bin/console")
protected static $defaultName = 'init';
private $extract_files = [
'/zhamao',
'/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',
];
protected function configure()
{
$this->setDescription('Initialize framework starter | 初始化框架运行的基础文件');
2021-03-25 16:50:32 +08:00
$this->setDefinition([
new InputOption('force', 'F', null, '强制重制,覆盖现有文件'),
2021-03-25 16:50:32 +08:00
]);
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 = WORKING_DIR;
$args = $input->getOption('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 = json_decode(file_get_contents(__DIR__ . '/../../../composer.json'), true)['name'];
copy($base_path . '/vendor/' . $package_name . $file, $base_path . $file);
2020-08-31 10:11:06 +08:00
} else {
echo 'Skipping ' . $file . ' , file exists.' . PHP_EOL;
2020-08-31 10:11:06 +08:00
}
}
chmod($base_path . '/zhamao', 0755);
2020-08-31 10:11:06 +08:00
$autoload = [
'psr-4' => [
'Module\\' => 'src/Module',
'Custom\\' => 'src/Custom',
],
'files' => [
'src/Custom/global_function.php',
2020-08-31 10:11:06 +08:00
],
];
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;
2020-08-31 10:11:06 +08:00
} else {
foreach ($autoload['psr-4'] as $k => $v) {
if (!isset($composer['autoload']['psr-4'][$k])) {
$composer['autoload']['psr-4'][$k] = $v;
}
2020-08-31 10:11:06 +08:00
}
foreach ($autoload['files'] as $v) {
if (!in_array($v, $composer['autoload']['files'])) {
$composer['autoload']['files'][] = $v;
}
2020-08-31 10:11:06 +08:00
}
}
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 zm_internal_errcode('E00041') . "Error occurred. Please check your updates.\n";
2021-04-06 01:19:56 +08:00
return 1;
2020-08-31 10:11:06 +08:00
}
$output->writeln('<info>Done!</info>');
2021-04-06 01:19:56 +08:00
return 0;
}
if (LOAD_MODE === 2) { // 从phar启动的框架包初始化的模式
$phar_link = new \Phar(__DIR__);
$current_dir = pathinfo($phar_link->getPath())['dirname'];
2020-08-31 10:11:06 +08:00
chdir($current_dir);
$phar_link = 'phar://' . $phar_link->getPath();
2020-08-31 10:11:06 +08:00
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;
2020-08-31 10:11:06 +08:00
file_put_contents($current_dir . $file, file_get_contents($phar_link . $file));
} else {
echo 'Skipping ' . $file . ' , file exists.' . PHP_EOL;
2020-08-31 10:11:06 +08:00
}
}
}
$output->writeln(zm_internal_errcode('E00042') . 'initialization must be started with composer-project mode!');
2021-04-06 01:19:56 +08:00
return 1;
2020-08-31 10:11:06 +08:00
}
private function getExtractFiles(): array
{
2020-08-31 10:11:06 +08:00
return $this->extract_files;
}
}