Skip to content

Commit 7ebc43a

Browse files
mabarMilan Felix Šulc
authored andcommitted
Nette 3
1 parent b773908 commit 7ebc43a

File tree

10 files changed

+102
-114
lines changed

10 files changed

+102
-114
lines changed

.docs/README.md

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,34 @@ extensions:
2626
curl: Contributte\Http\DI\CurlExtension
2727
```
2828
29-
Extension registers by automatic [`Contributte\Http\Curl\CurlClient`](https://github.com/contributte/http/blob/master/src/Curl/CurlClient.php) as a service.
29+
Extension registers [`Contributte\Http\Curl\CurlClient`](https://github.com/contributte/http/blob/master/src/Curl/CurlClient.php) as a service.
3030

3131
## SAPI
3232

3333
Every modern PHP application needs sometimes to run a few console commands. Let's say sending newsletter campaigns. There is
3434
a tiny problem, there is no request/URL in console/SAPI (Server API) mode. Don't worry, just use our fake request -
3535
`SapiRequestExtension`.
3636

37-
The easiest way is to register extension without any parameters.
38-
3937
```yaml
4038
extensions:
4139
sapi: Contributte\Http\DI\SapiRequestExtension
4240
```
4341

44-
Alternatively, you can pass directly URL address.
45-
46-
```yaml
47-
extensions:
48-
sapi: Contributte\Http\DI\SapiRequestExtension(https://contributte.org)
49-
```
50-
5142
List of all options:
5243

5344
```yaml
5445
sapi:
5546
url: https://contributte.org
5647
# other params
57-
query: NULL
58-
post: NULL
59-
files: NULL
60-
cookies: NULL
61-
headers: NULL
62-
method: NULL
63-
remoteAddress: NULL
64-
remoteHost: NULL
65-
rawBodyCallback: NULL
48+
query: null
49+
post: null
50+
files: null
51+
cookies: null
52+
headers: null
53+
method: null
54+
remoteAddress: null
55+
remoteHost: null
56+
rawBodyCallback: null
6657
```
6758

6859
## Basic Authentication

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ Extra contribution to [`nette/http`](https://github.com/nette/http).
2525

2626
## Versions
2727

28-
| State | Version | Branch | PHP |
29-
|-------------|---------|----------|----------|
30-
| dev | `^0.3` | `master` | `>= 7.1` |
31-
| stable | `^0.2` | `master` | `>= 7.1` |
32-
| stable | `^0.1` | `master` | `>= 5.6` |
28+
| State | Version | Branch | Nette | PHP |
29+
|-------------|---------|----------|-------|---------|
30+
| dev | `^0.4` | `master` | 3.0+ | `^7.2` |
31+
| stable | `^0.3` | `master` | 3.0+ | `^7.2` |
32+
| stable | `^0.2` | `master` | 2.4 | `>=7.1` |
33+
| stable | `^0.1` | `master` | 2.4 | `>=5.6` |
3334

3435
## Maintainers
3536

composer.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
}
2020
],
2121
"require": {
22-
"php": ">= 7.1",
23-
"nette/http": "~2.4.9"
22+
"php": "^7.2",
23+
"nette/http": "~3.0.1"
2424
},
2525
"require-dev": {
26-
"nette/di": "~2.4.13",
26+
"nette/di": "~3.0.0",
2727
"ninjify/nunjuck": "^0.2.0",
2828
"ninjify/qa": "^0.8.0",
2929
"phpstan/extension-installer": "^1.0",
@@ -34,8 +34,7 @@
3434
"tracy/tracy": "~2.5.1"
3535
},
3636
"conflict": {
37-
"nette/di": "<2.4.13",
38-
"nette/utils": "<2.5.2"
37+
"nette/di": "<3.0.0"
3938
},
4039
"suggest": {
4140
"nette/di": "to use CompilerExtensions"
@@ -52,7 +51,7 @@
5251
},
5352
"extra": {
5453
"branch-alias": {
55-
"dev-master": "0.3.x-dev"
54+
"dev-master": "0.4.x-dev"
5655
}
5756
}
5857
}

src/DI/BasicAuthExtension.php

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,59 @@
33
namespace Contributte\Http\DI;
44

55
use Contributte\Http\Auth\BasicAuthenticator;
6-
use LogicException;
76
use Nette\DI\CompilerExtension;
87
use Nette\Http\IRequest;
98
use Nette\Http\IResponse;
109
use Nette\PhpGenerator\ClassType;
11-
use Nette\Utils\Validators;
10+
use Nette\Schema\Expect;
11+
use Nette\Schema\Schema;
12+
use stdClass;
1213

14+
/**
15+
* @property-read stdClass $config
16+
*/
1317
class BasicAuthExtension extends CompilerExtension
1418
{
1519

16-
/** @var mixed[] */
17-
private $defaults = [
18-
'enabled' => false,
19-
'title' => 'Restrict zone',
20-
'users' => [],
21-
];
20+
public function getConfigSchema(): Schema
21+
{
22+
return Expect::structure([
23+
'enabled' => Expect::bool(false),
24+
'title' => Expect::string('Restricted zone'),
25+
'users' => Expect::arrayOf(Expect::structure([
26+
'password' => Expect::string()->required(),
27+
'unsecured' => Expect::bool(false),
28+
]))->min(1),
29+
]);
30+
}
2231

23-
/**
24-
* Register services
25-
*/
2632
public function loadConfiguration(): void
2733
{
2834
$builder = $this->getContainerBuilder();
29-
$config = $this->validateConfig($this->defaults);
30-
31-
Validators::assertField($config, 'enabled', 'bool');
32-
Validators::assertField($config, 'users', 'array');
35+
$config = $this->config;
3336

3437
// Skip if its disabled
35-
if ($config['enabled'] !== true) return;
36-
37-
// Throws if there's no user
38-
if ($config['users'] === []) throw new LogicException('You have to define any user or disable extension');
38+
if (!$config->enabled) {
39+
return;
40+
}
3941

4042
$def = $builder->addDefinition($this->prefix('authenticator'))
4143
->setType(BasicAuthenticator::class)
42-
->setArguments([$config['title']]);
44+
->setArguments([$config->title]);
4345

44-
foreach ($config['users'] as $user => $values) {
45-
if (is_string($values)) {
46-
trigger_error('Usage of `$username => $password` is deprecated, use `$username => ["password" => $password]` instead', E_USER_DEPRECATED);
47-
$password = $values;
48-
$unsecured = true;
49-
} else {
50-
$password = $values['password'];
51-
$unsecured = $values['unsecured'] ?? false;
52-
}
53-
$def->addSetup('addUser', [$user, $password, $unsecured]);
46+
foreach ($config->users as $user => $values) {
47+
$def->addSetup('addUser', [$user, $values->password, $values->unsecured]);
5448
}
5549
}
5650

57-
/**
58-
* Decorate initialize
59-
*/
6051
public function afterCompile(ClassType $class): void
6152
{
62-
$config = $this->validateConfig($this->defaults);
53+
$config = $this->config;
6354

64-
// Skip if its disabled or no user defined
65-
if ($config['enabled'] !== true || $config['users'] === []) return;
55+
// Skip if its disabled
56+
if (!$config->enabled) {
57+
return;
58+
}
6659

6760
$initialize = $class->methods['initialize'];
6861
$initialize->addBody('$this->getService(?)->authenticate($this->getByType(?), $this->getByType(?));', [

src/DI/CurlExtension.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
class CurlExtension extends CompilerExtension
99
{
1010

11-
/**
12-
* Register services
13-
*/
1411
public function loadConfiguration(): void
1512
{
1613
$builder = $this->getContainerBuilder();

src/DI/SapiRequestExtension.php

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,42 @@
33
namespace Contributte\Http\DI;
44

55
use Nette\DI\CompilerExtension;
6-
use Nette\DI\Statement;
6+
use Nette\DI\Definitions\Statement;
7+
use Nette\DI\ServiceDefinition;
78
use Nette\Http\Request;
89
use Nette\Http\UrlScript;
10+
use Nette\Schema\Expect;
11+
use Nette\Schema\Schema;
912
use RuntimeException;
13+
use stdClass;
1014

15+
/**
16+
* @property-read stdClass $config
17+
*/
1118
class SapiRequestExtension extends CompilerExtension
1219
{
1320

14-
/** @var mixed[] */
15-
private $defaults = [
16-
'url' => null,
17-
'query' => null,
18-
'post' => null,
19-
'files' => null,
20-
'cookies' => null,
21-
'headers' => null,
22-
'method' => null,
23-
'remoteAddress' => null,
24-
'remoteHost' => null,
25-
'rawBodyCallback' => null,
26-
];
27-
28-
public function __construct(?string $url = null)
21+
public function getConfigSchema(): Schema
2922
{
30-
if ($url !== null) {
31-
$this->defaults['url'] = $url;
32-
}
23+
return Expect::structure([
24+
'url' => Expect::string()->required(),
25+
'post' => Expect::array(),
26+
'files' => Expect::array(),
27+
'cookies' => Expect::array(),
28+
'headers' => Expect::array(),
29+
'method' => Expect::string(),
30+
'remoteAddress' => Expect::string(),
31+
'remoteHost' => Expect::string(),
32+
'rawBodyCallback' => Expect::mixed(),
33+
]);
3334
}
3435

35-
/**
36-
* Decorate services
37-
*/
3836
public function beforeCompile(): void
3937
{
4038
// Breaks at other mode then CLI
41-
if (PHP_SAPI !== 'cli') return;
39+
if (PHP_SAPI !== 'cli') {
40+
return;
41+
}
4242

4343
$builder = $this->getContainerBuilder();
4444

@@ -47,20 +47,22 @@ public function beforeCompile(): void
4747
throw new RuntimeException('Service http.request is needed');
4848
}
4949

50-
$config = $this->validateConfig($this->defaults);
51-
$builder->getDefinition('http.request')
52-
->setFactory(Request::class, [
53-
new Statement(UrlScript::class, [$config['url']]),
54-
$config['query'],
55-
$config['post'],
56-
$config['files'],
57-
$config['cookies'],
58-
$config['headers'],
59-
$config['method'],
60-
$config['remoteAddress'],
61-
$config['remoteHost'],
62-
$config['rawBodyCallback'],
63-
]);
50+
$requestDefinition = $builder->getDefinition('http.request');
51+
52+
assert($requestDefinition instanceof ServiceDefinition);
53+
54+
$config = $this->config;
55+
$requestDefinition->setFactory(Request::class, [
56+
new Statement(UrlScript::class, [$config->url]),
57+
$config->post,
58+
$config->files,
59+
$config->cookies,
60+
$config->headers,
61+
$config->method,
62+
$config->remoteAddress,
63+
$config->remoteHost,
64+
$config->rawBodyCallback,
65+
]);
6466
}
6567

6668
}

tests/cases/DI/BasicAuthExtension.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use Tester\FileMock;
1414

1515
require_once __DIR__ . '/../../bootstrap.php';
1616

17-
test(function (): void {
17+
test(static function (): void {
1818
$loader = new ContainerLoader(TEMP_DIR, true);
19-
$class = $loader->load(function (Compiler $compiler): void {
19+
$class = $loader->load(static function (Compiler $compiler): void {
2020
$compiler->addExtension('auth', new BasicAuthExtension());
2121
$compiler->loadConfig(FileMock::create('
2222
auth:

tests/cases/DI/CurlExtension.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use Tester\Assert;
1414

1515
require_once __DIR__ . '/../../bootstrap.php';
1616

17-
test(function (): void {
17+
test(static function (): void {
1818
$loader = new ContainerLoader(TEMP_DIR, true);
19-
$class = $loader->load(function (Compiler $compiler): void {
19+
$class = $loader->load(static function (Compiler $compiler): void {
2020
$compiler->addExtension('curl', new CurlExtension());
2121
}, 1);
2222

tests/cases/DI/SapiRequestExtension.phpt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ use Tester\Assert;
1414

1515
require_once __DIR__ . '/../../bootstrap.php';
1616

17-
test(function (): void {
17+
test(static function (): void {
1818
$loader = new ContainerLoader(TEMP_DIR, true);
19-
$class = $loader->load(function (Compiler $compiler): void {
19+
$class = $loader->load(static function (Compiler $compiler): void {
20+
$compiler->addConfig([
21+
'sapi' => [
22+
'url' => 'https://contributte.org',
23+
],
24+
]);
2025
$compiler->addExtension('http', new HttpExtension(true));
21-
$compiler->addExtension('sapi', new SapiRequestExtension('https://contributte.org'));
26+
$compiler->addExtension('sapi', new SapiRequestExtension());
2227
}, 1);
2328

2429
/** @var Container $container */

tests/cases/Url.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use Tester\Assert;
99

1010
require_once __DIR__ . '/../bootstrap.php';
1111

12-
test(function (): void {
12+
test(static function (): void {
1313
$url = new Url('https://github.com');
1414
Assert::equal('https://github.com/', (string) $url);
1515

0 commit comments

Comments
 (0)