织梦CMS - 轻松建站从此开始!

abg欧博官网|登陆|游戏|

php get namespace of included file

时间:2025-08-25 19:24来源: 作者:admin 点击: 0 次
I worked out the fairly laborious manual way to do this. Like discussed at top the process itself is simple: get your list of files for each file.

I worked out the fairly laborious manual way to do this.

Like discussed at top the process itself is simple:

get your list of files for each file. Now for each file:

create a random namespace id

trim file and replace the first start tag

add namespace id and start tag to file

write to temp file

import temp file

do any reflection required then cleanup

I've got an example with some zend here.... probably not the most efficient but it works.

<?php //first setup zend set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__)."/../zend/library/"); require_once 'Zend/Loader/Autoloader.php'; $loader = Zend_Loader_Autoloader::getInstance(); $loader->registerNamespace(dirname(__FILE__)."/../zend/library/"); //include my extender class class Zend_Reflection_File_WithNamespace extends Zend_Reflection_File { public function getFunctionsWithNamespace($namespace = '', $reflectionClass = 'Zend_Reflection_Function') { $functions = array(); foreach ($this->_functions as $function) { $newName = $namespace . "\\" . $function; $instance = new $reflectionClass($newName); if (!$instance instanceof Zend_Reflection_Function) { require_once 'Zend/Reflection/Exception.php'; throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Function'); } $functions[] = $instance; } return $functions; } } //find file(s) $startDir = 'hello/'; //$tempDir = 'php://temp/resource='; $tempDir = 'temp/'; $fileList = scandir($startDir); function ppPrintR($data) { echo "<pre>"; print_r($data); echo "</pre>"; } //Now loop through each file, first writing to temp, including and then testing foreach ($fileList as $key => &$fileItem) { if (is_file($startDir . $fileItem)) { //Take file and convert it $findDir = $startDir . $fileItem; echo $startDir . $fileItem; $inContents = file_get_contents($findDir); $randIden = 'm' . preg_replace('/\.|\s/', '', microtime()); //Replace the <?[php] at the start of the file with <? namespace xyz; $inContents = trim($inContents); $addString = 'namespace ' . $randIden . '; '; $longTagPos = strpos($inContents,'<?php'); $shortTagPos = strpos($inContents,'<?'); if ($longTagPos !== false && $longTagPos < 10) { $inContents = str_replace('<?php', '', $inContents); $addString = '<?php ' . $addString; } else if ($shortTagPage !== false && $longTagPos < 10) { $inContents = str_replace('<?', '', $inContents); $addString = '<? ' . $addString; } $outContents = $addString . $inContents; //Now write and require new temp file $tempItem = $tempDir . $fileItem; file_put_contents($tempItem, $outContents); require($tempItem); //Now do normal things $reflectedFile = new Zend_Reflection_File_WithNamespace($tempItem); echo 'Before<br/>'; $functions = $reflectedFile->getFunctionsWithNamespace($randIden); echo 'After<br/>'; //Now foreach function, read params and consider execution foreach($functions as &$functionItem) { echo $functionItem->name . "<br/>"; $functionParams = $functionItem->getParameters(); ppPrintR($functionParams); } //FIXME should clean here } } ?>

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-08-26 01:08 最后登录:2025-08-26 01:08
栏目列表
推荐内容