Skip to content
Snippets Groups Projects
Commit 2b4042c9 authored by Chaitanya Dessai's avatar Chaitanya Dessai
Browse files

Merge branch '3373694-fix-the-issues' into '2.0.x'

Fix the issues reported by phpcs.

See merge request !5
parents 1b95bf43 c0401805
No related branches found
No related tags found
No related merge requests found
Pipeline #272193 skipped
...@@ -5,47 +5,50 @@ ...@@ -5,47 +5,50 @@
* Devel debug log module install/schema hooks. * Devel debug log module install/schema hooks.
*/ */
/**
* {@inheritdoc}
*/
function devel_debug_log_schema() { function devel_debug_log_schema() {
$schema = array(); $schema = [];
$schema['devel_debug_log'] = array( $schema['devel_debug_log'] = [
'description' => 'Table for storing debug messages.', 'description' => 'Table for storing debug messages.',
'fields' => array( 'fields' => [
'id' => array( 'id' => [
'description' => 'The message identifier.', 'description' => 'The message identifier.',
'type' => 'serial', 'type' => 'serial',
'unsigned' => TRUE, 'unsigned' => TRUE,
'not null' => TRUE, 'not null' => TRUE,
), ],
'timestamp' => array( 'timestamp' => [
'description' => 'The Unix timestamp when the message was saved.', 'description' => 'The Unix timestamp when the message was saved.',
'type' => 'int', 'type' => 'int',
'not null' => TRUE, 'not null' => TRUE,
'default' => 0, 'default' => 0,
), ],
'title' => array( 'title' => [
'description' => 'The title of the debug message.', 'description' => 'The title of the debug message.',
'type' => 'varchar', 'type' => 'varchar',
'length' => '255', 'length' => '255',
'not null' => TRUE, 'not null' => TRUE,
'default' => '', 'default' => '',
), ],
'message' => array( 'message' => [
'description' => 'The debug message.', 'description' => 'The debug message.',
'type' => 'text', 'type' => 'text',
'size' => 'big', 'size' => 'big',
'not null' => TRUE, 'not null' => TRUE,
), ],
'serialized' => array( 'serialized' => [
'description' => 'Indicates whether the message is serialized.', 'description' => 'Indicates whether the message is serialized.',
'type' => 'int', 'type' => 'int',
'size' => 'small', 'size' => 'small',
'not null' => TRUE, 'not null' => TRUE,
'default' => 0, 'default' => 0,
), ],
), ],
'primary key' => array('id'), 'primary key' => ['id'],
); ];
return $schema; return $schema;
} }
...@@ -2,4 +2,4 @@ devel_debug_log.list: ...@@ -2,4 +2,4 @@ devel_debug_log.list:
title: Debug messages title: Debug messages
description: 'List debug messages.' description: 'List debug messages.'
route_name: devel_debug_log.list route_name: devel_debug_log.list
parent: system.admin_reports parent: system.admin_reports
\ No newline at end of file
...@@ -12,54 +12,56 @@ use Kint\Renderer\RichRenderer; ...@@ -12,54 +12,56 @@ use Kint\Renderer\RichRenderer;
* Saves a debug message. * Saves a debug message.
* *
* @param mixed $message * @param mixed $message
* A debug message to save, which can be: * A debug message to save, which can be:
* - string: saved as it is; * - string: saved as it is;
* - object/array: serialized before saving; * - object/array: serialized before saving;
* *
* Currently, there's development going on regarding the serialization part, * Currently, there's development going on regarding the serialization part,
* since most of the data in D8 contains references to injected services and * since most of the data in D8 contains references to injected services and
* maybe even plugins. * maybe even plugins.
* A discussion about this topic can be found here: * A discussion about this topic can be found here:.
* @see https://www.drupal.org/node/2705731
*
* @param string $title * @param string $title
* Title of the debug message. * Title of the debug message.
*
* @see https://www.drupal.org/node/2705731
* *
* @return void * @return void
* Return void.
*
* @throws \Exception * @throws \Exception
*/ */
function ddl(mixed $message, string $title = ''): void { function devel_debug_log_ddl(mixed $message, string $title = ''): void {
$serialized = FALSE; $serialized = FALSE;
$message = devel_debug_log_ob_kint($message); $message = devel_debug_log_ob_kint($message);
// TODO Find a way to store the initial message, instead of storing the entire // @todo Find a way to store the initial message, instead of storing the entire
// debug markup returned by kint(). // debug markup returned by kint().
$connection = \Drupal::database(); $connection = \Drupal::database();
$query = $connection->insert('devel_debug_log') $query = $connection->insert('devel_debug_log')
->fields(array( ->fields([
'timestamp' => \Drupal::time()->getRequestTime(), 'timestamp' => \Drupal::time()->getRequestTime(),
'title' => $title, 'title' => $title,
'message' => $message, 'message' => $message,
'serialized' => $serialized ? 1 : 0, 'serialized' => $serialized ? 1 : 0,
)); ]);
$query->execute(); $query->execute();
} }
/** /**
* Checks if the message has already been saved during the current page request, * Checks if the message has already been saved during the current page request.
* and saves the message only if it is not a repetition of a previous one.
* *
* @param mixed $message * @param mixed $message
* A debug message to save, which can be: * A debug message to save, which can be:
* - string: saved as is. * - string: saved as is.
* - object or array: saved serialized. * - object or array: saved serialized.
* @param string $title * @param string $title
* Title for the debug message. * Title for the debug message.
* *
* @return void * @return void
* Returns void.
*
* @throws \Exception * @throws \Exception
*/ */
function ddl_once(mixed $message, string $title = ''): void { function devel_debug_log_once(mixed $message, string $title = ''): void {
$message_history = &drupal_static(__FUNCTION__); $message_history = &drupal_static(__FUNCTION__);
if (!is_resource($message)) { if (!is_resource($message)) {
...@@ -72,7 +74,7 @@ function ddl_once(mixed $message, string $title = ''): void { ...@@ -72,7 +74,7 @@ function ddl_once(mixed $message, string $title = ''): void {
$message_history[] = $hash; $message_history[] = $hash;
} }
ddl($message, $title); devel_debug_log_ddl($message, $title);
} }
/** /**
...@@ -85,21 +87,21 @@ function devel_debug_log_theme(array &$cache, $type, $theme, $path): array { ...@@ -85,21 +87,21 @@ function devel_debug_log_theme(array &$cache, $type, $theme, $path): array {
'content' => NULL, 'content' => NULL,
'delete_form' => NULL, 'delete_form' => NULL,
], ],
] ],
]; ];
} }
/** /**
* Provides debug output for later printing. * Provides debug output for later printing.
* *
* kint() outputs the debug information at the top of the page. This function * Kint() outputs the debug information at the top of the page. This function
* allows you to get the output and use it for later printing. * allows you to get the output and use it for later printing.
* *
* @param mixed $message * @param mixed $message
* The data that's displayed for debugging. * The data that's displayed for debugging.
* *
* @return string * @return string
* The debug information. * The debug information.
*/ */
function devel_debug_log_ob_kint(mixed $message): string { function devel_debug_log_ob_kint(mixed $message): string {
ob_start(); ob_start();
...@@ -107,7 +109,8 @@ function devel_debug_log_ob_kint(mixed $message): string { ...@@ -107,7 +109,8 @@ function devel_debug_log_ob_kint(mixed $message): string {
Kint::$enabled_mode = Kint::MODE_RICH; Kint::$enabled_mode = Kint::MODE_RICH;
RichRenderer::$folder = FALSE; RichRenderer::$folder = FALSE;
Kint::dump($message); Kint::dump($message);
} else { }
else {
\Drupal::service('devel.dumper')->dump($message); \Drupal::service('devel.dumper')->dump($message);
} }
$output = ob_get_contents(); $output = ob_get_contents();
......
...@@ -6,13 +6,16 @@ use Drupal\Core\Controller\ControllerBase; ...@@ -6,13 +6,16 @@ use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection; use Drupal\Core\Database\Connection;
use Drupal\Core\Datetime\DateFormatter; use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormBuilderInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\Serializer;
/**
* The main controller for the module.
*/
class DevelDebugLogController extends ControllerBase { class DevelDebugLogController extends ControllerBase {
/** /**
* The Database Connection * The Database Connection.
* *
* @var \Drupal\Core\Database\Connection * @var \Drupal\Core\Database\Connection
*/ */
...@@ -21,7 +24,7 @@ class DevelDebugLogController extends ControllerBase { ...@@ -21,7 +24,7 @@ class DevelDebugLogController extends ControllerBase {
/** /**
* The Serializer service. * The Serializer service.
* *
* @var Symfony\Component\Serializer\Serializer; * @var Symfony\Component\Serializer\Serializer
*/ */
protected $serializer; protected $serializer;
...@@ -35,7 +38,7 @@ class DevelDebugLogController extends ControllerBase { ...@@ -35,7 +38,7 @@ class DevelDebugLogController extends ControllerBase {
/** /**
* The FormBuilder object. * The FormBuilder object.
* *
* @var Drupal\Core\Form\FormBuilderInterface; * @var Drupal\Core\Form\FormBuilderInterface
*/ */
protected $formBuilder; protected $formBuilder;
...@@ -54,10 +57,14 @@ class DevelDebugLogController extends ControllerBase { ...@@ -54,10 +57,14 @@ class DevelDebugLogController extends ControllerBase {
/** /**
* DevelDebugLogController constructor. * DevelDebugLogController constructor.
* *
* @param Connection $database * @param \Drupal\Core\Database\Connection $database
* @param DateFormatter $dateFormatter * The database object.
* @param Serializer $serializer * @param \Drupal\Core\Datetime\DateFormatter $dateFormatter
* @param FormBuilderInterface $formBuilder * The dateFormatter object.
* @param \Symfony\Component\Serializer\Serializer $serializer
* The serializer object.
* @param \Drupal\Core\Form\FormBuilderInterface $formBuilder
* The formBuilder object.
*/ */
public function __construct(Connection $database, DateFormatter $dateFormatter, Serializer $serializer, FormBuilderInterface $formBuilder) { public function __construct(Connection $database, DateFormatter $dateFormatter, Serializer $serializer, FormBuilderInterface $formBuilder) {
$this->database = $database; $this->database = $database;
...@@ -70,7 +77,7 @@ class DevelDebugLogController extends ControllerBase { ...@@ -70,7 +77,7 @@ class DevelDebugLogController extends ControllerBase {
* Lists debug information. * Lists debug information.
* *
* @return array * @return array
* Renderable array that contains a list of debug data. * Renderable array that contains a list of debug data.
*/ */
public function listLogs() { public function listLogs() {
$query = $this->database->select('devel_debug_log', 'm') $query = $this->database->select('devel_debug_log', 'm')
...@@ -81,31 +88,32 @@ class DevelDebugLogController extends ControllerBase { ...@@ -81,31 +88,32 @@ class DevelDebugLogController extends ControllerBase {
$rows = []; $rows = [];
foreach ($results as $result) { foreach ($results as $result) {
$rows[] = array( $rows[] = [
'title' => $result->title, 'title' => $result->title,
'time' => $this->dateFormatter 'time' => $this->dateFormatter
->format($result->timestamp, 'short'), ->format($result->timestamp, 'short'),
'message' => $result->message, 'message' => $result->message,
); ];
} }
if (empty($rows)) { if (empty($rows)) {
return array( return [
'#markup' => $this->t('No debug messages.'), '#markup' => $this->t('No debug messages.'),
); ];
} }
$build = array( $build = [
'messages' => array( 'messages' => [
'#theme' => 'devel_debug_log_list', '#theme' => 'devel_debug_log_list',
'#content' => $rows, '#content' => $rows,
'#delete_form' => $this->formBuilder->getForm('Drupal\devel_debug_log\Form\DevelDebugLogDeleteForm'), '#delete_form' => $this->formBuilder->getForm('Drupal\devel_debug_log\Form\DevelDebugLogDeleteForm'),
), ],
'pager' => array( 'pager' => [
'#type' => 'pager' '#type' => 'pager',
), ],
); ];
return $build; return $build;
} }
} }
...@@ -8,9 +8,14 @@ use Drupal\Core\Form\FormStateInterface; ...@@ -8,9 +8,14 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* The delete form.
*/
class DevelDebugLogDeleteForm extends FormBase { class DevelDebugLogDeleteForm extends FormBase {
/** /**
* The messenger interface.
*
* @var \Drupal\Core\Messenger\MessengerInterface * @var \Drupal\Core\Messenger\MessengerInterface
*/ */
protected $messenger; protected $messenger;
...@@ -34,39 +39,39 @@ class DevelDebugLogDeleteForm extends FormBase { ...@@ -34,39 +39,39 @@ class DevelDebugLogDeleteForm extends FormBase {
$this->messenger = $messenger; $this->messenger = $messenger;
} }
/** /**
* @inheritdoc * {@inheritdoc}
*/ */
public function getFormId() { public function getFormId() {
return 'ddl_delete_form'; return 'ddl_delete_form';
} }
/** /**
* @inheritdoc * {@inheritdoc}
*/ */
public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state) {
$form['ddl_clear'] = array( $form['ddl_clear'] = [
'#type' => 'fieldset', '#type' => 'fieldset',
'#title' => $this->t('Clear debug log messages'), '#title' => $this->t('Clear debug log messages'),
'#description' => $this->t('This will permanently remove the log messages from the database.'), '#description' => $this->t('This will permanently remove the log messages from the database.'),
'#collapsible' => TRUE, '#collapsible' => TRUE,
'#collapsed' => TRUE, '#collapsed' => TRUE,
); ];
$form['ddl_clear']['clear'] = array( $form['ddl_clear']['clear'] = [
'#type' => 'submit', '#type' => 'submit',
'#value' => $this->t('Clear log messages'), '#value' => $this->t('Clear log messages'),
); ];
return $form; return $form;
} }
/** /**
* @inheritdoc * {@inheritdoc}
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
Database::getConnection()->delete('devel_debug_log') Database::getConnection()->delete('devel_debug_log')
->execute(); ->execute();
$this->messenger->addMessage($this->t('All debug messages have been cleared.')); $this->messenger->addMessage($this->t('All debug messages have been cleared.'));
} }
} }
...@@ -24,12 +24,12 @@ class DevelDebugLogExtension extends AbstractExtension { ...@@ -24,12 +24,12 @@ class DevelDebugLogExtension extends AbstractExtension {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getFunctions() { public function getFunctions() {
return array( return [
new TwigFunction('ddl', array($this, 'ddl'), array( new TwigFunction('ddl', [$this, 'ddl'], [
'needs_environment' => TRUE, 'needs_environment' => TRUE,
'needs_context' => TRUE, 'needs_context' => TRUE,
)), ]),
); ];
} }
/** /**
...@@ -41,14 +41,14 @@ class DevelDebugLogExtension extends AbstractExtension { ...@@ -41,14 +41,14 @@ class DevelDebugLogExtension extends AbstractExtension {
* An array of parameters passed to the template. * An array of parameters passed to the template.
*/ */
public function ddl(\Twig_Environment $env, array $context) { public function ddl(\Twig_Environment $env, array $context) {
// Don't do anything unless twig_debug is enabled. This reads from the Twig // Don't do anything unless twig_debug is enabled. This reads from the Twig.
if (!$env->isDebug()) { if (!$env->isDebug()) {
return; return;
} }
if (func_num_args() === 2) { if (func_num_args() === 2) {
// No arguments passed, display full Twig context. // No arguments passed, display full Twig context.
$ddl_variables = array(); $ddl_variables = [];
foreach ($context as $key => $value) { foreach ($context as $key => $value) {
if (!$value instanceof \Twig_Template) { if (!$value instanceof \Twig_Template) {
$ddl_variables[$key] = $value; $ddl_variables[$key] = $value;
......
...@@ -5,4 +5,4 @@ ...@@ -5,4 +5,4 @@
{% endif %} {% endif %}
{{ value.time }} {{ value.time }}
{{ value.message|raw }} {{ value.message|raw }}
{% endfor %} {% endfor %}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment