좋아, 이제 파일을 통해 한 줄씩 읽습니다. 파일의 각 함수 이름은 XML 문서의 다른 곳에서 정의되어 있으므로이 함수 이름을 알고 있습니다. 이것이 무엇이되어야 하는가 :파일에서 전체 기능을 얻는 방법
function function_name
여기서 function_name은 함수의 이름입니다.
나는 모든 함수 정의를 이미 함수 이름의 배열에 넣은 XML 문서에서 얻었으며 PHP 함수에서 그 함수를 가져와야합니다. 그리고 php 파일을 재 구축하여 그 안에있는 함수 만 포함하도록하십시오. 즉, php 파일에 XML 태그에 정의 된 것보다 많은 함수가있는 경우 해당 함수를 제거하고 사용자가 XML 파일에 지정한 함수 만 사용하여 .php 파일을 다시 작성해야합니다.
필자가 직면 한 딜레마는 함수를 라인 단위로 읽는 방법을 결정하는 방법이며 함수가 함수를 가질 수 있다는 것을 알고 있습니다. 그래서 나는 그 안에있는 기능을 제거하고 싶지 않습니다. 독립형이며 함께 제공되는 XML 파일 내에서 정의되지 않은 기능들. 이 작업을 수행하는 방법에 대한 아이디어가 있습니까 ??
좋아, 이제 다음과 같은 기능을 사용하고 있습니다 :
//!!! - Used to grab the contents of all functions within a file with the functions array.
function get_functions($source, $functions = array())
{
global $txt;
if (!file_exists($source) || !is_readable($source))
return '';
$tokens = token_get_all(file_get_contents($source));
foreach($functions as $funcName)
{
for($i=0,$z=count($tokens); $i<$z; $i++)
{
if (is_array($tokens[$i]) && $tokens[$i][0] == T_FUNCTION && is_array($tokens[$i+1]) && $tokens[$i+1][0] == T_WHITESPACE && is_array($tokens[$i+2]) && $tokens[$i+2][1] == $funcName)
break;
$accumulator = array();
// collect tokens from function head through opening brace
while($tokens[$i] != '{' && ($i < $z)) {
$accumulator[] = is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
$i++;
}
if($i == $z) {
// handle error
fatal_error($txt['error_occurred'], false);
} else {
// note, accumulate, and position index past brace
$braceDepth = 1;
$accumulator[] = '{';
$i++;
}
while($braceDepth > 0 && ($i < $z)) {
if(is_array($tokens[$i]))
$accumulator[] = $tokens[$i][1];
else {
$accumulator[] = $tokens[i];
if($tokens[$i] == '{') $braceDepth++;
else if($tokens[i] == '}') $braceDepth--;
}
$i++;
}
$functionSrc = implode(null,$accumulator);
}
}
return $functionSrc;
}
OK, 그것은이 PHP 파일 내용 소요 있도록 :
<?php
function module_testing($params)
{
// Is it installed?
$test_param = !isset($params['test_param']) ? 'Testing Testing 1 2 3!' : $params['test_param'];
// Grab the params, if they exist.
if (is_array($params))
{
echo $test_param;
}
// Throw an error.
else
module_error();
}
?>
을 등처럼 변경 :
<?php
function module_testing($params)
{
// Is it installed?
$test_param isset$params'test_param' 'Testing Testing 1 2 3!' $params'test_param'
// Grab the params, if they exist.
if is_array$params
echo $test_param
// Throw an error.
else
module_error
?>
당신이 볼 수 있듯이, 여기에는 많은 것들이있었습니다. 마지막 닫기 괄호가 누락되었습니다. 여기에 함수가 있는지 확인하고 함수 전체를 잡고 같은 파일에 씁니다. 간단한데,하지만 와우, 당신은 아마도 PHP 토큰 화를 시도하려는
Or I could also check if a function is defined in here that isn't within the $functions array, if so, than just remove that function. Perhaps it's easier with this approach instead??
얘들 아, 나는 이것을 스스로 해결했다. 아래 내 대답을 참조하십시오. 다시 고마워요 : – SoLoGHoST