addArgument('name', InputArgument::REQUIRED, 'App plugin name'); } /** * @param InputInterface $input * @param OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output): int { $name = $input->getArgument('name'); $output->writeln("Create App Plugin $name"); if (strpos($name, '/') !== false) { $output->writeln('Bad name, name must not contain character \'/\''); return self::FAILURE; } // Create dir config/plugin/$name if (is_dir($plugin_config_path = base_path()."/plugin/$name")) { $output->writeln("Dir $plugin_config_path already exists"); return self::FAILURE; } $this->createAll($name); return self::SUCCESS; } /** * @param $name * @return void */ protected function createAll($name) { $base_path = base_path(); $this->mkdir("$base_path/plugin/$name/app/controller", 0777, true); $this->mkdir("$base_path/plugin/$name/app/model", 0777, true); $this->mkdir("$base_path/plugin/$name/app/middleware", 0777, true); $this->mkdir("$base_path/plugin/$name/app/view/index", 0777, true); $this->mkdir("$base_path/plugin/$name/config", 0777, true); $this->mkdir("$base_path/plugin/$name/public", 0777, true); $this->mkdir("$base_path/plugin/$name/api", 0777, true); $this->createFunctionsFile("$base_path/plugin/$name/app/functions.php"); $this->createControllerFile("$base_path/plugin/$name/app/controller/IndexController.php", $name); $this->createViewFile("$base_path/plugin/$name/app/view/index/index.html"); $this->createConfigFiles("$base_path/plugin/$name/config", $name); $this->createApiFiles("$base_path/plugin/$name/api", $name); $this->createInstallSqlFile("$base_path/plugin/$name/install.sql"); } /** * @param $path * @return void */ protected function mkdir($path) { if (is_dir($path)) { return; } echo "Create $path\r\n"; mkdir($path, 0777, true); } /** * @param $path * @param $name * @return void */ protected function createControllerFile($path, $name) { $content = << '$name']); } } EOF; file_put_contents($path, $content); } /** * @param $path * @return void */ protected function createViewFile($path) { $content = << webman app plugin hello EOF; file_put_contents($path, $content); } /** * @param $file * @return void */ protected function createFunctionsFile($file) { $content = << static::getMenus()]; } /** * 获取菜单 * * @return array|mixed */ public static function getMenus() { clearstatcache(); if (is_file(\$menu_file = __DIR__ . '/../config/menu.php')) { \$menus = include \$menu_file; return \$menus ?: []; } return []; } /** * 删除不需要的菜单 * * @param \$previous_menus * @return void */ public static function removeUnnecessaryMenus(\$previous_menus) { \$menus_to_remove = array_diff(Menu::column(\$previous_menus, 'name'), Menu::column(static::getMenus(), 'name')); foreach (\$menus_to_remove as \$name) { Menu::delete(\$name); } } /** * 安装SQL * * @return void */ protected static function installSql() { static::importSql(__DIR__ . '/../install.sql'); } /** * 卸载SQL * * @return void */ protected static function uninstallSql() { // 如果卸载数据库文件存在责直接使用 \$uninstallSqlFile = __DIR__ . '/../uninstall.sql'; if (is_file(\$uninstallSqlFile)) { static::importSql(\$uninstallSqlFile); return; } // 否则根据install.sql生成卸载数据库文件uninstall.sql \$installSqlFile = __DIR__ . '/../install.sql'; if (!is_file(\$installSqlFile)) { return; } \$installSql = file_get_contents(\$installSqlFile); preg_match_all('/CREATE TABLE `(.+?)`/si', \$installSql, \$matches); \$dropSql = ''; foreach (\$matches[1] as \$table) { \$dropSql .= "DROP TABLE IF EXISTS `\$table`;\\n"; } file_put_contents(\$uninstallSqlFile, \$dropSql); static::importSql(\$uninstallSqlFile); unlink(\$uninstallSqlFile); } /** * 导入数据库 * * @return void */ public static function importSql(\$mysqlDumpFile) { if (!\$mysqlDumpFile || !is_file(\$mysqlDumpFile)) { return; } foreach (explode(';', file_get_contents(\$mysqlDumpFile)) as \$sql) { if (\$sql = trim(\$sql)) { try { Db::connection(static::\$connection)->statement(\$sql); } catch (Throwable \$e) {} } } } } EOF; file_put_contents("$base/Install.php", $content); } /** * @return void */ protected function createInstallSqlFile($file) { file_put_contents($file, ''); } /** * @param $base * @param $name * @return void */ protected function createConfigFiles($base, $name) { // app.php $content = << true, 'controller_suffix' => 'Controller', 'controller_reuse' => false, 'version' => '1.0.0' ]; EOF; file_put_contents("$base/app.php", $content); // menu.php $content = << [ base_path() . '/plugin/$name/app/functions.php', ] ]; EOF; file_put_contents("$base/autoload.php", $content); // container.php $content = << support\\exception\\Handler::class, ]; EOF; file_put_contents("$base/exception.php", $content); // log.php $content = << [ 'handlers' => [ [ 'class' => Monolog\\Handler\\RotatingFileHandler::class, 'constructor' => [ runtime_path() . '/logs/$name.log', 7, Monolog\\Logger::DEBUG, ], 'formatter' => [ 'class' => Monolog\\Formatter\\LineFormatter::class, 'constructor' => [null, 'Y-m-d H:i:s', true], ], ] ], ], ]; EOF; file_put_contents("$base/log.php", $content); // middleware.php $content = << [ ] ]; EOF; file_put_contents("$base/middleware.php", $content); // process.php $content = << [ 'host' => '127.0.0.1', 'password' => null, 'port' => 6379, 'database' => 0, ], ]; EOF; file_put_contents("$base/redis.php", $content); // route.php $content = << true, 'middleware' => [], // Static file Middleware ]; EOF; file_put_contents("$base/static.php", $content); // translation.php $content = << 'zh_CN', // Fallback language 'fallback_locale' => ['zh_CN', 'en'], // Folder where language files are stored 'path' => base_path() . "/plugin/$name/resource/translations", ]; EOF; file_put_contents("$base/translation.php", $content); // view.php $content = << Raw::class ]; EOF; file_put_contents("$base/view.php", $content); // thinkorm.php $content = <<