@@ -312,9 +312,13 @@ private function analyzeCircularReferences(array $edges, &$checkedNodes, &$curre
312
312
if ($ node ->getValue () && ($ edge ->isLazy () || $ edge ->isWeak ())) {
313
313
// no-op
314
314
} elseif (isset ($ currentPath [$ id ])) {
315
+ $ currentId = $ id ;
315
316
foreach (array_reverse ($ currentPath ) as $ parentId ) {
316
- $ this ->circularReferences [$ parentId ][$ id ] = $ id ;
317
- $ id = $ parentId ;
317
+ $ this ->circularReferences [$ parentId ][$ currentId ] = $ currentId ;
318
+ if ($ parentId === $ id ) {
319
+ break ;
320
+ }
321
+ $ currentId = $ parentId ;
318
322
}
319
323
} elseif (!isset ($ checkedNodes [$ id ])) {
320
324
$ checkedNodes [$ id ] = true ;
@@ -429,7 +433,7 @@ private function addServiceInclude(string $cId, Definition $definition): string
429
433
* @throws InvalidArgumentException
430
434
* @throws RuntimeException
431
435
*/
432
- private function addServiceInstance (string $ id , Definition $ definition , string $ isSimpleInstance ): string
436
+ private function addServiceInstance (string $ id , Definition $ definition , bool $ isSimpleInstance ): string
433
437
{
434
438
$ class = $ this ->dumpValue ($ definition ->getClass ());
435
439
@@ -552,7 +556,7 @@ private function addService(string $id, Definition $definition, string &$file =
552
556
$ this ->definitionVariables = new \SplObjectStorage ();
553
557
$ this ->referenceVariables = array ();
554
558
$ this ->variableCount = 0 ;
555
- $ this ->definitionVariables [ $ definition ] = $ this -> referenceVariables [$ id ] = new Variable ('instance ' );
559
+ $ this ->referenceVariables [$ id ] = new Variable ('instance ' );
556
560
557
561
$ return = array ();
558
562
@@ -630,22 +634,7 @@ protected function {$methodName}($lazyInitialization)
630
634
$ code .= sprintf (" @trigger_error(%s, E_USER_DEPRECATED); \n\n" , $ this ->export ($ definition ->getDeprecationMessage ($ id )));
631
635
}
632
636
633
- $ head = $ tail = '' ;
634
- $ arguments = array ($ definition ->getArguments (), $ definition ->getFactory ());
635
- $ this ->addInlineVariables ($ head , $ tail , $ id , $ arguments , true );
636
- $ code .= '' !== $ head ? $ head ."\n" : '' ;
637
-
638
- if ($ arguments = array_filter (array ($ definition ->getProperties (), $ definition ->getMethodCalls (), $ definition ->getConfigurator ()))) {
639
- $ this ->addInlineVariables ($ tail , $ tail , $ id , $ arguments , false );
640
-
641
- $ tail .= '' !== $ tail ? "\n" : '' ;
642
- $ tail .= $ this ->addServiceProperties ($ definition );
643
- $ tail .= $ this ->addServiceMethodCalls ($ definition );
644
- $ tail .= $ this ->addServiceConfigurator ($ definition );
645
- }
646
-
647
- $ code .= $ this ->addServiceInstance ($ id , $ definition , '' === $ tail )
648
- .('' !== $ tail ? "\n" .$ tail ."\n return \$instance; \n" : '' );
637
+ $ code .= $ this ->addInlineService ($ id , $ definition );
649
638
650
639
if ($ asFile ) {
651
640
$ code = implode ("\n" , array_map (function ($ line ) { return $ line ? substr ($ line , 8 ) : $ line ; }, explode ("\n" , $ code )));
@@ -659,35 +648,41 @@ protected function {$methodName}($lazyInitialization)
659
648
return $ code ;
660
649
}
661
650
662
- private function addInlineVariables (string & $ head , string & $ tail , string $ id , array $ arguments , bool $ forConstructor ): bool
651
+ private function addInlineVariables (string $ id , Definition $ definition , array $ arguments , bool $ forConstructor ): string
663
652
{
664
- $ hasSelfRef = false ;
653
+ $ code = '' ;
665
654
666
655
foreach ($ arguments as $ argument ) {
667
656
if (\is_array ($ argument )) {
668
- $ hasSelfRef = $ this ->addInlineVariables ($ head , $ tail , $ id , $ argument , $ forConstructor ) || $ hasSelfRef ;
657
+ $ code . = $ this ->addInlineVariables ($ id , $ definition , $ argument , $ forConstructor );
669
658
} elseif ($ argument instanceof Reference) {
670
- $ hasSelfRef = $ this ->addInlineReference ($ head , $ id , $ argument , $ forConstructor ) || $ hasSelfRef ;
659
+ $ code . = $ this ->addInlineReference ($ id , $ definition , $ argument , $ forConstructor );
671
660
} elseif ($ argument instanceof Definition) {
672
- $ hasSelfRef = $ this ->addInlineService ($ head , $ tail , $ id , $ argument , $ forConstructor ) || $ hasSelfRef ;
661
+ $ code . = $ this ->addInlineService ($ id , $ definition , $ argument , $ forConstructor );
673
662
}
674
663
}
675
664
676
- return $ hasSelfRef ;
665
+ return $ code ;
677
666
}
678
667
679
- private function addInlineReference (string & $ code , string $ id , string $ targetId , bool $ forConstructor ): bool
668
+ private function addInlineReference (string $ id , Definition $ definition , string $ targetId , bool $ forConstructor ): string
680
669
{
681
- $ hasSelfRef = isset ($ this ->circularReferences [$ id ][$ targetId ]);
670
+ if ($ id === $ targetId ) {
671
+ return $ this ->addInlineService ($ id , $ definition , $ definition , $ forConstructor );
672
+ }
682
673
683
674
if ('service_container ' === $ targetId || isset ($ this ->referenceVariables [$ targetId ])) {
684
- return $ hasSelfRef ;
675
+ return '' ;
685
676
}
686
677
678
+ $ hasSelfRef = isset ($ this ->circularReferences [$ id ][$ targetId ]);
679
+ $ forConstructor = $ forConstructor && !isset ($ this ->definitionVariables [$ definition ]);
687
680
list ($ callCount , $ behavior ) = $ this ->serviceCalls [$ targetId ];
688
681
689
- if (2 > $ callCount && (!$ hasSelfRef || !$ forConstructor )) {
690
- return $ hasSelfRef ;
682
+ $ code = $ hasSelfRef && !$ forConstructor ? $ this ->addInlineService ($ id , $ definition , $ definition , $ forConstructor ) : '' ;
683
+
684
+ if (isset ($ this ->referenceVariables [$ targetId ]) || (2 > $ callCount && (!$ hasSelfRef || !$ forConstructor ))) {
685
+ return $ code ;
691
686
}
692
687
693
688
$ name = $ this ->getNextVariableName ();
@@ -697,7 +692,7 @@ private function addInlineReference(string &$code, string $id, string $targetId,
697
692
$ code .= sprintf (" \$%s = %s; \n" , $ name , $ this ->getServiceCall ($ targetId , $ reference ));
698
693
699
694
if (!$ hasSelfRef || !$ forConstructor ) {
700
- return $ hasSelfRef ;
695
+ return $ code ;
701
696
}
702
697
703
698
$ code .= sprintf (<<<'EOTXT'
@@ -712,46 +707,56 @@ private function addInlineReference(string &$code, string $id, string $targetId,
712
707
$ id
713
708
);
714
709
715
- return false ;
710
+ return $ code ;
716
711
}
717
712
718
- private function addInlineService (string & $ head , string & $ tail , string $ id , Definition $ definition , bool $ forConstructor ): bool
713
+ private function addInlineService (string $ id , Definition $ definition , Definition $ inlineDef = null , bool $ forConstructor = true ): string
719
714
{
720
- if (isset ($ this ->definitionVariables [$ definition ])) {
721
- return false ;
715
+ $ isSimpleInstance = $ isRootInstance = null === $ inlineDef ;
716
+
717
+ if (isset ($ this ->definitionVariables [$ inlineDef = $ inlineDef ?: $ definition ])) {
718
+ return '' ;
722
719
}
723
720
724
- $ arguments = array ($ definition ->getArguments (), $ definition ->getFactory ());
721
+ $ arguments = array ($ inlineDef ->getArguments (), $ inlineDef ->getFactory ());
725
722
726
- if (2 > $ this ->inlinedDefinitions [$ definition ] && !$ definition ->getMethodCalls () && !$ definition ->getProperties () && !$ definition ->getConfigurator ()) {
727
- return $ this ->addInlineVariables ($ head , $ tail , $ id , $ arguments , $ forConstructor );
728
- }
723
+ $ code = $ this ->addInlineVariables ($ id , $ definition , $ arguments , $ forConstructor );
729
724
730
- $ name = $ this ->getNextVariableName ();
731
- $ this ->definitionVariables [$ definition ] = new Variable ($ name );
725
+ if ($ arguments = array_filter (array ($ inlineDef ->getProperties (), $ inlineDef ->getMethodCalls (), $ inlineDef ->getConfigurator ()))) {
726
+ $ isSimpleInstance = false ;
727
+ } elseif ($ definition !== $ inlineDef && 2 > $ this ->inlinedDefinitions [$ inlineDef ]) {
728
+ return $ code ;
729
+ }
732
730
733
- $ code = '' ;
734
- if ($ forConstructor ) {
735
- $ hasSelfRef = $ this ->addInlineVariables ($ code , $ tail , $ id , $ arguments , $ forConstructor );
731
+ if (isset ($ this ->definitionVariables [$ inlineDef ])) {
732
+ $ isSimpleInstance = false ;
736
733
} else {
737
- $ hasSelfRef = $ this ->addInlineVariables ($ code , $ code , $ id , $ arguments , $ forConstructor );
738
- }
739
- $ code .= $ this ->addNewInstance ($ definition , '$ ' .$ name , ' = ' , $ id );
740
- $ hasSelfRef && !$ forConstructor ? $ tail .= ('' !== $ tail ? "\n" : '' ).$ code : $ head .= ('' !== $ head ? "\n" : '' ).$ code ;
734
+ $ name = $ definition === $ inlineDef ? 'instance ' : $ this ->getNextVariableName ();
735
+ $ this ->definitionVariables [$ inlineDef ] = new Variable ($ name );
736
+ $ code .= '' !== $ code ? "\n" : '' ;
741
737
742
- $ code = '' ;
743
- $ arguments = array ($ definition ->getProperties (), $ definition ->getMethodCalls (), $ definition ->getConfigurator ());
744
- $ hasSelfRef = $ this ->addInlineVariables ($ code , $ code , $ id , $ arguments , false ) || $ hasSelfRef ;
738
+ if ('instance ' === $ name ) {
739
+ $ code .= $ this ->addServiceInstance ($ id , $ definition , $ isSimpleInstance );
740
+ } else {
741
+ $ code .= $ this ->addNewInstance ($ inlineDef , '$ ' .$ name , ' = ' , $ id );
742
+ }
745
743
746
- $ code .= '' !== $ code ? "\n" : '' ;
747
- $ code .= $ this ->addServiceProperties ($ definition , $ name );
748
- $ code .= $ this ->addServiceMethodCalls ($ definition , $ name );
749
- $ code .= $ this ->addServiceConfigurator ($ definition , $ name );
750
- if ('' !== $ code ) {
751
- $ hasSelfRef ? $ tail .= ('' !== $ tail ? "\n" : '' ).$ code : $ head .= $ code ;
744
+ if ('' !== $ inline = $ this ->addInlineVariables ($ id , $ definition , $ arguments , false )) {
745
+ $ code .= "\n" .$ inline ."\n" ;
746
+ } elseif ($ arguments && 'instance ' === $ name ) {
747
+ $ code .= "\n" ;
748
+ }
749
+
750
+ $ code .= $ this ->addServiceProperties ($ inlineDef , $ name );
751
+ $ code .= $ this ->addServiceMethodCalls ($ inlineDef , $ name );
752
+ $ code .= $ this ->addServiceConfigurator ($ inlineDef , $ name );
753
+ }
754
+
755
+ if ($ isRootInstance && !$ isSimpleInstance ) {
756
+ $ code .= "\n return \$instance; \n" ;
752
757
}
753
758
754
- return $ hasSelfRef ;
759
+ return $ code ;
755
760
}
756
761
757
762
private function addServices (): string
0 commit comments