Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Compiler / ServicePass.php
1 <?php
2
3 namespace Drupal\webprofiler\Compiler;
4
5 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6 use Symfony\Component\DependencyInjection\Compiler\ServiceReferenceGraph;
7 use Symfony\Component\DependencyInjection\ContainerBuilder;
8 use Symfony\Component\DependencyInjection\Definition;
9
10 /**
11  * Class ServicePass
12  */
13 class ServicePass implements CompilerPassInterface {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function process(ContainerBuilder $container) {
19     if (FALSE === $container->hasDefinition('webprofiler.services')) {
20       return;
21     }
22
23     $definition = $container->getDefinition('webprofiler.services');
24     $graph = $container->getCompiler()->getServiceReferenceGraph();
25
26     $definition->addMethodCall('setServices', [$this->extractData($container, $graph)]);
27   }
28
29   /**
30    * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
31    * @param \Symfony\Component\DependencyInjection\Compiler\ServiceReferenceGraph $graph
32    *
33    * @return array
34    */
35   private function extractData(ContainerBuilder $container, ServiceReferenceGraph $graph) {
36     $data = [];
37
38     foreach ($container->getDefinitions() as $id => $definition) {
39       $inEdges = [];
40       $outEdges = [];
41
42       if ($graph->hasNode($id)) {
43         $node = $graph->getNode($id);
44
45         /** @var \Symfony\Component\DependencyInjection\Compiler\ServiceReferenceGraphEdge $edge */
46         foreach ($node->getInEdges() as $edge) {
47           /** @var \Symfony\Component\DependencyInjection\Reference $edgeValue */
48           $edgeValue = $edge->getValue();
49
50           $inEdges[] = [
51             'id' => $edge->getSourceNode()->getId(),
52             'invalidBehavior' => $edgeValue ? $edgeValue->getInvalidBehavior() : NULL,
53             'strict' => $edgeValue ? $edgeValue->isStrict() : NULL,
54           ];
55         }
56
57
58         /** @var \Symfony\Component\DependencyInjection\Compiler\ServiceReferenceGraphEdge $edge */
59         foreach ($node->getOutEdges() as $edge) {
60           /** @var \Symfony\Component\DependencyInjection\Reference $edgeValue */
61           $edgeValue = $edge->getValue();
62
63           $outEdges[] = [
64             'id' => $edge->getDestNode()->getId(),
65             'invalidBehavior' => $edgeValue ? $edgeValue->getInvalidBehavior() : NULL,
66             'strict' => $edgeValue ? $edgeValue->isStrict() : NULL,
67           ];
68         }
69       }
70
71       if ($definition instanceof Definition) {
72         $class = $definition->getClass();
73
74         try {
75           $reflectedClass = new \ReflectionClass($class);
76           $file = $reflectedClass->getFileName();
77         } catch (\ReflectionException $e) {
78           $file = NULL;
79         }
80
81         $tags = $definition->getTags();
82         $public = $definition->isPublic();
83         $synthetic = $definition->isSynthetic();
84       }
85       else {
86         $id = $definition->__toString();
87         $class = NULL;
88         $file = NULL;
89         $tags = [];
90         $public = NULL;
91         $synthetic = NULL;
92       }
93
94       $data[$id] = [
95         'inEdges' => $inEdges,
96         'outEdges' => $outEdges,
97         'value' => [
98           'class' => $class,
99           'file' => $file,
100           'id' => $id,
101           'tags' => $tags,
102           'public' => $public,
103           'synthetic' => $synthetic,
104         ],
105       ];
106     }
107
108     return $data;
109   }
110 }