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!
You could just build such preprocessor by yourself - PHP has built-in support for parsing PHP files: http://www.php.net/manual/en/function.token-get-all.php
I have used it to built an obfuscator and must say it is very easy to use. Just print the tokens you wish and remove the tokens that are within "## if premium ##" blocks.
UPDATE: I guess C preprocessor idea is better. Leaving this answer for completeness though. :)
Disclaimer: I am totally sure this is not the most efficient way to do this!
--
I do this in 4 parts:
source
directory structure intarget
source
intotarget
source
totarget
target
--
--
As I said, I'm sure this isn't hugely inefficient but that there is one .sh file that I execute and bang - I have my builds!
Well, Phing is the Ant for PHP. I've never used Phing, but I assume that it's tuned for stuff like this.
Use Phing to create two separate packages: One premium app and one for the free app.
You could use Ant too, but Ant is tuned for Java while Phing is tuned for PHP. The only reason you might want to use Ant over Phing is that there are much more resources available to Ant than Phing. But, if you're a PHP shop, learn to use Phing if for nothing else than it looks nice on a resume. ("Yes, I'm a senior PHP developer. I even know Phing").
I have come to an idea, the answer before this is accepted but I want to share it anyways. So here is the deal:
Store all the payed features in seperate files. With a hook class, you can check and add the features where you want. Do not include the payed features on free version of the build. You will have 2 zip files but one source.
Here is very simple Hook class that might help you:
Then you can check them in anywhere on your code. Example:
I know it's very simple and need to develop in many other ways. But If you can manage it:
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
orgrep -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 togcc
(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, likeUPDATE To make the paths work nicely, try this out:
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).Sorry for digging up this topic, but I had the same need and the CPP approach had too many side-effects for my use.
So I developed a basic pre-processor filter for phing that does the trick:
The filter is available on github: https://github.com/tmuguet/PreProcessorFilter