Skip to content

Commit

Permalink
Add php template engine
Browse files Browse the repository at this point in the history
  • Loading branch information
tejerka committed Dec 24, 2019
1 parent 621f6da commit 395ea53
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Expand Up @@ -5,6 +5,8 @@ COPY ./app /usr/src/app
RUN apk add --no-cache --virtual .gyp \
python \
make \
php7 \
php7-json \
g++

RUN cd /usr/src/app && \
Expand Down
45 changes: 45 additions & 0 deletions app/templating/PhpEngine.js
@@ -0,0 +1,45 @@
const BaseEngine = require('./BaseEngine');
const { spawnSync } = require('child_process');
const path = require('path');

const type = 'php';

module.exports = class PhpEngine extends BaseEngine {
static get type() {
return type;
}

getType() {
return type;
}

render({bodyFilePath, body, endpoint, request, context }) {
const mockservr = JSON.stringify({
request: {
url: request.url,
path: request.path,
method: request.method,
headers: request.headers,
query: request.query
},
endpoint,
context
});
const bodyFilePathDirname = path.dirname(bodyFilePath);
const absoluteBodyFilePath = path.resolve(bodyFilePath);
const phpRoot = `<?php
$_MOCKSERVR = json_decode(<<<JSON
${mockservr}
JSON, 'true');
include('${absoluteBodyFilePath}');
`;
const phpProcess = spawnSync('php', [], {
input: phpRoot,
stdio: 'pipe',
encoding: 'utf-8'
});

console.log(phpProcess.stderr);
return phpProcess.stdout;
}
};
2 changes: 2 additions & 0 deletions app/templating/TemplateEnginesStack.js
@@ -1,5 +1,6 @@
const VelocityEngine = require('./VelocityEngine');
const TwigEngine = require('./TwigEngine');
const PhpEngine = require('./PhpEngine');

const defaultEngine = 'twig';

Expand All @@ -8,6 +9,7 @@ module.exports = class TemplateEnginesStack {
this.engines = [
new VelocityEngine(),
new TwigEngine(),
new PhpEngine(),
];
}

Expand Down
2 changes: 0 additions & 2 deletions app/templating/TwigEngine.js
Expand Up @@ -14,8 +14,6 @@ module.exports = class TwigEngine extends BaseEngine {
}

render({bodyFilePath, body, endpoint, request, context }) {

console.log(path.dirname(bodyFilePath));
const template = Twig.twig({
data: body,
namespaces: { 'template-root-dir': path.dirname(bodyFilePath) }
Expand Down

0 comments on commit 395ea53

Please sign in to comment.