Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / DataCollector / ServicesDataCollector.php
1 <?php
2
3 namespace Drupal\webprofiler\DataCollector;
4
5 use Drupal\Core\StringTranslation\StringTranslationTrait;
6 use Drupal\webprofiler\DependencyInjection\TraceableContainer;
7 use Drupal\webprofiler\DrupalDataCollectorInterface;
8 use Symfony\Component\DependencyInjection\IntrospectableContainerInterface;
9 use Symfony\Component\HttpFoundation\Request;
10 use Symfony\Component\HttpFoundation\Response;
11 use Symfony\Component\HttpKernel\DataCollector\DataCollector;
12
13 /**
14  * Class ServicesDataCollector
15  */
16 class ServicesDataCollector extends DataCollector implements DrupalDataCollectorInterface {
17
18   use StringTranslationTrait, DrupalDataCollectorTrait;
19
20   /**
21    * @var \Symfony\Component\DependencyInjection\IntrospectableContainerInterface
22    *   $container
23    */
24   private $container;
25
26   /**
27    * @param IntrospectableContainerInterface $container
28    */
29   public function __construct(IntrospectableContainerInterface $container) {
30     $this->container = $container;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function collect(Request $request, Response $response, \Exception $exception = NULL) {
37     if ($this->getServicesCount()) {
38
39       $tracedData = [];
40       if ($this->container instanceof TraceableContainer) {
41         $tracedData = $this->container->getTracedData();
42       }
43
44       foreach (array_keys($this->getServices()) as $id) {
45         $this->data['services'][$id]['initialized'] = ($this->container->initialized($id)) ? TRUE : FALSE;
46         $this->data['services'][$id]['time'] = isset($tracedData[$id]) ? $tracedData[$id] : NULL;
47       }
48     }
49   }
50
51   /**
52    * @param $services
53    */
54   public function setServices($services) {
55     $this->data['services'] = $services;
56   }
57
58   /**
59    * @return array
60    */
61   public function getServices() {
62     return $this->data['services'];
63   }
64
65   /**
66    * @return int
67    */
68   public function getServicesCount() {
69     return count($this->getServices());
70   }
71
72   /**
73    * @return array
74    */
75   public function getInitializedServices() {
76     return array_filter($this->getServices(), function($item) {
77       return $item['initialized'];
78     });
79   }
80
81   /**
82    * @return int
83    */
84   public function getInitializedServicesCount() {
85     return count($this->getInitializedServices());
86   }
87
88   /**
89    * @return array
90    */
91   public function getInitializedServicesWithoutWebprofiler() {
92     return array_filter($this->getInitializedServices(), function($item) {
93       return strpos($item['value']['id'], 'webprofiler') !== 0;
94     });
95   }
96
97   /**
98    * @return int
99    */
100   public function getInitializedServicesWithoutWebprofilerCount() {
101     return count($this->getInitializedServicesWithoutWebprofiler());
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function getName() {
108     return 'services';
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function getTitle() {
115     return $this->t('Services');
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function getPanelSummary() {
122     return $this->t('Initialized: @count', [
123       '@count' => $this->getInitializedServicesCount(),
124     ]);
125   }
126
127   /**
128    * {@inheritdoc}
129    */
130   public function getIcon() {
131     return 'iVBORw0KGgoAAAANSUhEUgAAABUAAAAcCAYAAACOGPReAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAQVJREFUeNrkVe0NgjAQBeMAdYO6AWxQNtANGEFHcALZANyADegGsIFsIBvgu6Q/LtWmxdTEjyYvd6Hw8t5de6TzPCex1yp5w/pz0rVrQymVIXSAACqt9TGG0p0hpHWIZb9lebWENOXn1FgWbL8GJHACNHs+ohyjlxSEZPEcKGYC6SbEvljgUHzEOR3IXiiB6YOTlLqdo1Y54tZHDLIauCHtETtn962P6EUVqhhi0gelIJEEk1MjMg9Py9xol/0SuBqFva/DULY3ZSqQF767v8TyZKv83tFXWVaEufsUG+DCr2nwQLGOlGQNizZPy3fMU16K5uV5+qQEpFTC+hCN9Pd/0XcBBgBxwVqjDkAznAAAAABJRU5ErkJggg==';
132   }
133
134   /**
135    * @return array
136    */
137   public function getData() {
138     $data = $this->data;
139
140     $http_middleware = array_filter($data['services'], function($service) {
141       return isset($service['value']['tags']['http_middleware']);
142     });
143
144     foreach ($http_middleware as &$service) {
145       $service['value']['handle_method'] = $this->getMethodData($service['value']['class'], 'handle');
146     }
147
148     uasort($http_middleware, function ($a, $b) {
149       $va = $a['value']['tags']['http_middleware'][0]['priority'];
150       $vb = $b['value']['tags']['http_middleware'][0]['priority'];
151
152       if ($va == $vb) {
153         return 0;
154       }
155       return ($va > $vb) ? -1 : 1;
156     });
157
158     $data['http_middleware'] = $http_middleware;
159
160     return $data;
161   }
162 }