composer = $composer; $this->io = $io; $this->fs = new Filesystem(); } /** * @param $parentDir * @param bool $excludeRoot * @return \Symfony\Component\Finder\Finder|\Symfony\Component\Finder\SplFileInfo[] */ public function getVcsDirs($parentDir, $excludeRoot = false) { $finder = new Finder(); $iterator = $finder ->directories() ->in($parentDir) ->ignoreVCS(false) ->ignoreDotFiles(false) ->exclude(['node_modules', '.git/*']) ->name('.git'); if ($excludeRoot) { $iterator = $iterator->depth('> 0'); } return $iterator; } /** * @param $parentDir * @param bool $excludeRoot */ public function cleanupVcsDirs($parentDir, $excludeRoot = false) { $dirs = []; foreach ($this->getVcsDirs($parentDir, $excludeRoot) as $dir) { $this->io->write(sprintf("Deleting %s directory from %s", $dir->getBasename(), $dir->getRealPath())); $dirs[] = $dir; } $this->deleteVcsDirs($dirs); } /** * @param array $dirs */ public function deleteVcsDirs(array $dirs) { /** @var SplFileInfo $dir */ foreach ($dirs as $dir) { $this->fs->removeDirectory($dir->getRealPath()); } } /** * @param \Composer\Package\PackageInterface $package */ public function onPostPackageEvent(PackageInterface $package) { $packagePath = $this->composer->getInstallationManager()->getInstallPath($package); if (!empty($packagePath)) { $this->cleanupVcsDirs($packagePath); } } }