WMarkDown/PHP/ScriptsAnalyzer/WMarkDown.ScriptsAnalyzer.JS.php

143 lines
6.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace WMarkDown\ScriptsAnalyzer;
class JS extends \WMarkDown\Abstracts\ScriptsAnalyzer{
private static $language_name = [
"js" => "JavaScript",
"ecma" => "ECMAScript"
];
private static function get_parameters($block){
$parameters = [];
foreach(explode(",", $block) as $parameter)
($parameter = trim($parameter)) && ($parameters[] = ["name" => $parameter]);
return $parameters;
}
public function analyze($file, $path = null, &$languages = [], $level = 0){
if(!($content = \WMarkDown\ScriptsAnalyzer::get_content($file, $path)))
return;
$security = [0];
$class_name = "";
$construct_parameters = [];
$last_method = null;
$class = $this->class;
$data = \WMarkDown::string_variables(file_exists($wmd_path = $this->parent->get_wmd_path($file, $path)) ? file_get_contents($wmd_path) : $this->original_wmd, array_merge([
"subkey_words" => preg_match('/^.+' . $this->common["project"] . '\.([^\/]+)\.(ecma\.)?js\.w\.md$/', $wmd_path) ? preg_replace_callback('/^.+' . $this->common["project"] . '\.([^\/]+)\.(ecma\.)?js\.w\.md$/', function($values){
return substr(preg_replace('/[,\.]+/', ",", preg_replace_callback('/([A-Z]+)?([A-Z][a-z0-9]+)/', function($subvalues){
return ($subvalues[1] ? "," . $subvalues[1] : "") . "," . strtolower($subvalues[2]);
}, $values[1])), 1);
}, $wmd_path) : "main",
"path" => substr($wmd_path, $this->root_l + 8),
"subpath" => substr($wmd_path, $this->root_l + 8, -5),
"file" => preg_replace('/^.+\/([^\/]+)\.w\.md$/', "$1", $wmd_path)
], $this->common));
$language = strpos($wmd_path, ".ecma.") !== false ? "ecma" : "js";
$private_methods = [];
$methods = [];
!isset($languages[$language]) && ($languages[$language] = []);
$languages[$language][$j = count($languages[$language])] = ["path" => $wmd_path, "level" => $level, "methods" => []];
// print_r([$file]);
$content = preg_replace_callback('/("(([^\\\\"]+|\\\\.)*)"|\'(([^\\\\\']+|\\\\.)*)\'|`(([^\\\\`]+|\\\\.)*)`)|([\r\n]|[\s\t]|\/\/[^\r\n]*|\/\*(([\r\n]+|[^\*]+|\*[^\/])*)\*\/)+/', function($values) use($file){
$file == "AnP.Components.Tables.ecma.js" && print_r($values);
return count($values) == 1 || $values[1] ? $values[0] : " ";
}, $content);
while((false || $security[0] ++ < 200) && preg_match('/^([a-zA-Z0-9_\.]+)\s*=\s*function\s*\(([^\(\)]*)\)|\b(this\.|(const|let|var)\s+)([a-zA-Z0-9_]+)\s*=\s*((\(([^\(\)]*)\)|[a-zA-Z0-9_]+)\s*=>|function\(([^\(\)]*)\))/', $content, $matches, PREG_OFFSET_CAPTURE)){
$content = substr($content, $matches[0][1] + strlen($matches[0][0]));
if($matches[1][0]){
$class_name = $matches[1][0];
$construct_parameters = self::get_parameters($matches[2][0]);
}else{
$l = count($matches);
$security[1] = 0;
$i = 0;
$method_access = $matches[4][0] ? "private" : "public";
$method_name = $class_name . "." . $matches[5][0];
$parameters = self::get_parameters($matches[$l > 8 ? 8 : ($l > 7 ? 7 : 6)][0]);
$method_content = "";
$return = null;
$see = [];
$matches[4][0] && ($private_methods[] = $matches[5][0]);
while((false || $security[1] ++ < 100) && preg_match('/("(([^\\\\"]+|\\\\.)*)"|\'(([^\\\\\']+|\\\\.)*)\'|`(([^\\\\`]+|\\\\.)*)`|\/[^\s]([^\\\\\n\r\/]+|\\\\[^\r\n])*\/[igm]{0,3})|[\[\]\{\}\(\),;]/', $content, $submatches, PREG_OFFSET_CAPTURE)){
$ended = false;
switch($submatches[0][0]){
case "(":
case "[":
case "{":
$i ++;
break;
case ")":
case "]":
case "}":
$ended = -- $i < 1;
break;
case ",":
case ";":
$ended = !$i;
break;
};
if($i >= 0){
$method_content .= substr($content, 0, $submatches[0][1]) . $submatches[0][0];
$content = substr($content, $submatches[0][1] + strlen($submatches[0][0]));
};
if($ended)
break;
};
$return = trim($method_content)[0] != "{" ? true : preg_match('/\breturn [^;]+[ ;]*[\};]$/', $method_content);
$methods[] = $method_name;
// Implementamos los métodos dependientes.
preg_replace_callback('/\b((this|' . $this->object . ')\.)?([a-zA-Z0-9_]+)\(/', function($values) use(&$see, $class, $class_name, $methods){
$method = (!$values[2] || $values[2] == "this" ? $class_name : $class) . "." . $values[3];
($values[2] || in_array($method, $methods)) && !in_array($method, $see) && ($see[] = $method);
}, $method_content);
// Métodos privados dependientes.
foreach($private_methods as $method)
preg_match('/\b' . $method . '\b/', $method_content) && !in_array($method = $class_name . "." . $method, $see) && ($see[] = $method);
$languages[$language][$j]["methods"][] = $method_name;
$data = $this->parent->add_method($data, $last_method, [
"access" => $method_access,
"language_script" => self::$language_name[$language],
"name" => $method_name,
"type" => "object",
"see" => $see,
"parameters" => $parameters,
"return" => $return,
"hash" => md5($method_content)
]);
$last_method = $method_name;
};
};
\WMarkDown::save_file($wmd_path, $data);
}
};