Skip to content

1.1.0 #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2024
Merged

1.1.0 #141

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

- Put your changes here...

## 1.1.0

- Added `run` and `runWithData` methods so this module can be used as a general purpose PHP runner.
- Updated various dependencies.

## 1.0.2

- Added TypeScript definitions.
Expand Down
62 changes: 50 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,54 @@
# express-php-view-engine
# node-php-runner

[![Build Status](https://github.com/rooseveltframework/express-php-view-engine/workflows/CI/badge.svg
)](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)

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.
This module allows you to run [PHP](https://php.net) code in Node.js in various ways:

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

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

Then set PHP as a view engine in your Express app:
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.

## Run a PHP script in Node.js

```javascript
const php = require('php')
const output = await php.run('some_php_script.php')
```

## Run a PHP script in Node.js and pass it data

```javascript
const php = require('php')
const output = await php.runWithData('some_php_script.php', { hello: 'world' })
```

Then, assuming your `some_php_script.php` file looks like this:

```php
<p><?=$hello?></p>
```

The output will be:

```html
<p>world</p>
```

## Use with Express

```js
const express = require('express')
const app = express()
const php = require('php')

// setup php templating engine
// setup PHP templating engine
app.set('views', path.join(__dirname, 'templates'))
app.set('view engine', 'php')
app.set('view engine', 'php') // set PHP as a view engine in your Express app
app.engine('php', php.__express)

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

The ouptut will be:
The output will be:

```html
<p>world</p>
```

Note: This module presumes that the system you run this on has PHP installed and that it's in your PATH.

## Configuration

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:
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:

Disable registering globally:

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

Disable registering on a per route basis:
Disable registering on a per render basis in Express:

```js
app.get('/', (req, res) => {
Expand All @@ -65,3 +93,13 @@ app.get('/', (req, res) => {
})
})
```

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):

```js
const output = await php.runWithData('some_php_script.php', {
_REGISTER_GLOBAL_MODEL: false,
hello: 'world'
})
```

12 changes: 12 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const ava = require('eslint-plugin-ava')

module.exports = [
{
languageOptions: {
ecmaVersion: 'latest'
},
plugins: {
ava
}
}
]
68 changes: 56 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
const circular = require('circular')
const path = require('path')
const { execSync } = require('child_process')
const settings = {}
settings.disableRegisterGlobalModel = false

async function render (template, model, callback) {
const { execa } = await import('execa')
function run (script) {
try {
const stdout = execSync(`php ${path.join(__dirname, script)}`)
return stdout.toString()
} catch (err) {
throw new Error(`PHP process exited with code ${err.status}`)
}
}

function runWithData (template, model) {
if (!model) model = {}
model._TEMPLATE = template
if (typeof model._REGISTER_GLOBAL_MODEL === 'undefined') { // if not overridden by the model
// then source the setting from the global settings
if (typeof model._REGISTER_GLOBAL_MODEL === 'undefined') {
if (settings.disableRegisterGlobalModel) {
model._REGISTER_GLOBAL_MODEL = false
} else {
model._REGISTER_GLOBAL_MODEL = true
}
}
model._REGISTER_GLOBAL_MODEL = !!model._REGISTER_GLOBAL_MODEL // force a boolean
model._VIEWS_PATH = model.settings.views // pass views path to php
const jsonModel = JSON.stringify(model, circular()) // stringify with circular references stripped
const { stdout } = await execa('php', [path.join(__dirname, '/loader.php')], { input: jsonModel }) // e.g. php loader.php <<< '["array entry", "another", "etc"]'
const renderedTemplate = stdout
callback(null, renderedTemplate)
model._REGISTER_GLOBAL_MODEL = !!model._REGISTER_GLOBAL_MODEL
model._VIEWS_PATH = model?.settings?.views || './'
const jsonModel = JSON.stringify(model, circular())

try {
const stdout = execSync(`php ${path.join(__dirname, '/loader.php')}`, {
input: jsonModel
})
return stdout.toString()
} catch (err) {
throw new Error(`PHP process exited with code ${err.status}`)
}
}

function __express (template, model, callback) {
try {
const stdout = runWithData(template, model)
callback(null, stdout)
} catch (err) {
callback(err)
}
}

function disableRegisterGlobalModel () {
Expand All @@ -30,6 +53,27 @@ function enableRegisterGlobalModel () {
settings.disableRegisterGlobalModel = false
}

module.exports.__express = render
function circular (ref, methods) {
ref = ref || '[Circular]'
const seen = []
return function (key, val) {
if (typeof val === 'function' && methods) {
val = val.toString()
}
if (!val || typeof (val) !== 'object') {
return val
}
if (~seen.indexOf(val)) {
if (typeof ref === 'function') return ref(val)
return ref
}
seen.push(val)
return val
}
}

module.exports.run = run
module.exports.runWithData = runWithData
module.exports.__express = __express
module.exports.disableRegisterGlobalModel = disableRegisterGlobalModel
module.exports.enableRegisterGlobalModel = enableRegisterGlobalModel
Loading