Namespace
  Drupal\devel
File
  - 
              src/DevelDumperManager.php
    
   
  
    View source
  
  <?php
namespace Drupal\devel;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class DevelDumperManager implements DevelDumperManagerInterface {
  use StringTranslationTrait;
  use MessengerTrait;
  
  protected $config;
  
  protected $account;
  
  protected $dumperManager;
  
  public function __construct(ConfigFactoryInterface $config_factory, AccountProxyInterface $account, DevelDumperPluginManagerInterface $dumper_manager) {
    $this->config = $config_factory->get('devel.settings');
    $this->account = $account;
    $this->dumperManager = $dumper_manager;
  }
  
  protected function createInstance($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, $name = NULL, $plugin_id = NULL) {
    if ($this->hasAccessToDevelInformation()) {
      $this->createInstance($plugin_id)
        ->dump($input, $name);
    }
  }
  
  public function export($input, $name = NULL, $plugin_id = NULL) {
    if ($this->hasAccessToDevelInformation()) {
      return $this->createInstance($plugin_id)
        ->export($input, $name);
    }
    return NULL;
  }
  
  public function message($input, $name = NULL, $type = MessengerInterface::TYPE_STATUS, $plugin_id = NULL) {
    if ($this->hasAccessToDevelInformation()) {
      $output = $this->export($input, $name, $plugin_id);
      $this->messenger()
        ->addMessage($output, $type, TRUE);
    }
  }
  
  public function debug($input, $name = NULL, $plugin_id = NULL) {
    $output = $this->createInstance($plugin_id)
      ->export($input, $name) . "\n";
    
    $file = $this->config
      ->get('debug_logfile');
    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, $name = NULL, $export = TRUE, $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, $name = NULL, $plugin_id = NULL) {
    if ($this->hasAccessToDevelInformation()) {
      return $this->createInstance($plugin_id)
        ->exportAsRenderable($input, $name);
    }
    return [];
  }
  
  protected function hasAccessToDevelInformation() {
    return $this->account && $this->account
      ->hasPermission('access devel information');
  }
}
 
Classes