middleman-php 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/middleman-php/extension.rb +11 -7
- data/lib/middleman-php/injections.rb +63 -0
- data/lib/middleman-php/middleware.rb +50 -4
- data/lib/middleman-php/version.rb +5 -0
- data/middleman-php.gemspec +2 -1
- data/readme.md +8 -16
- metadata +10 -9
- data/lib/php_middleware.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26d98133e4c55c4a74b5ad121984e33cad9ece6b
|
4
|
+
data.tar.gz: 419431e20bdd347b8f9fa3cbc5267d039cb9454a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8c7785c40471662ac0af9dfee39b87be17b646d7b1e21cf1662575985fedb670c67849807e1f13898126554e0fc351192cc75d776b0e69e80669a759e7b2ba9
|
7
|
+
data.tar.gz: 8f29f157662d873b69b7ddbff71e9c110161ac2062decb2649d363a2281ccce66d18fd95333dced5d25d6ca3f880f640b7bed58dcca1e8ec31f01dc1be9854b8
|
@@ -1,17 +1,21 @@
|
|
1
|
+
require 'shellwords'
|
2
|
+
|
1
3
|
module Middleman
|
2
4
|
class PhpExtension < Extension
|
3
|
-
# option :set_blah, "default", "An example option"
|
4
5
|
|
5
6
|
def initialize(app, options_hash={}, &block)
|
6
|
-
# Call super to build options from the options_hash
|
7
7
|
super
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
def before
|
11
|
+
template_extensions :php => :html
|
12
|
+
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
14
|
+
def after_configuration
|
15
|
+
app.use Middleman::PhpMiddleware,
|
16
|
+
source_dir: app.source_dir,
|
17
|
+
environment: app.settings.environment
|
15
18
|
end
|
19
|
+
|
16
20
|
end
|
17
21
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Middleman
|
2
|
+
module Php
|
3
|
+
class Injections
|
4
|
+
|
5
|
+
def initialize debug=false
|
6
|
+
@debug = debug
|
7
|
+
@injections = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def add_server source_dir, env
|
11
|
+
full_path = File.join(source_dir, env['PATH_INFO'])
|
12
|
+
env.merge!({
|
13
|
+
'PHP_SELF' => env['PATH_INFO'],
|
14
|
+
'SCRIPT_NAME' => full_path,
|
15
|
+
'SCRIPT_FILENAME' => full_path,
|
16
|
+
'DOCUMENT_ROOT' => source_dir,
|
17
|
+
'REQUEST_TIME' => Time.now.to_i,
|
18
|
+
'REQUEST_TIME_FLOAT' => "%.4f" % Time.now.to_f,
|
19
|
+
'SERVER_ADMIN' => 'ruby@middlemanapp.com'
|
20
|
+
})
|
21
|
+
add_parse_str(URI.encode_www_form(env), '$_SERVER')
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_post rack_input
|
25
|
+
input = rack_input.read
|
26
|
+
unless input.length == 0
|
27
|
+
add_parse_str(input, '$_POST')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def add_get query_string
|
32
|
+
add_parse_str(query_string, '$_GET')
|
33
|
+
end
|
34
|
+
|
35
|
+
def add_include_path source_dir, path_info
|
36
|
+
path = File.dirname(File.join(source_dir, path_info))
|
37
|
+
add_raw("set_include_path(get_include_path() . PATH_SEPARATOR . '#{path}');")
|
38
|
+
end
|
39
|
+
|
40
|
+
def generate
|
41
|
+
if @injections.any?
|
42
|
+
injections = "<?php #{@injections.join(' ')} ?>"
|
43
|
+
if @debug
|
44
|
+
puts '== PHP Injections:'
|
45
|
+
puts injections
|
46
|
+
end
|
47
|
+
end
|
48
|
+
return injections || ''
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def add_parse_str values, array_name
|
54
|
+
@injections << "parse_str('#{values}', #{array_name});"
|
55
|
+
end
|
56
|
+
|
57
|
+
def add_raw source
|
58
|
+
@injections << source
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -1,22 +1,68 @@
|
|
1
|
+
require 'middleman-php/injections.rb'
|
2
|
+
|
1
3
|
module Middleman
|
2
4
|
class PhpMiddleware
|
3
5
|
|
4
|
-
def initialize(app)
|
5
|
-
@
|
6
|
+
def initialize(app, config={})
|
7
|
+
@injections = Middleman::Php::Injections.new(true)
|
8
|
+
@app = app
|
9
|
+
@config = config
|
10
|
+
@env = []
|
6
11
|
end
|
7
12
|
|
8
13
|
def call(env)
|
9
14
|
status, headers, response = @app.call(env)
|
10
15
|
|
11
16
|
if env['REQUEST_PATH'] =~ /\.php$/
|
17
|
+
set_environment(env)
|
12
18
|
response.body.map! do |item|
|
13
|
-
|
19
|
+
execute_php(item)
|
14
20
|
end
|
15
21
|
headers['Content-Length'] = response.body.join.length.to_s
|
16
|
-
headers['Content-Type']
|
22
|
+
headers['Content-Type'] = 'text/html'
|
23
|
+
headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
|
17
24
|
end
|
18
25
|
|
19
26
|
[status, headers, response]
|
20
27
|
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def set_environment env
|
32
|
+
@env = env
|
33
|
+
end
|
34
|
+
|
35
|
+
def execute_php source
|
36
|
+
inject_server
|
37
|
+
inject_include_path
|
38
|
+
inject_get
|
39
|
+
inject_post
|
40
|
+
`echo #{Shellwords.escape(@injections.generate + source)} | php`
|
41
|
+
end
|
42
|
+
|
43
|
+
def inject_server
|
44
|
+
if @config[:environment] == :development
|
45
|
+
@injections.add_server(@config[:source_dir], @env)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def inject_include_path
|
50
|
+
if @config[:environment] == :development
|
51
|
+
@injections.add_include_path(@config[:source_dir], @env['PATH_INFO'])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def inject_get
|
56
|
+
unless @env['QUERY_STRING'].empty?
|
57
|
+
@injections.add_get(@env['QUERY_STRING'])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def inject_post
|
62
|
+
if @env['REQUEST_METHOD'] == "POST"
|
63
|
+
@injections.add_post(@env["rack.input"])
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
21
67
|
end
|
22
68
|
end
|
data/middleman-php.gemspec
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "middleman-php/version"
|
3
4
|
|
4
5
|
Gem::Specification.new do |s|
|
5
6
|
s.name = "middleman-php"
|
6
|
-
s.version =
|
7
|
+
s.version = Middleman::Php::VERSION
|
7
8
|
s.platform = Gem::Platform::RUBY
|
8
9
|
s.authors = ["Robert Lord"]
|
9
10
|
s.email = ["robert@lord.io"]
|
data/readme.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# middleman-php
|
2
2
|
|
3
|
+
middleman-php 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.
|
4
|
+
|
5
|
+
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.
|
6
|
+
|
3
7
|
## Installation
|
4
8
|
|
5
9
|
In your Middleman project's `Gemfile`, add:
|
@@ -16,26 +20,14 @@ configure :development do
|
|
16
20
|
end
|
17
21
|
```
|
18
22
|
|
19
|
-
If you want the php to be parsed on builds, too (
|
23
|
+
If you want the php to be parsed on builds, too (as opposed to raw PHP generated for a server to run), just omit the `configure` block:
|
20
24
|
|
21
25
|
```ruby
|
22
26
|
activate :php
|
23
27
|
```
|
24
28
|
|
25
|
-
|
26
|
-
|
27
|
-
### So wait, what does this do?
|
28
|
-
|
29
|
-
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.
|
30
|
-
|
31
|
-
### Oh lord, why?? Why would you want that???
|
32
|
-
|
33
|
-
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.
|
34
|
-
|
35
|
-
### Are there any limitations?
|
36
|
-
|
37
|
-
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.
|
29
|
+
The PHP will only be rendered 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. It will however, let you do fancy stuff like files ending in `.php.erb`...so your ERB can generate some php output.
|
38
30
|
|
39
|
-
Also
|
31
|
+
Also thanks to [Mariano](https://github.com/mcavallo), who contributed a significant chunk of code to get $_GET, $_POST, and other PHP global request variables working.
|
40
32
|
|
41
|
-
There are probably also bugs I haven't found, so if you find one, feel free to submit an issue!
|
33
|
+
There are probably also bugs I haven't found, so if you find one, feel free to submit an issue!
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: middleman-php
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Lord
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: middleman-core
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 3.2.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 3.2.1
|
27
27
|
description:
|
@@ -31,14 +31,15 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
-
- .gitignore
|
34
|
+
- ".gitignore"
|
35
35
|
- Gemfile
|
36
36
|
- Rakefile
|
37
37
|
- lib/middleman-php.rb
|
38
38
|
- lib/middleman-php/extension.rb
|
39
|
+
- lib/middleman-php/injections.rb
|
39
40
|
- lib/middleman-php/middleware.rb
|
41
|
+
- lib/middleman-php/version.rb
|
40
42
|
- lib/middleman_extension.rb
|
41
|
-
- lib/php_middleware.rb
|
42
43
|
- middleman-php.gemspec
|
43
44
|
- readme.md
|
44
45
|
homepage: https://github.com/lord/middleman-php
|
@@ -50,17 +51,17 @@ require_paths:
|
|
50
51
|
- lib
|
51
52
|
required_ruby_version: !ruby/object:Gem::Requirement
|
52
53
|
requirements:
|
53
|
-
- -
|
54
|
+
- - ">="
|
54
55
|
- !ruby/object:Gem::Version
|
55
56
|
version: '0'
|
56
57
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
58
|
requirements:
|
58
|
-
- -
|
59
|
+
- - ">="
|
59
60
|
- !ruby/object:Gem::Version
|
60
61
|
version: '0'
|
61
62
|
requirements: []
|
62
63
|
rubyforge_project:
|
63
|
-
rubygems_version: 2.2.
|
64
|
+
rubygems_version: 2.2.2
|
64
65
|
signing_key:
|
65
66
|
specification_version: 4
|
66
67
|
summary: Use Middleman to build PHP files
|
data/lib/php_middleware.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
class PhpMiddleware
|
2
|
-
|
3
|
-
def initialize(app)
|
4
|
-
@app = app
|
5
|
-
end
|
6
|
-
|
7
|
-
def call(env)
|
8
|
-
status, headers, response = @app.call(env)
|
9
|
-
|
10
|
-
if env['REQUEST_PATH'] =~ /\.php$/
|
11
|
-
response.body.map! do |item|
|
12
|
-
`echo #{Shellwords.escape(item)} | php`
|
13
|
-
end
|
14
|
-
headers['Content-Length'] = response.body.join.length.to_s
|
15
|
-
headers['Content-Type'] = 'text/html'
|
16
|
-
end
|
17
|
-
|
18
|
-
[status, headers, response]
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|