Skip to content

Commit 71d712b

Browse files
author
Oleksii Korshenko
committed
MAGETWO-71529: Improvements to the phpserver router #10500
- Merge Pull Request #10500 from aredridel/magento2:improve-phpserver - Merged commits: 1. 36231da 2. b020517 3. 046585f 4. 0c2df2e
2 parents 4ee8196 + 0c2df2e commit 71d712b

File tree

3 files changed

+96
-10
lines changed

3 files changed

+96
-10
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Phpserver;
7+
8+
/**
9+
* @magentoAppIsolation enabled
10+
*
11+
* @magentoConfigFixture current_store web/secure/base_url http://127.0.0.1:8082/
12+
* @magentoConfigFixture current_store web/unsecure/base_link_url http://127.0.0.1:8082/
13+
* @magentoConfigFixture current_store web/secure/base_link_url http://127.0.0.1:8082/
14+
* @magentoConfigFixture current_store web/secure/use_in_frontend 0
15+
*
16+
* @magentoAppArea frontend
17+
*/
18+
class PhpserverTest extends \PHPUnit\Framework\TestCase
19+
{
20+
21+
const BASE_URL = '127.0.0.1:8082';
22+
23+
private static $serverPid;
24+
25+
private $httpClient;
26+
27+
/**
28+
* Instantiate phpserver in the pub folder
29+
*/
30+
public static function setUpBeforeClass()
31+
{
32+
$return = [];
33+
34+
$baseDir = __DIR__.'/../../../../../../';
35+
$command = sprintf(
36+
'cd %s && php -S %s -t ./pub/ ./phpserver/router.php >/dev/null 2>&1 & echo $!',
37+
$baseDir,
38+
static::BASE_URL
39+
);
40+
exec($command, $return);
41+
static::$serverPid = (int) $return[0];
42+
}
43+
44+
private function getUrl($url)
45+
{
46+
return sprintf('http://%s/%s', self::BASE_URL, ltrim($url, '/'));
47+
}
48+
49+
public function setUp()
50+
{
51+
$this->httpClient = new \Zend\Http\Client(null, ['timeout' => 10]);
52+
}
53+
54+
public function testServerHasPid()
55+
{
56+
$this->assertTrue(static::$serverPid > 0);
57+
}
58+
59+
public function testServerResponds()
60+
{
61+
$this->httpClient->setUri($this->getUrl('/'));
62+
$response = $this->httpClient->send();
63+
$this->assertFalse($response->isClientError());
64+
}
65+
66+
public function testStaticCssFile()
67+
{
68+
$this->httpClient->setUri($this->getUrl('/errors/default/css/styles.css'));
69+
$response = $this->httpClient->send();
70+
71+
$this->assertFalse($response->isClientError());
72+
$this->assertStringStartsWith('text/css', $response->getHeaders()->get('Content-Type')->getMediaType());
73+
}
74+
75+
public function testStaticImageFile()
76+
{
77+
$this->httpClient->setUri($this->getUrl('/errors/default/images/logo.gif'));
78+
$response = $this->httpClient->send();
79+
80+
$this->assertFalse($response->isClientError());
81+
$this->assertStringStartsWith('image/gif', $response->getHeaders()->get('Content-Type')->getMediaType());
82+
}
83+
84+
public static function tearDownAfterClass()
85+
{
86+
posix_kill(static::$serverPid, SIGKILL);
87+
}
88+
}

phpserver/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ php bin/magento setup:install --base-url=http://127.0.0.1:8082
2121
--db-host=localhost --db-name=magento --db-user=magento --db-password=magento
2222
--admin-firstname=Magento --admin-lastname=User [email protected]
2323
--admin-user=admin --admin-password=admin123 --language=en_US
24-
--currency=USD --timezone=America/Chicago --use-rewrites=0
24+
--currency=USD --timezone=America/Chicago --use-rewrites=1
2525
```
2626

27-
It's important to note that the router is not able to work with rewrite urls, that's why the flag `use-rewrites` is set to `0`.
28-
2927
Notes:
30-
- You must use `--use-rewrites=0` because the web server cannot rewrite URLs
3128
- By default, Magento creates a random Admin URI for you. Make sure to write this value down because it's how you access the Magento Admin later. For example : ```http://127.0.0.1:8082/index.php/admin_1vpn01```.
3229

3330
For more informations about the installation process using the CLI, you can consult the dedicated documentation that can found in [the developer documentation](https://github.com/magento/devdocs/blob/develop/guides/v2.0/install-gde/install/cli/install-cli-install.md).

phpserver/router.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@
7676

7777
$debug("file: $file");
7878

79-
if (file_exists($origFile)) {
79+
if (file_exists($origFile) || file_exists($file)) {
80+
if (file_exists($origFile)) {
81+
$file = $origFile;
82+
}
83+
8084
$debug('file exists');
81-
return false;
82-
} else if (file_exists($file)) {
8385
$mimeTypes = [
8486
'css' => 'text/css',
8587
'js' => 'application/javascript',
@@ -93,9 +95,8 @@
9395
'html' => 'text/html',
9496
];
9597

96-
$type = isset($mimeTypes[$ext]) && $mimeTypes[$ext];
97-
if ($type) {
98-
header("Content-Type: $type");
98+
if (isset($mimeTypes[$ext])) {
99+
header("Content-Type: $mimeTypes[$ext]");
99100
}
100101
readfile($file);
101102
return;

0 commit comments

Comments
 (0)