NewRelicConfigSubscriber.php in New Relic 2.x
File
src/EventSubscriber/NewRelicConfigSubscriber.php
View source
<?php
namespace Drupal\new_relic_rpm\EventSubscriber;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ConfigImporterEvent;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\new_relic_rpm\ExtensionAdapter\NewRelicAdapterInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class NewRelicConfigSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
protected $adapter;
protected $configFactory;
protected $currentUser;
public function __construct(NewRelicAdapterInterface $adapter, ConfigFactoryInterface $config_factory, AccountInterface $current_user) {
$this->adapter = $adapter;
$this->configFactory = $config_factory;
$this->currentUser = $current_user;
}
public static function getSubscribedEvents() {
$events[ConfigEvents::IMPORT][] = [
'onImport',
];
return $events;
}
public function onImport(ConfigImporterEvent $event) {
$config = $this->configFactory
->get('new_relic_rpm.settings');
if (!$config
->get('config_import')) {
return;
}
$changes = $event
->getChangelist();
$name = $this->currentUser
->getAccountName();
$description = $this
->t('A configuration import was run on the site.');
$changelog = '';
if (!empty($changes['create'])) {
$changelog .= 'Configurations created:
';
foreach ($changes['create'] as $config_key) {
$changelog .= $config_key . '
';
}
}
if (!empty($changes['update'])) {
$changelog .= 'Configurations updated:
';
foreach ($changes['update'] as $config_key) {
$changelog .= $config_key . '
';
}
}
if (!empty($changes['delete'])) {
$changelog .= 'Configurations deleted:
';
foreach ($changes['delete'] as $config_key) {
$changelog .= $config_key . '
';
}
}
if (!empty($changes['rename'])) {
$changelog .= 'Configurations renamed:
';
foreach ($changes['rename'] as $config_key) {
$changelog .= $config_key . '
';
}
}
$client = \Drupal::service('new_relic_rpm.client');
$client
->createDeployment('config_import', $description, $name, $changelog);
}
}