Namespace
Drupal\devel
File
-
src/DevelDumperManager.php
View source
<?php
namespace Drupal\devel;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
class DevelDumperManager implements DevelDumperManagerInterface {
use StringTranslationTrait;
protected ImmutableConfig $config;
public function __construct(ConfigFactoryInterface $config_factory, protected AccountProxyInterface $account, protected DevelDumperPluginManagerInterface $dumperManager, protected EntityTypeManagerInterface $entityTypeManager, protected MessengerInterface $messenger, TranslationInterface $string_translation) {
$this->config = $config_factory->get('devel.settings');
$this->stringTranslation = $string_translation;
}
protected function createInstance(?string $plugin_id = NULL) {
if (!$plugin_id || !$this->dumperManager
->isPluginSupported($plugin_id)) {
$plugin_id = $this->config
->get('devel_dumper');
}
return $this->dumperManager
->createInstance($plugin_id);
}
public function dump($input, ?string $name = NULL, $plugin_id = NULL) : void {
if ($this->hasAccessToDevelInformation()) {
$this->createInstance($plugin_id)
->dump($input, $name);
}
}
public function export(mixed $input, ?string $name = NULL, ?string $plugin_id = NULL, bool $load_references = FALSE) : MarkupInterface|string {
if (!$this->hasAccessToDevelInformation()) {
return '';
}
if ($load_references && $input instanceof EntityInterface) {
$input = $this->entityToArrayWithReferences($input);
}
return $this->createInstance($plugin_id)
->export($input, $name);
}
public function message($input, ?string $name = NULL, $type = MessengerInterface::TYPE_STATUS, ?string $plugin_id = NULL, $load_references = FALSE) : void {
if ($this->hasAccessToDevelInformation()) {
$output = $this->export($input, $name, $plugin_id, $load_references);
$this->messenger
->addMessage($output, $type, TRUE);
}
}
public function debug($input, ?string $name = NULL, ?string $plugin_id = NULL) {
$output = $this->createInstance($plugin_id)
->export($input, $name) . "\n";
$file = $this->config
->get('debug_logfile');
if (empty($file)) {
$file = 'temporary://drupal_debug.txt';
}
if (file_put_contents($file, $output, FILE_APPEND) === FALSE && $this->hasAccessToDevelInformation()) {
$this->messenger
->addError($this->t('Devel was unable to write to %file.', [
'%file' => $file,
]));
return FALSE;
}
}
public function dumpOrExport($input, ?string $name = NULL, $export = TRUE, ?string $plugin_id = NULL) {
if ($this->hasAccessToDevelInformation()) {
$dumper = $this->createInstance($plugin_id);
if ($export) {
return $dumper->export($input, $name);
}
$dumper->dump($input, $name);
}
return NULL;
}
public function exportAsRenderable($input, ?string $name = NULL, $plugin_id = NULL, $load_references = FALSE) : array {
if ($this->hasAccessToDevelInformation()) {
if ($load_references && $input instanceof EntityInterface) {
$input = $this->entityToArrayWithReferences($input);
}
return $this->createInstance($plugin_id)
->exportAsRenderable($input, $name);
}
return [];
}
protected function hasAccessToDevelInformation() : bool {
return $this->account
->hasPermission('access devel information');
}
protected function entityToArrayWithReferences(EntityInterface $entity, int $depth = 0, array $array_path = []) {
$seen =& drupal_static(__FUNCTION__);
$seen_key = $entity->getEntityTypeId() . '-' . $entity->id();
if (!isset($seen[$seen_key])) {
$seen[$seen_key] = $array_path;
}
$array = $entity->toArray();
if ($depth > 20) {
return $array;
}
if (!$entity instanceof FieldableEntityInterface) {
return $array;
}
foreach ($array as $field => &$value) {
if (is_array($value)) {
$fieldDefinition = $entity->getFieldDefinition($field);
$target_type = $fieldDefinition->getSetting('target_type');
if (!$target_type) {
continue;
}
try {
$storage = $this->entityTypeManager
->getStorage($target_type);
} catch (InvalidPluginDefinitionException|PluginNotFoundException) {
continue;
}
foreach ($value as $delta => &$item) {
if (is_array($item)) {
$referenced_entity = NULL;
if (isset($item['target_id'])) {
$referenced_entity = $storage->load($item['target_id']);
}
elseif (isset($item['target_revision_id'])) {
$referenced_entity = $storage->loadRevision($item['target_revision_id']);
}
$langcode = $entity->language()
->getId();
if ($referenced_entity instanceof TranslatableInterface && $referenced_entity->hasTranslation($langcode)) {
$referenced_entity = $referenced_entity->getTranslation($langcode);
}
if (empty($referenced_entity)) {
continue;
}
$seen_id = $referenced_entity->getEntityTypeId() . '-' . $referenced_entity->id();
if (isset($seen[$seen_id])) {
$item['message'] = 'Recursion detected.';
$item['array_path'] = implode('.', $seen[$seen_id]);
continue;
}
$item['entity'] = $this->entityToArrayWithReferences($referenced_entity, $depth++, array_merge($array_path, [
$field,
$delta,
'entity',
]));
$item['bundle'] = $referenced_entity->bundle();
}
}
}
}
return $array;
}
}
Classes