comparison vendor/symfony/dependency-injection/Dumper/PhpDumper.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children 1fec387a4317
comparison
equal deleted inserted replaced
11:bfffd8d7479a 12:7a779792577d
164 if (!$use) { 164 if (!$use) {
165 $unusedEnvs[] = $env; 165 $unusedEnvs[] = $env;
166 } 166 }
167 } 167 }
168 if ($unusedEnvs) { 168 if ($unusedEnvs) {
169 throw new EnvParameterException($unusedEnvs); 169 throw new EnvParameterException($unusedEnvs, null, 'Environment variables "%s" are never used. Please, check your container\'s configuration.');
170 } 170 }
171 171
172 return $code; 172 return $code;
173 } 173 }
174 174
265 } 265 }
266 266
267 /** 267 /**
268 * Generates the require_once statement for service includes. 268 * Generates the require_once statement for service includes.
269 * 269 *
270 * @param string $id The service id
271 * @param Definition $definition 270 * @param Definition $definition
272 * 271 *
273 * @return string 272 * @return string
274 */ 273 */
275 private function addServiceInclude($id, $definition) 274 private function addServiceInclude($definition)
276 { 275 {
277 $template = " require_once %s;\n"; 276 $template = " require_once %s;\n";
278 $code = ''; 277 $code = '';
279 278
280 if (null !== $file = $definition->getFile()) { 279 if (null !== $file = $definition->getFile()) {
345 } 344 }
346 345
347 $code .= $this->addNewInstance($sDefinition, '$'.$name, ' = ', $id); 346 $code .= $this->addNewInstance($sDefinition, '$'.$name, ' = ', $id);
348 347
349 if (!$this->hasReference($id, $sDefinition->getMethodCalls(), true) && !$this->hasReference($id, $sDefinition->getProperties(), true)) { 348 if (!$this->hasReference($id, $sDefinition->getMethodCalls(), true) && !$this->hasReference($id, $sDefinition->getProperties(), true)) {
350 $code .= $this->addServiceProperties(null, $sDefinition, $name); 349 $code .= $this->addServiceProperties($sDefinition, $name);
351 $code .= $this->addServiceMethodCalls(null, $sDefinition, $name); 350 $code .= $this->addServiceMethodCalls($sDefinition, $name);
352 $code .= $this->addServiceConfigurator(null, $sDefinition, $name); 351 $code .= $this->addServiceConfigurator($sDefinition, $name);
353 } 352 }
354 353
355 $code .= "\n"; 354 $code .= "\n";
356 } 355 }
357 } 356 }
387 * @throws InvalidArgumentException 386 * @throws InvalidArgumentException
388 * @throws RuntimeException 387 * @throws RuntimeException
389 */ 388 */
390 private function addServiceInstance($id, Definition $definition) 389 private function addServiceInstance($id, Definition $definition)
391 { 390 {
392 $class = $definition->getClass(); 391 $class = $this->dumpValue($definition->getClass());
393 392
394 if ('\\' === substr($class, 0, 1)) { 393 if (0 === strpos($class, "'") && false === strpos($class, '$') && !preg_match('/^\'(?:\\\{2})?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
395 $class = substr($class, 1);
396 }
397
398 $class = $this->dumpValue($class);
399
400 if (0 === strpos($class, "'") && false === strpos($class, '$') && !preg_match('/^\'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
401 throw new InvalidArgumentException(sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id)); 394 throw new InvalidArgumentException(sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id));
402 } 395 }
403 396
404 $simple = $this->isSimpleInstance($id, $definition); 397 $simple = $this->isSimpleInstance($id, $definition);
405 $isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition); 398 $isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition);
451 } 444 }
452 445
453 /** 446 /**
454 * Adds method calls to a service definition. 447 * Adds method calls to a service definition.
455 * 448 *
456 * @param string $id
457 * @param Definition $definition 449 * @param Definition $definition
458 * @param string $variableName 450 * @param string $variableName
459 * 451 *
460 * @return string 452 * @return string
461 */ 453 */
462 private function addServiceMethodCalls($id, Definition $definition, $variableName = 'instance') 454 private function addServiceMethodCalls(Definition $definition, $variableName = 'instance')
463 { 455 {
464 $calls = ''; 456 $calls = '';
465 foreach ($definition->getMethodCalls() as $call) { 457 foreach ($definition->getMethodCalls() as $call) {
466 $arguments = array(); 458 $arguments = array();
467 foreach ($call[1] as $value) { 459 foreach ($call[1] as $value) {
472 } 464 }
473 465
474 return $calls; 466 return $calls;
475 } 467 }
476 468
477 private function addServiceProperties($id, Definition $definition, $variableName = 'instance') 469 private function addServiceProperties(Definition $definition, $variableName = 'instance')
478 { 470 {
479 $code = ''; 471 $code = '';
480 foreach ($definition->getProperties() as $name => $value) { 472 foreach ($definition->getProperties() as $name => $value) {
481 $code .= sprintf(" \$%s->%s = %s;\n", $variableName, $name, $this->dumpValue($value)); 473 $code .= sprintf(" \$%s->%s = %s;\n", $variableName, $name, $this->dumpValue($value));
482 } 474 }
515 if ($this->isSimpleInstance($id, $definition)) { 507 if ($this->isSimpleInstance($id, $definition)) {
516 throw new ServiceCircularReferenceException($id, array($id)); 508 throw new ServiceCircularReferenceException($id, array($id));
517 } 509 }
518 510
519 $name = (string) $this->definitionVariables->offsetGet($iDefinition); 511 $name = (string) $this->definitionVariables->offsetGet($iDefinition);
520 $code .= $this->addServiceProperties(null, $iDefinition, $name); 512 $code .= $this->addServiceProperties($iDefinition, $name);
521 $code .= $this->addServiceMethodCalls(null, $iDefinition, $name); 513 $code .= $this->addServiceMethodCalls($iDefinition, $name);
522 $code .= $this->addServiceConfigurator(null, $iDefinition, $name); 514 $code .= $this->addServiceConfigurator($iDefinition, $name);
523 } 515 }
524 516
525 if ('' !== $code) { 517 if ('' !== $code) {
526 $code .= "\n"; 518 $code .= "\n";
527 } 519 }
530 } 522 }
531 523
532 /** 524 /**
533 * Adds configurator definition. 525 * Adds configurator definition.
534 * 526 *
535 * @param string $id
536 * @param Definition $definition 527 * @param Definition $definition
537 * @param string $variableName 528 * @param string $variableName
538 * 529 *
539 * @return string 530 * @return string
540 */ 531 */
541 private function addServiceConfigurator($id, Definition $definition, $variableName = 'instance') 532 private function addServiceConfigurator(Definition $definition, $variableName = 'instance')
542 { 533 {
543 if (!$callable = $definition->getConfigurator()) { 534 if (!$callable = $definition->getConfigurator()) {
544 return ''; 535 return '';
545 } 536 }
546 537
584 575
585 if ($definition->isSynthetic()) { 576 if ($definition->isSynthetic()) {
586 $return[] = '@throws RuntimeException always since this service is expected to be injected dynamically'; 577 $return[] = '@throws RuntimeException always since this service is expected to be injected dynamically';
587 } elseif ($class = $definition->getClass()) { 578 } elseif ($class = $definition->getClass()) {
588 $class = $this->container->resolveEnvPlaceholders($class); 579 $class = $this->container->resolveEnvPlaceholders($class);
589 $return[] = sprintf('@return %s A %s instance', 0 === strpos($class, '%') ? 'object' : '\\'.ltrim($class, '\\'), ltrim($class, '\\')); 580 $return[] = sprintf(0 === strpos($class, '%') ? '@return object A %1$s instance' : '@return \%s', ltrim($class, '\\'));
590 } elseif ($definition->getFactory()) { 581 } elseif ($definition->getFactory()) {
591 $factory = $definition->getFactory(); 582 $factory = $definition->getFactory();
592 if (is_string($factory)) { 583 if (is_string($factory)) {
593 $return[] = sprintf('@return object An instance returned by %s()', $factory); 584 $return[] = sprintf('@return object An instance returned by %s()', $factory);
594 } elseif (is_array($factory) && (is_string($factory[0]) || $factory[0] instanceof Definition || $factory[0] instanceof Reference)) { 585 } elseif (is_array($factory) && (is_string($factory[0]) || $factory[0] instanceof Definition || $factory[0] instanceof Reference)) {
609 } 600 }
610 601
611 $return = str_replace("\n * \n", "\n *\n", implode("\n * ", $return)); 602 $return = str_replace("\n * \n", "\n *\n", implode("\n * ", $return));
612 $return = $this->container->resolveEnvPlaceholders($return); 603 $return = $this->container->resolveEnvPlaceholders($return);
613 604
614 $doc = ''; 605 $shared = $definition->isShared() ? ' shared' : '';
615 if ($definition->isShared()) { 606 $public = $definition->isPublic() ? 'public' : 'private';
616 $doc .= <<<'EOF' 607 $autowired = $definition->isAutowired() ? ' autowired' : '';
617
618 *
619 * This service is shared.
620 * This method always returns the same instance of the service.
621 EOF;
622 }
623
624 if (!$definition->isPublic()) {
625 $doc .= <<<'EOF'
626
627 *
628 * This service is private.
629 * If you want to be able to request this service from the container directly,
630 * make it public, otherwise you might end up with broken code.
631 EOF;
632 }
633
634 if ($definition->isAutowired()) {
635 $doc .= <<<EOF
636
637 *
638 * This service is autowired.
639 EOF;
640 }
641 608
642 if ($definition->isLazy()) { 609 if ($definition->isLazy()) {
643 $lazyInitialization = '$lazyLoad = true'; 610 $lazyInitialization = '$lazyLoad = true';
644 $lazyInitializationDoc = "\n * @param bool \$lazyLoad whether to try lazy-loading the service with a proxy\n *";
645 } else { 611 } else {
646 $lazyInitialization = ''; 612 $lazyInitialization = '';
647 $lazyInitializationDoc = '';
648 } 613 }
649 614
650 // with proxies, for 5.3.3 compatibility, the getter must be public to be accessible to the initializer 615 // with proxies, for 5.3.3 compatibility, the getter must be public to be accessible to the initializer
651 $isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition); 616 $isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition);
652 $visibility = $isProxyCandidate ? 'public' : 'protected'; 617 $visibility = $isProxyCandidate ? 'public' : 'protected';
653 $methodName = $this->generateMethodName($id); 618 $methodName = $this->generateMethodName($id);
654 $code = <<<EOF 619 $code = <<<EOF
655 620
656 /*{$this->docStar} 621 /*{$this->docStar}
657 * Gets the '$id' service.$doc 622 * Gets the $public '$id'$shared$autowired service.
658 *$lazyInitializationDoc 623 *
659 * $return 624 * $return
660 */ 625 */
661 {$visibility} function {$methodName}($lazyInitialization) 626 {$visibility} function {$methodName}($lazyInitialization)
662 { 627 {
663 628
671 if ($definition->isDeprecated()) { 636 if ($definition->isDeprecated()) {
672 $code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", $this->export($definition->getDeprecationMessage($id))); 637 $code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", $this->export($definition->getDeprecationMessage($id)));
673 } 638 }
674 639
675 $code .= 640 $code .=
676 $this->addServiceInclude($id, $definition). 641 $this->addServiceInclude($definition).
677 $this->addServiceLocalTempVariables($id, $definition). 642 $this->addServiceLocalTempVariables($id, $definition).
678 $this->addServiceInlinedDefinitions($id, $definition). 643 $this->addServiceInlinedDefinitions($id, $definition).
679 $this->addServiceInstance($id, $definition). 644 $this->addServiceInstance($id, $definition).
680 $this->addServiceInlinedDefinitionsSetup($id, $definition). 645 $this->addServiceInlinedDefinitionsSetup($id, $definition).
681 $this->addServiceProperties($id, $definition). 646 $this->addServiceProperties($definition).
682 $this->addServiceMethodCalls($id, $definition). 647 $this->addServiceMethodCalls($definition).
683 $this->addServiceConfigurator($id, $definition). 648 $this->addServiceConfigurator($definition).
684 $this->addServiceReturn($id, $definition) 649 $this->addServiceReturn($id, $definition)
685 ; 650 ;
686 } 651 }
687 652
688 $this->definitionVariables = null; 653 $this->definitionVariables = null;
770 * @return string 735 * @return string
771 */ 736 */
772 private function startClass($class, $baseClass, $namespace) 737 private function startClass($class, $baseClass, $namespace)
773 { 738 {
774 $bagClass = $this->container->isFrozen() ? 'use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;' : 'use Symfony\Component\DependencyInjection\ParameterBag\\ParameterBag;'; 739 $bagClass = $this->container->isFrozen() ? 'use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;' : 'use Symfony\Component\DependencyInjection\ParameterBag\\ParameterBag;';
775 $namespaceLine = $namespace ? "namespace $namespace;\n" : ''; 740 $namespaceLine = $namespace ? "\nnamespace $namespace;\n" : '';
776 741
777 return <<<EOF 742 return <<<EOF
778 <?php 743 <?php
779 $namespaceLine 744 $namespaceLine
780 use Symfony\Component\DependencyInjection\ContainerInterface; 745 use Symfony\Component\DependencyInjection\ContainerInterface;
1189 return $code; 1154 return $code;
1190 } 1155 }
1191 1156
1192 $conditions = array(); 1157 $conditions = array();
1193 foreach ($services as $service) { 1158 foreach ($services as $service) {
1159 if ($this->container->hasDefinition($service) && !$this->container->getDefinition($service)->isPublic()) {
1160 continue;
1161 }
1162
1194 $conditions[] = sprintf("\$this->has('%s')", $service); 1163 $conditions[] = sprintf("\$this->has('%s')", $service);
1164 }
1165
1166 if (!$conditions) {
1167 return $code;
1195 } 1168 }
1196 1169
1197 // re-indent the wrapped code 1170 // re-indent the wrapped code
1198 $code = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $code))); 1171 $code = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $code)));
1199 1172
1443 private function dumpLiteralClass($class) 1416 private function dumpLiteralClass($class)
1444 { 1417 {
1445 if (false !== strpos($class, '$')) { 1418 if (false !== strpos($class, '$')) {
1446 return sprintf('${($_ = %s) && false ?: "_"}', $class); 1419 return sprintf('${($_ = %s) && false ?: "_"}', $class);
1447 } 1420 }
1448 if (0 !== strpos($class, "'") || !preg_match('/^\'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) { 1421 if (0 !== strpos($class, "'") || !preg_match('/^\'(?:\\\{2})?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
1449 throw new RuntimeException(sprintf('Cannot dump definition because of invalid class name (%s)', $class ?: 'n/a')); 1422 throw new RuntimeException(sprintf('Cannot dump definition because of invalid class name (%s)', $class ?: 'n/a'));
1450 } 1423 }
1451 1424
1452 return '\\'.substr(str_replace('\\\\', '\\', $class), 1, -1); 1425 $class = substr(str_replace('\\\\', '\\', $class), 1, -1);
1426
1427 return 0 === strpos($class, '\\') ? $class : '\\'.$class;
1453 } 1428 }
1454 1429
1455 /** 1430 /**
1456 * Dumps a parameter. 1431 * Dumps a parameter.
1457 * 1432 *
1476 * 1451 *
1477 * @return string 1452 * @return string
1478 */ 1453 */
1479 private function getServiceCall($id, Reference $reference = null) 1454 private function getServiceCall($id, Reference $reference = null)
1480 { 1455 {
1456 while ($this->container->hasAlias($id)) {
1457 $id = (string) $this->container->getAlias($id);
1458 }
1459
1481 if ('service_container' === $id) { 1460 if ('service_container' === $id) {
1482 return '$this'; 1461 return '$this';
1483 } 1462 }
1484 1463
1485 if ($this->container->hasDefinition($id) && !$this->container->getDefinition($id)->isPublic()) { 1464 if ($this->container->hasDefinition($id) && !$this->container->getDefinition($id)->isPublic()) {
1487 1466
1488 return "\${(\$_ = isset(\$this->services['$id']) ? \$this->services['$id'] : \$this->{$this->generateMethodName($id)}()) && false ?: '_'}"; 1467 return "\${(\$_ = isset(\$this->services['$id']) ? \$this->services['$id'] : \$this->{$this->generateMethodName($id)}()) && false ?: '_'}";
1489 } 1468 }
1490 if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) { 1469 if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
1491 return sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id); 1470 return sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
1492 }
1493
1494 if ($this->container->hasAlias($id)) {
1495 $id = (string) $this->container->getAlias($id);
1496 } 1471 }
1497 1472
1498 return sprintf('$this->get(\'%s\')', $id); 1473 return sprintf('$this->get(\'%s\')', $id);
1499 } 1474 }
1500 1475
1591 if (null === $this->expressionLanguage) { 1566 if (null === $this->expressionLanguage) {
1592 if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) { 1567 if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
1593 throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.'); 1568 throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
1594 } 1569 }
1595 $providers = $this->container->getExpressionLanguageProviders(); 1570 $providers = $this->container->getExpressionLanguageProviders();
1596 $this->expressionLanguage = new ExpressionLanguage(null, $providers); 1571 $this->expressionLanguage = new ExpressionLanguage(null, $providers, function ($arg) {
1572 $id = '""' === substr_replace($arg, '', 1, -1) ? stripcslashes(substr($arg, 1, -1)) : null;
1573
1574 if (null !== $id && ($this->container->hasAlias($id) || $this->container->hasDefinition($id))) {
1575 return $this->getServiceCall($id);
1576 }
1577
1578 return sprintf('$this->get(%s)', $arg);
1579 });
1597 1580
1598 if ($this->container->isTrackingResources()) { 1581 if ($this->container->isTrackingResources()) {
1599 foreach ($providers as $provider) { 1582 foreach ($providers as $provider) {
1600 $this->container->addObjectResource($provider); 1583 $this->container->addObjectResource($provider);
1601 } 1584 }