<commit_before># Webpack-php-mock

[![npm version](https://badge.fury.io/js/webpack-php-mock.svg)](https://badge.fury.io/js/webpack-php-mock)
[![Issue Stats](http://issuestats.com/github/chenckang/webpack-php-mock/badge/pr)](http://issuestats.com/github/chenckang/webpack-php-mock)
[![Issue Stats](http://issuestats.com/github/chenckang/webpack-php-mock/badge/issue)](http://issuestats.com/github/chenckang/webpack-php-mock)
[![Build Status](https://travis-ci.org/chenckang/webpack-php-mock.svg)](https://travis-ci.org/chenckang/webpack-php-mock)

## Installation

**Webpack-php-mock** has been published as a npm module, so that one can easily install it using the ***npm install*** command. 

Switch to you project root directory which should already have a ***package.json*** file in it, and run command as the following:

    npm install webpack-php-mock --save-dev

Now you get **webpack-php-mock** installed on your project, declaring it as one of the "devDependencies".

The next important thing is to add a configuration file to the root of the project named ***wpm.config.js***, where one can add custom settings.

## Usage

You can quickly launch the mock stuff using the demo [example](https://github.com/chenckang/webpack-php-mock/tree/master/example).

Switch to the root of the example. And run the command 
    
    npm install

And start it like this.

    gulp

After that just visit [http://localhost:3000/](http://localhost:3000/).

The configuration descriptions are as below.

***

## Configuration

The first thing to be anounced is that there should be a **webpack.config.js** file or the similiar to support the compiling of your project using ***webpack***. This is because ***webpack-php-mock*** is just based on ***webpack*** and provides no additional compiling stuff.

All the configuration usage could be found in the [wpm.config.js](https://github.com/chenckang/webpack-php-mock/blob/master/example/wpm.config.js) demo.

#### The Root Directory

***Configuration Name: root***

To help to locate all the static file, you should explicitly give the "webroot" directory. The root directory only contains static files such as JavaScript files, image files, font files and so on. The root directory will be cleaned every once you run ***gulp*** or ***gulp clean***.

    root: path.join(mockroot, 'webroot')

#### Webpack Output Path

***Configuration Name: outputPath***

This will override the the ***output.path*** configuration of the **webpack.config.js**. The purpose is put the output files to a specific place that will avoid unnecessary coverage of the ***webpack*** compiling output.

    outputPath: path.join(mockroot, 'webroot', 'app')

#### Jsons

***Configuration Name: jsons.path***

Specify the local path where all the json datas are located. This is mainly for the relative path of the type ***local*** rewrite rule.

    jsons: {
        path: path.join(mockroot, 'jsons')
    }

#### PHP Connect

***Configuration Name: phpconnect***

This is a configuration for the plugin ***gulp-connect-php***, [click to check the detail](https://github.com/micahblu/gulp-connect-php.git)

    phpconnect: {
        host: 'localhost',
        port: 8000,
        root: path.join(mockroot, 'php')
    }

#### Server

***Configuration Name: server.port***

Just specify the port that the mock server will use.

    server: {
        port: 3000
    }

#### Rewrite Path

***Configuration Name: rewrite.paths***

This is the main part of the configuration, which can achieve proxy, interface mock, php request redirect.

The paths is an array which contain many individual rewrite rules. Each rule is one of the type: ***local***, ***proxy***, ***php***. And cookie can also be injected into each rule to help the authorization.

Here is an example:
    
    rewrite: {
        paths: [
            {
                type: 'local',
                from: '/sample',
                to: '/sample.json',
                mime: 'application/json'
            },
            {
                type: 'php',
                from: '/sample.php',
                to: 'http://localhost:8000/',
                mime: 'text/html'
            },
            {
                type: 'proxy',
                from: '/app/*',
                to: 'http://host.com/',
                method: 'all',
                mime: 'application/json',
                cookie: {
                    file: 'fake.cookie',
                    opt: {
                        maxAge: 10000
                    }
                }
            }
        ]
    }

Below is the description of the type value:

##### Type: local
This is used to mock request interface, you can spcify a local json file in "to", and when you request "from", the content of the "to" will be returned. If the mime is "application/json" it will act like a real interface. The path value of the "to" is relative to the ***jsons.path*** configuration.

##### Type: proxy

This is used for proxy, redirect you "from" request to "to". It can be used to pull data from a real server. Also if you specify the cookie, the proxy will have the authorization info to request backend server.

##### Type: php

This is used for php interpretation. Request based on php can use this type.

#### Deploy to Reconstruct

Configuration Name: deploy.default

This is used to reconstruct the output files to be online-like. We think that developing and testing you project in an online-like structure will help to reduce error-prone problems that will jump out once you push your codes online.

So just test you project as online-like.

Tips: The deploy configuration is not so completed, it is only capable of simple reconstruct such as move the template file to some other place. When you move the static files, you browser may complain that it can't find the static files for the link path just fails.

Below is what it look like:
<commit_msg>complete the doc<commit_after># Webpack-php-mock

[![npm version](https://badge.fury.io/js/webpack-php-mock.svg)](https://badge.fury.io/js/webpack-php-mock)
[![Issue Stats](http://issuestats.com/github/chenckang/webpack-php-mock/badge/pr)](http://issuestats.com/github/chenckang/webpack-php-mock)
[![Issue Stats](http://issuestats.com/github/chenckang/webpack-php-mock/badge/issue)](http://issuestats.com/github/chenckang/webpack-php-mock)
[![Build Status](https://travis-ci.org/chenckang/webpack-php-mock.svg)](https://travis-ci.org/chenckang/webpack-php-mock)

## Installation

**Webpack-php-mock** has been published as a npm module, so that one can easily install it using the ***npm install*** command. 

Switch to you project root directory which should already have a ***package.json*** file in it, and run command as the following:

    npm install webpack-php-mock --save-dev

Now you get **webpack-php-mock** installed on your project, declaring it as one of the "devDependencies".

The next important thing is to add a configuration file to the root of the project named ***wpm.config.js***, where one can add custom settings.

## Usage

You can quickly launch the mock stuff using the demo [example](https://github.com/chenckang/webpack-php-mock/tree/master/example).

Switch to the root of the example. And run the command 
    
    npm install

And start it like this.

    gulp

After that just visit [http://localhost:3000/](http://localhost:3000/).

The configuration descriptions are as below.

***

## Configuration

The first thing to be anounced is that there should be a **webpack.config.js** file or the similiar to support the compiling of your project using ***webpack***. This is because ***webpack-php-mock*** is just based on ***webpack*** and provides no additional compiling stuff.

All the configuration usage could be found in the [wpm.config.js](https://github.com/chenckang/webpack-php-mock/blob/master/example/wpm.config.js) demo.

#### The Root Directory

***Configuration Name: root***

To help to locate all the static file, you should explicitly give the "webroot" directory. The root directory only contains static files such as JavaScript files, image files, font files and so on. The root directory will be cleaned every once you run ***gulp*** or ***gulp clean***.

    root: path.join(mockroot, 'webroot')

#### Webpack Output Path

***Configuration Name: outputPath***

This will override the the ***output.path*** configuration of the **webpack.config.js**. The purpose is put the output files to a specific place that will avoid unnecessary coverage of the ***webpack*** compiling output.

    outputPath: path.join(mockroot, 'webroot', 'app')

#### Jsons

***Configuration Name: jsons.path***

Specify the local path where all the json datas are located. This is mainly for the relative path of the type ***local*** rewrite rule.

    jsons: {
        path: path.join(mockroot, 'jsons')
    }

#### PHP Connect

***Configuration Name: phpconnect***

This is a configuration for the plugin ***gulp-connect-php***, [click to check the detail](https://github.com/micahblu/gulp-connect-php.git)

    phpconnect: {
        host: 'localhost',
        port: 8000,
        root: path.join(mockroot, 'php')
    }

#### Server

***Configuration Name: server.port***

Just specify the port that the mock server will use.

    server: {
        port: 3000
    }

#### Rewrite Path

***Configuration Name: rewrite.paths***

This is the main part of the configuration, which can achieve proxy, interface mock, php request redirect.

The paths is an array which contain many individual rewrite rules. Each rule is one of the type: ***local***, ***proxy***, ***php***. And cookie can also be injected into each rule to help the authorization.

Here is an example:
    
    rewrite: {
        paths: [
            {
                type: 'local',
                from: '/sample',
                to: '/sample.json',
                mime: 'application/json'
            },
            {
                type: 'php',
                from: '/sample.php',
                to: 'http://localhost:8000/',
                mime: 'text/html'
            },
            {
                type: 'proxy',
                from: '/app/*',
                to: 'http://host.com/',
                method: 'all',
                mime: 'application/json',
                cookie: {
                    file: 'fake.cookie',
                    opt: {
                        maxAge: 10000
                    }
                }
            }
        ]
    }

Below is the description of the type value:

##### Type: local
This is used to mock request interface, you can spcify a local json file in "to", and when you request "from", the content of the "to" will be returned. If the mime is "application/json" it will act like a real interface. The path value of the "to" is relative to the ***jsons.path*** configuration.

##### Type: proxy

This is used for proxy, redirect you "from" request to "to". It can be used to pull data from a real server. Also if you specify the cookie, the proxy will have the authorization info to request backend server.

##### Type: php

This is used for php interpretation. Request based on php can use this type.

#### Deploy to Reconstruct

***Configuration Name: deploy.default***

This is used to reconstruct the output files to be online-like. We think that developing and testing you project in an online-like structure will help to reduce error-prone problems that will jump out once you push your codes online.

So just test you project as online-like.

Tips: The deploy configuration is not so completed, it is only capable of simple reconstruct such as move the template file to some other place. When you move the static files, you browser may complain that it can't find the static files for the link path just fails.

Below is what it look like:

{'fraction_non_alphanumeric': 0.11032963308194221, 'fraction_numerical': 0.004236929073807304, 'mean_word_length': 2.9643936849177024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '691272', 'n_tokens_mistral': 3293, 'n_tokens_neox': 3102, 'n_words': 1366}