Skip to content

Commit eaa7c0b

Browse files
committed
updated for Nette 3
1 parent 9ae6c56 commit eaa7c0b

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
},
1212
"require": {
1313
"php": ">= 7.3",
14-
"nette/di": "~2.3",
15-
"nette/http": "~2.3"
14+
"nette/bootstrap": "^3.0",
15+
"nette/di": "^3.0",
16+
"nette/http": "^3.0"
1617
},
1718
"autoload": {
1819
"psr-0": {

src/HttpAuthExtension/HttpAuthExtension.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,45 @@
1212

1313
namespace HttpAuthExtension;
1414

15-
use Nette;
15+
use Nette\Configurator;
16+
use Nette\DI\CompilerExtension;
17+
use Nette\PhpGenerator\ClassType;
1618

1719

18-
class HttpAuthExtension extends Nette\DI\CompilerExtension
20+
class HttpAuthExtension extends CompilerExtension
1921
{
2022

21-
/** @var array */
22-
public $defaults = array(
23-
'title' => 'Frontend authentication',
24-
);
23+
public function __construct()
24+
{
25+
$this->config = new class {
26+
/** @var string */
27+
public $title = 'Frontend authentication';
28+
29+
/** @var string|null */
30+
public $username;
31+
32+
/** @var string|null */
33+
public $password;
34+
};
35+
}
2536

2637

27-
public function afterCompile(Nette\PhpGenerator\ClassType $class): void
38+
public function afterCompile(ClassType $class): void
2839
{
29-
$config = $this->getConfig($this->defaults);
40+
$config = $this->config;
3041

31-
if (isset($config['username'], $config['password'])) {
42+
if (isset($config->username, $config->password, $config->title)) {
3243
$initialize = $class->methods['initialize'];
3344

3445
$initialize->addBody('$auth = new HttpAuthExtension\HttpAuthenticator( $this->getByType(\'Nette\Http\IResponse\'), ?, ?, ? );',
35-
array($config['username'], $config['password'], $config['title']));
46+
[$config->username, $config->password, $config->title]);
3647

3748
$initialize->addBody('$auth->run();');
3849
}
3950
}
4051

4152

42-
public static function register(Nette\Configurator $configurator, string $prefix = 'httpAuth'): void
53+
public static function register(Configurator $configurator, string $prefix = 'httpAuth'): void
4354
{
4455
$class = __CLASS__;
4556
$configurator->onCompile[] = static function ($configurator, $compiler) use ($prefix, $class): void {

src/HttpAuthExtension/HttpAuthenticator.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212

1313
namespace HttpAuthExtension;
1414

15-
use Nette;
16-
use Nette\Http;
15+
use Nette\Http\IResponse;
1716

1817

19-
class HttpAuthenticator extends Nette\Object
18+
class HttpAuthenticator
2019
{
2120

22-
/** @var Http\Response */
21+
/** @var IResponse */
2322
private $response;
2423

2524
/** @var string */
@@ -32,7 +31,7 @@ class HttpAuthenticator extends Nette\Object
3231
private $title;
3332

3433

35-
public function __construct(Http\Response $response, string $username, string $password, string $title)
34+
public function __construct(IResponse $response, string $username, string $password, string $title)
3635
{
3736
$this->response = $response;
3837
$this->username = $username;
@@ -45,7 +44,7 @@ public function run(): void
4544
{
4645
if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] !== $this->username || $_SERVER['PHP_AUTH_PW'] !== $this->password) {
4746
$this->response->setHeader('WWW-Authenticate', 'Basic realm="' . $this->title . '"');
48-
$this->response->setCode(Http\IResponse::S401_UNAUTHORIZED);
47+
$this->response->setCode(IResponse::S401_UNAUTHORIZED);
4948
echo '<h1>Authentication failed.</h1>';
5049
die();
5150
}

0 commit comments

Comments
 (0)