config_preview_deploy-1.0.0-alpha3/src/EventSubscriber/ConfigTimestampSubscriber.php
src/EventSubscriber/ConfigTimestampSubscriber.php
<?php
declare(strict_types=1);
namespace Drupal\config_preview_deploy\EventSubscriber;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\State\StateInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Tracks configuration changes by updating timestamps.
*/
class ConfigTimestampSubscriber implements EventSubscriberInterface {
/**
* The state service.
*/
protected StateInterface $state;
/**
* Constructs a ConfigTimestampSubscriber object.
*/
public function __construct(StateInterface $state) {
$this->state = $state;
}
/**
* Responds to configuration save events.
*/
public function onConfigSave(ConfigCrudEvent $event): void {
$this->updateLastChangeTimestamp();
}
/**
* Responds to configuration delete events.
*/
public function onConfigDelete(ConfigCrudEvent $event): void {
$this->updateLastChangeTimestamp();
}
/**
* Updates the last change timestamp.
*/
protected function updateLastChangeTimestamp(): void {
$this->state->set('config_preview_deploy.last_change', time());
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
ConfigEvents::SAVE => ['onConfigSave'],
ConfigEvents::DELETE => ['onConfigDelete'],
];
}
}
