-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFetchFiles.php
144 lines (114 loc) · 3.88 KB
/
FetchFiles.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace DocumentPHP;
include_once getcwd() . '/system/engine/controller.php';
class FetchFiles {
private $results = [];
private $destination_path = '';
public function getResults() {
return $this->results;
}
public function setDestinationPath($path = '') {
if ($path) {
$this->destination_path = $path;
} else {
$this->destination_path = getcwd() . '/catalog/controller/api/';
}
}
private function recursive_copy($src) {
if (is_dir($src)) {
$dir = opendir($src);
while (($file = readdir($dir))) {
if ((substr($file, -1) != '_') && ($file != '.') && ($file != '..')) {
if (is_dir($src . '/' . $file)) {
$this->recursive_copy($src . '/' . $file);
} else {
if (is_file($this->destination_path . $file)) {
$this->start_fetching($this->destination_path . $file);
// print_r($this->destination_path . $file);
}
}
}
}
closedir($dir);
return;
} elseif (is_file($src)) {
} else {
die('<strong>Not found : </strong> ' . $src);
// print_r($src);die;
}
}
private function get_function($method, $class = null) {
if (!empty($class)) {
$func = new \ReflectionMethod($class, $method);
} else {
$func = new ReflectionFunction($method);
}
$f = $func->getFileName();
$start_line = $func->getStartLine() - 1;
$end_line = $func->getEndLine();
$length = $end_line - $start_line;
$source = file($f);
$source = implode('', array_slice($source, 0, count($source)));
$source = preg_split("/" . PHP_EOL . "/", $source);
$body = '';
for ($i = $start_line; $i < $end_line; $i++) {
$body .= "{$source[$i]}\n";
}
return $body;
}
public function start_processing($path = '') {
$this->setDestinationPath($path);
$this->recursive_copy($this->destination_path);
}
private function start_fetching($file) {
include_once $file;
$start = strpos($file, 'controller') + strlen('controller');
$end = strpos($file, '.php') - $start;
$route = substr($file, $start, $end);
$class_name = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $route);
$myclass = new $class_name([]);
$class_methods = get_class_methods($myclass);
$class_vars = get_class_vars(get_class($myclass));
$current_data = [];
// echo "<pre>";
foreach ($class_methods as $method_name) {
$function_string = $this->get_function($method_name, $myclass);
if ($this->isReturningJSONResponse($function_string)) {
$method_types_vars = $this->findMethodTypesVars($function_string);
$current_method_type = empty($method_types_vars['POST']) ? 'GET' : 'POST';
$current_data[] = [
'full_route' => $route . ($method_name == 'index' ? '' : ('/' . $method_name)),
'current_method_type' => $current_method_type,
'route' => $route,
'method_name' => $method_name,
'method_types_vars' => $method_types_vars,
'file' => $file,
];
}
}
$this->results[$class_name] = $current_data;
// print_r($this->results);
// echo "</pre>";
}
private function findMethodTypesVars($function_string = '') {
$method_types = [];
$method_types['POST'] = $this->getParams($function_string, "/->post\[/i", ['->post[', '\''], '');
$method_types['GET'] = $this->getParams($function_string, "/->get\[/i", ['->get[', '\''], '');
return $method_types;
}
private function isReturningJSONResponse($function_string = '') {
preg_match_all("/setOutput\(/i", $function_string, $matches);
return !empty($matches[0]) ? true : false;
}
private function getParams($function_string, $pattern, $find, $replace = '') {
preg_match_all($pattern, $function_string, $matches, PREG_OFFSET_CAPTURE);
$var_names = [];
foreach ($matches[0] as $key => $value) {
$strpos = strpos(substr($function_string, $value[1], strlen($function_string)), ']');
$var_name = str_replace($find, $replace, substr($function_string, $value[1], $strpos));
$var_names[] = $var_name;
}
return array_unique($var_names);
}
}
?>