2020-08-31 10:11:06 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
declare(strict_types=1);
|
2020-08-31 10:11:06 +08:00
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
// the name of the command (the part after "bin/console")
|
|
|
|
|
|
protected static $defaultName = 'init';
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
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([
|
2022-03-15 18:05:33 +08:00
|
|
|
|
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()));
|
|
|
|
|
|
// ...
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
|
|
|
{
|
2020-08-31 10:11:06 +08:00
|
|
|
|
if (LOAD_MODE === 1) { // 从composer依赖而来的项目模式,最基本的需要初始化的模式
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$output->writeln('<comment>Initializing files</comment>');
|
2021-03-27 16:30:15 +08:00
|
|
|
|
$base_path = WORKING_DIR;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$args = $input->getOption('force');
|
2020-08-31 10:11:06 +08:00
|
|
|
|
foreach ($this->extract_files as $file) {
|
2021-03-25 16:18:09 +08:00
|
|
|
|
if (!file_exists($base_path . $file) || $args) {
|
2020-08-31 10:11:06 +08:00
|
|
|
|
$info = pathinfo($file);
|
2022-03-15 18:05:33 +08:00
|
|
|
|
@mkdir($base_path . $info['dirname'], 0777, true);
|
|
|
|
|
|
echo 'Copying ' . $file . PHP_EOL;
|
2022-08-01 16:31:54 +08:00
|
|
|
|
$package_name = json_decode(file_get_contents(__DIR__ . '/../../../composer.json'), true)['name'];
|
2022-03-15 18:05:33 +08:00
|
|
|
|
copy($base_path . '/vendor/' . $package_name . $file, $base_path . $file);
|
2020-08-31 10:11:06 +08:00
|
|
|
|
} else {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
echo 'Skipping ' . $file . ' , file exists.' . PHP_EOL;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-03-15 18:05:33 +08:00
|
|
|
|
chmod($base_path . '/zhamao', 0755);
|
2020-08-31 10:11:06 +08:00
|
|
|
|
$autoload = [
|
2022-03-15 18:05:33 +08:00
|
|
|
|
'psr-4' => [
|
|
|
|
|
|
'Module\\' => 'src/Module',
|
|
|
|
|
|
'Custom\\' => 'src/Custom',
|
|
|
|
|
|
],
|
|
|
|
|
|
'files' => [
|
|
|
|
|
|
'src/Custom/global_function.php',
|
2020-08-31 10:11:06 +08:00
|
|
|
|
],
|
|
|
|
|
|
];
|
2022-03-15 18:05:33 +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 {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
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
|
|
|
|
}
|
2022-03-15 18:05:33 +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
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-03-15 18:05:33 +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 {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
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
|
|
|
|
}
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$output->writeln('<info>Done!</info>');
|
2021-04-06 01:19:56 +08:00
|
|
|
|
return 0;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
}
|
2022-03-20 16:23:07 +08:00
|
|
|
|
if (LOAD_MODE === 2) { // 从phar启动的框架包,初始化的模式
|
2020-08-31 10:11:06 +08:00
|
|
|
|
$phar_link = new Phar(__DIR__);
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$current_dir = pathinfo($phar_link->getPath())['dirname'];
|
2020-08-31 10:11:06 +08:00
|
|
|
|
chdir($current_dir);
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$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);
|
2022-03-15 18:05:33 +08:00
|
|
|
|
@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 {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
echo 'Skipping ' . $file . ' , file exists.' . PHP_EOL;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-03-15 18:05:33 +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
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
private function getExtractFiles(): array
|
|
|
|
|
|
{
|
2020-08-31 10:11:06 +08:00
|
|
|
|
return $this->extract_files;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|