You are here

class DevelopmentEnvironmentController in Development Environment 8

The DevelopmentEnvironmentController class.

Hierarchy

Expanded class hierarchy of DevelopmentEnvironmentController

File

src/Controller/DevelopmentEnvironmentController.php, line 19

Namespace

Drupal\development_environment\Controller
View source
class DevelopmentEnvironmentController extends ControllerBase implements DevelopmentEnvironmentControllerInterface {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * The variable dump service.
   *
   * @var \Drupal\development_environment\Service\VarDumpServiceInterface
   */
  protected $varDumpService;

  /**
   * The form builder service.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * Constructs a DevelopmentEnvironmentController object.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The datagbase connection.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
   *   The date formatter service.
   * @param \Drupal\Core\Session\AccountProxyInterface $currentUser
   *   The current user.
   * @param \Drupal\development_environment\Service\VarDumpServiceInterface $varDumpService
   *   The variable dump service.
   * @param \Drupal\Core\Form\FormBuilderInterface $formBuilder
   *   The form builder interface.
   */
  public function __construct(Connection $database, DateFormatterInterface $dateFormatter, AccountProxyInterface $currentUser, VarDumpServiceInterface $varDumpService, FormBuilderInterface $formBuilder) {
    $this->database = $database;
    $this->dateFormatter = $dateFormatter;
    $this->currentUser = $currentUser;
    $this->varDumpService = $varDumpService;
    $this->formBuilder = $formBuilder;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('database'), $container
      ->get('date.formatter'), $container
      ->get('current_user'), $container
      ->get('development_environment.var.dump.service'), $container
      ->get('form_builder'));
  }

  /**
   * {@inheritdoc}
   */
  public function logListPage() {
    $query = $this->database
      ->select('development_environment_log', 'log_data')
      ->extend('\\Drupal\\Core\\Database\\Query\\PagerSelectExtender');
    $query
      ->fields('log_data', [
      'lid',
      'email_data',
      'timestamp',
      'recipient_email',
      'subject',
    ]);
    $log_items = $query
      ->limit(20)
      ->orderBy('lid', 'DESC')
      ->execute();
    $page = [
      'form' => $this->formBuilder
        ->getForm('\\Drupal\\development_environment\\Form\\DevelopmentEnvironmentClearLogForm'),
      'items' => [
        '#theme' => 'table',
        '#header' => [
          '',
          $this
            ->t('Time'),
          $this
            ->t('Recipient'),
          $this
            ->t('Subject'),
        ],
        '#rows' => [],
        '#empty' => $this
          ->t('No emails have been logged'),
      ],
      'pager' => [
        '#type' => 'pager',
      ],
    ];
    foreach ($log_items as $item) {
      $url = Url::fromRoute('development_environment.suppressed_email_log', [
        'lid' => $item->lid,
      ]);
      $link = Link::fromTextAndUrl($this
        ->t('View'), $url);
      $page['items']['#rows'][] = [
        $link,
        $this->dateFormatter
          ->format($item->timestamp, 'short', '', $this->currentUser
          ->getTimeZone()),
        $item->recipient_email,
        $item->subject,
      ];
    }
    return $page;
  }

  /**
   * {@inheritdoc}
   */
  public function mailLogPage($lid) {
    $mail_log = $this->database
      ->query('SELECT timestamp, email_data FROM {development_environment_log} WHERE lid = :lid', [
      ':lid' => $lid,
    ])
      ->fetch();
    if (!$mail_log) {
      throw new NotFoundHttpException();
    }
    $mail_info = unserialize($mail_log->email_data);
    if (is_array($mail_info['body'])) {
      $body = implode('<br/>', $mail_info['body']);
    }
    else {
      $body = $mail_info['body'];
    }
    $page = [
      'header' => [
        '#prefix' => '<h2>',
        '#suffix' => '</h2>',
        '#markup' => $this
          ->t('The following email was not sent as this is a development environment'),
      ],
      'email' => [
        '#type' => 'details',
        '#title' => $this
          ->t('Email data'),
        '#open' => TRUE,
        'time' => [
          '#prefix' => '<p>',
          '#suffix' => '</p>',
          '#markup' => $this
            ->t('Time: %time', [
            '%time' => $this->dateFormatter
              ->format($mail_log->timestamp, 'short', '', $this->currentUser
              ->getTimeZone()),
          ]),
        ],
        'recipient' => [
          '#prefix' => '<p>',
          '#suffix' => '</p>',
          '#markup' => $this
            ->t('Recipient: %recipient', [
            '%recipient' => $mail_info['to'],
          ]),
        ],
        'subject' => [
          '#prefix' => '<p>',
          '#suffix' => '</p>',
          '#markup' => $this
            ->t('Subject: %subject', [
            '%subject' => $mail_info['subject'],
          ]),
        ],
        'body' => [
          '#prefix' => '<div>',
          '#suffix' => '</div><br/>',
          '#markup' => $this
            ->t('Body: %body', [
            '%body' => $body,
          ]),
        ],
        'headers' => [
          '#prefix' => '<div>',
          '#suffix' => '</div>',
          '#markup' => $this
            ->t('Headers:') . $this->varDumpService
            ->varDump($mail_info['headers'], TRUE, TRUE),
        ],
      ],
      'raw_mail_data' => [
        '#type' => 'details',
        '#title' => $this
          ->t('Raw email data'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        'data' => [
          '#markup' => $this->varDumpService
            ->varDump($mail_info, TRUE, TRUE),
        ],
      ],
    ];
    return $page;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsPage() {
    $page = [
      '#prefix' => '<div id="development_environment_settings_page">',
      '#suffix' => '</div>',
      'form' => $this->formBuilder
        ->getForm('\\Drupal\\development_environment\\Form\\DevelopmentEnvironmentSettingsForm'),
    ];
    return $page;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
DevelopmentEnvironmentController::$currentUser protected property The current user. Overrides ControllerBase::$currentUser
DevelopmentEnvironmentController::$database protected property The database connection.
DevelopmentEnvironmentController::$dateFormatter protected property The date formatter service.
DevelopmentEnvironmentController::$formBuilder protected property The form builder service. Overrides ControllerBase::$formBuilder
DevelopmentEnvironmentController::$varDumpService protected property The variable dump service.
DevelopmentEnvironmentController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
DevelopmentEnvironmentController::logListPage public function Creates the log page listing all email logs. Overrides DevelopmentEnvironmentControllerInterface::logListPage
DevelopmentEnvironmentController::mailLogPage public function Log page for an individual email. Overrides DevelopmentEnvironmentControllerInterface::mailLogPage
DevelopmentEnvironmentController::settingsPage public function Settings page for the Development Environment module. Overrides DevelopmentEnvironmentControllerInterface::settingsPage
DevelopmentEnvironmentController::__construct public function Constructs a DevelopmentEnvironmentController object.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.