@@ -342,9 +342,13 @@ private function analyzeCircularReferences(array $edges, &$checkedNodes, &$curre
342
342
if ($ node ->getValue () && ($ edge ->isLazy () || $ edge ->isWeak ())) {
343
343
// no-op
344
344
} elseif (isset ($ currentPath [$ id ])) {
345
+ $ currentId = $ id ;
345
346
foreach (array_reverse ($ currentPath ) as $ parentId ) {
346
- $ this ->circularReferences [$ parentId ][$ id ] = $ id ;
347
- $ id = $ parentId ;
347
+ $ this ->circularReferences [$ parentId ][$ currentId ] = $ currentId ;
348
+ if ($ parentId === $ id ) {
349
+ break ;
350
+ }
351
+ $ currentId = $ parentId ;
348
352
}
349
353
} elseif (!isset ($ checkedNodes [$ id ])) {
350
354
$ checkedNodes [$ id ] = true ;
@@ -459,7 +463,7 @@ private function addServiceInclude(string $cId, Definition $definition): string
459
463
* @throws InvalidArgumentException
460
464
* @throws RuntimeException
461
465
*/
462
- private function addServiceInstance (string $ id , Definition $ definition , string $ isSimpleInstance ): string
466
+ private function addServiceInstance (string $ id , Definition $ definition , bool $ isSimpleInstance ): string
463
467
{
464
468
$ class = $ this ->dumpValue ($ definition ->getClass ());
465
469
@@ -585,7 +589,7 @@ private function addService(string $id, Definition $definition): array
585
589
$ this ->definitionVariables = new \SplObjectStorage ();
586
590
$ this ->referenceVariables = array ();
587
591
$ this ->variableCount = 0 ;
588
- $ this ->definitionVariables [ $ definition ] = $ this -> referenceVariables [$ id ] = new Variable ('instance ' );
592
+ $ this ->referenceVariables [$ id ] = new Variable ('instance ' );
589
593
590
594
$ return = array ();
591
595
@@ -658,22 +662,7 @@ protected function {$methodName}($lazyInitialization)
658
662
$ code .= sprintf (" @trigger_error(%s, E_USER_DEPRECATED); \n\n" , $ this ->export ($ definition ->getDeprecationMessage ($ id )));
659
663
}
660
664
661
- $ head = $ tail = '' ;
662
- $ arguments = array ($ definition ->getArguments (), $ definition ->getFactory ());
663
- $ this ->addInlineVariables ($ head , $ tail , $ id , $ arguments , true );
664
- $ code .= '' !== $ head ? $ head ."\n" : '' ;
665
-
666
- if ($ arguments = array_filter (array ($ definition ->getProperties (), $ definition ->getMethodCalls (), $ definition ->getConfigurator ()))) {
667
- $ this ->addInlineVariables ($ tail , $ tail , $ id , $ arguments , false );
668
-
669
- $ tail .= '' !== $ tail ? "\n" : '' ;
670
- $ tail .= $ this ->addServiceProperties ($ definition );
671
- $ tail .= $ this ->addServiceMethodCalls ($ definition );
672
- $ tail .= $ this ->addServiceConfigurator ($ definition );
673
- }
674
-
675
- $ code .= $ this ->addServiceInstance ($ id , $ definition , '' === $ tail )
676
- .('' !== $ tail ? "\n" .$ tail ."\n return \$instance; \n" : '' );
665
+ $ code .= $ this ->addInlineService ($ id , $ definition );
677
666
678
667
if ($ asFile ) {
679
668
$ code = implode ("\n" , array_map (function ($ line ) { return $ line ? substr ($ line , 8 ) : $ line ; }, explode ("\n" , $ code )));
@@ -687,35 +676,41 @@ protected function {$methodName}($lazyInitialization)
687
676
return array ($ file , $ code );
688
677
}
689
678
690
- private function addInlineVariables (string & $ head , string & $ tail , string $ id , array $ arguments , bool $ forConstructor ): bool
679
+ private function addInlineVariables (string $ id , Definition $ definition , array $ arguments , bool $ forConstructor ): string
691
680
{
692
- $ hasSelfRef = false ;
681
+ $ code = '' ;
693
682
694
683
foreach ($ arguments as $ argument ) {
695
684
if (\is_array ($ argument )) {
696
- $ hasSelfRef = $ this ->addInlineVariables ($ head , $ tail , $ id , $ argument , $ forConstructor ) || $ hasSelfRef ;
685
+ $ code . = $ this ->addInlineVariables ($ id , $ definition , $ argument , $ forConstructor );
697
686
} elseif ($ argument instanceof Reference) {
698
- $ hasSelfRef = $ this ->addInlineReference ($ head , $ id , $ argument , $ forConstructor ) || $ hasSelfRef ;
687
+ $ code . = $ this ->addInlineReference ($ id , $ definition , $ argument , $ forConstructor );
699
688
} elseif ($ argument instanceof Definition) {
700
- $ hasSelfRef = $ this ->addInlineService ($ head , $ tail , $ id , $ argument , $ forConstructor ) || $ hasSelfRef ;
689
+ $ code . = $ this ->addInlineService ($ id , $ definition , $ argument , $ forConstructor );
701
690
}
702
691
}
703
692
704
- return $ hasSelfRef ;
693
+ return $ code ;
705
694
}
706
695
707
- private function addInlineReference (string & $ code , string $ id , string $ targetId , bool $ forConstructor ): bool
696
+ private function addInlineReference (string $ id , Definition $ definition , string $ targetId , bool $ forConstructor ): string
708
697
{
709
- $ hasSelfRef = isset ($ this ->circularReferences [$ id ][$ targetId ]);
698
+ if ($ id === $ targetId ) {
699
+ return $ this ->addInlineService ($ id , $ definition , $ definition , $ forConstructor );
700
+ }
710
701
711
702
if ('service_container ' === $ targetId || isset ($ this ->referenceVariables [$ targetId ])) {
712
- return $ hasSelfRef ;
703
+ return '' ;
713
704
}
714
705
706
+ $ hasSelfRef = isset ($ this ->circularReferences [$ id ][$ targetId ]);
707
+ $ forConstructor = $ forConstructor && !isset ($ this ->definitionVariables [$ definition ]);
715
708
list ($ callCount , $ behavior ) = $ this ->serviceCalls [$ targetId ];
716
709
717
- if (2 > $ callCount && (!$ hasSelfRef || !$ forConstructor )) {
718
- return $ hasSelfRef ;
710
+ $ code = $ hasSelfRef && !$ forConstructor ? $ this ->addInlineService ($ id , $ definition , $ definition , $ forConstructor ) : '' ;
711
+
712
+ if (isset ($ this ->referenceVariables [$ targetId ]) || (2 > $ callCount && (!$ hasSelfRef || !$ forConstructor ))) {
713
+ return $ code ;
719
714
}
720
715
721
716
$ name = $ this ->getNextVariableName ();
@@ -725,7 +720,7 @@ private function addInlineReference(string &$code, string $id, string $targetId,
725
720
$ code .= sprintf (" \$%s = %s; \n" , $ name , $ this ->getServiceCall ($ targetId , $ reference ));
726
721
727
722
if (!$ hasSelfRef || !$ forConstructor ) {
728
- return $ hasSelfRef ;
723
+ return $ code ;
729
724
}
730
725
731
726
$ code .= sprintf (<<<'EOTXT'
@@ -740,46 +735,56 @@ private function addInlineReference(string &$code, string $id, string $targetId,
740
735
$ id
741
736
);
742
737
743
- return false ;
738
+ return $ code ;
744
739
}
745
740
746
- private function addInlineService (string & $ head , string & $ tail , string $ id , Definition $ definition , bool $ forConstructor ): bool
741
+ private function addInlineService (string $ id , Definition $ definition , Definition $ inlineDef = null , bool $ forConstructor = true ): string
747
742
{
748
- if (isset ($ this ->definitionVariables [$ definition ])) {
749
- return false ;
743
+ $ isSimpleInstance = $ isRootInstance = null === $ inlineDef ;
744
+
745
+ if (isset ($ this ->definitionVariables [$ inlineDef = $ inlineDef ?: $ definition ])) {
746
+ return '' ;
750
747
}
751
748
752
- $ arguments = array ($ definition ->getArguments (), $ definition ->getFactory ());
749
+ $ arguments = array ($ inlineDef ->getArguments (), $ inlineDef ->getFactory ());
753
750
754
- if (2 > $ this ->inlinedDefinitions [$ definition ] && !$ definition ->getMethodCalls () && !$ definition ->getProperties () && !$ definition ->getConfigurator ()) {
755
- return $ this ->addInlineVariables ($ head , $ tail , $ id , $ arguments , $ forConstructor );
756
- }
751
+ $ code = $ this ->addInlineVariables ($ id , $ definition , $ arguments , $ forConstructor );
757
752
758
- $ name = $ this ->getNextVariableName ();
759
- $ this ->definitionVariables [$ definition ] = new Variable ($ name );
753
+ if ($ arguments = array_filter (array ($ inlineDef ->getProperties (), $ inlineDef ->getMethodCalls (), $ inlineDef ->getConfigurator ()))) {
754
+ $ isSimpleInstance = false ;
755
+ } elseif ($ definition !== $ inlineDef && 2 > $ this ->inlinedDefinitions [$ inlineDef ]) {
756
+ return $ code ;
757
+ }
760
758
761
- $ code = '' ;
762
- if ($ forConstructor ) {
763
- $ hasSelfRef = $ this ->addInlineVariables ($ code , $ tail , $ id , $ arguments , $ forConstructor );
759
+ if (isset ($ this ->definitionVariables [$ inlineDef ])) {
760
+ $ isSimpleInstance = false ;
764
761
} else {
765
- $ hasSelfRef = $ this ->addInlineVariables ($ code , $ code , $ id , $ arguments , $ forConstructor );
766
- }
767
- $ code .= $ this ->addNewInstance ($ definition , ' $ ' .$ name .' = ' , $ id );
768
- $ hasSelfRef && !$ forConstructor ? $ tail .= ('' !== $ tail ? "\n" : '' ).$ code : $ head .= ('' !== $ head ? "\n" : '' ).$ code ;
762
+ $ name = $ definition === $ inlineDef ? 'instance ' : $ this ->getNextVariableName ();
763
+ $ this ->definitionVariables [$ inlineDef ] = new Variable ($ name );
764
+ $ code .= '' !== $ code ? "\n" : '' ;
769
765
770
- $ code = '' ;
771
- $ arguments = array ($ definition ->getProperties (), $ definition ->getMethodCalls (), $ definition ->getConfigurator ());
772
- $ hasSelfRef = $ this ->addInlineVariables ($ code , $ code , $ id , $ arguments , false ) || $ hasSelfRef ;
766
+ if ('instance ' === $ name ) {
767
+ $ code .= $ this ->addServiceInstance ($ id , $ definition , $ isSimpleInstance );
768
+ } else {
769
+ $ code .= $ this ->addNewInstance ($ inlineDef , ' $ ' .$ name .' = ' , $ id );
770
+ }
773
771
774
- $ code .= '' !== $ code ? "\n" : '' ;
775
- $ code .= $ this ->addServiceProperties ($ definition , $ name );
776
- $ code .= $ this ->addServiceMethodCalls ($ definition , $ name );
777
- $ code .= $ this ->addServiceConfigurator ($ definition , $ name );
778
- if ('' !== $ code ) {
779
- $ hasSelfRef ? $ tail .= ('' !== $ tail ? "\n" : '' ).$ code : $ head .= $ code ;
772
+ if ('' !== $ inline = $ this ->addInlineVariables ($ id , $ definition , $ arguments , false )) {
773
+ $ code .= "\n" .$ inline ."\n" ;
774
+ } elseif ($ arguments && 'instance ' === $ name ) {
775
+ $ code .= "\n" ;
776
+ }
777
+
778
+ $ code .= $ this ->addServiceProperties ($ inlineDef , $ name );
779
+ $ code .= $ this ->addServiceMethodCalls ($ inlineDef , $ name );
780
+ $ code .= $ this ->addServiceConfigurator ($ inlineDef , $ name );
781
+ }
782
+
783
+ if ($ isRootInstance && !$ isSimpleInstance ) {
784
+ $ code .= "\n return \$instance; \n" ;
780
785
}
781
786
782
- return $ hasSelfRef ;
787
+ return $ code ;
783
788
}
784
789
785
790
private function addServices (array &$ services = null ): string
0 commit comments