Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
config_normalizer
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
config_normalizer
Merge requests
!13
Draft:
#3230397
Normalize the way core does.
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Draft:
#3230397
Normalize the way core does.
issue/config_normalizer-3230397:3230397-normalize-like-core
into
8.x-1.x
Overview
0
Commits
13
Pipelines
13
Changes
6
Open
Chris Green
requested to merge
issue/config_normalizer-3230397:3230397-normalize-like-core
into
8.x-1.x
1 month ago
Overview
0
Commits
13
Pipelines
13
Changes
6
Expand
Closes
#3230397
0
0
Merge request reports
Compare
8.x-1.x
version 12
4064b408
1 month ago
version 11
1d80e3be
1 month ago
version 10
57b24b23
1 month ago
version 9
dbed00f5
1 month ago
version 8
eab73a69
1 month ago
version 7
4ee379d0
1 month ago
version 6
e2f0ba3b
1 month ago
version 5
da6aad8f
1 month ago
version 4
813c4ccb
1 month ago
version 3
1faafa60
1 month ago
version 2
de8296e5
1 month ago
version 1
aa68fabc
1 month ago
8.x-1.x (HEAD)
and
latest version
latest version
6a996d8d
13 commits,
3 weeks ago
version 12
4064b408
12 commits,
1 month ago
version 11
1d80e3be
11 commits,
1 month ago
version 10
57b24b23
10 commits,
1 month ago
version 9
dbed00f5
9 commits,
1 month ago
version 8
eab73a69
8 commits,
1 month ago
version 7
4ee379d0
7 commits,
1 month ago
version 6
e2f0ba3b
6 commits,
1 month ago
version 5
da6aad8f
5 commits,
1 month ago
version 4
813c4ccb
4 commits,
1 month ago
version 3
1faafa60
3 commits,
1 month ago
version 2
de8296e5
2 commits,
1 month ago
version 1
aa68fabc
1 commit,
1 month ago
6 files
+
704
−
34
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
6
Search (e.g. *.vue) (Ctrl+P)
src/Config/ConfigSorter.php
0 → 100644
+
150
−
0
Options
<?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