Skip to content

Commit b18dbdc

Browse files
committed
Add nette/di 3.1 and 3.2 support
- Fix MigrationsExtension to accept Nette\DI\Statement for dynamic container parameters (nette/di 3.1+ uses Statement instead of PhpLiteral for dynamic parameters) - Return service reference strings instead of ServiceDefinition objects from definition methods (required for nette/di 3.2 compatibility) - Suppress E_USER_DEPRECATED during container compilation in tests to handle deprecation notices from nette/di and doctrine/orm - Replace deprecated `(...)` syntax with autowiring-compatible alternatives in test config files - Add nette-3.1.sh (PHP 8.0-8.3) and nette-3.2.sh (PHP 8.1-8.4) matrix files
1 parent cb0e39e commit b18dbdc

7 files changed

Lines changed: 75 additions & 50 deletions

File tree

src/Bridges/NetteDI/MigrationsExtension.php

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function loadConfiguration(): void
8989

9090
// groups
9191
if ($config['groups'] === null) {
92-
Validators::assertField($config, 'dir', 'string|Nette\PhpGenerator\PhpLiteral');
92+
Validators::assertField($config, 'dir', 'string|Nette\PhpGenerator\PhpLiteral|Nette\DI\Statement');
9393
Validators::assertField($config, 'withDummyData', 'bool');
9494
$config['groups'] = $this->createDefaultGroupConfiguration($config['dir'], $config['withDummyData']);
9595
}
@@ -164,18 +164,19 @@ public function beforeCompile(): void
164164

165165
/**
166166
* @param null|string|Statement $dbal
167-
* @return string|ServiceDefinition
168167
*/
169-
private function getDbalDefinition($dbal)
168+
private function getDbalDefinition($dbal): string
170169
{
171170
$factory = $this->getDbalFactory($dbal);
172171

173172
if ($factory) {
174-
return $this->getContainerBuilder()
173+
$this->getContainerBuilder()
175174
->addDefinition($this->prefix('dbal'))
176175
->setType(Nextras\Migrations\IDbal::class)
177176
->setFactory($factory);
178177

178+
return '@' . $this->prefix('dbal');
179+
179180
} elseif ($dbal === null) {
180181
return '@Nextras\Migrations\IDbal';
181182

@@ -207,20 +208,20 @@ private function getDbalFactory($dbal)
207208

208209

209210
/**
210-
* @param null|string|Statement $driver
211-
* @param string|ServiceDefinition $dbal
212-
* @return string|ServiceDefinition
211+
* @param null|string|Statement $driver
213212
*/
214-
private function getDriverDefinition($driver, $dbal)
213+
private function getDriverDefinition($driver, string $dbal): string
215214
{
216215
$factory = $this->getDriverFactory($driver, $dbal);
217216

218217
if ($factory) {
219-
return $this->getContainerBuilder()
218+
$this->getContainerBuilder()
220219
->addDefinition($this->prefix('driver'))
221220
->setType(Nextras\Migrations\IDriver::class)
222221
->setFactory($factory);
223222

223+
return '@' . $this->prefix('driver');
224+
224225
} elseif ($driver === null) {
225226
return '@Nextras\Migrations\IDriver';
226227

@@ -231,11 +232,10 @@ private function getDriverDefinition($driver, $dbal)
231232

232233

233234
/**
234-
* @param null|string|Statement $driver
235-
* @param string|ServiceDefinition $dbal
235+
* @param null|string|Statement $driver
236236
* @return string|Statement|null
237237
*/
238-
private function getDriverFactory($driver, $dbal)
238+
private function getDriverFactory($driver, string $dbal)
239239
{
240240
if ($driver instanceof Statement) {
241241
return $this->filterArguments([$driver])[0];
@@ -251,18 +251,19 @@ private function getDriverFactory($driver, $dbal)
251251

252252
/**
253253
* @param null|string|Statement $printer
254-
* @return string|ServiceDefinition
255254
*/
256-
private function getPrinterDefinition($printer)
255+
private function getPrinterDefinition($printer): string
257256
{
258257
$factory = $this->getPrinterFactory($printer);
259258

260259
if ($factory) {
261-
return $this->getContainerBuilder()
260+
$this->getContainerBuilder()
262261
->addDefinition($this->prefix('printer'))
263262
->setType(Nextras\Migrations\IPrinter::class)
264263
->setFactory($factory);
265264

265+
return '@' . $this->prefix('printer');
266+
266267
} elseif ($printer === null) {
267268
return '@Nextras\Migrations\IPrinter';
268269

@@ -294,12 +295,17 @@ private function getPrinterFactory($printer)
294295

295296

296297
/**
297-
* @param string|Nette\PhpGenerator\PhpLiteral $dir
298+
* @param string|Nette\PhpGenerator\PhpLiteral|Statement $dir
298299
* @return array<string, array{enabled?: bool, directory: string, dependencies?: list<string>, generator?: ServiceDefinition|null}>
299300
*/
300301
private function createDefaultGroupConfiguration($dir, bool $withDummyData): array
301302
{
302-
if ($dir instanceof Nette\PhpGenerator\PhpLiteral) {
303+
if ($dir instanceof Statement) {
304+
$append = function (string $path) use ($dir): Statement {
305+
return new Statement('? . ?', [$dir, $path]);
306+
};
307+
308+
} elseif ($dir instanceof Nette\PhpGenerator\PhpLiteral) {
303309
$append = function (string $path) use ($dir): Nette\PhpGenerator\PhpLiteral {
304310
return ContainerBuilder::literal('? . ?', [$dir, $path]);
305311
};
@@ -347,7 +353,7 @@ private function createGroupDefinitions(array $groups): array
347353
$groupDefinitions = [];
348354

349355
foreach ($groups as $groupName => $groupConfig) {
350-
Validators::assertField($groupConfig, 'directory', 'string|Nette\PhpGenerator\PhpLiteral');
356+
Validators::assertField($groupConfig, 'directory', 'string|Nette\PhpGenerator\PhpLiteral|Nette\DI\Statement');
351357

352358
$enabled = isset($groupConfig['enabled']) ? $groupConfig['enabled'] : true;
353359
$directory = $groupConfig['directory'];
@@ -371,11 +377,10 @@ private function createGroupDefinitions(array $groups): array
371377

372378

373379
/**
374-
* @param string|ServiceDefinition $driver
375-
* @param array<string, mixed> $phpParams
380+
* @param array<string, mixed> $phpParams
376381
* @return list<ServiceDefinition>
377382
*/
378-
private function createExtensionHandlerDefinitions($driver, array $phpParams): array
383+
private function createExtensionHandlerDefinitions(string $driver, array $phpParams): array
379384
{
380385
$builder = $this->getContainerBuilder();
381386

@@ -395,12 +400,14 @@ private function createExtensionHandlerDefinitions($driver, array $phpParams): a
395400
}
396401

397402

398-
private function createConfigurationDefinition(): ServiceDefinition
403+
private function createConfigurationDefinition(): string
399404
{
400-
return $this->getContainerBuilder()
405+
$this->getContainerBuilder()
401406
->addDefinition($this->prefix('configuration'))
402407
->setType(Nextras\Migrations\IConfiguration::class)
403408
->setFactory(Nextras\Migrations\Configurations\Configuration::class);
409+
410+
return '@' . $this->prefix('configuration');
404411
}
405412

406413

@@ -416,12 +423,7 @@ private function createDoctrineStructureDiffGeneratorDefinition(?string $ignored
416423
}
417424

418425

419-
/**
420-
* @param string|ServiceDefinition $driver
421-
* @param string|ServiceDefinition $configuration
422-
* @param string|ServiceDefinition $printer
423-
*/
424-
private function createSymfonyCommandDefinitions($driver, $configuration, $printer): void
426+
private function createSymfonyCommandDefinitions(string $driver, string $configuration, string $printer): void
425427
{
426428
$builder = $this->getContainerBuilder();
427429
$builder->addExcludedClasses([Nextras\Migrations\Bridges\SymfonyConsole\BaseCommand::class]);

tests/cases/integration/nette-di/MigrationsExtension.configC.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ migrations:
77

88
services:
99
- Dibi\Connection(%dibiConfig%)
10-
- Nextras\Migrations\Bridges\Dibi\DibiAdapter(...)
10+
- Nextras\Migrations\Bridges\Dibi\DibiAdapter

tests/cases/integration/nette-di/MigrationsExtension.configD.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ extensions:
33

44
migrations:
55
dir: migrations
6-
driver: Nextras\Migrations\Drivers\MySqlDriver(...)
6+
driver: Nextras\Migrations\Drivers\MySqlDriver(@Nextras\Migrations\IDbal)
77

88
services:
99
- Dibi\Connection(%dibiConfig%)
10-
- Nextras\Migrations\Bridges\Dibi\DibiAdapter(...)
10+
- Nextras\Migrations\Bridges\Dibi\DibiAdapter

tests/cases/integration/nette-di/MigrationsExtension.configE.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ migrations:
77
services:
88
- Dibi\Connection(%dibiConfig%)
99
- Nextras\Migrations\Drivers\MySqlDriver
10-
- Nextras\Migrations\Bridges\Dibi\DibiAdapter(...)
10+
- Nextras\Migrations\Bridges\Dibi\DibiAdapter

tests/cases/integration/nette-di/MigrationsExtension.phpt

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,23 +135,28 @@ class MigrationsExtensionTest extends TestCase
135135

136136
$loader = new Nette\DI\ContainerLoader(TEMP_DIR);
137137
$key = __FILE__ . ':' . __LINE__ . ':' . $config;
138-
$className = $loader->load(
139-
function (Nette\DI\Compiler $compiler) use ($config, $dibiConfig, $doctrineConfig, $dynamicParameters) {
140-
$compiler->addExtension('extensions', new Nette\DI\Extensions\ExtensionsExtension());
141-
$compiler->addConfig([
142-
'parameters' => [
143-
'dibiConfig' => $dibiConfig,
144-
'doctrineConfig' => $doctrineConfig,
145-
'doctrineDir' => __DIR__ . '/../../../fixtures/doctrine',
146-
]
147-
]);
148-
$compiler->loadConfig(__DIR__ . "/MigrationsExtension.$config.neon");
149-
if ($dynamicParameters !== null) {
150-
$compiler->setDynamicParameterNames(array_keys($dynamicParameters));
151-
}
152-
},
153-
$key
154-
);
138+
$previousLevel = error_reporting(E_ALL & ~E_USER_DEPRECATED);
139+
try {
140+
$className = $loader->load(
141+
function (Nette\DI\Compiler $compiler) use ($config, $dibiConfig, $doctrineConfig, $dynamicParameters) {
142+
$compiler->addExtension('extensions', new Nette\DI\Extensions\ExtensionsExtension());
143+
$compiler->addConfig([
144+
'parameters' => [
145+
'dibiConfig' => $dibiConfig,
146+
'doctrineConfig' => $doctrineConfig,
147+
'doctrineDir' => __DIR__ . '/../../../fixtures/doctrine',
148+
]
149+
]);
150+
$compiler->loadConfig(__DIR__ . "/MigrationsExtension.$config.neon");
151+
if ($dynamicParameters !== null) {
152+
$compiler->setDynamicParameterNames(array_keys($dynamicParameters));
153+
}
154+
},
155+
$key
156+
);
157+
} finally {
158+
error_reporting($previousLevel);
159+
}
155160

156161
return new $className($dynamicParameters ?: []);
157162
}

tests/matrix/nette-di/nette-3.1.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
PHP_VERSION_MIN="80000"
3+
PHP_VERSION_MAX="80399"
4+
COMPOSER_REQUIRE="$COMPOSER_REQUIRE nette/di:3.1.*"
5+
COMPOSER_REQUIRE="$COMPOSER_REQUIRE dibi/dibi:*"
6+
COMPOSER_REQUIRE="$COMPOSER_REQUIRE doctrine/dbal:^2.13|^3.0"
7+
COMPOSER_REQUIRE="$COMPOSER_REQUIRE doctrine/orm:^2.11"
8+
COMPOSER_REQUIRE="$COMPOSER_REQUIRE doctrine/cache:^1.11"
9+
COMPOSER_REQUIRE="$COMPOSER_REQUIRE symfony/console:*"

tests/matrix/nette-di/nette-3.2.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
PHP_VERSION_MIN="80100"
3+
PHP_VERSION_MAX="80499"
4+
COMPOSER_REQUIRE="$COMPOSER_REQUIRE nette/di:3.2.*"
5+
COMPOSER_REQUIRE="$COMPOSER_REQUIRE dibi/dibi:*"
6+
COMPOSER_REQUIRE="$COMPOSER_REQUIRE doctrine/dbal:^2.13|^3.0"
7+
COMPOSER_REQUIRE="$COMPOSER_REQUIRE doctrine/orm:^2.11"
8+
COMPOSER_REQUIRE="$COMPOSER_REQUIRE doctrine/cache:^1.11"
9+
COMPOSER_REQUIRE="$COMPOSER_REQUIRE symfony/console:*"

0 commit comments

Comments
 (0)