2

I would like to create a lite and full version of the same PHP component. Of course I would like that both shared a common code base -- so if I find a bug, or add a feature, I need to update it only in one place.

In a compiled language the better approach is to have just a single project, with multiple targets –- one for each version. In that case I would use Preprocessor Macros to identify the product version in the code, thus enabling logical decisions based on the version.

With PHP what's the best way to solve this problem? Of course it is essential that the specific code of the full version does not appear in the PHP files of the lite version, otherwise a shrewd developer might unlock the features with ease.

Dev
  • 7,027
  • 6
  • 37
  • 65
  • Do you need to have a public code repository for the lite component? – Álvaro González Apr 07 '15 at 12:12
  • If you can cleanly separate your functionality into different files, e.g. with a plugin system or class inheritance, you can use any build system you want to produce the final "binary" which contains all or only a subset of files. – deceze Apr 07 '15 at 12:13
  • All you need is an automated way to create two "targets" from the full source as you described. My suggestion is to use [Grunt][1]. There are many packages for PHP, and I'm sure you can build what you want using them. For example: - PHP CodeSniffer (https://npmjs.org/package/grunt-phpcs) - PHP linting (https://npmjs.org/package/grunt-phplint) - PHPUnit (https://npmjs.org/package/grunt-phpunit) - PHP Analyzer (https://npmjs.org/package/grunt-php-analyzer) - PHP’s built-in webserver (https://npmjs.org/package/grunt-php) [1]: http://gruntjs.com – gbuzogany Apr 07 '15 at 12:14
  • @ÁlvaroG.Vicario: no, the code is hosted on my private revision control server – Dev Apr 07 '15 at 12:29
  • @deceze: which build system would you suggest for this case? – Dev Apr 07 '15 at 12:30
  • I usually just throw something together with `make` and some scripting, I didn't have a particular need for a great build system for PHP so far. – deceze Apr 07 '15 at 12:35

2 Answers2

2

The best way would be to use OOP.

For example, create a basic version of your class:

class BasicComponent
{
    ... add basic functionality here
}

create an extended version of your class:

class FancyComponent extends BasicComponent
{
    ... add fancy stuff here
}

Deploy either only the basic version, or the fancy version too.


There are a lot of other ways to use OOP for that purpose. I can think of a factory which could instantiate a lot of features, where each feature is a class and you deploy only a few of them with the basic version of your component.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Great, but it only solves part of the question. Once created subclasses for the full and lite versions, I have to create by hand a script that makes the deploy of one or the other? Uncomfortable and unwieldy, in my opinion. There is not a better (automated) solution? – Dev Apr 07 '15 at 12:25
  • 1
    I would put the base component into a separate git repo, and use composer or even git submodule to include the base version into the development tree of the fancy one. You'll then build each one on it's own and deploy them separately, where the fancy one depends on the basic one. – hek2mgl Apr 07 '15 at 12:47
0

On the basis of what I found in this similar question I finally solved this way:

1. I installed Phing, a PHP project build system or build tool based on ​Apache Ant.

2. I created a build.xml file in which I included this targets:

<target name="preprocess" depends="prepare">
    <foreach param="fname" absparam="abs-fname" target="preprocessfile">
        <fileset dir="${build.dir}">
            <patternset>
                <include name="**/*.php"/>
            </patternset>
        </fileset>
    </foreach>
</target>

<target name="preprocessfile">
    <exec command="gcc -E -x c -P -C -D PREMIUM ${abs-fname} -Wno-invalid-pp-token -o ${abs-fname}" 
        escape="false"
        checkreturn="true"
        passthru="true" />
</target>

In practice, I launch the GCC preprocessor on every PHP file and I take advantage it to do exactly what I need. It works very well!

Not only: through Phing I created some targets to make the PHP validation, the JavaScript minification, the creation of the archive to deploy, and more.

Community
  • 1
  • 1
Dev
  • 7,027
  • 6
  • 37
  • 65