I recently did some PHP development and thought it would be nice if I could use Yeoman’s built-in development server to serve dynamic files, too.
The first step was to install
php-cgi which isn’t included with OS X.
brew tap homebrew/dupes brew tap josegonzalez/homebrew-php brew install php54
The next step was to add a middleware to Yeoman’s server in order to invoke the PHP interpreter for all files ending with
I found the node-cgi module that Nathan (@TooTallNate) had written to serve the GitWeb interface through Node, but unfortunately node-cgi only handles scripts with a shebang that are located in a
I needed a module that would serve scripts from arbitrary locations and choose the interpreter based on the file’s extension. As it should also support welcome-files so I decided to roll my own.
While Yeoman was now able to serve PHP files, one important aspect was still missing: The server had to inject the liverelaad snippet into the response that was generated by the PHP interpreter.
Hence I wrote a second middleware called tamper. What tamper does is that it temporarily overwrites the
end() methods of the ServerResponse object. Before the headers are sent to the client, tamper invokes a callback function that decides whether the response body should be captured or whether it should be streamed to the client without further tampering.
With these two little patches
yeoman server now happily serves PHP files. You can watch the screencast below to see the live reloading in action: