Skip to content

Instantly share code, notes, and snippets.

@cklosowski
Last active December 16, 2015 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cklosowski/5474208 to your computer and use it in GitHub Desktop.
Save cklosowski/5474208 to your computer and use it in GitHub Desktop.
Tokenization file for PHP, just remove the functions: ck_generator_is_constant ck_generator_dump ck_generator_strip Generates both constants and functions. Put this somewhere in the web directory of your codebase (preferably 1 folder above the codebase) and provide the absolute path to the code as a query string parameter of 'path': (IE: http://…
<?php
$path = NULL;
if ( isset( $_GET['path'] ) ) {
$path = $_GET['path'];
}
// File types to search in
$extensions = array(
'php',
'inc',
'module',
);
// Get the docroot
$root = $path;
// Scan through each file in the project and build an array containing the functions and parameters.
$di = new RecursiveDirectoryIterator($root);
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
if (in_array(end(explode('.', $filename)), $extensions)) {
// Open the file
$contents = file_get_contents($filename);
// Retrieve functions from file.
preg_match_all('/function\s(.+?)\((?:(.*?))?\)\s{$/im', $contents, $matches);
foreach ($matches[1] as $key => $function_name) {
//{ "trigger": "actions_do", "contents": "actions_do(${1:action_ids}, ${2:&object}, ${3:context = NULL}, ${4:a1 = NULL}, ${5:a2 = NULL})" },
$paramstring = str_replace(', ', ',', trim($matches[2][$key]));
$params = explode(',', str_replace('"', '\"', $paramstring));
$i = 1;
$function_params = array();
// var_dump($params);
if ($params[0] != '') {
foreach ($params as $param) {
$function_params[] = '${' . $i . ':' . str_replace('$', '\\\$', $param) . '}';
$i++;
}
}
$lines[$function_name] = ' { "trigger": "' . $function_name . '", "contents": "' . $function_name . '(' . implode(', ', $function_params) . ')" }';
}
}
}
// Create the string containing all the $lines in the right format.
$content = '{
"scope": "source.php - variable.other.php",
"completions":
[
"php",
' . "\n";
$content .= implode(",\n", $lines);
$content .= "
]
}";
echo $content;
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
if (in_array(end(explode('.', $filename)), $extensions)) {
// Open the file
$contents = file_get_contents($filename);
$defines = array();
$state = 0;
$key = '';
$value = '';
$tokens = token_get_all($contents);
$token = reset($tokens);
while ($token) {
// dump($state, $token);
if (is_array($token)) {
if ($token[0] == T_WHITESPACE || $token[0] == T_COMMENT || $token[0] == T_DOC_COMMENT) {
// do nothing
} else if ($token[0] == T_STRING && strtolower($token[1]) == 'define') {
$state = 1;
} else if ($state == 2 && ck_generator_is_constant($token[0])) {
$key = $token[1];
$state = 3;
} else if ($state == 4 && ck_generator_is_constant($token[0])) {
$value = $token[1];
$state = 5;
}
} else {
$symbol = trim($token);
if ($symbol == '(' && $state == 1) {
$state = 2;
} else if ($symbol == ',' && $state == 3) {
$state = 4;
} else if ($symbol == ')' && $state == 5) {
$defines[ck_generator_strip($key)] = ck_generator_strip($value);
$state = 0;
}
}
$token = next($tokens);
}
foreach ($defines as $k => $v) {
$lines2[$k] = ' { "trigger": "' . $k . '", "contents": "' . $k . '" }';
//echo "'$k' => '$v'\n";
}
// Retrieve functions from file.
/*
preg_match_all("^define\(\s*(['\"])[\w.∞]+\1\s*,\s*([\w/]+(\s*\.\s*[\w/]+)*\s*\);$", $contents, $matches);
foreach ($matches[1] as $key => $function_name) {
//{ "trigger": "actions_do", "contents": "actions_do(${1:action_ids}, ${2:&object}, ${3:context = NULL}, ${4:a1 = NULL}, ${5:a2 = NULL})" },
$lines[$function_name] = ' { "trigger": "' . $function_name . '", "contents": "' . $function_name . '" }';
}*/
}
}
echo "\n\n";
// Create the string containing all the $lines in the right format.
$content2 = '{
"scope": "source.php - variable.other.php",
"completions":
[
"php",
' . "\n";
$content2 .= implode(",\n", $lines2);
$content2 .= "
]
}";
echo $content2;
function ck_generator_is_constant($token) {
return $token == T_CONSTANT_ENCAPSED_STRING || $token == T_STRING ||
$token == T_LNUMBER || $token == T_DNUMBER;
}
function ck_generator_dump($state, $token) {
if (is_array($token)) {
echo "$state: " . token_name($token[0]) . " [$token[1]] on line $token[2]\n";
} else {
echo "$state: Symbol '$token'\n";
}
}
function ck_generator_strip($value) {
return preg_replace('!^([\'"])(.*)\1$!', '$2', $value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment