Skip to content

Commit 1dfa084

Browse files
author
Ivan Gavryshko
committed
MAGETWO-40675: Add router.php for php Built-in webserver #1517
- changes according to CR
1 parent 99e0537 commit 1dfa084

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

phpserver/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ php has a Built-in webserver since version 5.4
55
https://secure.php.net/manual/en/features.commandline.webserver.php
66

77
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.
8+
the same as Magento does, it offers an argument for a router script.
99
Either the script returns false which means, it should try to deliver the file
1010
as usual via file system lookup, or it executes the specific php scripts via include.
1111

@@ -22,14 +22,14 @@ example usage: ```php -S 127.0.0.41:8082 -t ./pub/ ./phpserver/router.php```
2222

2323
first we have an low level `$debug` closure, for the case you need to debug execution.
2424

25-
If the requestpath starts with index.php, get.php, static.php, we return to normal request flow.
25+
If the request path starts with index.php, get.php, static.php, we return to normal request flow.
2626
If we notice a favicon.ico request, the same.
2727

2828
Then rewrite paths for `pub/errors/default/` by removing the `pub/` part. (was at least needed for older versions)
2929

3030
Request starting with `media/`, `opt/`, `static/` test if the file exists.
3131
If Yes, then handle it, if not "forward" `static` to `static.php` and `media` to `get.php`
3232

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
33+
If none of the rules matched, return 404.
34+
You may instead include the index.php, if 404 should be handled by Magento or you want
3535
urls without `index.php/`.

phpserver/router.php

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,28 @@
1414
* example usage: php -S 127.0.0.41:8082 -t ./pub/ ./router.php
1515
*/
1616

17-
$debug = function($val){
18-
return;
19-
if(is_array($val)){
20-
$val = json_encode($val);
17+
/**
18+
* Set it to true to enable debug mode
19+
*/
20+
define('DEBUG_ROUTER', false);
21+
22+
$debug = function ($val) {
23+
24+
if (!DEBUG_ROUTER) {
25+
return;
2126
}
27+
28+
if (is_array($val)) {
29+
$val = json_encode($val);
30+
}
31+
2232
echo 'debug: '.$val.PHP_EOL.'<br/>'.PHP_EOL;
2333
};
2434

2535
/**
2636
* Caution, this is very experimental stuff
27-
* no garant for working result
28-
* has tons of potencial big security holes
37+
* no guarantee for working result
38+
* has tons of potential big security holes
2939
*/
3040

3141
if (php_sapi_name() === 'cli-server') {
@@ -42,48 +52,46 @@
4252
$debug($path);
4353
$debug($route);
4454

45-
if( $path["basename"] == 'favicon.ico' ){
55+
if ($path["basename"] == 'favicon.ico') {
4656
return false;
4757
}
4858

4959
$debug($route);
5060
$debug(strpos($route, 'errors/default/css/'));
5161

52-
if(strpos($route, 'pub/errors/default/') === 0){
62+
if (strpos($route, 'pub/errors/default/') === 0) {
5363
$route = preg_replace('#pub/errors/default/#', 'errors/default/', $route, 1);
5464
}
5565

5666
$debug($route);
5767

58-
59-
if(
68+
if (
6069
strpos($route, 'media/') === 0 ||
6170
strpos($route, 'opt/') === 0 ||
6271
strpos($route, 'static/') === 0 ||
6372
strpos($route, 'errors/default/css/') === 0 ||
6473
strpos($route, 'errors/default/images/') === 0
65-
){
74+
) {
6675
$magentoPackagePubDir = __DIR__."/../pub";
6776

6877
$file = $magentoPackagePubDir.'/'.$route;
6978
$debug($file);
70-
if(file_exists($file)){
71-
$debug('file exists');
72-
return false;
73-
}else{
74-
$debug('file exists not');
75-
if(strpos($route, 'static/') === 0){
79+
if (file_exists($file)) {
80+
$debug('file exists');
81+
return false;
82+
} else {
83+
$debug('file does not exist');
84+
if (strpos($route, 'static/') === 0) {
7685
$route = preg_replace('#static/#', '', $route, 1);
7786
$_GET['resource'] = $route;
7887
include($magentoPackagePubDir.'/static.php');
7988
exit;
80-
}elseif(strpos($route, 'media/') === 0){
89+
} elseif (strpos($route, 'media/') === 0) {
8190
include($magentoPackagePubDir.'/get.php');
8291
exit;
8392
}
8493
}
8594
}
8695

87-
header('HTTP/1.0 404 Not Found');
88-
96+
header('HTTP/1.0 404 Not Found');
8997
}

0 commit comments

Comments
 (0)