预处理 PHP 以从构建的文件中删除功能 [英] Preprocessing PHP to remove functionality from built files

查看:27
本文介绍了预处理 PHP 以从构建的文件中删除功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关 Phing 和 Ant 的文章,但我不确定这些工具中的哪一个(如果有)最适合这种情况.

它很容易是调试语句等,但我会给你我们的文字扫描.

我们有一个可下载的 PHP 应用程序的免费和高级版本,而不是只包含一个隐藏在某处的变量然后执行:

if($premium == true) {echo '一些附加功能';} 别的 {echo '基本功能';}

显然有人可以获取源代码并更改该变量,然后砰的一声 - 他们窃取了我们的代码.根据我的经验,像 Ioncube 等之类的东西完全笨拙,对托管公司的支持还不够好.

我更喜欢一些东西..也许与此类似:

## 如果溢价 ##echo '一些附加功能';## 别的 ##echo '基本功能';## 万一 ##

然后我会运行两个构建,一个将 premium 设置为 true,一个设置为 false,这将生成两个简单的文件:

echo '一些附加功能';

echo '基本功能';

能够仅包含基于传递给构建应用程序的相同条件的整个文件也非常有帮助.

我找不到办法做到这一点,但如果可能,我愿意接受任何其他想法.

帮助将非常出色,

更新

使用 C 预处理器很棒,看起来它可以完成我需要的一切.但是,我找不到如何做以下 3 件事.

#1 我需要删除生成到输出文件中的注释.以下是其中的一个示例.

#1 "./index.php"# 1 "<内置>"# 1 "<命令行>"# 1 "./index.php"

我在您链接到的手册页中没有找到如何执行此操作的示例.

#2 我需要让它递归遍历每个发现的文件.当我运行当前代码时,出现错误:../target/./folder/test.php: No such file or directory

所以基本上我有我所在的源"文件夹,其中包含一个名为文件夹"的子文件夹,它不会重新创建它,也不会重新创建其中的文件(test.php)

#3 我相信这很容易 - 我怎样才能让它处理 .js 文件和 .html 只是为了安全?在一个电话中,我的意思是.我认为在 .jpg 等文件上运行它是一个坏主意..

再次感谢!

解决方案

它的技术含量很低,但当然有 C 预处理器可以完全满足您的需求;只需敲入几个 makefile 以使用 findgrep -R 调用它,您就会得到一个简单、易于理解的解决方案,其中包含您可能知道的语法.

更多细节

您可能已经在任何 *nix 主机上安装了 gcc.否则,它将是一个标准包.一些发行版将它单独提供给 gcc(如 Debian 的 cpp 包).

程序有一些简单的指令;维基页面是一个好的开始,手册 比您需要的更多.基本上,只需使用 -E 选项在每个文件上调用它就可以进行宏处理,然后将输出复制到某个构建目录.

您可以编写一个单行脚本来使用 find 来做到这一点,沿着 find <proj dir>-type f -name '*.php' -exec cpp -E -D {} -o <build dir>/{} \; 并在您的 PHP 中引用宏 FULL 和 RESTRICTED,例如

#ifdef FULL<付费客户线路>#万一

更新要使路径正常工作,请尝试以下操作:

#!/bin/bashcd/.../phing/source/找 .-type f -name '*.php' -exec cpp -E -D FULL {} -o ../target/{} \;

然后 ../target/{} 应该扩展为 ../target/./index.php.

更新

添加了 -P 以删除线标记 (#1).添加了一行来复制目录结构 (#2).更改文件名匹配以在 html 上的 js 上运行(#3).

#!/bin/bashcd/.../phing/source/找 .-type d -exec mkdir -p ../target/{} \;找 .-type f -regex '.*\.(php|html|js)' -exec cpp -E -P -D FULL {} -o ../target/{} \;

I've been reading about Phing and Ant and I'm not sure which, if either, of these tools are most useful for this scenario.

It could easily be debug statements etc, but I'll give you our literal scanario.

We have a free and premium version of a downloadable PHP app, and rather than including just a variable hidden somewhere and then doing:

if($premium == true) {
   echo 'some additional functionality';
} else {
    echo 'basic functionality';
}

Obviously someone could then take the source and change that variable, and bang - they've stolen our code. And something like Ioncube etc is just totally unwieldy in my experience and support on hosting companies is just not good enough.

I would prefer something.. perhaps similar to this:

## if premium ##
echo 'some additional functionality';
## else ##
echo 'basic functionality';
## endif ##

And then I would run two builds, one setting the premium to true and one to false, which would generate two files of simply:

echo 'some additional functionality';

and

echo 'basic functionality';

It would also be very helpful to be able to only include entire files based on this same condition passed to the build app.

I can't find a way to do this but I am open to any alternative ideas if possible.

Help would be outstanding,

UPDATE

Using the C preprocessor is great and looks like it does everything I need. However, I can't find how to do the following 3 things.

#1 I need to remove the comments generated into the output files. Below is an example of those.

# 1 "./index.php"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "./index.php"

I haven't found an example of how to do this in the manual page you linked me to.

#2 I need to make it recursively go through every discovered file. When I run my current code I get an error: ../target/./folder/test.php: No such file or directory

So basically I have my 'source' folder which I'm in, which contains a subfolder called 'folder' and it doesn't recreate that, nor the files inside it (test.php)

#3 I'm sure this one is easy - how can I get it to process .js files and probably .html just to be safe as well? In one call, I mean. I assume running it on .jpg etc etc files is a bad idea..

Thanks again!

解决方案

It's pretty low-tech, but there is of course the C preprocessor which does exactly what you want; just bang in a couple of makefiles to call it with find or grep -R and you get a simple, easy-to-understand solution with syntax you probably know.

More detail

You probably have gcc installed already on any *nix host. Otherwise, it'll be a standard package. Some distributions provide it separately to to gcc (like Debian's cpp package).

The program has some simple instructions; the wiki page is a good start, and the manual has more detail than you need. Basically, it's a matter of calling it on each file with the -E option just to do the macro processing, and then copying the output some build directory.

You can write a one-liner script to do that with find, along of the lines of find <proj dir> -type f -name '*.php' -exec cpp -E -D <FULL or RESTRICTED> {} -o <build dir>/{} \; and reference the macros FULL and RESTRICTED in your PHP, like

#ifdef FULL
    <lines for paying customers>
#endif

UPDATE To make the paths work nicely, try this out:

#!/bin/bash
cd /.../phing/source/
find . -type f -name '*.php' -exec cpp -E -D FULL {} -o ../target/{} \;

Then ../target/{} should be expanded to ../target/./index.php.

UPDATE

Added -P to remove the linemarkers (#1). Added a line to copy directory structure (#2). Changed filename match to run on js on html (#3).

#!/bin/bash
cd /.../phing/source/
find . -type d -exec mkdir -p ../target/{} \;
find . -type f -regex '.*\.(php|html|js)' -exec cpp -E -P -D FULL {} -o ../target/{} \;

这篇关于预处理 PHP 以从构建的文件中删除功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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