ThinkFramework.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace think\composer;
  3. use Composer\Package\PackageInterface;
  4. use Composer\Repository\InstalledRepositoryInterface;
  5. use InvalidArgumentException;
  6. class ThinkFramework extends LibraryInstaller
  7. {
  8. /**
  9. * {@inheritDoc}
  10. */
  11. public function getInstallPath(PackageInterface $package)
  12. {
  13. if ('topthink/framework' !== $package->getPrettyName()) {
  14. throw new InvalidArgumentException('Unable to install this library!');
  15. }
  16. if ($this->composer->getPackage()->getType() !== 'project') {
  17. return parent::getInstallPath($package);
  18. }
  19. if ($this->composer->getPackage()) {
  20. $extra = $this->composer->getPackage()->getExtra();
  21. if (!empty($extra['think-path'])) {
  22. return $extra['think-path'];
  23. }
  24. }
  25. return 'thinkphp';
  26. }
  27. public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
  28. {
  29. return parent::install($repo, $package)
  30. ->then(function () use ($package) {
  31. $this->removeTestDir($package);
  32. });
  33. }
  34. public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
  35. {
  36. return parent::update($repo, $initial, $target)
  37. ->then(function () use ($target) {
  38. $this->removeTestDir($target);
  39. });
  40. }
  41. protected function removeTestDir(PackageInterface $target)
  42. {
  43. if ($this->composer->getPackage()->getType() == 'project' && $target->getInstallationSource() != 'source') {
  44. //remove tests dir
  45. $this->filesystem->removeDirectory($this->getInstallPath($target) . DIRECTORY_SEPARATOR . 'tests');
  46. }
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function supports($packageType)
  52. {
  53. return 'think-framework' === $packageType;
  54. }
  55. }