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