Skip to content

Commit

Permalink
Merge pull request #41 from grmartin/grmartin/rewrite-1.0.0
Browse files Browse the repository at this point in the history
[WIP] - ES6, Multiple Instances, General Cleanup.
  • Loading branch information
micahblu authored Apr 18, 2017
2 parents d625f71 + 68ce3a0 commit 6106703
Show file tree
Hide file tree
Showing 10 changed files with 675 additions and 215 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf

[*.md]
trim_trailing_whitespace = false
108 changes: 105 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> Start a [PHP-server](http://php.net/manual/en/features.commandline.webserver.php)
This is pretty much a gulp version of [@sindresorhus's](https://github.com/sindresorhus) [grunt-php] (https://github.com/sindresorhus/grunt-php) and acts as a _basic version_ drop-in replacement for [gulp-connect](https://www.npmjs.com/package/gulp-connect), though please note not all features from gulp-connect are supported with gulp-connect-php. I am open to supporting other features and pull requests that implement them.
This is pretty much a gulp version of [@sindresorhus's](https://github.com/sindresorhus) [grunt-php](https://github.com/sindresorhus/grunt-php) and acts as a _basic version_ drop-in replacement for [gulp-connect](https://www.npmjs.com/package/gulp-connect), though please note not all features from gulp-connect are supported with gulp-connect-php. I am open to supporting other features and pull requests that implement them.

Uses the built-in server in PHP 5.4.0+.

Expand All @@ -14,6 +14,7 @@ $ npm install --save-dev gulp-connect-php

## Usage

### As a Singleton
```js
var gulp = require('gulp'),
connect = require('gulp-connect-php');
Expand All @@ -25,9 +26,26 @@ gulp.task('connect', function() {
gulp.task('default', ['connect']);
```

### As an Instance
```js
var gulp = require('gulp'),
connect = require('gulp-connect-php');

let server = new connect();

gulp.task('connect', function() {
server.server();
});
gulp.task('disconnect', function() {
server.closeServer();
});

gulp.task('default', ['connect', 'disconnect']);
```

## Examples

#### Use it with Browser Sync
### Use it with Browser Sync

```js
var gulp = require('gulp'),
Expand All @@ -47,6 +65,89 @@ gulp.task('connect-sync', function() {
});
```

### Advanced Option Manipulation

```js
gulp.task('connect', function() {
connect.server({
configCallback: function _configCallback(type, collection) {
// If you wish to leave one of the argument types alone, simply return the passed in collection.
if (type === connect.OPTIONS_SPAWN_OBJ) { // As the constant suggests, collection is an Object.

// Lets add a custom env var. Good for injecting AWS_RDS config variables.
collection.env = Object.assign({
MY_CUSTOM_ENV_VAR: "env_var_value"
}, process.env);

return collection;
} else if (type === connect.OPTIONS_PHP_CLI_ARR) { // As the constant suggests, collection is an Array.
let newArgs = [
'-e', // Generate extended information for debugger/profiler.
'-d', 'memory_limit=2G' // Define INI entry, Up memory limit to 2G.
];

// Ensure our argument switches appear before the rest.
return newArgs.concat(collection);
}
}
}, function _connected_callback() {
console.log("PHP Development Server Connected.");
});
});

gulp.task('disconnect', function() {
connect.closeServer();
});

gulp.task('default', ['connect', 'disconnect']);
```

### Windows (via Batch file)

Windows Batch file execution via a `%PATH%` specified batchfile is possible, but some considerations are required.

1. The batch file must be on your `%PATH%` and executable with permissions of the invoker.
2. You must pass the parameter set off to the PHP process.
3. We have no -real- way of detecting an error state at this point.
4. You must use the 'Advanced Option Maniulation' scheme and set the `shell` option on `spawn(...)`.

#### Scenario

- PHP is located at `C:\Users\mainuser\Applications\PHP\7.0.17-NTS-VC14\php.exe`.
- The batch file is located at `C:\Users\mainuser\MyProject\strap\php.bat`.
- I have set `%PATH%` manually to `C:\Users\mainuser\MyProject\strap\;%PATH%`.

#### Contents of php.bat

```batch
@echo off
REM We specify the whole path to PHP since the working directory is that of gulp...
REM unless we also changed that in our gulp callback.
C:\Users\mainuser\Applications\PHP\7.0.17-NTS-VC14\php.exe %*
```

#### Contents of our gulp task
```js
gulp.task('connect', function _gulp_connect_task() {
connect.server({
configCallback: function _configCallback(type, collection) {
if (type === connect.OPTIONS_SPAWN_OBJ) {
// Windows Batch files are NOT executable on their own. This will start a shell
// session then execute.
collection.shell = true;
return collection;
}
}
}, function _connected_callback() {
console.log("PHP Development Server Connected.");
});
});

gulp.task('default', ['connect']);
````

## Options

### port
Expand Down Expand Up @@ -123,7 +224,8 @@ Node's [stdio parameter](https://nodejs.org/api/child_process.html#child_process
Type: `function (type, collection) : collection`
Prototype:
Prototype:
- `type` - String, either `OPTIONS_SPAWN_OBJ` or `OPTIONS_PHP_CLI_ARR`.
- `collection` - Array or Object, the initial version of the collection specified by `type`.
Expand Down
19 changes: 11 additions & 8 deletions examples/browser-sync-02/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
var gulp = require('gulp'),
connect = require('../../index.js'),
browserSync = require('browser-sync');
/* jshint esversion: 6, node: true */
'use strict';

const gulp = require('gulp'),
connect = require('../../index.js'),
browserSync = require('browser-sync');


//task that fires up php server at port 8001
gulp.task('connect', function(callback) {
gulp.task('connect', function (callback) {
connect.server({
port: 8001
}, callback);
});


//task that fires up browserSync proxy after connect server has started
gulp.task('browser-sync',['connect'], function() {
browserSync({
proxy: '127.0.0.1:8001',
port: 8910
gulp.task('browser-sync', ['connect'], function () {
browserSync({
proxy: '127.0.0.1:8001',
port: 8910
});
});

Expand Down
13 changes: 8 additions & 5 deletions examples/browser-sync/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
var gulp = require('gulp'),
connect = require('../../index.js'),
browserSync = require('browser-sync');
/* jshint esversion: 6, node: true */
'use strict';

gulp.task('connect-sync', function() {
connect.server({}, function (){
const gulp = require('gulp'),
connect = require('../../index.js'),
browserSync = require('browser-sync');

gulp.task('connect-sync', function () {
connect.server({}, function () {
browserSync({
proxy: 'localhost:8000'
});
Expand Down
47 changes: 25 additions & 22 deletions examples/simple-with-argument-manipulation/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
var gulp = require('gulp'),
connect = require('../../index.js');
/* jshint esversion: 6, node: true */
'use strict';

const gulp = require('gulp'),
connect = require('../../index.js');

gulp.task('connect', function _gulp_connect_task() {
connect.server({
configCallback: function _configCallback(type, collection) {
// If you wish to leave one of the argument types alone, simply return the passed in collection.
if (type === connect.OPTIONS_SPAWN_OBJ) { // As the constant suggests, collection is an Object.
connect.server({
configCallback: function _configCallback(type, collection) {
// If you wish to leave one of the argument types alone, simply return the passed in collection.
if (type === connect.OPTIONS_SPAWN_OBJ) { // As the constant suggests, collection is an Object.

// Lets add a custom env var. Good for injecting AWS_RDS config variables.
collection.env = Object.assign({
MY_CUSTOM_ENV_VAR: "env_var_value"
}, process.env);
// Lets add a custom env var. Good for injecting AWS_RDS config variables.
collection.env = Object.assign({
MY_CUSTOM_ENV_VAR: "env_var_value"
}, process.env);

return collection;
} else if (type === connect.OPTIONS_PHP_CLI_ARR) { // As the constant suggests, collection is an Array.
var newArgs = [
'-e', // Generate extended information for debugger/profiler.
'-d', 'memory_limit=2G' // Define INI entry, Up memory limit to 2G.
];
return collection;
} else if (type === connect.OPTIONS_PHP_CLI_ARR) { // As the constant suggests, collection is an Array.
let newArgs = [
'-e', // Generate extended information for debugger/profiler.
'-d', 'memory_limit=2G' // Define INI entry, Up memory limit to 2G.
];

// Ensure our argument switches appear before the rest.
return newArgs.concat(collection);
}
// Ensure our argument switches appear before the rest.
return newArgs.concat(collection);
}
}, function _connected_callback(){
console.log("PHP Development Server Connected.");
});
}
}, function _connected_callback() {
console.log("PHP Development Server Connected.");
});
});

gulp.task('default', ['connect']);
15 changes: 9 additions & 6 deletions examples/simple/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
var gulp = require('gulp'),
connect = require('../../index.js');
/* jshint esversion: 6, node: true */
'use strict';

gulp.task('connect', function() {
connect.server({}, function(){
// connected
});
const gulp = require('gulp'),
connect = require('../../index.js');

gulp.task('connect', function () {
connect.server({}, function () {
// connected
});
});

gulp.task('default', ['connect']);
Loading

0 comments on commit 6106703

Please sign in to comment.