-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreflect.php
More file actions
145 lines (134 loc) · 4.37 KB
/
reflect.php
File metadata and controls
145 lines (134 loc) · 4.37 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env php
<?php
require __DIR__ . "/vendor/nikic/php-parser/lib/bootstrap.php";
$file = is_dir($argv[1]) ? $argv[1] : dirname($argv[1]);
if (!file_exists($file)) {
exit();
}
$files = array();
while (strlen($file) > 1) {
if (file_exists($file . DIRECTORY_SEPARATOR . "vendor" . DIRECTORY_SEPARATOR . "autoload.php")) {
$autoloader = realpath($file) . DIRECTORY_SEPARATOR . "vendor" . DIRECTORY_SEPARATOR . "autoload.php";
$autoload = true;
break;
}
$file = dirname($file);
}
$indexFile = $file . DIRECTORY_SEPARATOR . "vendor" . DIRECTORY_SEPARATOR . "sublimeAutoComplete.json";
$parser = new PhpParser\Parser(new PhpParser\Lexer);
if ($autoload) {
chdir($file);
if (file_exists($indexFile)) {
echo file_get_contents($indexFile);
return;
}
foreach (new AdvancedDirectoryIterator("-R *.php") as $i) {
$path = $i->getPathname();
if (stristr($path, DIRECTORY_SEPARATOR . "composer")) {
continue;
}
$files[] = $path;
}
}
$classes = array();
$out = [];
foreach ($files as $key => $phpfile) {
try {
$stmts = $parser->parse(file_get_contents($file . DIRECTORY_SEPARATOR .$phpfile));
// $stmts is an array of statement nodes
} catch (PhpParser\Error $e) {
echo 'Parse Error: ', $e->getMessage();
}
foreach ($stmts as $key => $value) {
$namespace = "";
if ($value instanceof PhpParser\Node\Stmt\Namespace_) {
$namespace = implode("\\", $value->name->parts);
foreach ($value->stmts as $key => $obj) {
if ($obj instanceof PhpParser\Node\Stmt\Class_) {
$classes[] = $namespace . "\\" . $obj->name;
}
}
}
}
}
foreach ($classes as $key => $class) {
try {
$return = null;
$cmd = "php " .escapeshellarg( __DIR__ . "/reflector.php") ." " .escapeshellarg($autoloader) . " " . escapeshellarg($class);
exec($cmd, $return);
$meths = json_decode(implode("\n", $return));
if (!$meths) {
echo "broke $class \n\n";
echo implode("\n", $return)."\n\n";
continue;
}
foreach ($meths as $meth => $info) {
if(!isset($out[$meth])) $out[$meth] = array();
$out[$meth] = array_merge($out[$meth], $info);
# code...
}
} catch (Exception $e) {
}
}
if(count($out) == 0) exit;
file_put_contents($indexFile, json_encode($out));
echo json_encode($out);
/**
* Real Recursive Directory Iterator
*/
class RRDI extends RecursiveIteratorIterator
{
/**
* Creates Real Recursive Directory Iterator
* @param string $path
* @param int $flags
* @return DirectoryIterator
*/
public function __construct($path, $flags = 0)
{
parent::__construct(new RecursiveDirectoryIterator($path, $flags));
}
}
/**
* Real RecursiveDirectoryIterator Filtered Class
* Returns only those items which filenames match given regex
*/
class AdvancedDirectoryIterator extends FilterIterator
{
/**
* Regex storage
* @var string
*/
private $regex;
/**
* Creates new AdvancedDirectoryIterator
* @param string $path, prefix with '-R ' for recursive, postfix with /[wildcards] for matching
* @param int $flags
* @return DirectoryIterator
*/
public function __construct($path, $flags = 0)
{
if (strpos($path, '-R ') === 0) {
$recursive = true;
$path = substr($path, 3);}
if (preg_match('~/?([^/]*\*[^/]*)$~', $path, $matches)) {
// matched wildcards in filename
$path = substr($path, 0, -strlen($matches[1]) - 1); // strip wildcards part from path
$this->regex = '~^' . str_replace('*', '.*', str_replace('.', '\.', $matches[1])) . '$~'; // convert wildcards to regex
if (!$path) {
$path = '.';
}
// if no path given, we assume CWD
}
parent::__construct($recursive ? new RRDI($path, $flags) : new DirectoryIterator($path));
}
/**
* Checks for regex in current filename, or matches all if no regex specified
* @return bool
*/
public function accept()
{
// FilterIterator method
return $this->regex === null ? true : preg_match($this->regex, $this->getInnerIterator()->getFilename());
}
}