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.

114 lines
3.1KB

  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\Composer;
  11. use Composer\DependencyResolver\Operation\InstallOperation;
  12. use Composer\DependencyResolver\Operation\OperationInterface;
  13. use Composer\DependencyResolver\Operation\UpdateOperation;
  14. use Composer\Package\PackageInterface;
  15. use Composer\Installer\PackageEvent;
  16. use Fxp\Composer\AssetPlugin\Assets;
  17. use Fxp\Composer\AssetPlugin\Installer\IgnoreFactory;
  18. /**
  19. * Composer script handler.
  20. *
  21. * @author François Pluchino <francois.pluchino@gmail.com>
  22. */
  23. class ScriptHandler
  24. {
  25. /**
  26. * Remove ignored files of the installed package defined in the root
  27. * package extra section.
  28. *
  29. * @param PackageEvent $event
  30. */
  31. public static function deleteIgnoredFiles(PackageEvent $event)
  32. {
  33. if (null === $package = static::getLibraryPackage($event->getOperation())) {
  34. return;
  35. }
  36. $section = static::getIgnoreExtraSection();
  37. $manager = IgnoreFactory::create($event->getComposer(), $package, null, $section);
  38. $manager->cleanup();
  39. }
  40. /**
  41. * Get the root extra section of igore file patterns for each package.
  42. *
  43. * @return string The extra section name
  44. */
  45. protected static function getIgnoreExtraSection()
  46. {
  47. return 'asset-ignore-files';
  48. }
  49. /**
  50. * Get the library package (not asset package).
  51. *
  52. * @param OperationInterface $operation The operation
  53. *
  54. * @return PackageInterface|null Return NULL if the package is an asset
  55. */
  56. protected static function getLibraryPackage(OperationInterface $operation)
  57. {
  58. $package = static::getOperationPackage($operation);
  59. $data = null;
  60. if ($package && !static::isAsset($package)) {
  61. $data = $package;
  62. }
  63. return $data;
  64. }
  65. /**
  66. * Get the package defined in the composer operation.
  67. *
  68. * @param OperationInterface $operation The operation
  69. *
  70. * @return PackageInterface|null Return NULL if the operation is not INSTALL or UPDATE
  71. */
  72. protected static function getOperationPackage(OperationInterface $operation)
  73. {
  74. $data = null;
  75. if ($operation instanceof UpdateOperation) {
  76. $data = $operation->getTargetPackage();
  77. } elseif ($operation instanceof InstallOperation) {
  78. $data = $operation->getPackage();
  79. }
  80. return $data;
  81. }
  82. /**
  83. * Check if the package is a asset package.
  84. *
  85. * @param PackageInterface $package The package instance
  86. *
  87. * @return bool
  88. */
  89. protected static function isAsset(PackageInterface $package)
  90. {
  91. foreach (Assets::getTypes() as $type) {
  92. $type = Assets::createType($type);
  93. if ($package->getType() === $type->getComposerType()) {
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. }