Skip to content

Commit

Permalink
HTTP: Use Nette HttpRequest for detect baseUri.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Jan 9, 2020
1 parent a435047 commit 5303496
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"php": ">=7.1.0",
"ext-json": "*",
"nette/di": "^3.0",
"nette/utils": "^3.0"
"nette/utils": "^3.0",
"nette/http": "^3.0"
},
"autoload": {
"classmap": [
Expand Down
6 changes: 3 additions & 3 deletions src/ApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class ApiExtension extends CompilerExtension
public function afterCompile(ClassType $class): void
{
$class->getMethod('initialize')->addBody(
'if (strncmp(' . Helpers::class . '::processPath(), \'api/\', 4) === 0) {'
. "\n\t" . '$this->getByType(' . Application::class . '::class)->onStartup[] = function(' . Application::class . ' $a) {'
. "\n\t\t" . '$this->getByType(\'' . ApiManager::class . '\')->run();'
'if (strncmp($structuredApi__basePath = ' . Helpers::class . '::processPath($this->getService(\'http.request\')), \'api/\', 4) === 0) {'
. "\n\t" . '$this->getByType(' . Application::class . '::class)->onStartup[] = function(' . Application::class . ' $a) use ($structuredApi__basePath) {'
. "\n\t\t" . '$this->getByType(\'' . ApiManager::class . '\')->run($structuredApi__basePath);'
. "\n\t" . '};'
. "\n" . '}'
);
Expand Down
6 changes: 3 additions & 3 deletions src/ApiManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ public function __construct(array $namespaceConventions, Container $container)
/**
* By given inputs or current URL route specific API endpoint and send full HTTP response.
*
* @param string|null $path
* @param string $path
* @param mixed[]|null $params
* @param string|null $method
* @throws StructuredApiException
*/
public function run(?string $path = null, ?array $params = [], ?string $method = null): void
public function run(string $path, ?array $params = [], ?string $method = null): void
{
$params = array_merge($_GET, $params ?? []);

if (preg_match('/^api\/v([^\/]+)\/?(.*?)$/', $path = $path ?? Helpers::processPath(), $pathParser)) {
if (preg_match('/^api\/v([^\/]+)\/?(.*?)$/', $path, $pathParser)) {
if (($version = (int) $pathParser[1]) < 1 || $version > 999 || !preg_match('#^[+-]?\d+$#D', $pathParser[1])) {
StructuredApiException::invalidVersion($pathParser[1]);
}
Expand Down
17 changes: 5 additions & 12 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Baraja\StructuredApi;


use Nette\Http\Request;

final class Helpers
{

Expand All @@ -21,21 +23,12 @@ public function __construct()
* Return current API path by current HTTP URL.
* In case of CLI return empty string.
*
* @param Request $httpRequest
* @return string
*/
public static function processPath(): string
public static function processPath(Request $httpRequest): string
{
static $return;

if (($currentUrl = self::getCurrentUrl()) !== null) {
if (preg_match('/^(?:https?:\/\/.+)\/www\/?(.*)$/', $currentUrl, $localUrlParser)) {
$return = $localUrlParser[1];
} elseif (preg_match('/^(?:https?:\/\/[^\/]+)(.*?)$/', $currentUrl, $publicUrlParser)) {
$return = $publicUrlParser[1];
}
}

return $return !== null ? trim($return, '/') : '';
return trim(str_replace(rtrim($httpRequest->getUrl()->withoutUserInfo()->getBaseUrl(), '/'), '', (string) self::getCurrentUrl()), '/');
}

/**
Expand Down

0 comments on commit 5303496

Please sign in to comment.