forked from yiisoft/yii-web
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Somogyi Márton
committed
Oct 9, 2019
1 parent
3398f9b
commit dc71c52
Showing
3 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Web\NetworkResolver; | ||
|
||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class BasicNetworkResolver implements NetworkResolverInterface | ||
{ | ||
/** | ||
* @var ServerRequestInterface|null | ||
*/ | ||
private $serverRequest; | ||
|
||
public function getRemoteIp(): string | ||
{ | ||
if($this->serverRequest === null) { | ||
throw new \RuntimeException('ServerRequest doesn\'t set, see NetworkResolverInterface::withServerRequest()!'); | ||
} | ||
return $this->serverRequest->getServerParams()['REMOTE_ADDR']; | ||
} | ||
|
||
public function getUserIp(): string | ||
{ | ||
return $this->getRemoteIp(); | ||
} | ||
|
||
public function withServerRequest(ServerRequestInterface $serverRequest) | ||
{ | ||
$new = clone $this; | ||
$new->serverRequest = $serverRequest; | ||
return $new; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
|
||
namespace Yiisoft\Yii\Web\NetworkResolver; | ||
|
||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
interface NetworkResolverInterface | ||
{ | ||
public function getRemoteIp(): string; | ||
|
||
public function getUserIp(): string; | ||
|
||
/** | ||
* @return static | ||
*/ | ||
public function withServerRequest(ServerRequestInterface $serverRequest); | ||
} |