Skip to content

Commit b12137b

Browse files
authored
Merge pull request #141 from kethinov/1.1.0
1.1.0
2 parents ae9a13e + 8bc0bdb commit b12137b

10 files changed

Lines changed: 2028 additions & 1139 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
- Put your changes here...
66

7+
## 1.1.0
8+
9+
- Added `run` and `runWithData` methods so this module can be used as a general purpose PHP runner.
10+
- Updated various dependencies.
11+
712
## 1.0.2
813

914
- Added TypeScript definitions.

README.md

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,54 @@
1-
# express-php-view-engine
1+
# node-php-runner
22

33
[![Build Status](https://github.com/rooseveltframework/express-php-view-engine/workflows/CI/badge.svg
44
)](https://github.com/rooseveltframework/express-php-view-engine/actions?query=workflow%3ACI) [![codecov](https://codecov.io/gh/rooseveltframework/express-php-view-engine/branch/master/graph/badge.svg)](https://codecov.io/gh/rooseveltframework/express-php-view-engine) [![npm](https://img.shields.io/npm/v/php.svg)](https://www.npmjs.com/package/php)
55

6-
This module allows you to use [PHP](https://php.net) as a templating system for [Express framework](https://expressjs.com) applications. This module was built and is maintained by the [Roosevelt web framework](https://github.com/rooseveltframework/roosevelt) [team](https://github.com/orgs/rooseveltframework/people), but it can be used independently of Roosevelt as well.
6+
This module allows you to run [PHP](https://php.net) code in Node.js in various ways:
77

8-
## Usage
8+
- Run PHP scripts.
9+
- Run PHP scripts and pass them JSON data from Node.js.
10+
- Use PHP as a view engine (templating system) for [Express framework](https://expressjs.com) applications.
911

10-
First declare `php` as a dependency in your app.
12+
To use this module, you must have PHP installed and in your PATH.
1113

12-
Then set PHP as a view engine in your Express app:
14+
This module was built and is maintained by the [Roosevelt web framework](https://github.com/rooseveltframework/roosevelt) [team](https://github.com/orgs/rooseveltframework/people), but it can be used independently of Roosevelt as well.
15+
16+
## Run a PHP script in Node.js
17+
18+
```javascript
19+
const php = require('php')
20+
const output = await php.run('some_php_script.php')
21+
```
22+
23+
## Run a PHP script in Node.js and pass it data
24+
25+
```javascript
26+
const php = require('php')
27+
const output = await php.runWithData('some_php_script.php', { hello: 'world' })
28+
```
29+
30+
Then, assuming your `some_php_script.php` file looks like this:
31+
32+
```php
33+
<p><?=$hello?></p>
34+
```
35+
36+
The output will be:
37+
38+
```html
39+
<p>world</p>
40+
```
41+
42+
## Use with Express
1343

1444
```js
1545
const express = require('express')
1646
const app = express()
1747
const php = require('php')
1848

19-
// setup php templating engine
49+
// setup PHP templating engine
2050
app.set('views', path.join(__dirname, 'templates'))
21-
app.set('view engine', 'php')
51+
app.set('view engine', 'php') // set PHP as a view engine in your Express app
2252
app.engine('php', php.__express)
2353

2454
// define a route
@@ -35,17 +65,15 @@ Then, assuming your `templates/index.php` looks like this:
3565
<p><?=$hello?></p>
3666
```
3767

38-
The ouptut will be:
68+
The output will be:
3969

4070
```html
4171
<p>world</p>
4272
```
4373

44-
Note: This module presumes that the system you run this on has PHP installed and that it's in your PATH.
45-
4674
## Configuration
4775

48-
As shown in the above example, this module will register values from the Express model as global variables in your PHP script by default. You can disable this behavior if desired two ways:
76+
As shown in the above examples, this module will register values from the data model you pass to the PHP script as global variables in your PHP script by default when you use PHP as an Express view engine or when you call `runWithData`. You can disable this behavior if desired in the following ways:
4977

5078
Disable registering globally:
5179

@@ -55,7 +83,7 @@ php.disableRegisterGlobalModel()
5583
// can be reenabled by calling php.enableRegisterGlobalModel()
5684
```
5785

58-
Disable registering on a per route basis:
86+
Disable registering on a per render basis in Express:
5987

6088
```js
6189
app.get('/', (req, res) => {
@@ -65,3 +93,13 @@ app.get('/', (req, res) => {
6593
})
6694
})
6795
```
96+
97+
Disable registering on a per render basis in `runWithData` (though if you're doing this, you probably should just use `php.run()` instead, as that method was written to use simpler logic that doesn't support passing data to PHP):
98+
99+
```js
100+
const output = await php.runWithData('some_php_script.php', {
101+
_REGISTER_GLOBAL_MODEL: false,
102+
hello: 'world'
103+
})
104+
```
105+

eslint.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const ava = require('eslint-plugin-ava')
2+
3+
module.exports = [
4+
{
5+
languageOptions: {
6+
ecmaVersion: 'latest'
7+
},
8+
plugins: {
9+
ava
10+
}
11+
}
12+
]

index.js

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,48 @@
1-
const circular = require('circular')
21
const path = require('path')
2+
const { execSync } = require('child_process')
33
const settings = {}
44
settings.disableRegisterGlobalModel = false
55

6-
async function render (template, model, callback) {
7-
const { execa } = await import('execa')
6+
function run (script) {
7+
try {
8+
const stdout = execSync(`php ${path.join(__dirname, script)}`)
9+
return stdout.toString()
10+
} catch (err) {
11+
throw new Error(`PHP process exited with code ${err.status}`)
12+
}
13+
}
14+
15+
function runWithData (template, model) {
16+
if (!model) model = {}
817
model._TEMPLATE = template
9-
if (typeof model._REGISTER_GLOBAL_MODEL === 'undefined') { // if not overridden by the model
10-
// then source the setting from the global settings
18+
if (typeof model._REGISTER_GLOBAL_MODEL === 'undefined') {
1119
if (settings.disableRegisterGlobalModel) {
1220
model._REGISTER_GLOBAL_MODEL = false
1321
} else {
1422
model._REGISTER_GLOBAL_MODEL = true
1523
}
1624
}
17-
model._REGISTER_GLOBAL_MODEL = !!model._REGISTER_GLOBAL_MODEL // force a boolean
18-
model._VIEWS_PATH = model.settings.views // pass views path to php
19-
const jsonModel = JSON.stringify(model, circular()) // stringify with circular references stripped
20-
const { stdout } = await execa('php', [path.join(__dirname, '/loader.php')], { input: jsonModel }) // e.g. php loader.php <<< '["array entry", "another", "etc"]'
21-
const renderedTemplate = stdout
22-
callback(null, renderedTemplate)
25+
model._REGISTER_GLOBAL_MODEL = !!model._REGISTER_GLOBAL_MODEL
26+
model._VIEWS_PATH = model?.settings?.views || './'
27+
const jsonModel = JSON.stringify(model, circular())
28+
29+
try {
30+
const stdout = execSync(`php ${path.join(__dirname, '/loader.php')}`, {
31+
input: jsonModel
32+
})
33+
return stdout.toString()
34+
} catch (err) {
35+
throw new Error(`PHP process exited with code ${err.status}`)
36+
}
37+
}
38+
39+
function __express (template, model, callback) {
40+
try {
41+
const stdout = runWithData(template, model)
42+
callback(null, stdout)
43+
} catch (err) {
44+
callback(err)
45+
}
2346
}
2447

2548
function disableRegisterGlobalModel () {
@@ -30,6 +53,27 @@ function enableRegisterGlobalModel () {
3053
settings.disableRegisterGlobalModel = false
3154
}
3255

33-
module.exports.__express = render
56+
function circular (ref, methods) {
57+
ref = ref || '[Circular]'
58+
const seen = []
59+
return function (key, val) {
60+
if (typeof val === 'function' && methods) {
61+
val = val.toString()
62+
}
63+
if (!val || typeof (val) !== 'object') {
64+
return val
65+
}
66+
if (~seen.indexOf(val)) {
67+
if (typeof ref === 'function') return ref(val)
68+
return ref
69+
}
70+
seen.push(val)
71+
return val
72+
}
73+
}
74+
75+
module.exports.run = run
76+
module.exports.runWithData = runWithData
77+
module.exports.__express = __express
3478
module.exports.disableRegisterGlobalModel = disableRegisterGlobalModel
3579
module.exports.enableRegisterGlobalModel = enableRegisterGlobalModel

0 commit comments

Comments
 (0)