Skip to content

Commit

Permalink
MAGETWO-40675: Add router.php for php Built-in webserver #1517
Browse files Browse the repository at this point in the history
- changes according to CR
  • Loading branch information
Ivan Gavryshko committed Jul 23, 2015
1 parent 99e0537 commit 1dfa084
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
8 changes: 4 additions & 4 deletions phpserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ php has a Built-in webserver since version 5.4
https://secure.php.net/manual/en/features.commandline.webserver.php

As many applications and frameworks rely on rewrites on webserver side,
the same as magento does, it offers an argument for a router script.
the same as Magento does, it offers an argument for a router script.
Either the script returns false which means, it should try to deliver the file
as usual via file system lookup, or it executes the specific php scripts via include.

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

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

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

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

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

If non of the rules matched, return 404.
You may instead include the index.php, if 404 should be handled by magento or you want
If none of the rules matched, return 404.
You may instead include the index.php, if 404 should be handled by Magento or you want
urls without `index.php/`.
48 changes: 28 additions & 20 deletions phpserver/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@
* example usage: php -S 127.0.0.41:8082 -t ./pub/ ./router.php
*/

$debug = function($val){
return;
if(is_array($val)){
$val = json_encode($val);
/**
* Set it to true to enable debug mode
*/
define('DEBUG_ROUTER', false);

$debug = function ($val) {

if (!DEBUG_ROUTER) {
return;
}

if (is_array($val)) {
$val = json_encode($val);
}

echo 'debug: '.$val.PHP_EOL.'<br/>'.PHP_EOL;
};

/**
* Caution, this is very experimental stuff
* no garant for working result
* has tons of potencial big security holes
* no guarantee for working result
* has tons of potential big security holes
*/

if (php_sapi_name() === 'cli-server') {
Expand All @@ -42,48 +52,46 @@
$debug($path);
$debug($route);

if( $path["basename"] == 'favicon.ico' ){
if ($path["basename"] == 'favicon.ico') {
return false;
}

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

if(strpos($route, 'pub/errors/default/') === 0){
if (strpos($route, 'pub/errors/default/') === 0) {
$route = preg_replace('#pub/errors/default/#', 'errors/default/', $route, 1);
}

$debug($route);


if(
if (
strpos($route, 'media/') === 0 ||
strpos($route, 'opt/') === 0 ||
strpos($route, 'static/') === 0 ||
strpos($route, 'errors/default/css/') === 0 ||
strpos($route, 'errors/default/images/') === 0
){
) {
$magentoPackagePubDir = __DIR__."/../pub";

$file = $magentoPackagePubDir.'/'.$route;
$debug($file);
if(file_exists($file)){
$debug('file exists');
return false;
}else{
$debug('file exists not');
if(strpos($route, 'static/') === 0){
if (file_exists($file)) {
$debug('file exists');
return false;
} else {
$debug('file does not exist');
if (strpos($route, 'static/') === 0) {
$route = preg_replace('#static/#', '', $route, 1);
$_GET['resource'] = $route;
include($magentoPackagePubDir.'/static.php');
exit;
}elseif(strpos($route, 'media/') === 0){
} elseif (strpos($route, 'media/') === 0) {
include($magentoPackagePubDir.'/get.php');
exit;
}
}
}

header('HTTP/1.0 404 Not Found');

header('HTTP/1.0 404 Not Found');
}

0 comments on commit 1dfa084

Please sign in to comment.