/framework/Pear/lib/Horde/Pear/Package/Contents/Ignore/Git.php

https://github.com/ewandor/horde · PHP · 183 lines · 81 code · 13 blank · 89 comment · 13 complexity · 06583d66d92ef40311b196dd214cb1f4 MD5 · raw file

  1. <?php
  2. /**
  3. * Horde_Pear_Package_Contents_Ignore_Git:: indicates which files in a content
  4. * listing should be ignored based on the contents from a .gitignore file.
  5. *
  6. * PHP version 5
  7. *
  8. * @category Horde
  9. * @package Pear
  10. * @author Gunnar Wrobel <wrobel@pardus.de>
  11. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  12. * @link http://pear.horde.org/index.php?package=Pear
  13. */
  14. /**
  15. * Horde_Pear_Package_Contents_Ignore_Git:: indicates which files in a content
  16. * listing should be ignored based on the contents from a .gitignore file.
  17. *
  18. * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
  19. *
  20. * See the enclosed file COPYING for license information (LGPL). If you
  21. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  22. *
  23. * @category Horde
  24. * @package Pear
  25. * @author Gunnar Wrobel <wrobel@pardus.de>
  26. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  27. * @link http://pear.horde.org/index.php?package=Pear
  28. */
  29. class Horde_Pear_Package_Contents_Ignore_Git
  30. implements Horde_Pear_Package_Contents_Ignore
  31. {
  32. /**
  33. * The regular expressions for ignored files.
  34. *
  35. * @var array
  36. */
  37. private $_ignore = array();
  38. /**
  39. * The regular expressions for files to exclude from ignoring.
  40. *
  41. * @var array
  42. */
  43. private $_include = array();
  44. /**
  45. * The root position of the repository.
  46. *
  47. * @var string
  48. */
  49. private $_root;
  50. /**
  51. * Constructor.
  52. *
  53. * @param string $gitignore The gitignore information
  54. * @param string $root The root position for the files that should be
  55. * checked.
  56. */
  57. public function __construct($gitignore, $root)
  58. {
  59. $this->_root = $root;
  60. $this->_prepare($gitignore);
  61. }
  62. /**
  63. * Prepare the list of ignores and includes from the gitignore input.
  64. *
  65. * @param string $gitignore The content of the .gitignore file.
  66. *
  67. * @return NULL
  68. */
  69. private function _prepare($gitignore)
  70. {
  71. foreach (explode("\n", $gitignore) as $line) {
  72. $line = strtr($line, ' ', '');
  73. if (empty($line) || strpos($line, '#') === 0) {
  74. continue;
  75. }
  76. if (strpos($line, '!') === 0) {
  77. $this->_include[] = $this->_getRegExpableSearchString(
  78. substr($line, 1)
  79. );
  80. } else {
  81. $this->_ignore[] = $this->_getRegExpableSearchString($line);
  82. }
  83. }
  84. }
  85. /**
  86. * Return the list of ignored patterns.
  87. *
  88. * @return array The list of patterns.
  89. */
  90. public function getIgnores()
  91. {
  92. return $this->_ignore;
  93. }
  94. /**
  95. * Return the list of included patterns.
  96. *
  97. * @return array The list of patterns.
  98. */
  99. public function getIncludes()
  100. {
  101. return $this->_include;
  102. }
  103. /**
  104. * Tell whether to ignore the element.
  105. *
  106. * @param SplFileInfo $element The element to check.
  107. *
  108. * @return bool True if the element should be ignored, false otherwise.
  109. */
  110. public function isIgnored(SplFileInfo $element)
  111. {
  112. $rooted_path = substr($element->getPathname(), strlen($this->_root));
  113. if ($this->_matches($this->_ignore, $rooted_path)
  114. && !$this->_matches($this->_include, $rooted_path)) {
  115. return true;
  116. }
  117. return false;
  118. }
  119. /**
  120. * Does the given path match one of the regular expression patterns?
  121. *
  122. * @param array $matches The regular expression patterns.
  123. * @param string $path The file path.
  124. *
  125. * @return NULL
  126. */
  127. private function _matches($matches, $path)
  128. {
  129. foreach ($matches as $match) {
  130. preg_match('/' . $match.'/', $path, $find);
  131. if (count($find)) {
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. /**
  138. * Converts $s into a string that can be used with preg_match
  139. *
  140. * @param string $s string with wildcards ? and *
  141. *
  142. * @return string converts * to .*, ? to ., etc.
  143. */
  144. private function _getRegExpableSearchString($s)
  145. {
  146. if ($s[0] == DIRECTORY_SEPARATOR) {
  147. $pre = '^';
  148. } else {
  149. $pre = '.*';
  150. }
  151. $x = strtr(
  152. $s,
  153. array(
  154. '?' => '.',
  155. '*' => '[^\/]*',
  156. '.' => '\\.',
  157. '\\' => '\\\\',
  158. '/' => '\\/',
  159. '-' => '\\-'
  160. )
  161. );
  162. if (substr($s, strlen($s) - 1) == DIRECTORY_SEPARATOR) {
  163. $post = '.*';
  164. } else {
  165. $post = '$';
  166. }
  167. return $pre . $x . $post;
  168. }
  169. }