This commit is contained in:
2025-10-20 10:02:41 +08:00
parent a4858d47fc
commit dc0a271adf
2805 changed files with 451240 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
<?php
namespace think\composer;
use Composer\Package\PackageInterface;
use Composer\Repository\InstalledRepositoryInterface;
use React\Promise\PromiseInterface;
abstract class LibraryInstaller extends \Composer\Installer\LibraryInstaller
{
public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
{
return $this->makePromise(parent::install($repo, $package));
}
public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
{
return $this->makePromise(parent::update($repo, $initial, $target));
}
protected function makePromise($promise)
{
if ($promise instanceof PromiseInterface) {
return $promise;
}
return new Promise();
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace think\composer;
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
class Plugin implements PluginInterface
{
public function activate(Composer $composer, IOInterface $io)
{
$manager = $composer->getInstallationManager();
//框架核心
$manager->addInstaller(new ThinkFramework($io, $composer));
//单元测试
$manager->addInstaller(new ThinkTesting($io, $composer));
//扩展
$manager->addInstaller(new ThinkExtend($io, $composer));
}
public function deactivate(Composer $composer, IOInterface $io)
{
}
public function uninstall(Composer $composer, IOInterface $io)
{
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace think\composer;
class Promise
{
public function then($callable)
{
$callable();
}
}

View File

@@ -0,0 +1,77 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\composer;
use Composer\Package\PackageInterface;
use Composer\Repository\InstalledRepositoryInterface;
class ThinkExtend extends LibraryInstaller
{
public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
{
return parent::install($repo, $package)->then(function () use ($package) {
$this->copyExtraFiles($package);
});
}
public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
{
return parent::update($repo, $initial, $target)->then(function () use ($target) {
$this->copyExtraFiles($target);
});
}
protected function copyExtraFiles(PackageInterface $package)
{
if ($this->composer->getPackage()->getType() == 'project') {
$extra = $package->getExtra();
if (!empty($extra['think-config'])) {
$composerExtra = $this->composer->getPackage()->getExtra();
$appDir = !empty($composerExtra['app-path']) ? $composerExtra['app-path'] : 'application';
if (is_dir($appDir)) {
$extraDir = $appDir . DIRECTORY_SEPARATOR . 'extra';
$this->filesystem->ensureDirectoryExists($extraDir);
//配置文件
foreach ((array) $extra['think-config'] as $name => $config) {
$target = $extraDir . DIRECTORY_SEPARATOR . $name . '.php';
$source = $this->getInstallPath($package) . DIRECTORY_SEPARATOR . $config;
if (is_file($target)) {
$this->io->write("<info>File {$target} exist!</info>");
continue;
}
if (!is_file($source)) {
$this->io->write("<info>File {$target} not exist!</info>");
continue;
}
copy($source, $target);
}
}
}
}
}
public function supports($packageType)
{
return 'think-extend' === $packageType;
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace think\composer;
use Composer\Package\PackageInterface;
use Composer\Repository\InstalledRepositoryInterface;
use InvalidArgumentException;
class ThinkFramework extends LibraryInstaller
{
public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
{
return parent::install($repo, $package)->then(function () use ($package) {
if ($this->composer->getPackage()
->getType() == 'project' && $package->getInstallationSource() != 'source') {
//remove tests dir
$this->filesystem->removeDirectory($this->getInstallPath($package) . DIRECTORY_SEPARATOR . 'tests');
}
});
}
/**
* {@inheritDoc}
*/
public function getInstallPath(PackageInterface $package)
{
if ('topthink/framework' !== $package->getPrettyName()) {
throw new InvalidArgumentException('Unable to install this library!');
}
if ($this->composer->getPackage()->getType() !== 'project') {
return parent::getInstallPath($package);
}
if ($this->composer->getPackage()) {
$extra = $this->composer->getPackage()->getExtra();
if (!empty($extra['think-path'])) {
return $extra['think-path'];
}
}
return 'thinkphp';
}
public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
{
return parent::update($repo, $initial, $target)->then(function () use ($target) {
if ($this->composer->getPackage()->getType() == 'project' && $target->getInstallationSource() != 'source') {
//remove tests dir
$this->filesystem->removeDirectory($this->getInstallPath($target) . DIRECTORY_SEPARATOR . 'tests');
}
});
}
/**
* {@inheritDoc}
*/
public function supports($packageType)
{
return 'think-framework' === $packageType;
}
}

View File

@@ -0,0 +1,64 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\composer;
use Composer\Package\PackageInterface;
use Composer\Repository\InstalledRepositoryInterface;
use InvalidArgumentException;
class ThinkTesting extends LibraryInstaller
{
/**
* {@inheritDoc}
*/
public function getInstallPath(PackageInterface $package)
{
if ('topthink/think-testing' !== $package->getPrettyName()) {
throw new InvalidArgumentException('Unable to install this library!');
}
return parent::getInstallPath($package);
}
public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
{
parent::install($repo, $package)->then(function () use ($package) {
$this->copyTestDir($package);
});
}
public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
{
return parent::update($repo, $initial, $target)->then(function () use ($target) {
$this->copyTestDir($target);
});
}
private function copyTestDir(PackageInterface $package)
{
$appDir = dirname($this->vendorDir);
$source = $this->getInstallPath($package) . DIRECTORY_SEPARATOR . 'example';
if (!is_file($appDir . DIRECTORY_SEPARATOR . 'phpunit.xml')) {
$this->filesystem->copyThenRemove($source, $appDir);
} else {
$this->filesystem->removeDirectoryPhp($source);
}
}
/**
* {@inheritDoc}
*/
public function supports($packageType)
{
return 'think-testing' === $packageType;
}
}