From 08fb1ff8689152cfdf2311e2c0394e78a8e93fba Mon Sep 17 00:00:00 2001 From: zyimm Date: Fri, 7 Jul 2023 16:33:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++ composer.json | 24 +++++++++++++++ src/Plugin.php | 25 ++++++++++++++++ src/Promise.php | 13 +++++++++ src/Vendor.php | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Plugin.php create mode 100644 src/Promise.php create mode 100644 src/Vendor.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..49dbaea --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/vendor/ +/.idea/ +/composer.lock diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..668cb4b --- /dev/null +++ b/composer.json @@ -0,0 +1,24 @@ +{ + "name": "tm_xls/composer-thinkphp-installer", + "description": "description", + "minimum-stability": "stable", + "type": "composer-plugin", + "license": "proprietary", + "authors": [ + { + "name": "zyimm", + "email": "email@example.com" + } + ], + "autoload": { + "psr-4": { + "tm\\xls\\composer\\thinkphp\\": "src" + } + }, + "require": { + "composer-plugin-api": "^2.0" + }, + "require-dev": { + "composer/composer": "^2.0" + } +} \ No newline at end of file diff --git a/src/Plugin.php b/src/Plugin.php new file mode 100644 index 0000000..6d07f98 --- /dev/null +++ b/src/Plugin.php @@ -0,0 +1,25 @@ +getInstallationManager(); + //扩展 + $manager->addInstaller(new Vendor($io, $composer)); + } + + public function deactivate(Composer $composer, IOInterface $io) + { + } + + public function uninstall(Composer $composer, IOInterface $io) + { + } +} \ No newline at end of file diff --git a/src/Promise.php b/src/Promise.php new file mode 100644 index 0000000..fb39a76 --- /dev/null +++ b/src/Promise.php @@ -0,0 +1,13 @@ +then(function () use ($package) { + $this->config($package); + })->then(function () use ($package) { + $this->route($package); + }); + } + + public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target): PromiseInterface + { + return parent::update($repo, $initial, $target) + ->then(function () use ($target) { + $this->config($target); + })->then(function () use ($target) { + $this->route($target); + }); + } + + protected function config(PackageInterface $package): PackageInterface + { + if ($this->composer->getPackage()->getType() == 'project') { + $extra = $package->getExtra(); + if (!empty($extra['think-config'])) { + $configDir = 'config'; + $this->filesystem->ensureDirectoryExists($configDir); + //配置文件 + foreach ((array)$extra['think-config'] as $name => $config) { + $this->copyFile($configDir, $package, $name, $config); + } + } + } + return $package; + } + + protected function route(PackageInterface $package): PackageInterface + { + if ($this->composer->getPackage()->getType() == 'project') { + $extra = $package->getExtra(); + if (!empty($extra['think-route'])) { + $configDir = 'route'; + $this->filesystem->ensureDirectoryExists($configDir); + //配置文件 + foreach ((array)$extra['think-route'] as $name => $config) { + $this->copyFile($configDir, $package, $name, $config); + } + } + } + return $package; + } + + private function copyFile($configDir, $package, $name, $config) + { + $target = $configDir.DIRECTORY_SEPARATOR.$name.'.php'; + $source = $this->getInstallPath($package).DIRECTORY_SEPARATOR.$config; + if (is_file($target)) { + $this->io->write("File $target exist!"); + return; + } + if (!is_file($source)) { + $this->io->write("File $target not exist!"); + return; + } + copy($source, $target); + } +} \ No newline at end of file