-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhpArrayModernizer.php
More file actions
75 lines (67 loc) · 2.25 KB
/
PhpArrayModernizer.php
File metadata and controls
75 lines (67 loc) · 2.25 KB
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
<?php
namespace Brainexploded;
use Brainexploded\FSTools\FSTraverser;
class PhpArrayModernizer
{
protected $searchSeq = [
'(//).*?$',
'/(\*).*?\*/',
'(")[^"\\\\]*(?:\\\\.[^"\\\\]*)*"',
"(')[^'\\\\]*(?:\\\\.[^'\\\\]*)*'",
'\b(array)\s*\(',
'(\()',
'(\))'
];
public function modernize($path)
{
$tr = new FSTraverser(
// root dir
$path,
// callback
function($path, $entry, $content) {
$fullpath = $path.'/'.$entry;
$handle_write = fopen($fullpath, 'wb');
fwrite($handle_write, $this->process($content));
fclose($handle_write);
},
// exclude nodes
['.git'],
// allowed extensions
['php']
);
$tr->go(true);
}
protected function process($data)
{
$offset = 0;
$pattern = '~(?|' . implode($this->searchSeq, '|') . ')~mus';
$pairs = [];
while (preg_match($pattern, $data, $matches, PREG_OFFSET_CAPTURE, $offset)) {
if ($matches) {
if (count($matches) > 1) {
switch ($matches[1][0]) {
case 'array':
case '(':
$pairs[] = $matches[1][0];
if ($matches[1][0] == 'array') {
$data = substr_replace($data, '[', $matches[0][1], strlen($matches[0][0]));
}
$offset = $matches[0][1] + 1;
break;
case ')':
if (array_pop($pairs) == 'array') {
$data = substr_replace($data, ']', $matches[0][1], strlen($matches[0][0]));
}
$offset = $matches[0][1] + 1;
break;
default:
$offset = $matches[0][1] + strlen($matches[0][0]);
}
} else {
$offset = $matches[0][1] + strlen($matches[0][0]);
}
}
}
return $data;
}
}