preprocessing PHP从内置文件中删除功能 [英] Preprocessing PHP to remove functionality from built files

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

问题描述

我一直在阅读有关Phing和Ant,我不知道这些工具,如果不是,是此方案最有用的。

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.

这可以很容易地调试语句等,但我给你我们的文字scanario。

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

我们有一个可下载的PHP应用程序的免费和premium版本,而不是只包括一个变量隐藏的地方,然后做:

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';
}

显然有人可以再取源和改变变量,砰 - 他们已经偷了code。而像Ioncube等只是在我的托管公司的经验和支持,完全笨拙只是不够好。

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.

我想preFER东西..也许与此类似:

I would prefer something.. perhaps similar to this:

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

然后我会跑两个版本,一个设置premium真一假,这将产生的根本就是两个文件:

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';

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.

帮助将会是出色的,

更新

使用C preprocessor是伟大的,看起来像它我需要的一切。但是,我找不到怎么做以下三件事。

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 我需要删除生成到输出文件的意见。下面是这些的一个例子。

#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 我需要做递归遍历每个发现的文件。当我运行我目前的code我得到一个错误: ../目标/./文件夹/ test.php的:没有这样的文件或目录

#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

所以基本上我有我的'源'文件夹而我在,其中包含一个名为文件夹,并没有重新创建子文件夹,也不能在里面(test.php的)文件

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 我敢肯定,这个人是很容易 - 我怎样才能得到它来处理.js文件,可能.HTML只是为了安全起见呢?在一个电话,我的意思。我认为上运行它.JPG等等等等的文件是一个坏主意。

#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..

再次感谢!

推荐答案

这是pretty技术含量低,但当然中C preprocessor这不正是你想要的东西;只是一对夫妇的makefile一鼓作气以来调用它找到的grep -R ,你会得到一个简单,简易理解与语法的解决方案,你可能知道的。

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.

您可能有 GCC 任何* nix的主机上已安装。否则,这将是一个标准软件包。有些发行之外单独提供对 GCC (如Debian的软件包CPP)。

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).

该程序有一些简单的指令; 维基页面是一个良好的开端,和的手动比你需要更多的细节。基本上,它是调用它与 -E 选项每个文件只是做宏处理,然后复制输出一些构建目录的问题。

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.

您可以编写一个班轮脚本来做到这一点与发现,以及的行查找和LT的;凸出DIR&GT;型的F -name* .PHP-exec CPP -E -D&LT; FULL或限制&GT; {} -o&LT;建立目录&GT; / {} \\; 并引用宏全在你的PHP的限制,像

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/{} \;

然后 ../目标/ {} 应扩大到 ../目标/./的index.php

更新

新增 -P 来删除linemarkers(#1)。添加了一行复制目录结构(#2)。更改文件名匹配的HTML JS上运行(#3)。

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/{} \;

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

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