@@ -1258,13 +1258,17 @@ class $class extends $baseClass
1258
1258
{
1259
1259
private const DEPRECATED_PARAMETERS = [];
1260
1260
1261
+ private const NONEMPTY_PARAMETERS = [];
1262
+
1261
1263
protected \$parameters = [];
1262
1264
1263
1265
public function __construct()
1264
1266
{
1265
1267
1266
1268
EOF ;
1267
1269
$ code = str_replace (" private const DEPRECATED_PARAMETERS = []; \n\n" , $ this ->addDeprecatedParameters (), $ code );
1270
+ $ code = str_replace (" private const NONEMPTY_PARAMETERS = []; \n\n" , $ this ->addNonEmptyParameters (), $ code );
1271
+
1268
1272
if ($ this ->asFiles ) {
1269
1273
$ code = str_replace ('__construct() ' , '__construct(private array $buildParameters = [], protected string $containerDir = __DIR__) ' , $ code );
1270
1274
@@ -1429,6 +1433,24 @@ private function addDeprecatedParameters(): string
1429
1433
return " private const DEPRECATED_PARAMETERS = [ \n{$ code } ]; \n\n" ;
1430
1434
}
1431
1435
1436
+ private function addNonEmptyParameters (): string
1437
+ {
1438
+ if (!($ bag = $ this ->container ->getParameterBag ()) instanceof ParameterBag) {
1439
+ return '' ;
1440
+ }
1441
+
1442
+ if (!$ nonEmpty = $ bag ->allNonEmpty ()) {
1443
+ return '' ;
1444
+ }
1445
+ $ code = '' ;
1446
+ ksort ($ nonEmpty );
1447
+ foreach ($ nonEmpty as $ param => $ message ) {
1448
+ $ code .= ' ' .$ this ->doExport ($ param ).' => ' .$ this ->doExport ($ message ).", \n" ;
1449
+ }
1450
+
1451
+ return " private const NONEMPTY_PARAMETERS = [ \n{$ code } ]; \n\n" ;
1452
+ }
1453
+
1432
1454
private function addMethodMap (): string
1433
1455
{
1434
1456
$ code = '' ;
@@ -1563,14 +1585,16 @@ private function addInlineRequires(bool $hasProxyClasses): string
1563
1585
1564
1586
private function addDefaultParametersMethod (): string
1565
1587
{
1566
- if (!$ this ->container ->getParameterBag ()->all ()) {
1588
+ $ bag = $ this ->container ->getParameterBag ();
1589
+
1590
+ if (!$ bag ->all () && (!$ bag instanceof ParameterBag || !$ bag ->allNonEmpty ())) {
1567
1591
return '' ;
1568
1592
}
1569
1593
1570
1594
$ php = [];
1571
1595
$ dynamicPhp = [];
1572
1596
1573
- foreach ($ this -> container -> getParameterBag () ->all () as $ key => $ value ) {
1597
+ foreach ($ bag ->all () as $ key => $ value ) {
1574
1598
if ($ key !== $ resolvedKey = $ this ->container ->resolveEnvPlaceholders ($ key )) {
1575
1599
throw new InvalidArgumentException (\sprintf ('Parameter name cannot use env parameters: "%s". ' , $ resolvedKey ));
1576
1600
}
@@ -1600,13 +1624,20 @@ public function getParameter(string $name): array|bool|string|int|float|\UnitEnu
1600
1624
}
1601
1625
1602
1626
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters))) {
1603
- throw new ParameterNotFoundException($name);
1627
+ throw new ParameterNotFoundException($name, extraMessage: self::NONEMPTY_PARAMETERS[$name] ?? null );
1604
1628
}
1629
+
1605
1630
if (isset($this->loadedDynamicParameters[$name])) {
1606
- return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
1631
+ $value = $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
1632
+ } else {
1633
+ $value = $this->parameters[$name];
1634
+ }
1635
+
1636
+ if (isset(self::NONEMPTY_PARAMETERS[$name]) && empty($value)) {
1637
+ throw new \Symfony\Component\DependencyInjection\Exception\EmptyParameterValueException(self::NONEMPTY_PARAMETERS[$name]);
1607
1638
}
1608
1639
1609
- return $this->parameters[$name] ;
1640
+ return $value ;
1610
1641
}
1611
1642
1612
1643
public function hasParameter(string $name): bool
@@ -1633,7 +1664,7 @@ public function getParameterBag(): ParameterBagInterface
1633
1664
foreach ($this->buildParameters as $name => $value) {
1634
1665
$parameters[$name] = $value;
1635
1666
}
1636
- $this->parameterBag = new FrozenParameterBag($parameters, self::DEPRECATED_PARAMETERS);
1667
+ $this->parameterBag = new FrozenParameterBag($parameters, self::DEPRECATED_PARAMETERS, self::NONEMPTY_PARAMETERS );
1637
1668
}
1638
1669
1639
1670
return $this->parameterBag;
@@ -1645,9 +1676,15 @@ public function getParameterBag(): ParameterBagInterface
1645
1676
$ code = preg_replace ('/^.*buildParameters.*\n.*\n.*\n\n?/m ' , '' , $ code );
1646
1677
}
1647
1678
1648
- if (!( $ bag = $ this -> container -> getParameterBag ()) instanceof ParameterBag || !$ bag ->allDeprecated ()) {
1679
+ if (!$ bag instanceof ParameterBag || !$ bag ->allDeprecated ()) {
1649
1680
$ code = preg_replace ("/ \n.*DEPRECATED_PARAMETERS.* \n.* \n.* \n/m " , '' , $ code , 1 );
1650
- $ code = str_replace (', self::DEPRECATED_PARAMETERS ' , '' , $ code );
1681
+ $ code = str_replace (', self::DEPRECATED_PARAMETERS ' , ', [] ' , $ code );
1682
+ }
1683
+
1684
+ if (!$ bag instanceof ParameterBag || !$ bag ->allNonEmpty ()) {
1685
+ $ code = str_replace (', extraMessage: self::NONEMPTY_PARAMETERS[$name] ?? null ' , '' , $ code );
1686
+ $ code = str_replace (', self::NONEMPTY_PARAMETERS ' , '' , $ code );
1687
+ $ code = preg_replace ("/ \n.*NONEMPTY_PARAMETERS.* \n.* \n.* \n/m " , '' , $ code , 1 );
1651
1688
}
1652
1689
1653
1690
if ($ dynamicPhp ) {
0 commit comments