clean install

This commit is contained in:
matze committed 2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 files changed
+3555714

No files matched your search

@@ -0,0 +1,9 @@
{
"extra": {
"drush": {
"services": {
"drush.services.yml": "^10"
}
}
}
}
@@ -0,0 +1,10 @@
farm_update.settings:
type: config_object
label: 'farmOS Update settings'
mapping:
exclude_config:
type: sequence
label: 'Configuration items excluded from automatic updates'
sequence:
type: string
label: 'Configuration item'
@@ -0,0 +1,6 @@
services:
farm_update.commands:
class: Drupal\farm_update\Commands\FarmUpdateCommands
arguments: [ '@farm.update']
tags:
- { name: drush.command }
@@ -0,0 +1,32 @@
<?php
/**
* @file
* Hooks provided by farm_update.
*
* This file contains no working PHP code; it exists to provide additional
* documentation for doxygen as well as to document hooks in the standard
* Drupal manner.
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Specify config items that should be excluded from automatic updates.
*
* @return array
* An array of config item names.
*/
function hook_farm_update_exclude_config() {
return [
'views.view.farm_log',
'asset.type.structure',
];
}
/**
* @} End of "addtogroup hooks".
*/
@@ -0,0 +1,7 @@
name: farmOS Update
description: Automatically update farmOS configuration entities. Turn this off if configuration is managed by other means.
type: module
package: farmOS
core_version_requirement: ^10
dependencies:
- config_update:config_update
@@ -0,0 +1,25 @@
<?php
/**
* @file
* Hooks and customizations for the farm_update module.
*/
/**
* Implements hook_rebuild().
*/
function farm_update_rebuild() {
\Drupal::service('farm.update')->rebuild();
}
/**
* Implements hook_farm_update_exclude_config().
*/
function farm_update_farm_update_exclude_config() {
// Exclude Drupal core configurations from automatic updates.
return [
'user.role.anonymous',
'user.role.authenticated',
];
}
@@ -0,0 +1,7 @@
services:
logger.channel.farm_update:
parent: logger.channel_base
arguments: [ 'farm_update' ]
farm.update:
class: Drupal\farm_update\FarmUpdate
arguments: [ '@logger.channel.farm_update', '@module_handler', '@entity_type.manager', '@config.factory', '@config_update.config_diff', '@config_update.config_list', '@config_update.config_update' ]
@@ -0,0 +1,44 @@
<?php
namespace Drupal\farm_update\Commands;
use Drupal\farm_update\FarmUpdateInterface;
use Drush\Commands\DrushCommands;
/**
* Farm Update Drush commands.
*
* @ingroup farm
*/
class FarmUpdateCommands extends DrushCommands {
/**
* Farm update service.
*
* @var \Drupal\farm_update\FarmUpdateInterface
*/
protected $farmUpdate;
/**
* FarmUpdateCommands constructor.
*
* @param \Drupal\farm_update\FarmUpdateInterface $farm_update
* Farm update service.
*/
public function __construct(FarmUpdateInterface $farm_update) {
parent::__construct();
$this->farmUpdate = $farm_update;
}
/**
* Rebuild farmOS configuration.
*
* @command farm_update:rebuild
*
* @usage farm_update:rebuild
*/
public function rebuild() {
$this->farmUpdate->rebuild();
}
}
@@ -0,0 +1,233 @@
<?php
namespace Drupal\farm_update;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\config_update\ConfigDiffer;
use Drupal\config_update\ConfigListerWithProviders;
use Drupal\config_update\ConfigReverter;
use Psr\Log\LoggerInterface;
/**
* Farm update service.
*
* @internal
*/
class FarmUpdate implements FarmUpdateInterface {
use StringTranslationTrait;
/**
* The logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityManager;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The config differ.
*
* @var \Drupal\config_update\ConfigDiffer
*/
protected $configDiff;
/**
* The config lister.
*
* @var \Drupal\config_update\ConfigListerWithProviders
*/
protected $configList;
/**
* The config reverter.
*
* @var \Drupal\config_update\ConfigReverter
*/
protected $configUpdate;
/**
* Constructs a FarmUpdate object.
*
* @param \Psr\Log\LoggerInterface $logger
* The logger.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
* The entity type manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\config_update\ConfigDiffer $config_diff
* The config differ.
* @param \Drupal\config_update\ConfigListerWithProviders $config_list
* The config lister.
* @param \Drupal\config_update\ConfigReverter $config_update
* The config reverter.
*/
public function __construct(LoggerInterface $logger, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, ConfigDiffer $config_diff, ConfigListerWithProviders $config_list, ConfigReverter $config_update) {
$this->logger = $logger;
$this->moduleHandler = $module_handler;
$this->entityManager = $entity_manager;
$this->configFactory = $config_factory;
$this->configDiff = $config_diff;
$this->configList = $config_list;
$this->configUpdate = $config_update;
}
/**
* {@inheritdoc}
*/
public function rebuild(): void {
// Get a list of excluded config.
$exclude_config = $this->getExcludedItems();
// Build a list of config to revert, without excluded config.
$revert_config = array_diff($this->getDifferentItems('type', 'system.all'), $exclude_config);
// Iterate through config items and revert them.
foreach ($revert_config as $name) {
// Get the config type and bail if simple configuration.
// The lister gives NULL if simple configuration.
$type = $this->configList->getTypeNameByConfigName($name);
if ($type === NULL) {
continue;
}
// Get the config short name.
$shortname = $this->getConfigShortname($type, $name);
// Perform the operation.
$result = $this->configUpdate->revert($type, $shortname);
// Log the result.
if ($result) {
$this->logger->notice('Reverted config: @config', ['@config' => $name]);
}
else {
$this->logger->error('Failed to revert config: @config', ['@config' => $name]);
}
}
}
/**
* Lists excluded config items.
*
* Lists config items that should be excluded from all automatic updates.
*
* @return array
* An array of config item names.
*/
protected function getExcludedItems() {
// Ask modules for config exclusions.
$exclude_config = $this->moduleHandler->invokeAll('farm_update_exclude_config');
// Load farm_update.settings to get additional exclusions.
$settings_exclude_config = $this->configFactory->get('farm_update.settings')->get('exclude_config');
if (!empty($settings_exclude_config)) {
$exclude_config = array_merge($exclude_config, $settings_exclude_config);
}
// Always exclude this module's configuration.
// This isn't strictly necessary because we don't provide default config
// in config/install. But in the unlikely event that a custom module does
// provide this config, and then it is somehow overridden by another means,
// it would be reverted. So we exclude it here just to be extra safe.
$exclude_config[] = 'farm_update.settings';
return $exclude_config;
}
/**
* Lists differing config items.
*
* Lists config items that differ from the versions provided by your
* installed modules, themes, or install profile. See config-diff to show
* what the differences are.
*
* This method is copied directly from ConfigUpdateUiCliService.
*
* @param string $type
* Run the report for: module, theme, profile, or "type" for config entity
* type.
* @param string $name
* The machine name of the module, theme, etc. to report on. See
* config-list-types to list types for config entities; you can also use
* system.all for all types, or system.simple for simple config.
*
* @return array
* An array of differing configuration items.
*
* @see \Drupal\config_update_ui\ConfigUpdateUiCliService::getDifferentItems()
*/
protected function getDifferentItems($type, $name) {
[$activeList, $installList, $optionalList] = $this->configList->listConfig($type, $name);
$addedItems = array_diff($activeList, $installList, $optionalList);
$activeAndAddedItems = array_diff($activeList, $addedItems);
$differentItems = [];
foreach ($activeAndAddedItems as $name) {
$active = $this->configUpdate->getFromActive('', $name);
$extension = $this->configUpdate->getFromExtension('', $name);
if (!$this->configDiff->same($active, $extension)) {
$differentItems[] = $name;
}
}
sort($differentItems);
return $differentItems;
}
/**
* Gets the config item shortname given the type and name.
*
* This method is copied directly from ConfigUpdateUiCliService.
*
* @param string $type
* The type of the config item.
* @param string $name
* The name of the config item.
*
* @return string
* The shortname for the configuration item.
*
* @see \Drupal\config_update_ui\ConfigUpdateUiCliService::getConfigShortname()
*/
protected function getConfigShortname($type, $name) {
$shortname = $name;
if ($type != 'system.simple') {
$definition = $this->entityManager->getDefinition($type);
$prefix = $definition->getConfigPrefix() . '.';
if (strpos($name, $prefix) === 0) {
$shortname = substr($name, strlen($prefix));
}
}
return $shortname;
}
}
@@ -0,0 +1,17 @@
<?php
namespace Drupal\farm_update;
/**
* Farm update service interface.
*
* @internal
*/
interface FarmUpdateInterface {
/**
* Rebuild farmOS configuration.
*/
public function rebuild(): void;
}
@@ -0,0 +1,2 @@
exclude_config:
- farm_flag.flag.review
@@ -0,0 +1,7 @@
name: farmOS Update Tests
description: 'Support module for farmOS Update testing.'
type: module
package: Testing
core_version_requirement: ^10
dependencies:
- farm:farm_update
@@ -0,0 +1,13 @@
<?php
/**
* @file
* Contains farm_update_test.module.
*/
/**
* Implements hook_farm_update_exclude_config().
*/
function farm_update_test_farm_update_exclude_config() {
return ['farm_flag.flag.priority'];
}
@@ -0,0 +1,93 @@
<?php
namespace Drupal\Tests\farm_update\Kernel;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests for farmOS Update module.
*
* @group farm
*/
class FarmUpdateTest extends KernelTestBase {
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The farm update service.
*
* @var \Drupal\farm_update\FarmUpdateInterface
*/
protected $farmUpdate;
/**
* {@inheritdoc}
*/
protected static $modules = [
'config_update',
'farm_update',
'farm_update_test',
'farm_flag',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->configFactory = \Drupal::service('config.factory');
$this->farmUpdate = \Drupal::service('farm.update');
$this->installEntitySchema('flag');
$this->installConfig([
'farm_update_test',
'farm_flag',
]);
}
/**
* Test farmOS Update module.
*/
public function testFarmUpdate() {
// Confirm that overridden config gets reverted.
$this->farmUpdateTestRevertSetting('farm_flag.flag.monitor', 'label', 'Changed');
// Confirm that config excluded via hook_farm_update_exclude_config() does
// not get reverted.
$this->farmUpdateTestRevertSetting('farm_flag.flag.priority', 'label', 'Changed', TRUE);
// Confirm that config excluded via farm_update.settings does not get
// reverted.
$this->farmUpdateTestRevertSetting('farm_flag.flag.review', 'label', 'Changed', TRUE);
}
/**
* Helper method to test reverting a setting.
*
* @param string $config
* Configuration name.
* @param string $setting
* Setting name within the configuration.
* @param string $override
* Value to use for override.
* @param bool $excluded
* Whether or not we expect this config to be excluded. Defaults to FALSE.
* If set to TRUE, then we expect that the config will still be overridden
* after rebuild.
*/
protected function farmUpdateTestRevertSetting(string $config, string $setting, string $override, bool $excluded = FALSE) {
$original = \Drupal::config($config)->get($setting);
$this->configFactory->getEditable($config)->set($setting, $override)->save();
$this->assertEquals($override, \Drupal::config($config)->get($setting), 'Setting is overridden before rebuild.');
$this->farmUpdate->rebuild();
$expected_value = $excluded ? $override : $original;
$expected_message = $excluded ? 'Setting is overridden after rebuild.' : 'Setting is reverted after rebuild.';
$this->assertEquals($expected_value, \Drupal::config($config)->get($setting), $expected_message);
}
}