Skip to content

Commit 0cb7fc4

Browse files
authored
Merge pull request #629 from skipperbent/v5-release
V5 release
2 parents 749f252 + d5bf77c commit 0cb7fc4

File tree

8 files changed

+116
-8
lines changed

8 files changed

+116
-8
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,12 @@ If you do not want a redirect, but want the error-page rendered on the current-u
972972
$request->setRewriteCallback('ErrorController@notFound');
973973
```
974974

975+
If you will set the correct status for the browser error use:
976+
977+
```php
978+
SimpleRouter::response()->httpCode(404);
979+
```
980+
975981
## Using custom exception handlers
976982

977983
This is a basic example of an ExceptionHandler implementation (please see "[Easily overwrite route about to be loaded](#easily-overwrite-route-about-to-be-loaded)" for examples on how to change callback).
@@ -2073,4 +2079,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20732079
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20742080
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20752081
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2076-
SOFTWARE.
2082+
SOFTWARE.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
}
2828
],
2929
"require": {
30-
"php": ">=7.1",
30+
"php": ">=7.4",
3131
"ext-json": "*"
3232
},
3333
"require-dev": {

src/Pecee/Http/Input/InputItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function offsetExists($offset): bool
8181
return isset($this->value[$offset]);
8282
}
8383

84-
public function offsetGet($offset)
84+
public function offsetGet($offset): ?self
8585
{
8686
if ($this->offsetExists($offset) === true) {
8787
return $this->value[$offset];

src/Pecee/Http/Request.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ public function __construct()
129129
$this->setHost($this->getHeader('http-host'));
130130

131131
// Check if special IIS header exist, otherwise use default.
132-
$this->setUrl(new Url($this->getFirstHeader(['unencoded-url', 'request-uri'])));
132+
$url = $this->getHeader('unencoded-url');
133+
if($url !== null){
134+
$this->setUrl(new Url($url));
135+
}else{
136+
$this->setUrl(new Url(urldecode($this->getHeader('request-uri'))));
137+
}
133138
$this->setContentType((string)$this->getHeader('content-type'));
134139
$this->setMethod((string)($_POST[static::FORCE_METHOD_KEY] ?? $this->getHeader('request-method')));
135140
$this->inputHandler = new InputHandler($this);

src/Pecee/SimpleRouter/Route/Route.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,16 @@ protected function parseParameters($route, $url, $parameterRegex = null): ?array
123123
);
124124

125125
// Ensures that host names/domains will work with parameters
126-
$url = '/' . ltrim($url, '/');
126+
127+
if($route[0] == '{') $url = '/' . ltrim($url, '/');
127128
$urlRegex = '';
128129
$parameters = [];
129130

130131
if ($regex === null || (bool)preg_match_all('/' . $regex . '/u', $route, $parameters) === false) {
131132
$urlRegex = preg_quote($route, '/');
132133
} else {
133134

134-
foreach (preg_split('/((-?\/?){[^}]+})/', $route) as $key => $t) {
135+
foreach (preg_split('/((\.?-?\/?){[^}]+})/', $route) as $key => $t) {
135136

136137
$regex = '';
137138

@@ -146,7 +147,7 @@ protected function parseParameters($route, $url, $parameterRegex = null): ?array
146147
$regex = $parameterRegex ?? $this->defaultParameterRegex ?? static::PARAMETERS_DEFAULT_REGEX;
147148
}
148149

149-
$regex = sprintf('((\/|-)(?P<%2$s>%3$s))%1$s', $parameters[2][$key], $name, $regex);
150+
$regex = sprintf('((\/|-|\.)(?P<%2$s>%3$s))%1$s', $parameters[2][$key], $name, $regex);
150151
}
151152

152153
$urlRegex .= preg_quote($t, '/') . $regex;

src/Pecee/SimpleRouter/Router.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class Router
3535
* @var bool
3636
*/
3737
protected $isProcessingRoute;
38+
39+
/**
40+
* Defines all data from current processing route.
41+
* @var ILoadableRoute
42+
*/
43+
protected $currentProcessingRoute;
3844

3945
/**
4046
* All added routes
@@ -371,6 +377,9 @@ public function routeRequest(): ?string
371377
foreach ($this->processedRoutes as $key => $route) {
372378

373379
$this->debug('Matching route "%s"', get_class($route));
380+
381+
/* Add current processing route to constants */
382+
$this->currentProcessingRoute = $route;
374383

375384
/* If the route matches */
376385
if ($route->matchRoute($url, $this->request) === true) {
@@ -927,6 +936,16 @@ public function getDebugLog(): array
927936
{
928937
return $this->debugList;
929938
}
939+
940+
/**
941+
* Get the current processing route details.
942+
*
943+
* @return ILoadableRoute
944+
*/
945+
public function getCurrentProcessingRoute(): ILoadableRoute
946+
{
947+
return $this->currentProcessingRoute;
948+
}
930949

931950
/**
932951
* Changes the rendering behavior of the router.
@@ -950,4 +969,4 @@ public function addExceptionHandler(IExceptionHandler $handler): self
950969
return $this;
951970
}
952971

953-
}
972+
}

tests/Pecee/SimpleRouter/RouterRouteTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,70 @@ public function testDomainNotAllowedRoute()
175175

176176
TestRouter::debug('/test', 'get');
177177

178+
$this->assertFalse($result);
179+
180+
}
181+
182+
public function testFixedSubdomainDynamicDomain()
183+
{
184+
TestRouter::request()->setHost('other.world.com');
185+
186+
$result = false;
187+
188+
TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) {
189+
TestRouter::get('/test', function ($domain = null) use (&$result) {
190+
191+
$result = true;
192+
});
193+
});
194+
195+
TestRouter::debug('/test', 'get');
196+
197+
$this->assertTrue($result);
198+
199+
}
200+
201+
public function testFixedSubdomainDynamicDomainParameter()
202+
{
203+
TestRouter::request()->setHost('other.world.com');
204+
205+
$result = false;
206+
207+
TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) {
208+
TestRouter::get('/test', 'DummyController@param');
209+
TestRouter::get('/test/{key}', 'DummyController@param');
210+
});
211+
212+
$response = TestRouter::debugOutputNoReset('/test', 'get');
213+
214+
$this->assertEquals('world.com', $response);
215+
216+
$response = TestRouter::debugOutput('/test/unittest', 'get');
217+
218+
$this->assertEquals('unittest, world.com', $response);
219+
220+
}
221+
222+
public function testWrongFixedSubdomainDynamicDomain()
223+
{
224+
TestRouter::request()->setHost('wrong.world.com');
225+
226+
$result = false;
227+
228+
TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) {
229+
TestRouter::get('/test', function ($domain = null) use (&$result) {
230+
231+
$result = true;
232+
});
233+
});
234+
235+
try {
236+
TestRouter::debug('/test', 'get');
237+
} catch(\Exception $e) {
238+
239+
}
240+
241+
178242
$this->assertFalse($result);
179243

180244
}

tests/TestRouter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,17 @@ public static function debugOutput(string $testUrl, string $testMethod = 'get',
4848
return $response;
4949
}
5050

51+
public static function debugOutputNoReset(string $testUrl, string $testMethod = 'get', bool $reset = true): string
52+
{
53+
$response = null;
54+
55+
// Route request
56+
ob_start();
57+
static::debugNoReset($testUrl, $testMethod, $reset);
58+
$response = ob_get_clean();
59+
60+
// Return response
61+
return $response;
62+
}
63+
5164
}

0 commit comments

Comments
 (0)