在C#中快速解析PHP [英] Fast parsing of PHP in C#

查看:188
本文介绍了在C#中快速解析PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用C#解析PHP文件的要求.我们本质上要求另一个国家的一些开发人员上载PHP文件,一旦上载,我们需要检查php文件并获取所有方法和类/函数等的列表.

I've got a requirement for parsing PHP files in C#. We essentially require some of the devs in another country to upload PHP files and once uploaded we need to check the php files and get a list of all the methods and classes/functions etc.

我考虑过使用正则表达式,但是我无法锻炼某个函数是否属于类等,因此我想知道是否已经有外面的东西"可以解析PHP文件并吐出其功能(我我试图避免编写全面的AST实现).

I thought of using a regex but I can't workout if a function belongs to a class etc, so I was wondering if theres already something 'out there' that will parse out PHP files and spit out its functions (I'm trying to avoid writing a full blow AST implementation).

有人有什么主意吗?我查看了Coco/R,但找不到PHP语法文件.我正在使用.NET 2.0和C#.

Does anyone have any idea? I looked at Coco/R but I couldn't find a PHP grammar file. I'm using .NET 2.0 and C#.

推荐答案

为什么在C#中这样做?在PHP中,这很简单.使用 token_get_all() 函数,它将把PHP文件分解成词素流,您可以使用该词素通过编写有限状态机来确定类和方法的列表.

Why do this in C#? In PHP this is trivial to do. Use the token_get_all() function and it will break a PHP file into a stream of lexemes that you can use to definitively determine the list of classes and methods by writing a finite state machine.

无论您做什么,都不要尝试使用正则表达式执行此操作.这将非常繁琐且容易出错.

Whatever you do don't try and do this with regular expressions. It will be incredibly tedious and error-prone.

编辑:执行此操作有三种基本方法:

There are three basic possibilities for doing this:

  1. 使用PHP进行操作.这将是最快(开发)和最简单的选择;
  2. 运行命令行PHP脚本以执行此操作或生成一系列可由C#程序解释的令牌.这是下一个最简单的方法;
  3. 使用 Phalanger ,它是.Net框架的PHP端口.对于管理人员来说,这可能更可口,因为它仍然是所有.Net代码.或
  4. 使用 Quercus ,这是PHP到Java VM的端口.
  1. Do it in PHP. This will be the fastest (to develop) and simplest option;
  2. Run a command line PHP script to either do this or generate a series of tokens that can be interpreted by a C# program. This is the next easiest;
  3. Use Phalanger, a port of PHP to the .Net framework. This might be more palatable to management since it's still all .Net code; or
  4. Use Quercus, a port of PHP to the Java VM.

其他任何事情都将涉及编写PHP解析器(大量工作)或使用真正不稳定的正则表达式,这将是不可靠的支持噩梦.

Anything else will involve either writing a PHP parser (a lot of work) or using really flaky regular expressions that will be an unreliable support nightmare.

要担心PHP的所谓安全漏洞"有几个问题:

To be concerned about supposed "security flaws" of PHP has several problems:

  1. 任何框架或技术堆栈都可能存在安全漏洞.您的系统管理员只允许.Net在Java的抗议下有效地运作,这一事实仅表明存在非理性偏见.我说这是一个长期的Java开发人员:Java,.Net和PHP都可能存在安全漏洞;
  2. 您可以从命令行运行PHP,这样它就不会处理任何HTTP请求,从而将安全漏洞的问题基本上减少到了零;
  3. 如果您担心内部安全威胁(来自具有访问权限的人员),只需将PHP CLI可执行文件限制为只能由只有您的程序所在的组才能执行.

这篇关于在C#中快速解析PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆