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.

167 lines
3.9KB

  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\Util\Filesystem;
  12. use Symfony\Component\Finder\Finder;
  13. use Symfony\Component\Finder\Glob;
  14. /**
  15. * Manager of ignore patterns.
  16. *
  17. * @author Martin Hasoň <martin.hason@gmail.com>
  18. * @author François Pluchino <francois.pluchino@gmail.com>
  19. */
  20. class IgnoreManager
  21. {
  22. /**
  23. * @var string
  24. */
  25. protected $installDir;
  26. /**
  27. * @var Filesystem
  28. */
  29. private $filesystem;
  30. /**
  31. * @var bool
  32. */
  33. protected $enabled;
  34. /**
  35. * @var bool
  36. */
  37. protected $hasPattern;
  38. /**
  39. * @var Finder
  40. */
  41. private $finder;
  42. /**
  43. * Constructor.
  44. *
  45. * @param string $installDir The install dir
  46. * @param Filesystem|null $filesystem The filesystem
  47. */
  48. public function __construct($installDir, Filesystem $filesystem = null)
  49. {
  50. $this->installDir = $installDir;
  51. $this->filesystem = $filesystem ?: new Filesystem();
  52. $this->enabled = true;
  53. $this->hasPattern = false;
  54. $this->finder = Finder::create()->ignoreVCS(true)->ignoreDotFiles(false);
  55. }
  56. /**
  57. * Enable or not this ignore files manager.
  58. *
  59. * @param bool $enabled
  60. *
  61. * @return self
  62. */
  63. public function setEnabled($enabled)
  64. {
  65. $this->enabled = (bool) $enabled;
  66. return $this;
  67. }
  68. /**
  69. * Check if this ignore files manager is enabled.
  70. *
  71. * @return bool
  72. */
  73. public function isEnabled()
  74. {
  75. return $this->enabled;
  76. }
  77. /**
  78. * Check if a pattern is added.
  79. *
  80. * @return bool
  81. */
  82. public function hasPattern()
  83. {
  84. return $this->hasPattern;
  85. }
  86. /**
  87. * Adds an ignore pattern.
  88. *
  89. * @param string $pattern The pattern
  90. */
  91. public function addPattern($pattern)
  92. {
  93. $this->doAddPattern($this->convertPattern($pattern));
  94. $this->hasPattern = true;
  95. }
  96. /**
  97. * Deletes all files and directories that matches patterns.
  98. */
  99. public function cleanup()
  100. {
  101. if ($this->isEnabled() && $this->hasPattern() && realpath($this->installDir)) {
  102. $paths = iterator_to_array($this->finder->in($this->installDir));
  103. /* @var \SplFileInfo $path */
  104. foreach ($paths as $path) {
  105. $this->filesystem->remove($path);
  106. }
  107. }
  108. }
  109. /**
  110. * Action for Add an ignore pattern.
  111. *
  112. * @param string $pattern The pattern
  113. */
  114. public function doAddPattern($pattern)
  115. {
  116. if (0 === strpos($pattern, '!')) {
  117. $this->finder->notPath(Glob::toRegex(substr($pattern, 1), true, true));
  118. } else {
  119. $this->finder->path(Glob::toRegex($pattern, true, true));
  120. }
  121. }
  122. /**
  123. * Converter pattern to glob.
  124. *
  125. * @param string $pattern The pattern
  126. *
  127. * @return string The pattern converted
  128. */
  129. protected function convertPattern($pattern)
  130. {
  131. $prefix = 0 === strpos($pattern, '!') ? '!' : '';
  132. $searchPattern = trim(ltrim($pattern, '!'), '/');
  133. $pattern = $prefix.$searchPattern;
  134. if (in_array($searchPattern, array('*', '*.*'))) {
  135. $this->doAddPattern($prefix.'.*');
  136. } elseif (0 === strpos($searchPattern, '**/')) {
  137. $this->doAddPattern($prefix.'**/'.$searchPattern);
  138. $this->doAddPattern($prefix.substr($searchPattern, 3));
  139. } elseif ('.*' === $searchPattern) {
  140. $this->doAddPattern($prefix.'**/.*');
  141. } elseif (preg_match('/\/\*$|\/\*\*$/', $pattern, $matches)) {
  142. $this->doAddPattern(substr($pattern, 0, strlen($pattern) - strlen($matches[0])));
  143. }
  144. return $pattern;
  145. }
  146. }