-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHttpAuthExtension.php
More file actions
62 lines (44 loc) · 1.35 KB
/
HttpAuthExtension.php
File metadata and controls
62 lines (44 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types = 1);
/**
* This file is part of the HttpAuthExtension package
*
* @license MIT
* @author Petr Kessler (https://kesspess.cz)
* @link https://github.com/uestla/HttpAuthExtension
*/
namespace HttpAuthExtension;
use Nette\Configurator;
use Nette\DI\CompilerExtension;
use Nette\PhpGenerator\ClassType;
final class HttpAuthExtension extends CompilerExtension
{
public function __construct()
{
$this->config = new class {
/** @var string */
public $title = 'Frontend authentication';
/** @var string|null */
public $username;
/** @var string|null */
public $password;
};
}
public function afterCompile(ClassType $class): void
{
$config = $this->config;
if (isset($config->username, $config->password, $config->title)) {
$initialize = $class->getMethod('initialize');
$initialize->addBody('$auth = new HttpAuthExtension\HttpAuthenticator( $this->getByType(\'Nette\Http\IResponse\'), ?, ?, ? );',
[$config->username, $config->password, $config->title]);
$initialize->addBody('$auth->run();');
}
}
public static function register(Configurator $configurator, string $prefix = 'httpAuth'): void
{
$class = __CLASS__;
$configurator->onCompile[] = static function ($configurator, $compiler) use ($prefix, $class): void {
$compiler->addExtension($prefix, new $class);
};
}
}