Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

init

  • Loading branch information...
commit f66f949036942a85ecbfe9364c9a57d21fb68f4d 0 parents
@sindresorhus authored
12 .editorconfig
@@ -0,0 +1,12 @@
+# editorconfig.org
+root = true
+
+[*]
+indent_style = tab
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[*.md]
+trim_trailing_whitespace = false
1  .gitattributes
@@ -0,0 +1 @@
+* text=auto
1  .gitignore
@@ -0,0 +1 @@
+node_modules
21 .jshintrc
@@ -0,0 +1,21 @@
+{
+ "node": true,
+ "es5": true,
+ "esnext": true,
+ "bitwise": true,
+ "camelcase": true,
+ "curly": true,
+ "eqeqeq": true,
+ "immed": true,
+ "indent": 4,
+ "latedef": true,
+ "newcap": true,
+ "noarg": true,
+ "quotmark": "single",
+ "regexp": true,
+ "undef": true,
+ "unused": true,
+ "strict": true,
+ "trailing": true,
+ "smarttabs": true
+}
5 .npmignore
@@ -0,0 +1,5 @@
+.npmignore
+.editorconfig
+.jshintrc
+test
+Gruntfile.js
33 Gruntfile.js
@@ -0,0 +1,33 @@
+'use strict';
+module.exports = function (grunt) {
+ grunt.initConfig({
+ simplemocha: {
+ test: {
+ src: 'test/test-php.js'
+ }
+ },
+ php: {
+ open: {
+ options: {
+ port: 7000,
+ hostname: '0.0.0.0',
+ base: 'test',
+ keepalive: true,
+ open: true
+ }
+ },
+ test: {
+ options: {
+ port: 6000,
+ hostname: '0.0.0.0',
+ base: 'test'
+ }
+ }
+ }
+ });
+
+ grunt.loadTasks('tasks');
+ grunt.loadNpmTasks('grunt-simple-mocha');
+
+ grunt.registerTask('default', ['php:test', 'simplemocha:test']);
+};
43 package.json
@@ -0,0 +1,43 @@
+{
+ "name": "grunt-php",
+ "version": "0.1.0",
+ "description": "Start a PHP-server",
+ "keywords": [
+ "gruntplugin",
+ "server",
+ "php"
+ ],
+ "homepage": "https://github.com/sindresorhus/grunt-php",
+ "bugs": "https://github.com/sindresorhus/grunt-php/issues",
+ "author": {
+ "name": "Sindre Sorhus",
+ "email": "sindresorhus@gmail.com",
+ "url": "http://sindresorhus.com"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/sindresorhus/grunt-php.git"
+ },
+ "scripts": {
+ "test": "grunt"
+ },
+ "dependencies": {
+ "open": "0.0.3"
+ },
+ "devDependencies": {
+ "grunt": "~0.4.0",
+ "grunt-simple-mocha": "~0.3.2",
+ "request": "~2.14.0"
+ },
+ "peerDependencies": {
+ "grunt": "~0.4.0"
+ },
+ "engines": {
+ "node": ">=0.8.0"
+ },
+ "licenses": [
+ {
+ "type": "MIT"
+ }
+ ]
+}
136 readme.md
@@ -0,0 +1,136 @@
+# grunt-php [![Build Status](https://secure.travis-ci.org/sindresorhus/grunt-php.png?branch=master)](http://travis-ci.org/sindresorhus/grunt-php)
+
+*requires grunt 0.4 and PHP 5.4.0+*
+
+> Start a [PHP-server](http://php.net/manual/en/features.commandline.webserver.php)
+
+Pretty much a drop-in replacement for [grunt-contrib-connect](https://github.com/gruntjs/grunt-contrib-connect). Useful for eg. running tests on a PHP project. Uses the built-in server in PHP 5.4.0+.
+
+
+## Getting Started
+
+If you haven't used [grunt][] before, be sure to check out the [Getting Started][] guide, as it explains how to create a [gruntfile][Getting Started] as well as install and use grunt plugins. Once you're familiar with that process, install this plugin with this command:
+
+```shell
+npm install grunt-php --save-dev
+```
+
+Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
+
+```js
+grunt.loadNpmTasks('grunt-php');
+```
+
+[grunt]: http://gruntjs.com
+[Getting Started]: https://github.com/gruntjs/grunt/wiki/Getting-started
+
+
+## Documentation
+
+
+### Example usage
+
+
+#### Start a short-lived PHP-server
+
+Useful for you need to fire up a PHP-server for running unit tests or something.
+
+```javascript
+grunt.initConfig({
+ php: {
+ dist: {
+ options: {
+ port: 5000,
+ base: 'subfolder'
+ }
+ }
+ }
+});
+
+grunt.registerTask('default', ['php']);
+```
+
+Run this with `grunt php` or `grunt php:dist`.
+
+
+#### Start a persistent PHP-server and open in browser
+
+```javascript
+grunt.initConfig({
+ php: {
+ test: {
+ options: {
+ keepalive: true,
+ open: true
+ }
+ }
+ }
+});
+
+grunt.registerTask('test', ['php', 'mocha']);
+```
+
+
+### Options
+
+#### port
+
+Type: `Integer`
+Default: `8000`
+
+The port on which you want to access the webserver. Task will fail if the port is already in use.
+
+#### hostname
+
+Type: `String`
+Default: `'localhost'`
+
+The hostname the webserver will use.
+
+#### base
+
+Type: `String`
+Default: `'.'`
+
+From which folder the webserver will be served. Defaults to the directory of the Gruntfile.
+
+#### keepalive
+
+Type: `Boolean`
+Default: `false`
+
+Keep the server alive indefinitely. Any task specified after this will not run.
+
+This option can also be enabled ad-hoc by running the task like `grunt php:targetname:keepalive`
+
+#### open
+
+Type: `Boolean`
+Default: `false`
+
+Open the server in the browser when the task is triggered.
+
+#### router
+
+Type: `String`
+
+Optionally specify the path to a [router script](http://php.net/manual/en/features.commandline.webserver.php#example-380) that is run at the start of each HTTP request. If this script returns `false`, then the requested resource is returned as-is. Otherwise the script's output is returned to the browser.
+
+Example router script:
+
+```php
+<?php
+// router.php
+if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
+ return false; // serve the requested resource as-is
+} else {
+ echo "<p>Thanks for using grunt-php :)</p>";
+}
+?>
+```
+
+
+## License
+
+MIT License
+(c) [Sindre Sorhus](http://sindresorhus.com)
55 tasks/php.js
@@ -0,0 +1,55 @@
+'use strict';
+module.exports = function (grunt) {
+ var path = require('path');
+ var spawn = require('child_process').spawn;
+ var http = require('http');
+ var open = require('open');
+
+ function checkServer(url, cb) {
+ setTimeout(function () {
+ http.get(url, function (res) {
+ if (res.statusCode === 200) {
+ return cb();
+ }
+
+ checkServer(url, cb);
+ }).on('error', function () {
+ checkServer(url, cb);
+ });
+ }, 400);
+ }
+
+ grunt.registerMultiTask('php', function () {
+ var cb = this.async();
+ var options = this.options({
+ port: 8000,
+ hostname: 'localhost',
+ base: '.',
+ keepalive: false,
+ open: false
+ });
+ var host = options.hostname + ':' + options.port;
+ var args = ['-S', host];
+
+ if (options.router) {
+ args.push(options.router);
+ }
+
+ spawn('php', args, {
+ cwd: path.resolve(options.base),
+ stdio: 'inherit'
+ });
+
+ // check when the server is ready. tried doing it by listening
+ // to the child process `data` event, but it's not triggered...
+ checkServer('http://' + host, function () {
+ if (!this.flags.keepalive && !options.keepalive) {
+ cb();
+ }
+
+ if (options.open) {
+ open('http://' + host);
+ }
+ }.bind(this));
+ });
+};
1  test/index.php
@@ -0,0 +1 @@
+<?php echo 'Hello World' ?>
18 test/test-php.js
@@ -0,0 +1,18 @@
+/*global describe it */
+'use strict';
+var assert = require('assert');
+var request = require('request');
+
+describe('grunt-php', function () {
+ it('should start a PHP-server', function (cb) {
+ request.get('http://0.0.0.0:6000', function (err, response, body) {
+ if (err) {
+ return cb(err);
+ }
+
+ assert.equal(response.statusCode, 200);
+ assert.equal(body, 'Hello World');
+ cb();
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.