Skip to content
Snippets Groups Projects
Commit e7af1397 authored by Randal V's avatar Randal V
Browse files

Resolve #3519844 "Support oop hooks"

parent f8638504
No related branches found
No related tags found
1 merge request!5Resolve #3519844 "Support oop hooks"
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
"source": "https://git.drupalcode.org/project/fileslog" "source": "https://git.drupalcode.org/project/fileslog"
}, },
"require": { "require": {
"drupal/core": "^10 || ^11", "drupal/core": "^10.1 || ^11",
"php": ">=8.1" "php": ">=8.1"
} }
} }
name: Private Files Logging name: Private Files Logging
type: module type: module
package: Logging package: Logging
core_version_requirement: ^10 || ^11 core_version_requirement: ^10.1 || ^11
description: 'Logs and records system events to the private filesystem.' description: 'Logs and records system events to the private filesystem.'
configure: system.logging_settings configure: system.logging_settings
...@@ -5,68 +5,33 @@ ...@@ -5,68 +5,33 @@
* Redirects logging messages to the private filesystem. * Redirects logging messages to the private filesystem.
*/ */
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Hook\Attribute\LegacyHook;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\fileslog\FilesLogInterface; use Drupal\fileslog\Hook\Cron;
use Drupal\fileslog\Hook\FormAlter;
use Drupal\fileslog\Hook\Help;
/** /**
* Implements hook_help(). * Implements hook_help().
*/ */
function fileslog_help($route_name, RouteMatchInterface $route_match) { #[LegacyHook]
switch ($route_name) { function fileslog_help($route_name, RouteMatchInterface $route_match): ?string {
case 'help.page.fileslog': return \Drupal::service(Help::class)->help($route_match, $route_match);
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The fileslog module saves logs to the private filesystem.') . '</p>';
return $output;
}
} }
/** /**
* Implements hook_cron(). * Implements hook_cron().
*/ */
function fileslog_cron() { #[LegacyHook]
/** @var \Drupal\Core\File\FileSystemInterface $filesystem */ function fileslog_cron(): void {
$filesystem = \Drupal::service('file_system'); \Drupal::service(Cron::class)->cron();
/** @var \Drupal\fileslog\FilesLogManagerInterface $files_log_manager */
$files_log_manager = \Drupal::service('fileslog.manager');
$dir = FilesLogInterface::DIRECTORY;
$max_items = (int) \Drupal::config('fileslog.settings')->get('max_items') ?: 1000;
$filesystem->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$files = $files_log_manager->getLogFiles([], 0);
if (count($files) > $max_items) {
$to_delete = array_slice($files, $max_items, (count($files) - $max_items));
foreach ($to_delete as $uri => $file) {
$filesystem->unlink($uri);
}
}
} }
/** /**
* Implements hook_form_FORM_ID_alter(). * Implements hook_form_FORM_ID_alter().
*/ */
function fileslog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) { #[LegacyHook]
$config = \Drupal::configFactory()->getEditable('fileslog.settings'); function fileslog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state): void {
$form['max_items'] = [ \Drupal::service(FormAlter::class)->alterSystemLoggingSettingsForm($form, $form_state);
'#type' => 'number',
'#title' => t('Maximum logs'),
'#default_value' => $config->get('max_items'),
'#required' => TRUE,
'#description' => t('The maximum log items to keep in the private filesystem before deleting the oldest ones.'),
];
$form['#submit'][] = 'fileslog_logging_settings_submit';
}
/**
* Form submission handler for system_logging_settings().
*
* @see fileslog_form_system_logging_settings_alter()
*/
function fileslog_logging_settings_submit($form, FormStateInterface $form_state) {
\Drupal::configFactory()->getEditable('fileslog.settings')
->set('max_items', $form_state->getValue('max_items'))
->save();
} }
parameters:
fileslog.hooks_converted: true
services: services:
_defaults:
autowire: true
# The fileslog manager. # The fileslog manager.
fileslog.manager: fileslog.manager:
class: Drupal\fileslog\FilesLogManager class: Drupal\fileslog\FilesLogManager
arguments: ['@file_system', '@request_stack'] Drupal\fileslog\FilesLogManagerInterface: '@fileslog.manager'
# The logger service. # The logger service.
logger.fileslog: logger.fileslog:
class: Drupal\fileslog\Logger\FilesLog class: Drupal\fileslog\Logger\FilesLog
arguments: ['@logger.log_message_parser']
tags: tags:
- { name: logger } - { name: logger }
# Hook services.
Drupal\fileslog\Hook\Help:
class: Drupal\fileslog\Hook\Help
Drupal\fileslog\Hook\Cron:
class: Drupal\fileslog\Hook\Cron
Drupal\fileslog\Hook\FormSystemLoggingSettingsAlter:
class: Drupal\fileslog\Hook\FormSystemLoggingSettingsAlter
<?php
namespace Drupal\fileslog\Hook;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\fileslog\FilesLogInterface;
use Drupal\fileslog\FilesLogManagerInterface;
/**
* Hooks onto cron events.
*/
class Cron {
/**
* Constructs the cron hook class.
*/
public function __construct(
protected FileSystemInterface $fileSystem,
protected ConfigFactoryInterface $configFactory,
protected FilesLogManagerInterface $filesLogManager,
) {}
/**
* Executes the 'cron'-hook.
*/
#[Hook('cron')]
public function cron(): void {
$max_items = (int) ($this->configFactory->get('fileslog.settings')->get('max_items') ?: 1000);
$dir = FilesLogInterface::DIRECTORY;
$this->fileSystem->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$files = $this->filesLogManager->getLogFiles([], 0);
if (count($files) > $max_items) {
$to_delete = array_slice($files, $max_items, (count($files) - $max_items));
foreach ($to_delete as $uri => $file) {
$this->fileSystem->unlink($uri);
}
}
}
}
<?php
namespace Drupal\fileslog\Hook;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Hook\Attribute\Hook;
/**
* Hooks onto form alter events.
*/
class FormAlter {
/**
* Constructs the form_system_logging_settings_alter hook class.
*/
public function __construct(
protected ConfigFactoryInterface $configFactory,
) {}
/**
* Executes the 'form_system_logging_settings_alter'-hook.
*/
#[Hook('form_system_logging_settings_alter')]
public function alterSystemLoggingSettingsForm(&$form, FormStateInterface $form_state): void {
$config = $this->configFactory->getEditable('fileslog.settings');
$form['max_items'] = [
'#type' => 'number',
'#title' => t('Maximum logs'),
'#default_value' => $config->get('max_items'),
'#required' => TRUE,
'#description' => t('The maximum log items to keep in the private filesystem before deleting the oldest ones.'),
];
$form['#submit'][] = [$this, 'submitForm'];
}
/**
* Form submission handler for system_logging_settings().
*
* @see static::alterForm()
*/
public function submitForm($form, FormStateInterface $form_state) {
$this->configFactory->getEditable('fileslog.settings')
->set('max_items', $form_state->getValue('max_items'))
->save();
}
}
<?php
namespace Drupal\fileslog\Hook;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Hooks onto help events.
*/
class Help {
/**
* Executes the 'help'-hook.
*/
#[Hook('help')]
public function help($route_name, RouteMatchInterface $route_match): ?string {
if ($route_name === 'help.page.fileslog') {
return '<h3>' . t('About') . '</h3>'
. '<p>' . t('The fileslog module saves logs to the private filesystem.') . '</p>';
}
return NULL;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment