├── .npmrc
├── .gitattributes
├── .gitignore
├── test
├── 200
│ ├── index.php
│ └── index2.php
├── 301
│ ├── 301.php
│ └── index.php
├── 400
│ └── index.php
├── 404
│ └── index.php
├── 500
│ └── index.php
├── env
│ └── index.php
├── directives
│ └── index.php
├── browsersync
│ ├── main.css
│ └── index.php
└── test.js
├── .github
├── funding.yml
└── workflows
│ └── main.yml
├── .editorconfig
├── tasks
└── php.js
├── package.json
├── license
├── gruntfile.js
└── readme.md
/.npmrc:
--------------------------------------------------------------------------------
1 | package-lock=false
2 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | * text=auto eol=lf
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | yarn.lock
3 |
--------------------------------------------------------------------------------
/test/200/index.php:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/test/301/301.php:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/test/200/index2.php:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/test/env/index.php:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/test/directives/index.php:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/test/301/index.php:
--------------------------------------------------------------------------------
1 |
5 |
--------------------------------------------------------------------------------
/test/404/index.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/test/400/index.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/test/500/index.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.github/funding.yml:
--------------------------------------------------------------------------------
1 | github: sindresorhus
2 | open_collective: sindresorhus
3 | custom: https://sindresorhus.com/donate
4 |
--------------------------------------------------------------------------------
/test/browsersync/main.css:
--------------------------------------------------------------------------------
1 | .m-box {
2 | border: 10px solid #190;
3 | margin: 10px;
4 | padding: 10px;
5 | line-height: 1.4em;
6 | }
7 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | indent_style = tab
5 | end_of_line = lf
6 | charset = utf-8
7 | trim_trailing_whitespace = true
8 | insert_final_newline = true
9 |
10 | [*.yml]
11 | indent_style = space
12 | indent_size = 2
13 |
--------------------------------------------------------------------------------
/.github/workflows/main.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 | on:
3 | - push
4 | - pull_request
5 | jobs:
6 | test:
7 | name: Node.js ${{ matrix.node-version }}
8 | runs-on: ubuntu-latest
9 | strategy:
10 | fail-fast: false
11 | matrix:
12 | node-version:
13 | - 14
14 | - 12
15 | - 10
16 | - 8
17 | steps:
18 | - uses: actions/checkout@v2
19 | - uses: actions/setup-node@v1
20 | with:
21 | node-version: ${{ matrix.node-version }}
22 | - run: npm install
23 | - run: npm test
24 |
--------------------------------------------------------------------------------
/test/browsersync/index.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | grunt-php BrowserSync Instance
6 |
7 |
8 |
9 |
10 |
11 |
grunt-php
12 | Your PHP Server is running!' . "\n";
14 | echo '
Change the border color in test/browsersync/main.css to see if BrowserSync is working.
' . "\n";
15 | ?>
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/tasks/php.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 | const phpServer = require('php-server');
3 |
4 | module.exports = grunt => {
5 | grunt.registerMultiTask('php', 'Start a PHP server', async function () {
6 | const done = this.async();
7 |
8 | const options = this.options({
9 | silent: false,
10 | keepAlive: false
11 | });
12 |
13 | try {
14 | const server = await phpServer(options);
15 |
16 | if (!options.silent) {
17 | server.stdout.pipe(process.stdout);
18 | server.stderr.pipe(process.stderr);
19 | }
20 |
21 | if (!this.flags.keepAlive && !options.keepAlive) {
22 | done();
23 | }
24 | } catch (error) {
25 | grunt.warn(error);
26 | done();
27 | }
28 | });
29 | };
30 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "grunt-php",
3 | "version": "3.0.0",
4 | "description": "Start a PHP server",
5 | "license": "MIT",
6 | "repository": "sindresorhus/grunt-php",
7 | "author": {
8 | "name": "Sindre Sorhus",
9 | "email": "sindresorhus@gmail.com",
10 | "url": "sindresorhus.com"
11 | },
12 | "engines": {
13 | "node": ">=8"
14 | },
15 | "scripts": {
16 | "test": "xo && grunt"
17 | },
18 | "files": [
19 | "tasks"
20 | ],
21 | "keywords": [
22 | "gruntplugin",
23 | "php",
24 | "server",
25 | "webserver",
26 | "web-server",
27 | "http"
28 | ],
29 | "dependencies": {
30 | "php-server": "^0.2.0"
31 | },
32 | "devDependencies": {
33 | "got": "^9.6.0",
34 | "grunt": "^1.0.4",
35 | "grunt-browser-sync": "^2.0.0",
36 | "grunt-cli": "^1.2.0",
37 | "grunt-contrib-watch": "^1.0.0",
38 | "grunt-simple-mocha": "^0.4.0",
39 | "xo": "^0.24.0"
40 | },
41 | "peerDependencies": {
42 | "grunt": ">=1"
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/license:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) Sindre Sorhus (sindresorhus.com)
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 |
--------------------------------------------------------------------------------
/test/test.js:
--------------------------------------------------------------------------------
1 | /* eslint-env mocha */
2 | 'use strict';
3 | const {strict: assert} = require('assert');
4 | const got = require('got');
5 |
6 | it('start a PHP server', async () => {
7 | const response = await got('http://0.0.0.0:8008');
8 | assert.equal(response.statusCode, 200);
9 | assert.equal(response.body, 'Hello World');
10 | });
11 |
12 | it('start a PHP server when the status code is 301', async () => {
13 | const response = await got('http://0.0.0.0:8009');
14 | assert.equal(response.statusCode, 200);
15 | assert.equal(response.body, '301 Redirected!');
16 | });
17 |
18 | it('start a PHP server when the status code is 400', async () => {
19 | const error = await got('http://0.0.0.0:8010', {throwHttpErrors: false});
20 | assert.equal(error.statusCode, 400);
21 | });
22 |
23 | it('start a PHP server when the status code is 404', async () => {
24 | const error = await got('http://0.0.0.0:8011', {throwHttpErrors: false});
25 | assert.equal(error.statusCode, 404);
26 | });
27 |
28 | it('expose environment variables', async () => {
29 | const {body} = await got('http://0.0.0.0:8021');
30 | assert.equal(body, 'foobar');
31 | });
32 |
33 | it('expose custom INI directive', async () => {
34 | const {body} = await got('http://0.0.0.0:8022');
35 | assert.equal(body, 'foobar');
36 | });
37 |
--------------------------------------------------------------------------------
/gruntfile.js:
--------------------------------------------------------------------------------
1 | /* eslint-disable quote-props */
2 | 'use strict';
3 |
4 | module.exports = grunt => {
5 | grunt.initConfig({
6 | simplemocha: {
7 | test: {
8 | src: 'test/test.js'
9 | }
10 | },
11 | watch: {
12 | test: {
13 | files: [
14 | 'noop'
15 | ]
16 | }
17 | },
18 | php: {
19 | open: {
20 | options: {
21 | port: 7007,
22 | base: 'test',
23 | keepAlive: true,
24 | open: true
25 | }
26 | },
27 | openUrl: {
28 | options: {
29 | port: 7008,
30 | base: 'test',
31 | keepAlive: true,
32 | open: '200/index2.php'
33 | }
34 | },
35 | test200: {
36 | options: {
37 | port: 8008,
38 | base: 'test/200'
39 | }
40 | },
41 | test301: {
42 | options: {
43 | port: 8009,
44 | base: 'test/301'
45 | }
46 | },
47 | test400: {
48 | options: {
49 | port: 8010,
50 | base: 'test/400'
51 | }
52 | },
53 | test404: {
54 | options: {
55 | port: 8011,
56 | base: 'test/404'
57 | }
58 | },
59 | test500: {
60 | options: {
61 | port: 8020,
62 | base: 'test/500'
63 | }
64 | },
65 | testEnv: {
66 | options: {
67 | port: 8021,
68 | base: 'test/env',
69 | env: {
70 | FOOBAR: 'foobar'
71 | }
72 | }
73 | },
74 | testDirectives: {
75 | options: {
76 | port: 8022,
77 | base: 'test/directives',
78 | directives: {
79 | 'error_log': 'foobar'
80 | }
81 | }
82 | },
83 | serve: {
84 | options: {
85 | port: 9000,
86 | base: 'test/browsersync'
87 | }
88 | }
89 | },
90 | browserSync: {
91 | serve: {
92 | bsFiles: {
93 | src: [
94 | 'test/browsersync/styles.css',
95 | 'test/browsersync/index.php'
96 | ]
97 | },
98 | options: {
99 | proxy: '<%= php.serve.options.hostname %>:<%= php.serve.options.port %>',
100 | watchTask: true,
101 | notify: true,
102 | open: true,
103 | logLevel: 'silent',
104 | ghostMode: {
105 | clicks: true,
106 | scroll: true,
107 | links: true,
108 | forms: true
109 | }
110 | }
111 | }
112 | }
113 | });
114 |
115 | grunt.loadTasks('tasks');
116 | grunt.loadNpmTasks('grunt-simple-mocha');
117 | grunt.loadNpmTasks('grunt-contrib-watch');
118 | grunt.loadNpmTasks('grunt-browser-sync');
119 |
120 | grunt.registerTask('phpwatch', [
121 | 'php:test200',
122 | 'watch'
123 | ]);
124 |
125 | grunt.registerTask('serve', [
126 | 'php:serve',
127 | 'browserSync:serve',
128 | 'watch'
129 | ]);
130 |
131 | grunt.registerTask('default', [
132 | 'php:test200',
133 | 'php:test301',
134 | 'php:test400',
135 | 'php:test404',
136 | 'php:testEnv',
137 | 'php:testDirectives',
138 | 'simplemocha:test'
139 | ]);
140 | };
141 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # grunt-php
2 |
3 | > Start a [PHP server](https://php.net/manual/en/features.commandline.webserver.php)
4 |
5 | Useful for developing PHP projects or running tests on them.
6 |
7 | Pretty much a drop-in replacement for [grunt-contrib-connect](https://github.com/gruntjs/grunt-contrib-connect), except for the `middleware` option.
8 |
9 | Uses the PHP built-in server.
10 |
11 |
12 | ## Install
13 |
14 | ```
15 | $ npm install --save-dev grunt-php
16 | ```
17 |
18 |
19 | ## Usage
20 |
21 | ```js
22 | require('load-grunt-tasks')(grunt);
23 |
24 | grunt.initConfig({
25 | php: {
26 | dist: {
27 | options: {
28 | port: 5000
29 | }
30 | }
31 | }
32 | });
33 |
34 | grunt.registerTask('default', ['php']);
35 | ```
36 |
37 |
38 | ## Examples
39 |
40 | #### Start a persistent PHP server and open in browser
41 |
42 | ```js
43 | grunt.initConfig({
44 | php: {
45 | test: {
46 | options: {
47 | keepAlive: true,
48 | open: true
49 | }
50 | }
51 | }
52 | });
53 |
54 | grunt.registerTask('test', ['php', 'mocha']);
55 | ```
56 |
57 | #### Use it with [BrowserSync](http://www.browsersync.io)
58 |
59 | ```js
60 | grunt.initConfig({
61 | php: {
62 | dist: {
63 | options: {
64 | port: 9000,
65 | base: 'dist' // Project root
66 | }
67 | }
68 | },
69 | browserSync: {
70 | dist: {
71 | bsFiles: {
72 | src: [
73 | // Files you want to watch for changes
74 | ]
75 | },
76 | options: {
77 | proxy: '<%= php.dist.options.hostname %>:<%= php.dist.options.port %>',
78 | watchTask: true,
79 | notify: true,
80 | open: true,
81 | logLevel: 'silent',
82 | ghostMode: {
83 | clicks: true,
84 | scroll: true,
85 | links: true,
86 | forms: true
87 | }
88 | }
89 | }
90 | },
91 | watch: {
92 | // Your watch tasks
93 | }
94 | });
95 |
96 | grunt.registerTask('serve', [
97 | 'php:dist', // Start PHP Server
98 | 'browserSync:dist', // Using the PHP instance as a proxy
99 | 'watch' // Any other watch tasks you want to run
100 | ]);
101 | ```
102 |
103 | #### Use it with [grunt-contrib-watch](https://github.com/gruntjs/grunt-contrib-watch)
104 |
105 | ```js
106 | grunt.initConfig({
107 | php: {
108 | watch: {}
109 | }
110 | });
111 |
112 | grunt.registerTask('phpwatch', ['php:watch', 'watch']);
113 | ```
114 |
115 | #### Add path for a custom error log
116 |
117 | ```js
118 | const path = require('path');
119 |
120 | grunt.initConfig({
121 | php: {
122 | dist: {
123 | options: {
124 | directives: {
125 | 'error_log': path.resolve('logs/error.log')
126 | }
127 | }
128 | }
129 | }
130 | });
131 |
132 | grunt.registerTask('default', ['php']);
133 | ```
134 |
135 | ## Options
136 |
137 | Supports all the [`php-server` options](https://github.com/sindresorhus/php-server#options) in addition to the ones below.
138 |
139 | ### keepAlive
140 |
141 | Type: `boolean`
142 | Default: `false`
143 |
144 | Keep the server alive indefinitely. Any task specified after this will not run.
145 |
146 | This option can also be enabled ad-hoc by running the task like `grunt php:targetname:keepAlive`.
147 |
148 | ### silent
149 |
150 | Type: `boolean`
151 | Default: `false`
152 |
153 | Suppress output produced by the PHP server.
154 |
155 |
156 | ## Related
157 |
158 | - [php-server](https://github.com/sindresorhus/php-server) - Start a PHP server from Node.js
159 |
160 |
161 | ## License
162 |
163 | MIT © [Sindre Sorhus](https://sindresorhus.com)
164 |
--------------------------------------------------------------------------------