| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- namespace plugin\saiadmin\command;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Input\InputArgument;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Console\Style\SymfonyStyle;
- /**
- * SaiAdmin 插件创建命令
- * 用于创建 saiadmin 插件
- */
- class SaiPlugin extends Command
- {
- protected static $defaultName = 'sai:plugin';
- protected static $defaultDescription = '创建 SaiAdmin 插件';
- /**
- * @return void
- */
- protected function configure()
- {
- $this->addArgument('name', InputArgument::REQUIRED, 'App plugin name');
- }
- /**
- * @param InputInterface $input
- * @param OutputInterface $output
- * @return int
- */
- protected function execute(InputInterface $input, OutputInterface $output): int
- {
- $io = new SymfonyStyle($input, $output);
- $io->title('SaiAdmin 插件创建工具');
- $io->text('此命令用于创建基于webman的 saiadmin 插件, 用于扩展 saiadmin 框架功能!');
- $io->newLine();
- $name = $input->getArgument('name');
- $io->text("创建 SaiAdmin 插件 $name");
- if (strpos($name, '/') !== false) {
- $io->error('名称错误,名称必须不包含字符 \'/\'');
- return self::FAILURE;
- }
- // Create dir config/plugin/$name
- if (is_dir($plugin_config_path = base_path() . "/plugin/$name")) {
- $io->error("目录 $plugin_config_path 已存在");
- return self::FAILURE;
- }
- $this->createAll($name);
- $io->newLine();
- $io->success("SaiAdmin 插件创建成功!");
- return self::SUCCESS;
- }
- /**
- * @param $name
- * @return void
- */
- protected function createAll($name)
- {
- $base_path = base_path();
- $this->mkdir("$base_path/plugin/$name/app", 0777, true);
- $this->mkdir("$base_path/plugin/$name/app/admin/controller", 0777, true);
- $this->mkdir("$base_path/plugin/$name/app/admin/logic", 0777, true);
- $this->mkdir("$base_path/plugin/$name/app/api/controller", 0777, true);
- $this->mkdir("$base_path/plugin/$name/app/api/logic", 0777, true);
- $this->mkdir("$base_path/plugin/$name/app/cache", 0777, true);
- $this->mkdir("$base_path/plugin/$name/app/event", 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/config", 0777, true);
- $this->createControllerFile("$base_path/plugin/$name/app/api/controller/IndexController.php", $name);
- $this->createFunctionsFile("$base_path/plugin/$name/app/functions.php");
- $this->createConfigFiles("$base_path/plugin/$name/config", $name);
- }
- /**
- * @param $path
- * @return void
- */
- protected function mkdir($path, $mode = 0777, $recursive = false)
- {
- if (is_dir($path)) {
- return;
- }
- echo "Create $path\r\n";
- mkdir($path, $mode, $recursive);
- }
- /**
- * @param $path
- * @param $name
- * @return void
- */
- protected function createControllerFile($path, $name)
- {
- $content = <<<EOF
- <?php
- namespace plugin\\$name\\app\\api\\controller;
- use plugin\\saiadmin\\basic\\OpenController;
- class IndexController extends OpenController
- {
- public function index()
- {
- return \$this->success([
- 'app' => '$name',
- 'version' => '1.0.0',
- ]);
- }
- }
- EOF;
- file_put_contents($path, $content);
- }
- /**
- * @param $file
- * @return void
- */
- protected function createFunctionsFile($file)
- {
- $content = <<<EOF
- <?php
- /**
- * Here is your custom functions.
- */
- EOF;
- file_put_contents($file, $content);
- }
- /**
- * @param $base
- * @param $name
- * @return void
- */
- protected function createConfigFiles($base, $name)
- {
- // app.php
- $content = <<<EOF
- <?php
- use support\\Request;
- return [
- 'debug' => true,
- 'controller_suffix' => 'Controller',
- 'controller_reuse' => false,
- 'version' => '1.0.0'
- ];
- EOF;
- file_put_contents("$base/app.php", $content);
- // autoload.php
- $content = <<<EOF
- <?php
- return [
- 'files' => [
- base_path() . '/plugin/$name/app/functions.php',
- ]
- ];
- EOF;
- file_put_contents("$base/autoload.php", $content);
- // container.php
- $content = <<<EOF
- <?php
- return new Webman\\Container;
- EOF;
- file_put_contents("$base/container.php", $content);
- // exception.php
- $content = <<<EOF
- <?php
- return [
- '' => \\plugin\\saiadmin\\app\\exception\\Handler::class,
- ];
- EOF;
- file_put_contents("$base/exception.php", $content);
- // log.php
- $content = <<<EOF
- <?php
- return [
- 'default' => [
- '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
- <?php
- use plugin\saiadmin\app\middleware\SystemLog;
- use plugin\saiadmin\app\middleware\CheckLogin;
- use plugin\saiadmin\app\middleware\CheckAuth;
- return [
- 'admin' => [
- CheckLogin::class,
- CheckAuth::class,
- SystemLog::class,
- ],
- 'api' => [
- ]
- ];
- EOF;
- file_put_contents("$base/middleware.php", $content);
- // process.php
- $content = <<<EOF
- <?php
- return [];
- EOF;
- file_put_contents("$base/process.php", $content);
- // route.php
- $content = <<<EOF
- <?php
- use Webman\\Route;
- EOF;
- file_put_contents("$base/route.php", $content);
- // static.php
- $content = <<<EOF
- <?php
- return [
- 'enable' => true,
- 'middleware' => [], // Static file Middleware
- ];
- EOF;
- file_put_contents("$base/static.php", $content);
- // translation.php
- $content = <<<EOF
- <?php
- return [
- // Default language
- 'locale' => '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);
- }
- }
|