<?php
namespace think\composer;
use Composer\Package\PackageInterface;
use Composer\Repository\InstalledRepositoryInterface;
use InvalidArgumentException;
class ThinkFramework extends LibraryInstaller
{
    
    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 install(InstalledRepositoryInterface $repo, PackageInterface $package)
    {
        return parent::install($repo, $package)
            ->then(function () use ($package) {
                $this->removeTestDir($package);
            });
    }
    public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
    {
        return parent::update($repo, $initial, $target)
            ->then(function () use ($target) {
                $this->removeTestDir($target);
            });
    }
    protected function removeTestDir(PackageInterface $target)
    {
        if ($this->composer->getPackage()->getType() == 'project' && $target->getInstallationSource() != 'source') {
                       $this->filesystem->removeDirectory($this->getInstallPath($target) . DIRECTORY_SEPARATOR . 'tests');
        }
    }
    
    public function supports($packageType)
    {
        return 'think-framework' === $packageType;
    }
}