Skip to content

Commit 2812ad6

Browse files
committed
Cleaned code a little bit. Pass environment to evaluate functions.
1 parent ac3a233 commit 2812ad6

24 files changed

Lines changed: 770 additions & 218 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
vendor
22
example/output
3+
example/env.php
4+
example/example.nginx.conf
5+
example/example.php-fpm.conf

README.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,109 @@ Generates config files from PHP based templates and PHP style config files.
55

66

77

8-
Please run the example/example.php for an example of how to use it.
8+
## Generate config files
9+
10+
11+
Source config file
12+
13+
```
14+
access_log ${'nginx.log.directory'}/project.access.log requestTime;
15+
error_log ${'nginx.log.directory'}/project.error.log;
16+
root ${'project.root.directory'}/public;
17+
```
18+
19+
Settings file:
20+
21+
```
22+
<?php
23+
24+
$centos = [
25+
'nginx.log.directory' => '/var/log/nginx',
26+
'project.root.directory' => '/home/project',
27+
];
28+
29+
$windows = [
30+
'nginx.log.directory' => 'c:/nginx',
31+
'project.root.directory' => 'c:/documents/project',
32+
];
33+
34+
$dave = [
35+
'project.root.directory' => '/home/workdir/project',
36+
]
37+
38+
```
39+
40+
41+
configurate data/nginx.conf.php var/generated/nginx.conf centos,dave -p settings.php
42+
43+
44+
```
45+
access_log /var/log/nginx/project.access.log requestTime;
46+
error_log /var/log/nginx/project.error.log;
47+
root /home/workdir/project/public;
48+
```
49+
50+
51+
52+
53+
## Generate environment settings
54+
55+
56+
57+
A file that returns an array of what env settings are required by this application
58+
59+
```
60+
<?php
61+
62+
use ImagickDemo\Config;
63+
64+
$env = [
65+
Config::CACHING_SETTING,
66+
Config::SCRIPT_PACKING,
67+
];
68+
69+
return $env;
70+
```
71+
72+
73+
Produces a file containing a single function that contains all the requested env settings.
74+
75+
```
76+
<?php
77+
78+
function getAppEnv() {
79+
static $env = [
80+
'caching.setting' => 'caching.revalidate',
81+
'script.packing' => '',
82+
];
83+
84+
return $env;
85+
}
86+
87+
```
88+
89+
The keys are the actual strings, rather than the constants used in the application, to allow the settings to be used outside of the application.
90+
91+
92+
93+
94+
95+
## Convert PHP ini files to PHP-FPM format
96+
97+
Because of reasons, PHP-FPM doesn't use the standard PHP in file format when including ini files in a pool in the PHP-FPM conf file.
98+
99+
This aspect of the Configurator converts PHP style ini files:
100+
101+
```
102+
extension=imagick.so
103+
default_charset = "utf-8";
104+
post_max_size = 10M
105+
```
106+
107+
to PHP-FPM style files:
108+
109+
```
110+
php_admin_value[extension] = "imagick.so"
111+
php_admin_value[default_charset] = "utf-8"
112+
php_admin_value[post_max_size] = "10M"
113+
```

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
{
22
"name": "intahwebz/configurator",
33
"description": "Generates config files from PHP based config files and PHP and JSON data files.",
4-
"keywords": ["php", "config", "tool", "UTF-8"],
4+
"keywords": ["php", "config", "environment", "configuration", "tool", "UTF-8"],
55
"license": "MIT",
66
"autoload": {
77
"psr-0": {
88
"Configurator": "src/"
99
}
1010
},
11+
"autoload-dev": {
12+
"psr-0": { "ExampleApp\\": "example/" }
13+
},
1114
"bin": [
1215
"bin/configurate",
1316
"bin/genenv",
@@ -27,7 +30,5 @@
2730
"email": "Danack@basereality.com",
2831
"homepage": "http://www.basereality.com"
2932
}
30-
],
31-
"minimum-stability": "dev",
32-
"prefer-stable": true
33+
]
3334
}

example/ExampleApp/AppConfig.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace ExampleApp;
4+
5+
class AppConfig
6+
{
7+
// Whether assets should be cached or not.
8+
const CACHING_SETTING = 'caching.setting';
9+
10+
// Whether JS / CSS script are packed together
11+
// or served separately.
12+
const SCRIPT_PACKING = 'script.packing';
13+
14+
15+
const FILE_STORAGE = 'file.storage';
16+
}
17+
18+
19+
20+

example/config/config.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
// This is a sample configuration file
4+
$socketDir = '/var/run/php-fpm';
5+
6+
$liveMemory = 32;
7+
8+
9+
// By deploying each version of an app into its own directory,
10+
// this allows a unique socket to be used per deploy.
11+
// e.g. if the app is deployed into the directory /home/appname/${commitSHA}
12+
//
13+
$fullSocketPath = $socketDir."/php-fpm-example-".basename(dirname(__DIR__)).".sock";
14+
15+
$default = [
16+
//global/default variables go here.
17+
'nginx.sendFile' => 'off',
18+
'mysql.charset' => 'utf8mb4',
19+
'mysql.collation' => 'utf8mb4_unicode_ci',
20+
];
21+
22+
$centos = [
23+
'nginx.log.directory' => '/var/log/nginx',
24+
'nginx.root.directory' => '/usr/share/nginx',
25+
'nginx.conf.directory' => '/etc/nginx',
26+
'nginx.run.directory ' => '/var/run',
27+
'nginx.user' => 'nginx',
28+
'nginx.sendFile' => 'on',
29+
30+
'example.root.directory' => dirname(__DIR__),
31+
32+
'phpfpm.www.maxmemory' => "".$liveMemory."M",
33+
'phpfpm.user' => 'intahwebz',
34+
'phpfpm.group' => 'www-data',
35+
'phpfpm.socket.directory' => $socketDir,
36+
'phpfpm.conf.directory' => '/etc/php-fpm.d',
37+
'phpfpm.pid.directory' => '/var/run/php-fpm',
38+
39+
//
40+
'phpfpm.fullsocketpath' => $fullSocketPath,
41+
42+
'php.conf.directory' => '/etc/php',
43+
'php.log.directory' => '/var/log/php',
44+
'php.errorlog.directory' => '/var/log/php',
45+
'php.session.directory' => '/var/lib/php/session',
46+
];
47+
48+
49+
50+
$dev = [];
51+
52+
// Dev mode should be run with less memory than live, to make
53+
// out of memory issues more likely to show up on dev than live
54+
$dev['phpfpm.www.maxmemory'] = "".($liveMemory - 10)."M";
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
$config = <<< END
4+
5+
server {
6+
7+
gzip on;
8+
gzip_http_version 1.0;
9+
gzip_vary on;
10+
gzip_comp_level 6;
11+
gzip_proxied any;
12+
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;
13+
14+
gzip_buffers 16 8k;
15+
16+
# ~ nginx performs a regular expression match.
17+
# ~* removes case sensitivity from the matches
18+
# ^~) matches literal string and stops processing
19+
# =) as an argument to location forces an exact match with the path requested and then stops searching for more specific matches. will only match http://ducklington.org/ but not http://ducklington.org/index.html
20+
21+
listen 80;
22+
listen 8080;
23+
server_name example.com www.example.com test.example.com example.test;
24+
25+
access_log ${'nginx.log.directory'}/example.access.log requestTime;
26+
error_log ${'nginx.log.directory'}/example.error.log;
27+
28+
root ${'example.root.directory'}/example;
29+
30+
client_max_body_size 1m;
31+
32+
rewrite ^/(.*)/$ /$1 permanent;
33+
34+
error_page 404 /404.html;
35+
location = /404.html {
36+
try_files /html/404.html =500;
37+
}
38+
39+
# redirect server error pages to the static page /50x.html
40+
#error_page 500 502 503 504 /50x.html;
41+
42+
#location = /50x.html {
43+
# root /usr/share/nginx/html;
44+
#}
45+
46+
fastcgi_intercept_errors off;
47+
48+
# this prevents hidden files (beginning with a period) from being served
49+
location ~ /\. { access_log off; log_not_found off; deny all; }
50+
51+
location = /robots.txt { access_log off; log_not_found off; }
52+
location = /favicon.ico { access_log off; log_not_found off; }
53+
location ~ /\. { access_log off; log_not_found off; deny all; }
54+
location ~ ~$ { access_log off; log_not_found off; deny all; }
55+
56+
#This must be the last regular epxression match
57+
location ~* ^[^\?\&]+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|psd|txt|tar|mid|midi|wav|bmp|rtf|js|svg|woff|ttf)$ {
58+
try_files \$uri /index.php?file=\$1;
59+
60+
#access_log off;
61+
expires 7d;
62+
add_header Pragma public;
63+
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
64+
}
65+
66+
location ~ ^/(www-status|ping)$ {
67+
if ( \$remote_addr != 127.0.0.1 ) { return 444;}
68+
access_log off;
69+
allow 127.0.0.1;
70+
deny all;
71+
fastcgi_param QUERY_STRING \$query_string;
72+
include ${'example.root.directory'}/data/conf/fastcgi.conf;
73+
fastcgi_pass unix:${'phpfpm.fullsocketpath'};
74+
}
75+
76+
location / {
77+
try_files \$uri /index.php =404;
78+
fastcgi_param QUERY_STRING \$query_string;
79+
fastcgi_pass unix:${'phpfpm.fullsocketpath'};
80+
include ${'example.root.directory'}/data/conf/fastcgi.conf;
81+
}
82+
}
83+
84+
END;
85+
86+
return $config;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
$config = <<< END
4+
5+
6+
;;;;;;;;;;;;;;;;;;;;
7+
; Pool Definitions ;
8+
;;;;;;;;;;;;;;;;;;;;
9+
10+
; Start a new pool named 'www'.
11+
[example]
12+
13+
; Unix user/group of processes
14+
user = exampleuser
15+
group = ${'phpfpm.group'}
16+
17+
listen = ${'phpfpm.fullsocketpath'}
18+
19+
; List of ipv4 addresses of FastCGI clients which are allowed to connect.
20+
listen.allowed_clients = 127.0.0.1
21+
22+
listen.owner = exampleuser
23+
listen.group = ${'phpfpm.group'}
24+
listen.mode = 0664
25+
26+
; Per pool prefix
27+
;prefix = /path/to/pools/\$pool
28+
;prefix = \$pool
29+
30+
request_slowlog_timeout = 10
31+
slowlog = ${'php.log.directory'}/slow.\$pool.log
32+
33+
request_terminate_timeout=500
34+
35+
catch_workers_output = yes
36+
37+
pm = dynamic
38+
39+
pm.max_children = 20
40+
pm.start_servers = 4
41+
pm.min_spare_servers = 2
42+
pm.max_spare_servers = 10
43+
pm.max_requests = 5000
44+
45+
; The URI to view the FPM status page.
46+
pm.status_path = /www-status
47+
48+
; Additional php.ini defines
49+
php_admin_value[memory_limit] = ${'phpfpm.www.maxmemory'}
50+
php_admin_value[error_log] = ${'php.errorlog.directory'}/\$pool-error.log
51+
52+
security.limit_extensions = .php
53+
54+
include = ${'example.root.directory'}/autogen/example.php.fpm.ini
55+
56+
END;
57+
58+
return $config;

example/config/fastcgi.conf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
2+
3+
#QUERY_STRING is not set in here - set it in Nginx to prevent extra redirect
4+
#fastcgi_param QUERY_STRING $query_string;
5+
6+
fastcgi_param REQUEST_METHOD $request_method;
7+
fastcgi_param CONTENT_TYPE $content_type;
8+
fastcgi_param CONTENT_LENGTH $content_length;
9+
10+
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
11+
fastcgi_param REQUEST_URI $request_uri;
12+
fastcgi_param DOCUMENT_URI $document_uri;
13+
fastcgi_param DOCUMENT_ROOT $document_root;
14+
fastcgi_param SERVER_PROTOCOL $server_protocol;
15+
fastcgi_param HTTPS $https if_not_empty;
16+
17+
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
18+
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
19+
20+
fastcgi_param REMOTE_ADDR $remote_addr;
21+
fastcgi_param REMOTE_PORT $remote_port;
22+
fastcgi_param SERVER_ADDR $server_addr;
23+
fastcgi_param SERVER_PORT $server_port;
24+
fastcgi_param SERVER_NAME $server_name;
25+
26+
# PHP only, required if PHP was built with --enable-force-cgi-redirect
27+
fastcgi_param REDIRECT_STATUS 200;
28+

0 commit comments

Comments
 (0)