ThinkExtend.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Installer\LibraryInstaller;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Repository\InstalledRepositoryInterface;
  15. class ThinkExtend extends LibraryInstaller
  16. {
  17. public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
  18. {
  19. parent::install($repo, $package);
  20. $this->copyExtraFiles($package);
  21. }
  22. public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
  23. {
  24. parent::update($repo, $initial, $target);
  25. $this->copyExtraFiles($target);
  26. }
  27. protected function copyExtraFiles(PackageInterface $package)
  28. {
  29. if ($this->composer->getPackage()->getType() == 'project') {
  30. $extra = $package->getExtra();
  31. if (!empty($extra['think-config'])) {
  32. $composerExtra = $this->composer->getPackage()->getExtra();
  33. $appDir = !empty($composerExtra['app-path']) ? $composerExtra['app-path'] : 'application';
  34. if (is_dir($appDir)) {
  35. $extraDir = $appDir . DIRECTORY_SEPARATOR . 'extra';
  36. $this->filesystem->ensureDirectoryExists($extraDir);
  37. //配置文件
  38. foreach ((array) $extra['think-config'] as $name => $config) {
  39. $target = $extraDir . 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. }
  55. public function supports($packageType)
  56. {
  57. return 'think-extend' === $packageType;
  58. }
  59. }