-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathbuildPHAR.php
124 lines (108 loc) · 3.05 KB
/
buildPHAR.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
// fnmatch implementation taken from:
// https://www.php.net/manual/en/function.fnmatch.php#100207
if (!function_exists('fnmatch'))
define('FNM_PATHNAME', 1);
define('FNM_NOESCAPE', 2);
define('FNM_PERIOD', 4);
define('FNM_CASEFOLD', 16);
function fnmatch($pattern, $string, $flags=0)
{
return pcre_fnmatch($pattern, $string, $flags);
}
}
function pcre_fnmatch($pattern, $string, $flags = 0)
{
$modifiers = null;
$transforms = array(
'\*' => '.*',
'\?' => '.',
'\[\!' => '[^',
'\[' => '[',
'\]' => ']',
'\.' => '\.',
'\\' => '\\\\'
);
// Forward slash in string must be in pattern:
if ($flags & FNM_PATHNAME) {
$transforms['\*'] = '[^/]*';
}
// Back slash should not be escaped:
if ($flags & FNM_NOESCAPE) {
unset($transforms['\\']);
}
// Perform case insensitive match:
if ($flags & FNM_CASEFOLD) {
$modifiers .= 'i';
}
// Period at start must be the same as pattern:
if ($flags & FNM_PERIOD) {
if (strpos($string, '.') === 0 && strpos($pattern, '.') !== 0) {
return false;
}
}
$pattern = '#^'
. strtr(preg_quote($pattern, '#'), $transforms)
. '$#'
. $modifiers
;
return (boolean)preg_match($pattern, $string);
}
$phar = new Phar('build/phpctags.phar', 0, 'phpctags.phar');
if (version_compare(PHP_VERSION, '5.4.0') < 0) {
class RecursiveCallbackFilterIterator extends RecursiveFilterIterator
{
public function __construct(RecursiveIterator $iterator, $callback)
{
$this->callback = $callback;
parent::__construct($iterator);
}
public function accept()
{
$callback = $this->callback;
return $callback(
parent::current(),
parent::key(),
parent::getInnerIterator()
);
}
public function getChildren()
{
return new self(
$this->getInnerIterator()->getChildren(),
$this->callback
);
}
}
}
$phar->buildFromIterator(
new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator(
getcwd(),
FilesystemIterator::SKIP_DOTS
),
function ($current) {
$excludes = array(
'.*',
'tags',
'build/*',
'tests/*',
'Makefile',
'bin/phpctags',
'buildPHAR.php',
);
foreach ($excludes as $exclude) {
if (fnmatch(getcwd().'/'.$exclude, $current->getPathName())) {
return false;
}
}
return true;
}
)
),
getcwd()
);
$phar->setStub(
"#!/usr/bin/env php\n".$phar->createDefaultStub('bootstrap.php')
);