You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AssetInstaller.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /*
  3. * This file is part of the Fxp Composer Asset Plugin package.
  4. *
  5. * (c) François Pluchino <francois.pluchino@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Fxp\Composer\AssetPlugin\Installer;
  11. use Composer\Composer;
  12. use Composer\Installer\LibraryInstaller;
  13. use Composer\IO\IOInterface;
  14. use Composer\Package\PackageInterface;
  15. use Composer\Util\Filesystem;
  16. use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface;
  17. use Fxp\Composer\AssetPlugin\Util\AssetPlugin;
  18. /**
  19. * Installer for asset packages.
  20. *
  21. * @author Martin Hasoň <martin.hason@gmail.com>
  22. * @author François Pluchino <francois.pluchino@gmail.com>
  23. */
  24. class AssetInstaller extends LibraryInstaller
  25. {
  26. /**
  27. * Constructor.
  28. *
  29. * @param IOInterface $io
  30. * @param Composer $composer
  31. * @param AssetTypeInterface $assetType
  32. * @param Filesystem $filesystem
  33. */
  34. public function __construct(IOInterface $io, Composer $composer, AssetTypeInterface $assetType, Filesystem $filesystem = null)
  35. {
  36. parent::__construct($io, $composer, $assetType->getComposerType(), $filesystem);
  37. $extra = $composer->getPackage()->getExtra();
  38. if (!empty($extra['asset-installer-paths'][$this->type])) {
  39. $this->vendorDir = rtrim($extra['asset-installer-paths'][$this->type], '/');
  40. } else {
  41. $this->vendorDir = rtrim($this->vendorDir.'/'.$assetType->getComposerVendorName(), '/');
  42. }
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function supports($packageType)
  48. {
  49. return $packageType === $this->type;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getInstallPath(PackageInterface $package)
  55. {
  56. $this->initializeVendorDir();
  57. $targetDir = $package->getTargetDir();
  58. list(, $name) = explode('/', $package->getPrettyName(), 2);
  59. return ($this->vendorDir ? $this->vendorDir.'/' : '').$name.($targetDir ? '/'.$targetDir : '');
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. protected function getPackageBasePath(PackageInterface $package)
  65. {
  66. return $this->getInstallPath($package);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. protected function installCode(PackageInterface $package)
  72. {
  73. $package = AssetPlugin::addMainFiles($this->composer, $package);
  74. parent::installCode($package);
  75. $this->deleteIgnoredFiles($package);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function updateCode(PackageInterface $initial, PackageInterface $target)
  81. {
  82. $target = AssetPlugin::addMainFiles($this->composer, $target);
  83. parent::updateCode($initial, $target);
  84. $this->deleteIgnoredFiles($target);
  85. }
  86. /**
  87. * Deletes files defined in bower.json in section "ignore".
  88. *
  89. * @param PackageInterface $package
  90. */
  91. protected function deleteIgnoredFiles(PackageInterface $package)
  92. {
  93. $manager = IgnoreFactory::create($this->composer, $package, $this->getInstallPath($package));
  94. if ($manager->isEnabled() && !$manager->hasPattern()) {
  95. $this->addIgnorePatterns($manager, $package);
  96. }
  97. $manager->cleanup();
  98. }
  99. /**
  100. * Add ignore patterns in the manager.
  101. *
  102. * @param IgnoreManager $manager The ignore manager instance
  103. * @param PackageInterface $package The package instance
  104. */
  105. protected function addIgnorePatterns(IgnoreManager $manager, PackageInterface $package)
  106. {
  107. // override this method
  108. }
  109. }