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.

IgnoreFactory.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Package\PackageInterface;
  13. /**
  14. * Factory of ignore manager patterns.
  15. *
  16. * @author François Pluchino <francois.pluchino@gmail.com>
  17. */
  18. class IgnoreFactory
  19. {
  20. /**
  21. * Create a ignore manager.
  22. *
  23. * @param Composer $composer The composer instance
  24. * @param PackageInterface $package The package instance
  25. * @param string|null $installDir The custom installation directory
  26. * @param string|null $section The extra section of ignore patterns
  27. *
  28. * @return IgnoreManager
  29. */
  30. public static function create(Composer $composer, PackageInterface $package, $installDir = null, $section = 'asset-ignore-files')
  31. {
  32. $installDir = static::getInstallDir($composer, $package, $installDir);
  33. $manager = new IgnoreManager($installDir);
  34. $extra = $composer->getPackage()->getExtra();
  35. if (isset($extra[$section])) {
  36. foreach ($extra[$section] as $packageName => $patterns) {
  37. if ($packageName === $package->getName()) {
  38. static::addPatterns($manager, $patterns);
  39. break;
  40. }
  41. }
  42. }
  43. return $manager;
  44. }
  45. /**
  46. * Get the installation directory of the package.
  47. *
  48. * @param Composer $composer The composer instance
  49. * @param PackageInterface $package The package instance
  50. * @param string|null $installDir The custom installation directory
  51. *
  52. * @return string The installation directory
  53. */
  54. protected static function getInstallDir(Composer $composer, PackageInterface $package, $installDir = null)
  55. {
  56. if (null === $installDir) {
  57. $installDir = rtrim($composer->getConfig()->get('vendor-dir'), '/').'/'.$package->getName();
  58. }
  59. return rtrim($installDir, '/');
  60. }
  61. /**
  62. * Add ignore file patterns in the ignore manager.
  63. *
  64. * @param IgnoreManager $manager The ignore files manager
  65. * @param bool|array $patterns The patterns for ignore files
  66. */
  67. protected static function addPatterns(IgnoreManager $manager, $patterns)
  68. {
  69. $enabled = false === $patterns ? false : true;
  70. $manager->setEnabled($enabled);
  71. if (is_array($patterns)) {
  72. foreach ($patterns as $pattern) {
  73. $manager->addPattern($pattern);
  74. }
  75. }
  76. }
  77. }