PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Composer/Package/Archiver/HgExcludeFilter.php

https://bitbucket.org/gencer/composer
PHP | 107 lines | 47 code | 15 blank | 45 comment | 7 complexity | 96646de5bf5ff52350ee885077b8e3de MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Package\Archiver;
  12. use Symfony\Component\Finder;
  13. /**
  14. * An exclude filter that processes hgignore files
  15. *
  16. * @author Nils Adermann <naderman@naderman.de>
  17. */
  18. class HgExcludeFilter extends BaseExcludeFilter
  19. {
  20. const HG_IGNORE_REGEX = 1;
  21. const HG_IGNORE_GLOB = 2;
  22. /**
  23. * Either HG_IGNORE_REGEX or HG_IGNORE_GLOB
  24. * @var integer
  25. */
  26. protected $patternMode;
  27. /**
  28. * Parses .hgignore file if it exist
  29. *
  30. * @param string $sourcePath
  31. */
  32. public function __construct($sourcePath)
  33. {
  34. parent::__construct($sourcePath);
  35. $this->patternMode = self::HG_IGNORE_REGEX;
  36. if (file_exists($sourcePath.'/.hgignore')) {
  37. $this->excludePatterns = $this->parseLines(
  38. file($sourcePath.'/.hgignore'),
  39. array($this, 'parseHgIgnoreLine')
  40. );
  41. }
  42. }
  43. /**
  44. * Callback line parser which process hgignore lines
  45. *
  46. * @param string $line A line from .hgignore
  47. *
  48. * @return array An exclude pattern for filter()
  49. */
  50. public function parseHgIgnoreLine($line)
  51. {
  52. if (preg_match('#^syntax\s*:\s*(glob|regexp)$#', $line, $matches)) {
  53. if ($matches[1] === 'glob') {
  54. $this->patternMode = self::HG_IGNORE_GLOB;
  55. } else {
  56. $this->patternMode = self::HG_IGNORE_REGEX;
  57. }
  58. return null;
  59. }
  60. if ($this->patternMode == self::HG_IGNORE_GLOB) {
  61. return $this->patternFromGlob($line);
  62. } else {
  63. return $this->patternFromRegex($line);
  64. }
  65. }
  66. /**
  67. * Generates an exclude pattern for filter() from a hg glob expression
  68. *
  69. * @param string $line A line from .hgignore in glob mode
  70. *
  71. * @return array An exclude pattern for filter()
  72. */
  73. protected function patternFromGlob($line)
  74. {
  75. $pattern = '#'.substr(Finder\Glob::toRegex($line), 2, -1).'#';
  76. $pattern = str_replace('[^/]*', '.*', $pattern);
  77. return array($pattern, false, true);
  78. }
  79. /**
  80. * Generates an exclude pattern for filter() from a hg regexp expression
  81. *
  82. * @param string $line A line from .hgignore in regexp mode
  83. *
  84. * @return array An exclude pattern for filter()
  85. */
  86. public function patternFromRegex($line)
  87. {
  88. // WTF need to escape the delimiter safely
  89. $pattern = '#'.preg_replace('/((?:\\\\\\\\)*)(\\\\?)#/', '\1\2\2\\#', $line).'#';
  90. return array($pattern, false, true);
  91. }
  92. }