Skip to content
Snippets Groups Projects

Draft: #3230397 Normalize the way core does.

Files
6
+ 150
0
<?php
namespace Drupal\config_normalizer\Config;
use Drupal\Core\Config\StorableConfigBase;
use Drupal\Core\Config\TypedConfigManagerInterface;
/**
* Provides schema-based sorting of configuration arrays.
*
* This class leverages Drupal's internal sorting logic introduced in
* https://www.drupal.org/project/drupal/issues/2852557 to sort configuration
* data based on its schema.
*/
class ConfigSorter {
/**
* The typed configuration manager service.
*
* @var \Drupal\Core\Config\TypedConfigManagerInterface
*/
protected TypedConfigManagerInterface $typedConfigManager;
/**
* Constructs a ConfigSorter object.
*
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typedConfigManager
* The typed configuration manager service to manage configuration schemas.
*/
public function __construct(TypedConfigManagerInterface $typedConfigManager) {
$this->typedConfigManager = $typedConfigManager;
}
/**
* Sorts a configuration array based on its schema.
*
* This method creates an anonymous class that extends StorableConfigBase
* and uses the internal sorting mechanism to sort the provided configuration
* data according to the schema for the given configuration name.
*
* @param string $name
* The name of the configuration to be sorted.
* @param array $data
* The configuration data to be sorted.
*
* @return array
* The sorted configuration data.
*/
public function sort(string $name, array $data): array {
// Create an anonymous class that extends StorableConfigBase
// for sorting the configuration data based on its schema.
$sorter = new class($this->typedConfigManager) extends StorableConfigBase {
/**
* Constructs the anonymous class instance.
*
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typedConfigManager
* The typed configuration manager service.
*/
public function __construct(TypedConfigManagerInterface $typedConfigManager) {
$this->typedConfigManager = $typedConfigManager;
}
/**
* Retrieves the schema wrapper for the configuration data.
*
* This method creates and returns the schema wrapper for the given
* configuration data using the TypedConfigManager service.
*
* @return \Drupal\Core\Config\ConfigSchemaInterface
* The schema wrapper for the configuration data.
*/
protected function getSchemaWrapper() {
if (!isset($this->schemaWrapper)) {
// Create the schema wrapper if it hasn't been created already.
$this->schemaWrapper = $this->typedConfigManager->createFromNameAndData($this->name, $this->data);
}
return $this->schemaWrapper;
}
/**
* Sorts the configuration data based on the schema.
*
* This method checks if the configuration has a schema, casts the value
* if necessary, and validates the data values before returning
* the sorted data.
*
* @param string $name
* The name of the configuration to be sorted.
* @param array $data
* The configuration data to be sorted.
*
* @return array
* The sorted configuration data.
*/
public function anonymousSort(string $name, array $data): array {
// Set the name and initialize the data for sorting.
$this->setName($name)->initWithData($data);
// If a schema exists, cast the value according to the schema.
if ($this->typedConfigManager->hasConfigSchema($name)) {
$this->data = $this->castValue(NULL, $this->data);
}
else {
// If no schema, validate the data values.
foreach ($this->data as $key => $value) {
$this->validateValue($key, $value);
}
}
// Return the sorted configuration data.
return $this->data;
}
/**
* Throws a LogicException when attempting to save the configuration.
*
* This method is not supported in the current class and will always throw
* an exception.
*
* @param bool $has_trusted_data
* Whether the data has been trusted. Not used in this case.
*
* @throws \LogicException
* Always throws an exception.
*/
public function save($has_trusted_data = FALSE) {
throw new \LogicException('Saving is not supported.');
}
/**
* Throws a LogicException when attempting to delete the configuration.
*
* This method is not supported in the current class and will always throw
* an exception.
*
* @throws \LogicException
* Always throws an exception.
*/
public function delete() {
throw new \LogicException('Deletion is not supported.');
}
};
// Return the sorted configuration data.
return $sorter->anonymousSort($name, $data);
}
}
Loading