A preprocessor for a PHP script?

Discussion in 'PHP' started by TwistMyArm, Oct 20, 2006.

  1. #1
    Hi all,

    So this may seem like a bizarre request, but here goes. What I'm after is a script that will parse my PHP project and basically strip away certain parts depending on values I've set. If you're a C coder, I essentially want a way to #include files along with doing things like #ifdef / #else and so on.

    Please note: I'm not talking about including files at runtime like the normal PHP include. Imagine I have 2 files, neither include the other in the 'classic' PHP sense of the word. I want to be able to have a '#include otherfile.php' command in one file so that if I run another script over that file, it will substitute that line with the source code of 'otherfile.php' and output a single file: the original with 'otherfile.php' 'embedded' in it.

    Bizarre request, I know. On top of that, as I mentioned earlier, I want to be able to #define, #ifdef and so on.

    At the same time I need the scripts to run as expected in their original state.

    Basically, the reason I want to do this is because I have a library of code that provides certain features only when the end user has licenced it. That is to say, the developer codes as if the feature exists but it will only actually do anything if has been licenced. At the moment, I 'hand build' a single include file that provides a skeleton implementation of the functionality for the developer to use and distribute. What I'm hoping to do is remove this 'hand building' by adding markup to my original code and have a script build this skeleton file for me.

    So, that being said, does anyone have any ideas of what to use or at least any suggestions of code that I might be able to use to build this functionality?

    BTW, I appreciate that the scenario might seem stupid, but I'm hoping that enough people can see through my terrible explanation of the situation to understand it!

    Thanks...
     
    TwistMyArm, Oct 20, 2006 IP
  2. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not at all familiar with C so I don't know what #ifdef / #else etc do but including files and the like could probably be handled with a simple php script using preg_replace or something similar. You could also look at templating engines, smarty, etc. PHP is afterall just plain text.
     
    streety, Oct 20, 2006 IP
  3. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That's true regarding the includes and preg_replace, but the problem comes about with the #ifdefs.

    For those who are interested, from a really basic viewpoint, #ifdef / #else and so on basically operate like normal if / else type things, but before it actually gets compiled.

    For example (in C-like code):
    
    x = 1;
    if (x == 1) {
        // do this if x equals 1
    } else {
        // otherwise do this
    }
    
    Code (markup):
    will give you an if / else in the source code that gets sent to the compiler.

    On the other hand, with something like:
    
    #define SOMETHING
    #ifdef SOMETHING
    // do this if SOMETHING is defined
    #else
    // otherwise do this
    #endif
    
    Code (markup):
    the stuff between the else and endif is stripped out entirely before the compiler sees it. It's 'precompiled' out of the source code altogether.


    So although the preg_replace makes sense for handling includes, the problem then comes when I want to #define something in the included file then reference that value in the including file, for example.

    At the moment I'm thinking of basically reading the code into an array then replacing bits and pieces of the array as I walk through it.

    I appreciate the reply, streety! I was actually half-expecting people to maybe give a little yelp then back slowly away from the thread... :)
     
    TwistMyArm, Oct 20, 2006 IP
  4. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #4
    noppid, Oct 20, 2006 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi noppid: I do know about function_exists but unfortunately can't see how this would help in this case. Any chance of giving my head a shake and explaining it to me?

    Thanks!
     
    TwistMyArm, Oct 20, 2006 IP
  6. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for the explanation on the various aspects of what you wish to achieve. I think you should be able to do all that with a templating engine. Smarty can certainly do if / else constructs and define is essentially the most basic function of a templating engine.

    I vaguely remember an article that may be of use to you. Strong emphasis on the 'vaguely' and 'may' however, I don't think I ever read the article as I didn't think it would be of much value to me. I'll have a little look around and see what I can find. Don't hold your breath though.
     
    streety, Oct 20, 2006 IP
  7. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hmmm... that's a really good point streety. To be honest, I never even thought of using a template engine to do this.

    I might have to hack the Smarty code a little to let me use my own tags, but I think I have something to investigate this weekend!

    Thanks. But please, everyone else, feel free to suggest other, non-template engine type solutions :)
     
    TwistMyArm, Oct 20, 2006 IP