a6f2e7a84e4b4ce9892dd79202f01a61d2dbf4cd
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / DataCollector / BlocksDataCollector.php
1 <?php
2
3 namespace Drupal\webprofiler\DataCollector;
4
5 use Drupal\block\Entity\Block;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 use Drupal\webprofiler\DrupalDataCollectorInterface;
10 use Drupal\webprofiler\Entity\EntityDecorator;
11 use Symfony\Component\HttpFoundation\Request;
12 use Symfony\Component\HttpFoundation\Response;
13 use Symfony\Component\HttpKernel\DataCollector\DataCollector;
14
15 /**
16  * Class BlocksDataCollector
17  */
18 class BlocksDataCollector extends DataCollector implements DrupalDataCollectorInterface {
19
20   use StringTranslationTrait, DrupalDataCollectorTrait;
21
22   /**
23    * @var \Drupal\Core\Entity\EntityManagerInterface
24    */
25   private $entityManager;
26
27   /**
28    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityManager
29    */
30   public function __construct(EntityTypeManagerInterface $entityManager) {
31     $this->entityManager = $entityManager;
32
33     $this->data['blocks']['loaded'] = [];
34     $this->data['blocks']['rendered'] = [];
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function collect(Request $request, Response $response, \Exception $exception = NULL) {
41     $storage = $this->entityManager->getStorage('block');
42
43     $loaded = $this->entityManager->getLoaded('config', 'block');
44     $rendered = $this->entityManager->getRendered('block');
45
46     if ($loaded) {
47       $this->data['blocks']['loaded'] = $this->getBlocksData($loaded, $storage);
48
49     }
50
51     if ($rendered) {
52       $this->data['blocks']['rendered'] = $this->getBlocksData($rendered, $storage);
53     }
54   }
55
56   /**
57    * @return array
58    */
59   public function getRenderedBlocks() {
60     return $this->data['blocks']['rendered'];
61   }
62
63   /**
64    * @return int
65    */
66   public function getRenderedBlocksCount() {
67     return count($this->getRenderedBlocks());
68   }
69
70   /**
71    * @return array
72    */
73   public function getLoadedBlocks() {
74     return $this->data['blocks']['loaded'];
75   }
76
77   /**
78    * @return int
79    */
80   public function getLoadedBlocksCount() {
81     return count($this->getLoadedBlocks());
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function getName() {
88     return 'blocks';
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function getTitle() {
95     return $this->t('Blocks');
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function getPanelSummary() {
102     return $this->t('Loaded: @loaded, rendered: @rendered', [
103       '@loaded' => $this->getLoadedBlocksCount(),
104       '@rendered' => $this->getRenderedBlocksCount()
105     ]);
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function getIcon() {
112     return 'iVBORw0KGgoAAAANSUhEUgAAABUAAAAcCAYAAACOGPReAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA2hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYxIDY0LjE0MDk0OSwgMjAxMC8xMi8wNy0xMDo1NzowMSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDowNDgwMTE3NDA3MjA2ODExOEY2MkNCNjI0NDY3NzkwRCIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDowQjg5OTA4OEYwQTgxMUUzQkJDRThFQjA5Q0E1REFCRCIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDowQjg5OTA4N0YwQTgxMUUzQkJDRThFQjA5Q0E1REFCRCIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ1M1LjEgTWFjaW50b3NoIj4gPHhtcE1NOkRlcml2ZWRGcm9tIHN0UmVmOmluc3RhbmNlSUQ9InhtcC5paWQ6MDM4MDExNzQwNzIwNjgxMTg3MUZDQ0I0RjY1RTlEM0IiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6MDQ4MDExNzQwNzIwNjgxMThGNjJDQjYyNDQ2Nzc5MEQiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz68h+kGAAAAWElEQVR42mL8//8/A7UBEwMNwAg3lAVEODg4UCW2Dhw4wDgapgyMtEj8eCMKFPCkyI1GFCJMiUnQVDUUX0SNllK4wxRf+JAdUeQUfaMRRd+ib4SHKUCAAQAMcyf8vLAstgAAAABJRU5ErkJggg==';
113   }
114
115   /**
116    * @param $decorator
117    * @param $storage
118    *
119    * @return array
120    */
121   private function getBlocksData(EntityDecorator $decorator, EntityStorageInterface $storage) {
122     $blocks = [];
123
124     /** @var \Drupal\block\BlockInterface $block */
125     foreach ($decorator->getEntities() as $block) {
126       /** @var Block $entity */
127       if (null !== $block && $entity = $storage->load($block->get('id'))) {
128
129         $route = '';
130         if ($entity->hasLinkTemplate('edit-form')) {
131           $route = $entity->toUrl('edit-form')->toString();
132         }
133
134         $id = $block->get('id');
135         $blocks[$id] = [
136           'id' => $id,
137           'region' => $block->getRegion(),
138           'status' => $block->get('status'),
139           'theme' => $block->getTheme(),
140           'plugin' => $block->get('plugin'),
141           'settings' => $block->get('settings'),
142           'route' => $route,
143         ];
144       }
145     }
146
147     return $blocks;
148   }
149 }