Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
eca
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
eca
Commits
142f980b
Commit
142f980b
authored
3 weeks ago
by
Jürgen Haas
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3501663
by jurgenhaas: Provide eca:update functionality as a service
parent
538e9b09
No related branches found
No related tags found
No related merge requests found
Pipeline
#404801
passed with warnings
3 weeks ago
Stage: build
Stage: validate
Stage: test
Changes
3
Pipelines
4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
eca.services.yml
+4
-0
4 additions, 0 deletions
eca.services.yml
src/Drush/Commands/EcaCommands.php
+9
-21
9 additions, 21 deletions
src/Drush/Commands/EcaCommands.php
src/EcaUpdate.php
+116
-0
116 additions, 0 deletions
src/EcaUpdate.php
with
129 additions
and
21 deletions
eca.services.yml
+
4
−
0
View file @
142f980b
...
...
@@ -111,6 +111,10 @@ services:
tags
:
-
{
name
:
eca.token_data_provider
,
priority
:
-50
}
eca.update
:
class
:
Drupal\eca\EcaUpdate
arguments
:
[
'
@entity_type.manager'
,
'
@eca.service.modeller'
,
'
@messenger'
]
eca.export.recipe
:
class
:
Drupal\eca\Service\ExportRecipe
arguments
:
...
...
This diff is collapsed.
Click to expand it.
src/Drush/Commands/EcaCommands.php
+
9
−
21
View file @
142f980b
...
...
@@ -5,6 +5,7 @@ namespace Drupal\eca\Drush\Commands;
use
Drupal\Core\Entity\EntityStorageException
;
use
Drupal\Core\Entity\EntityStorageInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\eca\EcaUpdate
;
use
Drupal\eca\Service\ExportRecipe
;
use
Drupal\eca\Service\Modellers
;
use
Drush\Attributes\Argument
;
...
...
@@ -33,6 +34,7 @@ final class EcaCommands extends DrushCommands {
EntityTypeManagerInterface
$entityTypeManager
,
private
readonly
Modellers
$ecaServiceModeller
,
private
readonly
ExportRecipe
$exportRecipe
,
private
readonly
EcaUpdate
$ecaUpdate
,
)
{
parent
::
__construct
();
$this
->
configStorage
=
$entityTypeManager
->
getStorage
(
'eca'
);
...
...
@@ -52,6 +54,7 @@ final class EcaCommands extends DrushCommands {
$container
->
get
(
'entity_type.manager'
),
$container
->
get
(
'eca.service.modeller'
),
$container
->
get
(
'eca.export.recipe'
),
$container
->
get
(
'eca.update'
),
);
}
...
...
@@ -137,27 +140,12 @@ final class EcaCommands extends DrushCommands {
#[Command(name: 'eca:update', aliases: [])]
#[Usage(name: 'eca:update', description: 'Update all models if plugins got changed.')]
public
function
updateAllModels
():
void
{
/** @var \Drupal\eca\Entity\Eca $eca */
foreach
(
$this
->
configStorage
->
loadMultiple
()
as
$eca
)
{
$modeller
=
$this
->
ecaServiceModeller
->
getModeller
(
$eca
->
get
(
'modeller'
));
if
(
$modeller
===
NULL
)
{
$this
->
logger
->
error
(
'This modeller plugin '
.
$eca
->
get
(
'modeller'
)
.
' does not exist.'
);
continue
;
}
$model
=
$eca
->
getModel
();
$modeller
->
setConfigEntity
(
$eca
);
if
(
$modeller
->
updateModel
(
$model
))
{
$filename
=
$model
->
getFilename
();
if
(
$filename
&&
file_exists
(
$filename
))
{
file_put_contents
(
$filename
,
$model
->
getModeldata
());
}
try
{
$modeller
->
save
(
$model
->
getModeldata
(),
$filename
);
}
catch
(
\LogicException
|
EntityStorageException
$e
)
{
$this
->
io
()
->
error
(
$e
->
getMessage
());
}
}
$this
->
ecaUpdate
->
updateAllModels
();
if
(
$infos
=
$this
->
ecaUpdate
->
getInfos
())
{
$this
->
io
()
->
info
(
implode
(
PHP_EOL
,
$infos
));
}
if
(
$errors
=
$this
->
ecaUpdate
->
getErrors
())
{
$this
->
io
()
->
error
(
implode
(
PHP_EOL
,
$errors
));
}
}
...
...
This diff is collapsed.
Click to expand it.
src/EcaUpdate.php
0 → 100644
+
116
−
0
View file @
142f980b
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\eca
;
use
Drupal\Core\Entity\EntityStorageException
;
use
Drupal\Core\Entity\EntityStorageInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Messenger\MessengerInterface
;
use
Drupal\eca\Service\Modellers
;
/**
* Provides methods to update all existing ECA models and to output messages.
*/
final
class
EcaUpdate
{
/**
* ECA config entity storage manager.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected
EntityStorageInterface
$configStorage
;
/**
* List of errors.
*
* @var array
*/
protected
array
$errors
;
/**
* List of info messages.
*
* @var array
*/
protected
array
$infos
;
/**
* Constructs an EcaUpdate object.
*/
public
function
__construct
(
private
readonly
EntityTypeManagerInterface
$entityTypeManager
,
private
readonly
Modellers
$ecaServiceModeller
,
private
readonly
MessengerInterface
$messenger
,
)
{
$this
->
configStorage
=
$this
->
entityTypeManager
->
getStorage
(
'eca'
);
}
/**
* Updates all existing ECA entities calling ::updateModel in their modeller.
*/
public
function
updateAllModels
():
void
{
$this
->
errors
=
[];
$this
->
infos
=
[];
/** @var \Drupal\eca\Entity\Eca $eca */
foreach
(
$this
->
configStorage
->
loadMultiple
()
as
$eca
)
{
$modeller
=
$this
->
ecaServiceModeller
->
getModeller
(
$eca
->
get
(
'modeller'
));
if
(
$modeller
===
NULL
)
{
$this
->
errors
[]
=
'['
.
$eca
->
label
()
.
'] This modeller plugin '
.
$eca
->
get
(
'modeller'
)
.
' for this ECA model does not exist.'
;
continue
;
}
$model
=
$eca
->
getModel
();
$modeller
->
setConfigEntity
(
$eca
);
if
(
$modeller
->
updateModel
(
$model
))
{
$filename
=
$model
->
getFilename
();
if
(
$filename
&&
file_exists
(
$filename
))
{
file_put_contents
(
$filename
,
$model
->
getModeldata
());
}
try
{
$modeller
->
save
(
$model
->
getModeldata
(),
$filename
);
$this
->
infos
[]
=
'['
.
$eca
->
label
()
.
'] Model has been updated.'
;
}
catch
(
\LogicException
|
EntityStorageException
$e
)
{
$this
->
errors
[]
=
'['
.
$eca
->
label
()
.
'] Error while updating this model: '
.
$e
->
getMessage
();
}
}
else
{
$this
->
infos
[]
=
'['
.
$eca
->
label
()
.
'] Model does not require any updates.'
;
}
}
}
/**
* Gets the list of all collected error messages.
*
* @return array
* The list of all collected error messages.
*/
public
function
getErrors
():
array
{
return
$this
->
errors
;
}
/**
* Gets the list of all collected info messages.
*
* @return array
* The list of all collected info messages.
*/
public
function
getInfos
():
array
{
return
$this
->
infos
;
}
/**
* Outputs all messages (info and error) to the user.
*/
public
function
displayMessages
():
void
{
foreach
(
$this
->
infos
??
[]
as
$info
)
{
$this
->
messenger
->
addMessage
(
$info
);
}
foreach
(
$this
->
errors
??
[]
as
$error
)
{
$this
->
messenger
->
addError
(
$error
);
}
}
}
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