-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathPathPattern.php
78 lines (75 loc) · 2.57 KB
/
PathPattern.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Helper;
/**
* Path pattern creation helper
*/
class PathPattern
{
/**
* Translate pattern with glob syntax into regular expression
*
* @param string $path
* @return string
*/
public function translatePatternFromGlob($path)
{
$pattern = str_replace(['\\?', '\\*'], ['[^/]', '[^/]*'], preg_quote($path));
$pattern = $this->translateGroupsFromGlob($pattern);
$pattern = $this->translateCharacterGroupsFromGlob($pattern);
return $pattern;
}
/**
* Translate groups from escaped glob syntax into regular expression
*
* Example: filename\.\{php,css,js\} -> filename\.(?:php|css|js)
* Transformations:
* 1. \{ -> (?:
* 2. , -> |
* 3. \} -> )
*
* @param string $pattern
* @return string
*/
protected function translateGroupsFromGlob($pattern)
{
preg_match_all('~\\\\\\{[^,\\}]+(?:,[^,\\}]*)*\\\\\\}~', $pattern, $matches, PREG_OFFSET_CAPTURE);
for ($index = count($matches[0]) - 1; $index >= 0; $index -= 1) {
list($match, $offset) = $matches[0][$index];
$replacement = substr_replace($match, '(?:', 0, 2);
$replacement = substr_replace($replacement, ')', -2);
$replacement = str_replace(',', '|', $replacement);
$pattern = substr_replace($pattern, $replacement, $offset, strlen($match));
}
return $pattern;
}
/**
* Translate character groups from escaped glob syntax into regular expression
*
* Example: \[\!a\-f\]\.php -> [^a-f]\.php
* Transformations:
* 1. \[ -> [
* 2. \! -> ^
* 3. \- -> -
* 4. \] -> ]
*
* @param string $pattern
* @return string
*/
protected function translateCharacterGroupsFromGlob($pattern)
{
preg_match_all('~\\\\\\[(\\\\\\!)?[^\\]]+\\\\\\]~i', $pattern, $matches, PREG_OFFSET_CAPTURE);
for ($index = count($matches[0]) - 1; $index >= 0; $index -= 1) {
list($match, $offset) = $matches[0][$index];
$exclude = !empty($matches[1][$index]);
$replacement = substr_replace($match, '[' . ($exclude ? '^' : ''), 0, $exclude ? 4 : 2);
$replacement = substr_replace($replacement, ']', -2);
$replacement = str_replace('\\-', '-', $replacement);
$pattern = substr_replace($pattern, $replacement, $offset, strlen($match));
}
return $pattern;
}
}