ThinkExtend.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\composer;
  12. use Composer\Package\PackageInterface;
  13. use Composer\Repository\InstalledRepositoryInterface;
  14. class ThinkExtend extends LibraryInstaller
  15. {
  16. public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
  17. {
  18. return parent::install($repo, $package)
  19. ->then(function () use ($package) {
  20. $this->copyExtraFiles($package);
  21. });
  22. }
  23. public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
  24. {
  25. return parent::update($repo, $initial, $target)
  26. ->then(function () use ($target) {
  27. $this->copyExtraFiles($target);
  28. });
  29. }
  30. protected function copyExtraFiles(PackageInterface $package)
  31. {
  32. if ($this->composer->getPackage()->getType() == 'project') {
  33. $extra = $package->getExtra();
  34. if (!empty($extra['think-config'])) {
  35. $configDir = 'config';
  36. $this->filesystem->ensureDirectoryExists($configDir);
  37. //配置文件
  38. foreach ((array) $extra['think-config'] as $name => $config) {
  39. $target = $configDir . DIRECTORY_SEPARATOR . $name . '.php';
  40. $source = $this->getInstallPath($package) . DIRECTORY_SEPARATOR . $config;
  41. if (is_file($target)) {
  42. $this->io->write("<info>File {$target} exist!</info>");
  43. continue;
  44. }
  45. if (!is_file($source)) {
  46. $this->io->write("<info>File {$target} not exist!</info>");
  47. continue;
  48. }
  49. copy($source, $target);
  50. }
  51. }
  52. }
  53. }
  54. public function supports($packageType)
  55. {
  56. return 'think-extend' === $packageType;
  57. }
  58. }