Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
automatic_updates
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
automatic_updates
Merge requests
!1046
Issue
#3437409
ForbidCoreChangesValidator.php
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Issue
#3437409
ForbidCoreChangesValidator.php
issue/automatic_updates-3437409:3437409-dont-allow-core
into
3.0.x
Overview
11
Commits
9
Pipelines
10
Changes
3
2 unresolved threads
Show all comments
Merged
Ted Bowman
requested to merge
issue/automatic_updates-3437409:3437409-dont-allow-core
into
3.0.x
11 months ago
Overview
11
Commits
9
Pipelines
10
Changes
3
2 unresolved threads
Show all comments
Expand
Closes
#3437409
0
0
Merge request reports
Compare
3.0.x
version 8
afca55cb
11 months ago
version 7
d5b8d0ac
11 months ago
version 6
8817d4b7
11 months ago
version 5
fcc2a654
11 months ago
version 4
9a6f76c5
11 months ago
version 3
aacdc1ed
11 months ago
version 2
58048535
11 months ago
version 1
89c71933
11 months ago
3.0.x (base)
and
version 5
latest version
1227dd21
9 commits,
11 months ago
version 8
afca55cb
8 commits,
11 months ago
version 7
d5b8d0ac
7 commits,
11 months ago
version 6
8817d4b7
6 commits,
11 months ago
version 5
fcc2a654
5 commits,
11 months ago
version 4
9a6f76c5
4 commits,
11 months ago
version 3
aacdc1ed
3 commits,
11 months ago
version 2
58048535
2 commits,
11 months ago
version 1
89c71933
1 commit,
11 months ago
3 files
+
253
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
automatic_updates_extensions/src/Validator/ForbidCoreChangesValidator.php
0 → 100644
+
118
−
0
Options
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\automatic_updates_extensions\Validator
;
use
Drupal\Core\StringTranslation\StringTranslationTrait
;
use
Drupal\Core\Url
;
use
Drupal\package_manager
\ComposerInspector
;
use
Drupal\package_manager
\Event\PreApplyEvent
;
use
Drupal\package_manager
\Event\StatusCheckEvent
;
use
Drupal\package_manager
\InstalledPackagesList
;
use
Drupal\package_manager
\PathLocator
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
/**
* Validates that no changes were made to Drupal Core packages.
*
* @internal
* This is an internal part of Automatic Updates and may be changed or removed
* at any time without warning. External code should not interact with this
* class.
*/
final
class
ForbidCoreChangesValidator
implements
EventSubscriberInterface
{
use
StringTranslationTrait
;
/**
* Constructs a ForbidCoreChangesValidator object.
*
* @param \Drupal\package_manager\PathLocator $pathLocator
* The path locator service.
* @param \Drupal\package_manager\ComposerInspector $composerInspector
* The Composer inspector service.
*/
public
function
__construct
(
private
readonly
PathLocator
$pathLocator
,
private
readonly
ComposerInspector
$composerInspector
)
{}
/**
* Validates the staged packages.
*
* @param \Drupal\package_manager\Event\StatusCheckEvent|\Drupal\package_manager\Event\PreApplyEvent $event
* The event object.
*/
public
function
validateStagedCorePackages
(
StatusCheckEvent
|
PreApplyEvent
$event
):
void
{
$stage
=
$event
->
stage
;
// We only want to do this check if the stage belongs to Automatic Updates
// Extensions.
if
(
$stage
->
getType
()
!==
'automatic_updates_extensions:attended'
||
!
$stage
->
stageDirectoryExists
())
{
return
;
}
$active_core_packages
=
$this
->
getInstalledCorePackages
(
$this
->
pathLocator
->
getProjectRoot
());
$stage_core_packages
=
$this
->
getInstalledCorePackages
(
$stage
->
getStageDirectory
());
$new_packages
=
$stage_core_packages
->
getPackagesNotIn
(
$active_core_packages
);
$removed_packages
=
$active_core_packages
->
getPackagesNotIn
(
$stage_core_packages
);
$changed_packages
=
$active_core_packages
->
getPackagesWithDifferentVersionsIn
(
$stage_core_packages
);
$error_messages
=
[];
foreach
(
$new_packages
as
$new_package
)
{
$error_messages
[]
=
$this
->
t
(
"'@name' installed."
,
[
'@name'
=>
$new_package
->
name
]);
}
foreach
(
$removed_packages
as
$removed_package
)
{
$error_messages
[]
=
$this
->
t
(
"'@name' removed."
,
[
'@name'
=>
$removed_package
->
name
]);
}
foreach
(
$changed_packages
as
$name
=>
$updated_package
)
{
$error_messages
[]
=
$this
->
t
(
"'@name' version changed from @active_version to @staged_version."
,
[
'@name'
=>
$updated_package
->
name
,
'@staged_version'
=>
$stage_core_packages
[
$name
]
->
version
,
'@active_version'
=>
$updated_package
->
version
,
]
);
}
if
(
$error_messages
)
{
$event
->
addError
(
$error_messages
,
$this
->
t
(
'Updating Drupal Core while updating extensions is currently not supported. Use <a href=":url" >this form</a> to update Drupal core. The following changes were made to the Drupal core packages:'
,
[
':url'
=>
Url
::
fromRoute
(
'update.report_update'
)
->
toString
()]
));
}
}
/**
* Gets all the installed core packages for a given project root.
*
* This method differs from
* \Drupal\package_manager\ComposerInspector::getInstalledPackagesList in that
* it ensures that the 'drupal/core' is included in the list if present.
*
* @param string $composer_root
* The path to the composer root.
*
* @return \Drupal\package_manager\InstalledPackagesList
* The installed core packages.
*/
private
function
getInstalledCorePackages
(
string
$composer_root
):
InstalledPackagesList
{
$installed_package_list
=
$this
->
composerInspector
->
getInstalledPackagesList
(
$composer_root
);
$core_packages
=
$installed_package_list
->
getCorePackages
();
if
(
isset
(
$installed_package_list
[
'drupal/core'
])
&&
!
isset
(
$core_packages
[
'drupal/core'
]))
{
$core_packages
=
new
InstalledPackagesList
(
array_merge
(
$core_packages
->
getArrayCopy
(),
[
'drupal/core'
=>
$installed_package_list
[
'drupal/core'
]]));
}
return
$core_packages
;
}
/**
* {@inheritdoc}
*/
public
static
function
getSubscribedEvents
():
array
{
$events
[
StatusCheckEvent
::
class
][]
=
[
'validateStagedCorePackages'
];
$events
[
PreApplyEvent
::
class
][]
=
[
'validateStagedCorePackages'
];
return
$events
;
}
}
Loading