Download the PHP package constant-null/backstubber without Composer
On this page you can find all versions of the php package constant-null/backstubber. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download constant-null/backstubber
More information about constant-null/backstubber
Files in constant-null/backstubber
Package backstubber
Short Description Helps to generate full PHP files from tepmlates (stubs)
License MIT
Informations about the package backstubber
Meet the Backstubber, the PHP file generator
Backstubber makes generation of PHP code files from templates fast and easy.
Say no to tons of str_replace
.
installation
Backstubber can easily be installed using composer.
To do so, just run php composer.phar require-dev constant-null/backstubber
.
Or you can add following to your composer.json
file:
and then run:
Usage
FileGenerator
Basic substitution
Let's say we have a DummyStarship.stub template
Where $captain
supposed to be a string, $officers
an array and $crew
is numeric.
Well, just give required data to the backstubber using set()
method!
The first parameter in the set()
method is the string which needs to be replaced in template file,
while the second is variable needs to be inserted.
Backstubber will insert values according to they types, so in output we'll have something like that:
But sometimes we need to insert the variable as it is, in case of Class or Namespace name for example.
For this purpose Backstubber has the setRaw()
method.
This method has the same signature, but recieve only strings. Lets update previous example:
So in result file EnterpriseClass.php
will contain the following:
Using prefixes
If you using the same prefix for all your placeholders (just like "Dummy" prefix in example above), you can use withPrefix
method.
So the following code and the one shown above, will have the same results.
Templates with delimiters
Using the basic text substitution with or without prefixes like "Dummy" as in the example above is good, but sometimes you might want to specify parts to be replaced more explicitly. And this is where delimiters shows up!
Let's use the Laravel Blade style delimiters "{{" and "}}" for example, then our template file will looks like this:
And now we just tell the backstubber to use our delimiters
And this is it!
Multi-line arrays
Starting from version 0.3.0 the backstubber has support of multi-line array formatting.
With default settings associative arrays will be formatted using multi-line format and non-associative arrays using single-line format (just like in the examples shown above), so the following:
will result in:
Default behaviour can be changed using ConstantNull\Backstubber\Utility\Formatter::setArrayMode
.
For now, method accepts one of three parameters, which is:
Formatter::ARR_MODE_AUTO
for default behaviourFormatter::ARR_MODE_MULTILINE
force backstabber to format all arrays in multi-line formatFormatter::ARR_MODE_SINGLELINE
force backstabber to format all arrays in single-line format
Authors
This library was developed by me, Mark Belotskiy. Special thanks (as promised) to my friend [Dmitriy Shulgin]() for helping with the name of the library.