Permalink
Browse files

Initial commit

  • Loading branch information...
lord committed Jan 20, 2014
0 parents commit b5c0328c0ea8efd1034906ccc92306b3425e2d2e
Showing with 151 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +12 −0 Gemfile
  3. +4 −0 Rakefile
  4. +4 −0 lib/middleman-php.rb
  5. +17 −0 lib/middleman-php/extension.rb
  6. +22 −0 lib/middleman-php/middleware.rb
  7. +1 −0 lib/middleman_extension.rb
  8. +21 −0 lib/php_middleware.rb
  9. +24 −0 middleman-php.gemspec
  10. +41 −0 readme.md
@@ -0,0 +1,5 @@
# Ignore bundler lock file
/Gemfile.lock
# Ignore pkg folder
/pkg
12 Gemfile
@@ -0,0 +1,12 @@
# If you have OpenSSL installed, we recommend updating
# the following line to use "https"
source 'http://rubygems.org'
# Specify your gem's dependencies in middleman-php.gemspec
gemspec
group :development do
gem "rake"
gem "rdoc"
gem "yard"
end
@@ -0,0 +1,4 @@
require 'bundler'
Bundler::GemHelper.install_tasks
require 'rake/clean'
@@ -0,0 +1,4 @@
require 'middleman-php/middleware'
require 'middleman-php/extension'
Middleman::PhpExtension.register(:php)
@@ -0,0 +1,17 @@
module Middleman
class PhpExtension < Extension
# option :set_blah, "default", "An example option"
def initialize(app, options_hash={}, &block)
# Call super to build options from the options_hash
super
require 'shellwords'
app.use Middleman::PhpMiddleware
app.before do
template_extensions :php => :html
end
end
end
end
@@ -0,0 +1,22 @@
module Middleman
class PhpMiddleware
def initialize(app)
@app = app
end
def call(env)
status, headers, response = @app.call(env)
if env['REQUEST_PATH'] =~ /\.php$/
response.body.map! do |item|
`echo #{Shellwords.escape(item)} | php`
end
headers['Content-Length'] = response.body.join.length.to_s
headers['Content-Type'] = 'text/html'
end
[status, headers, response]
end
end
end
@@ -0,0 +1 @@
require "middleman-php"
@@ -0,0 +1,21 @@
class PhpMiddleware
def initialize(app)
@app = app
end
def call(env)
status, headers, response = @app.call(env)
if env['REQUEST_PATH'] =~ /\.php$/
response.body.map! do |item|
`echo #{Shellwords.escape(item)} | php`
end
headers['Content-Length'] = response.body.join.length.to_s
headers['Content-Type'] = 'text/html'
end
[status, headers, response]
end
end
@@ -0,0 +1,24 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
Gem::Specification.new do |s|
s.name = "middleman-php"
s.version = "0.0.1"
s.platform = Gem::Platform::RUBY
s.authors = ["Robert Lord"]
s.email = ["robert@lord.io"]
s.homepage = "https://github.com/lord/middleman-php"
s.summary = %q{Use Middleman to build PHP files}
# s.description = %q{A longer description of your extension}
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
# The version of middleman-core your extension depends on
s.add_runtime_dependency("middleman-core", [">= 3.2.1"])
# Additional dependencies
# s.add_runtime_dependency("gem-name", "gem-version")
end
@@ -0,0 +1,41 @@
# middleman-php
## Installation
In your Middleman project's `Gemfile`, add:
```ruby
gem "middleman-php"
```
and in your `config.rb`, add:
```ruby
configure :development do
activate :php
end
```
If you want the php to be parsed on builds, too (instead of left for your server to parse), just omit the `configure` block:
```ruby
activate :php
```
## FAQ
### So wait, what does this do?
It lets [Middleman](https://github.com/middleman/middleman)'s server render PHP. So your `source/kittens.php` file will actually render kittens, and not a mess of `<?php` open tags.
### Oh lord, why?? Why would you want that???
Sometimes I have clients who want little bits of PHP interactivity on their site, but I still want to use Middleman to generate the PHP pages. This makes it so you don't have to `middleman build` and then upload to your site just to get a preview.
### Are there any limitations?
It will only render on pages where the URL ends in `.php`...so if you want to see `index.php`, you'll have to actually stick `index.php` in your browser.
Also, right now the rendering is done through PHP's command line interface, which has [some limitations](http://www.php.net/manual/en/features.commandline.differences.php)...including no $_GET or $_POST. Hopefully this will be fixed in the future.
There are probably also bugs I haven't found, so if you find one, feel free to submit an issue!

0 comments on commit b5c0328

Please sign in to comment.