Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
uw_cfg_common
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
WCMS
uw_cfg_common
Commits
8242bbd6
Commit
8242bbd6
authored
3 years ago
by
Eric Bremner
Browse files
Options
Downloads
Patches
Plain Diff
ISTWCMS-5234: adding class to perform feature reverts programtically
parent
12db8cc2
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!184
Feature/istwcms 5234 ebremner hero
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Helpers/UwFeatureRevert.php
+203
-0
203 additions, 0 deletions
src/Helpers/UwFeatureRevert.php
uw_cfg_common.services.yml
+4
-0
4 additions, 0 deletions
uw_cfg_common.services.yml
with
207 additions
and
0 deletions
src/Helpers/UwFeatureRevert.php
0 → 100644
+
203
−
0
View file @
8242bbd6
<?php
namespace
Drupal\uw_cfg_common\Helpers
;
use
Drupal\features\FeaturesManagerInterface
;
use
Drupal\features\ConfigurationItem
;
use
Drupal\features\Package
;
use
Drupal\config_update
\ConfigRevertInterface
;
/**
* Class to perform feature revert.
*/
class
UwFeatureRevert
{
/**
* Features manager.
*
* @var \Drupal\features\FeaturesManagerInterface
*/
private
$featuresManager
;
/**
* Config update revert.
*
* @var \Drupal\config_update\ConfigRevertInterface
*/
private
$configRevert
;
/**
* Default constructor.
*
* @param \Drupal\features\FeaturesManagerInterface $featuresManager
* Features manager.
* @param Drupal\config_update\ConfigRevertInterface $configRevert
* The config update revert.
*/
public
function
__construct
(
FeaturesManagerInterface
$featuresManager
,
ConfigRevertInterface
$configRevert
)
{
$this
->
featuresManager
=
$featuresManager
;
$this
->
configRevert
=
$configRevert
;
}
/**
* Load the features from the module.
*
* @param array $module
* Array of modules.
* @param bool $any
* Flag for all modules.
*
* @return \Drupal\features\Package
* The list of packages.
*/
private
function
drushFeaturesLoadFeature
(
array
$module
,
bool
$any
=
FALSE
):
Package
{
// Get the features manager.
$manager
=
$this
->
featuresManager
;
// Get teh feature.
$feature
=
$manager
->
getPackage
(
$module
);
// Check if this get any or not a feature module.
if
(
$any
&&
!
isset
(
$feature
))
{
// See if this is a non-features module.
$module_handler
=
\Drupal
::
moduleHandler
();
$modules
=
$module_handler
->
getModuleList
();
// If it is not a feature module, set it up as one.
if
(
!
empty
(
$modules
[
$module
]))
{
$extension
=
$modules
[
$module
];
$feature
=
$manager
->
initPackageFromExtension
(
$extension
);
$config
=
$manager
->
listExtensionConfig
(
$extension
);
$feature
->
setConfig
(
$config
);
$feature
->
setStatus
(
FeaturesManagerInterface
::
STATUS_INSTALLED
);
}
}
return
$feature
;
}
/**
* Copy from file features.drush.inc.
*/
public
function
import
(
$args
)
{
// Determine if revert should be forced.
$force
=
TRUE
;
// Determine if -y was supplied. If so, we can filter out needless output
// from this command.
$skip_confirmation
=
TRUE
;
// Get the feature manager.
$manager
=
$this
->
featuresManager
;
// Get the config update revert.
$config_revert
=
$this
->
configRevert
;
// Parse list of arguments.
$modules
=
[];
foreach
(
$args
as
$arg
)
{
$arg
=
explode
(
':'
,
$arg
);
$module
=
array_shift
(
$arg
);
$component
=
array_shift
(
$arg
);
if
(
isset
(
$module
))
{
if
(
empty
(
$component
))
{
// If we received just a feature name, this means that we need all of
// its components.
$modules
[
$module
]
=
TRUE
;
}
elseif
(
$modules
[
$module
]
!==
TRUE
)
{
if
(
!
isset
(
$modules
[
$module
]))
{
$modules
[
$module
]
=
[];
}
$modules
[
$module
][]
=
$component
;
}
}
}
// Process modules.
foreach
(
$modules
as
$module
=>
$components_needed
)
{
// Setup the modules array.
$dt_args
[
'@module'
]
=
$module
;
// Get the feature information.
$feature
=
$this
->
drushFeaturesLoadFeature
(
$module
,
TRUE
);
// If the feature is empty, throw an error and exit.
if
(
empty
(
$feature
))
{
\Drupal
::
logger
(
'UwFeatureRevert'
)
->
error
(
'No such feature is available: @module'
,
$dt_args
);
return
;
}
// If the feature status is not set as active, throw error and exit.
if
(
$feature
->
getStatus
()
!=
FeaturesManagerInterface
::
STATUS_INSTALLED
)
{
\Drupal
::
logger
(
'UwFeatureRevert'
)
->
error
(
'No such feature is installed: @module'
,
$dt_args
);
return
;
}
// Forcefully revert all components of a feature.
if
(
$force
)
{
$components
=
$feature
->
getConfigOrig
();
}
// Only revert components that are detected to be Overridden.
else
{
$components
=
$manager
->
detectOverrides
(
$feature
);
$missing
=
$manager
->
reorderMissing
(
$manager
->
detectMissing
(
$feature
));
// Be sure to import missing components first.
$components
=
array_merge
(
$missing
,
$components
);
}
if
(
!
empty
(
$components_needed
)
&&
is_array
(
$components_needed
))
{
$components
=
array_intersect
(
$components
,
$components_needed
);
}
if
(
empty
(
$components
))
{
\Drupal
::
logger
(
'UwFeatureRevert'
)
->
notice
(
'Current state already matches active config, aborting.'
);
}
else
{
$config
=
$manager
->
getConfigCollection
();
foreach
(
$components
as
$component
)
{
$dt_args
[
'@component'
]
=
$component
;
if
(
$skip_confirmation
)
{
// If there is no config for this present, import it.
if
(
!
isset
(
$config
[
$component
]))
{
// Import missing component.
$item
=
$manager
->
getConfigType
(
$component
);
$type
=
ConfigurationItem
::
fromConfigStringToConfigType
(
$item
[
'type'
]);
$config_revert
->
import
(
$type
,
$item
[
'name_short'
]);
\Drupal
::
logger
(
'd_application_api'
)
->
notice
(
'Import @module : @component.'
,
$dt_args
);
}
else
{
// Revert existing component.
$item
=
$config
[
$component
];
$type
=
ConfigurationItem
::
fromConfigStringToConfigType
(
$item
->
getType
());
$config_revert
->
revert
(
$type
,
$item
->
getShortName
());
\Drupal
::
logger
(
'UwFeatureRevert'
)
->
notice
(
'Reverted @module : @component.'
,
$dt_args
);
}
}
else
{
\Drupal
::
logger
(
'UwFeatureRevert'
)
->
notice
(
'Skipping @module : @component.'
,
$dt_args
);
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
uw_cfg_common.services.yml
+
4
−
0
View file @
8242bbd6
...
...
@@ -29,3 +29,7 @@ services:
class
:
'
\Drupal\uw_cfg_common\EventSubscriber\UwLayoutBuilderEventSubscriber'
tags
:
-
{
name
:
'
event_subscriber'
}
uw_cfg_common.features
:
class
:
Drupal\uw_cfg_common\Helpers\UwFeatureRevert
arguments
:
[
'
@features.manager'
]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment