Skip to content

Commit 50db025

Browse files
committed
add router.php for php Built-in webserver
1 parent b0372d6 commit 50db025

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

phpserver/.htaccess

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Order deny,allow
2+
Deny from all

phpserver/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
PHP Built-in webserver
2+
======================
3+
4+
php has a Built-in webserver since version 5.4
5+
https://secure.php.net/manual/en/features.commandline.webserver.php
6+
7+
As many applications and frameworks rely on rewrites on webserver side,
8+
the same as magento does, it offers an argument for a router script.
9+
Either the script returns false which means, it should try to deliver the file
10+
as usual via file system lookup, or it executes the specific php scripts via include.
11+
12+
Example:
13+
requests to `/static/frontend/Magento/blank/en_US/mage/calendar.css` should deliver the file if it exists, or execute `/static.php` if not.
14+
15+
Without a router script, that is not possible via the php built-in server.
16+
17+
### How to use it
18+
19+
example usage: ```php -S 127.0.0.41:8082 -t ./pub/ ./phpserver/router.php```
20+
21+
### What exactly does the script
22+
23+
first we have an low level `$debug` closure, for the case you need to debug execution.
24+
25+
If the requestpath starts with index.php, get.php, static.php, we return to normal request flow.
26+
If we notice a favicon.ico request, the same.
27+
28+
Then rewrite paths for `pub/errors/default/` by removing the `pub/` part. (was at least needed for older versions)
29+
30+
Request starting with `media/`, `opt/`, `static/` test if the file exists.
31+
If Yes, then handle it, if not "forward" `static` to `static.php` and `media` to `get.php`
32+
33+
If non of the rules matched, return 404.
34+
You may instead include the index.php, if 404 should be handled by magento or you want
35+
urls without `index.php/`.

phpserver/router.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* this is a router file for the php Built-in web server
4+
* https://secure.php.net/manual/en/features.commandline.webserver.php
5+
*
6+
* It provides the same "rewrites" as the .htaccess for apache,
7+
* or the nginx.conf.sample for nginx.
8+
*
9+
* example usage: php -S 127.0.0.41:8082 -t ./pub/ ./router.php
10+
*/
11+
12+
$debug = function($val){
13+
return;
14+
if(is_array($val)){
15+
$val = json_encode($val);
16+
}
17+
echo 'debug: '.$val.PHP_EOL.'<br/>'.PHP_EOL;
18+
};
19+
20+
/**
21+
* Caution, this is very experimental stuff
22+
* no garant for working result
23+
* has tons of potencial big security holes
24+
*/
25+
26+
if (php_sapi_name() === 'cli-server') {
27+
28+
$debug($_SERVER["REQUEST_URI"]);
29+
if (preg_match('/^\/(index|get|static)\.php(\/)?/', $_SERVER["REQUEST_URI"])) {
30+
return false; // serve the requested resource as-is.
31+
}
32+
33+
$path = pathinfo($_SERVER["SCRIPT_FILENAME"]);
34+
$url = pathinfo(substr($_SERVER["REQUEST_URI"], 1));
35+
$route = parse_url(substr($_SERVER["REQUEST_URI"], 1))["path"];
36+
37+
$debug($path);
38+
$debug($route);
39+
40+
if( $path["basename"] == 'favicon.ico' ){
41+
return false;
42+
}
43+
44+
$debug($route);
45+
$debug(strpos($route, 'errors/default/css/'));
46+
47+
if(strpos($route, 'pub/errors/default/') === 0){
48+
$route = preg_replace('#pub/errors/default/#', 'errors/default/', $route, 1);
49+
}
50+
51+
$debug($route);
52+
53+
54+
if(
55+
strpos($route, 'media/') === 0 ||
56+
strpos($route, 'opt/') === 0 ||
57+
strpos($route, 'static/') === 0 ||
58+
strpos($route, 'errors/default/css/') === 0 ||
59+
strpos($route, 'errors/default/images/') === 0
60+
){
61+
$magentoPackagePubDir = __DIR__."/../pub";
62+
63+
$file = $magentoPackagePubDir.'/'.$route;
64+
$debug($file);
65+
if(file_exists($file)){
66+
$debug('file exists');
67+
return false;
68+
}else{
69+
$debug('file exists not');
70+
if(strpos($route, 'static/') === 0){
71+
$route = preg_replace('#static/#', '', $route, 1);
72+
$_GET['resource'] = $route;
73+
include($magentoPackagePubDir.'/static.php');
74+
exit;
75+
}elseif(strpos($route, 'media/') === 0){
76+
include($magentoPackagePubDir.'/get.php');
77+
exit;
78+
}
79+
}
80+
}
81+
82+
header('HTTP/1.0 404 Not Found');
83+
84+
}

0 commit comments

Comments
 (0)