2013-09-05 2 views
-1

PHP에서 다른 PHP 소스 파일을 읽고 include, require 및 once functions에서 언급 된 파일 경로를 추출하는 스크립트가 필요합니다. 특히, 출력 패키지가 프로젝트에서 사용되는 라이브러리 폴더의 파일 만 포함 할 수 있도록 배치 프로젝트를 작성해야합니다.특정 PHP 소스 파일 집합의 포함 파일 목록 가져 오기

require, include 및 require_once 문에서 소스 파일을 읽고 경로를 추출 할 수있는 함수가 PHP에 있습니까? 이 같은

+0

시도한 코드와 잘못된 코드를 함께 적어주십시오. 코드를 묻는 질문은 해결 된 문제에 대해 최소한의 이해를 보여 주어야합니다. 시도한 해결책, 실패한 이유 및 예상되는 결과를 포함시킵니다. http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist – showdev

+0

아무 잘못도 없었습니다. 소스 파일을 파싱하고 경로를 추출하는 알고리즘이 필요합니다. –

+0

어떻게 든 [get_included_files] (http://www.php.net/manual/en/function.get-included-files.php)를 사용하십시오. –

답변

1

여기에 올바른 답변이 있습니다. 실제로 PHP 구문을 구문 분석합니다. 진정으로 원하는 것을 할 수는 없습니다. 예를 들어 자동 로더 또는 자동 추출 할 수없는 변수 포함 경로가있을 수 있습니다.

<?php 
$required_file = ''; 
$required_files = array(); 
$is_required_file = false; 

// Parse the PHP source using the Tokenizer library (PHP 4.3.0+) 
$tokens   = token_get_all($text); 

foreach ($tokens as $token) { 
    // Get the token name to make our code more readable. 
    $token_name = is_int($token[0]) ? token_name($token[0]) : $token[0]; 

    echo $token[0]. ': ' . htmlentities($token[1]) . '<br>'; 

    // Is this token a require/include keyword? 
    if (in_array($token_name, array('T_INCLUDE', 'T_INCLUDE_ONCE', 'T_REQUIRE', 'T_REQUIRE_ONCE'))) { 
     $is_required_file = true; 
    } 

    elseif ($is_required_file && $token[0] != ';' && $token[0] != '.' && $token_name != 'T_WHITESPACE') { 
     if ($token_name == 'T_STRING') { 
      $token[1] = '{{CONST:' . $token[1] . '}}'; 
     } 
     $required_file .= $token[1] ? trim($token[1], '\'"') : $token[0]; 
    } 

    elseif ($is_required_file && $token[0] == ';') { 
     $is_required_file = false; 
     $required_files[] = trim($required_file, '()'); 
     $required_file = ''; 
    } 
} 

$required_files은 파일의 배열입니다. 일치 require, require_once, includeinclude_once과 일치합니다. 다음과 같은 입력에 대한

는 :

<?php 
include APPLICATION_MODULES_ROOT . 'test1.php'; 
include($some_var . 'test55.php'); 
include('test2.php'); 
include ('test3.php'); 
include_once 'test4.php'; 
include_once('test5.php'); 
include_once ('test6.php'); 
require 'test7.php'; 
require('test8.php'); 
require ('test9.php'); 
//require ('test99.php'); 
require_once 'test10.php'; 
require_once('test11.php'); 
require_once ('test12.php'); 

이와 끝까지 :

Array 
(
    [0] => {{CONST:APPLICATION_MODULES_ROOT}}test1.php 
    [1] => test55.php 
    [2] => test2.php 
    [3] => test3.php 
    [4] => test4.php 
    [5] => test5.php 
    [6] => test6.php 
    [7] => test7.php 
    [8] => test8.php 
    [9] => test9.php 
    [10] => test10.php 
    [11] => test11.php 
    [12] => test12.php 
) 

은 당신이해야 할 모든 응용 프로그램이없는 str_replace 또는 유사한을 사용하여, 사용하는 상수를 교체합니다.

중요 사항 : 변수 포함 이름과 함께 작동하지 않습니다. 예 : include $some_path;. 당신은 그것들을 파싱 할 수 없다.

+0

경고 : token_name()은 매개 변수 1이 길어야합니다. 줄은 378 줄의 F : \ dev \ htdocs \ package.php에 있습니다. 줄 378은 다음을 참조합니다. $ token_name = token_name ($ 토큰 [0]); –

+0

@ asim-ishaq 잠시 후 내 코드를 업데이트하십시오. –

+0

나는이 알고리즘을 디버깅했습니다. $ token [0]에는 종종 숫자가 들어 있지만 항상 그런 것은 아닙니다. 잘못된 값 중 일부는 다음과 같습니다.,;)} {{숫자에 대해 $ token [0]을 확인한 다음 추가로 처리하도록 코드에 체크를 추가 할 수 있습니까? –

1

시도 뭔가 :

<?php 
$source = file_get_contents('source.php'); 
preg_match_all('/(include|include_once|require|require_once) *\(? *[\'"](.*?)[\'"] *\)? *;/', $source, $matches); 
$files = $matches[2]; 

$files 이제/포함 요구 된 파일의 배열입니다.

주석 처리 된 코드와 일치하므로 실제로는 적절한 해결책은 아닙니다.

+0

프로젝트에서이 솔루션을 사용해 보았습니다. require ("myfile.php")와 같은 구문을 위해 파일 이름을 추출합니다. 내 프로젝트에는 require (APPLICATION_MODULES_ROOT. "\\ pages \\ Screen.php")와 같은 코드가 있습니다. 하지만 내 코드는 읽지 않습니다. 다른 솔루션을 사용해 보도록하겠습니다. –

+0

@ asim-ishaq 알겠습니다. 내 다른 예제는 당신이 필요로하는 것에 대해서 작동하지 않을 것이다. –

관련 문제