PHPackages                             emsifa/power-stub - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. emsifa/power-stub

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

emsifa/power-stub
=================

Stub engine that support control statements and looping without breaking your indentations.

01PHP

Since May 14Pushed 7y ago1 watchersCompare

[ Source](https://github.com/emsifa/power-stub)[ Packagist](https://packagist.org/packages/emsifa/power-stub)[ RSS](/packages/emsifa-power-stub/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

POWER STUB
==========

[](#power-stub)

[![Build Status](https://camo.githubusercontent.com/f921c657eac40e5e064b863f6b287df002aa8e6a66cf1e94c11f6304df35bb8e/68747470733a2f2f7472617669732d63692e6f72672f656d736966612f706f7765722d737475622e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/emsifa/power-stub)[![License: MIT](https://camo.githubusercontent.com/08cef40a9105b6526ca22088bc514fbfdbc9aac1ddbf8d4e6c750e3a88a44dca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d626c75652e737667)](https://opensource.org/licenses/MIT)

Power Stub is a *stub engine* that support control statements, looping and several other features that make it easier for you to generate code from stub file.

Stub file is a file that contains raw code with several parameters that will be replaced with certain text.

We use term *stub engine* because Power Stub not only able to replace parameter with given text, but also support control statement (`if-elseif-else-endif`), looping (`foreach-endforeach`, `while-endwhile`), etc. It is like *template engine* who also keep your indentation in the right place, so your generated code will stay neat.

Understanding The Problem
-------------------------

[](#understanding-the-problem)

In common template engine such as Blade, Twig, etc. They will render your view file into PHP code below:

```

        lorem ipsum

```

When we render that file with `$a = [1,2,3]`, the result will be messy like this:

```

        lorem ipsum 1

        lorem ipsum 2

        lorem ipsum 3

```

It is OK, because template engine results are intended for browsers who don't care about indentation.

But this is not acceptable if we want to make code generator.

So with Power Stub, our stub file would looks like this:

```

    |# foreach($a as $b) #|

        lorem ipsum [# $b #]

    |# endforeach #|

```

When we render that stub with `$a = [1,2,3]`, the result would looks like this:

```

        lorem ipsum 1

        lorem ipsum 2

        lorem ipsum 3

```

Now everybody happy :D

Other Features
--------------

[](#other-features)

#### Include

[](#include)

We also handle include function to keep indentation relative to the definition. For example you have 2 stub files like below:

`main.js.stub`

```
import something from 'something';

[# include("timeout.js", ['message' => 'first', 'delay' => 1000]) #]

something.on('event', () => {
    something.asyncStuff(() => {
        // timeout 2 seconds
        [# include("timeout.js", ['message' => 'second', 'delay' => 2000]) #]
    });
});

```

`timeout.js.stub`

```
setTimeout(() => {
    console.log("[# $message #]");
}, [# $delay #]);

```

If we render `main.js.stub`, the result would looks like this:

```
import something from 'something';

setTimeout(() => {
    console.log("first");
}, 1000);

something.on('event', () => {
    something.asyncStuff(() => {
        // timeout 2 seconds
        setTimeout(() => {
            console.log("second");
        }, 2000);
    });
});
```

GETTING STARTED
---------------

[](#getting-started)

> Power Stub still experimental. We have done some simple tests. You can use it, but there will may be some breaking changes.

#### Requirements

[](#requirements)

- PHP &gt;= 7.1

#### Installation

[](#installation)

Make directory where you want to place your code.

Go to that directory using your cmd/terminal, and run composer command below:

```
composer require emsifa/power-stub:dev-master

```

#### Preparation

[](#preparation)

Before we start using Power Stub, we need to make 2 directories.

You can use commands below:

- `mkdir stubs`
- `mkdir compiled`

#### Render

[](#render)

First let's make our first stub. Create file `app.js.stub` inside our `stubs` directory.

In this example we will create an express app stub containing dynamic routes.

```
const express = require('express');
const app = express();

|# foreach($routes as $r) #|
app.[# strtolower($r['method']) #]('[# $r['path'] #]', function (req, res) {
    return res.send('OK');
});
|# endforeach #|

app.listen(3000);

```

Now let's create PHP script to render that stub. Create file named `render.php`.

```
