Skip to content

Commit 5dc0fc2

Browse files
committed
Add rewriter support
1 parent 12f1269 commit 5dc0fc2

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

lib/plugin.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ const capitalizeHeaders = header => header.replace(/(^|-)([a-z])/g, (_, dash, le
88
export async function plugin (server, opts) {
99
// We import this dynically to provide better error reporting in case
1010
// this module fails to load one of the native bindings
11-
const { Php, Request } = await import('@platformatic/php-node')
11+
const { Php, Request, Rewriter } = await import('@platformatic/php-node')
1212

1313
/* c8 ignore next */
1414
const configuration = server.platformatic?.config ?? opts.context?.stackable.configManager.current
1515
const docroot = configuration.php.docroot
16+
let rewriter = configuration.php.rewriter
1617

1718
// All files in the docroot that are not PHP files, should be served as static files
1819
await server.register(import('@fastify/static'), {
@@ -28,9 +29,15 @@ export async function plugin (server, opts) {
2829
done(null, body)
2930
})
3031

32+
// TODO: Make php-node support passing the rewriter config as an object to new Php()
33+
if (rewriter) {
34+
rewriter = new Rewriter(rewriter)
35+
}
36+
3137
const php = new Php({
3238
argv: process.argv,
33-
docroot
39+
docroot,
40+
rewriter
3441
})
3542

3643
for (const method of HTTP_METHODS) {

lib/schema.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ export const schema = {
1818
type: 'string',
1919
description: 'Path to the root of the PHP project',
2020
resolvePath: true
21+
},
22+
rewriter: {
23+
type: 'object',
24+
description: 'Rewriter configuration for PHP requests',
25+
additionalProperties: true
2126
}
2227
},
2328
required: ['docroot'],

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"homepage": "https://github.com/platformatic/php#readme",
3232
"dependencies": {
3333
"@fastify/static": "^8.2.0",
34-
"@platformatic/php-node": "^1.0.4",
34+
"@platformatic/php-node": "^1.2.0",
3535
"@platformatic/service": "^2.63.3",
3636
"json-schema-to-typescript": "^15.0.4"
3737
},

test/plugin.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ async function startStackable (t, docroot = join(import.meta.dirname, './fixture
1111
$schema: '../../schema.json',
1212
module: '../../lib/index.js',
1313
php: {
14-
docroot
14+
docroot,
15+
rewriter: opts.rewriter
1516
},
1617
port: 0,
1718
server: {
@@ -78,3 +79,18 @@ test('404', async t => {
7879

7980
t.assert.deepStrictEqual(res.statusCode, 404)
8081
})
82+
83+
test('support rewriter', async t => {
84+
const server = await startStackable(t, undefined, {
85+
rewriter: [{
86+
rewriters: [{
87+
type: 'href',
88+
args: ['^/(.*)$', '/index.php']
89+
}]
90+
}]
91+
})
92+
const res = await server.inject('/rewrite_me')
93+
94+
t.assert.deepStrictEqual(res.statusCode, 200)
95+
t.assert.deepStrictEqual(res.body, 'Hello World!')
96+
})

0 commit comments

Comments
 (0)