Skip to content

Commit 52a51c3

Browse files
Merge pull request #31 from discoverygarden/fix/logger
DGI9-617: Fix up logger of user switching.
2 parents ffb5ede + fcc8ac1 commit 52a51c3

File tree

5 files changed

+29
-18
lines changed

5 files changed

+29
-18
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"extra": {
1919
"drush": {
2020
"services": {
21+
"drush.11.6-12.5.yml": ">=11.6",
2122
"drush.10.services.yml": "^10 || ^11"
2223
}
2324
}

drush.10.services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ services:
2828
arguments:
2929
- '@account_switcher'
3030
- '@entity_type.manager'
31+
- '@logger.islandora_drush_utils'
3132
tags:
3233
- name: drush.command
3334
islandora_drush_utils.command.user_wrapping_alterer:

drush.11.6-12.5.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
# XXX: Command files are expected to make use of newer methods of instantiation,
3+
# such as:
4+
# - create() method as of Drush 11.6; or,
5+
# - autowiring as of Drush 12.5
6+
services: {}

islandora_drush_utils.module

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* Implements hook_batch_alter().
1010
*/
11-
function islandora_drush_utils_batch_alter(&$batch) {
11+
function islandora_drush_utils_batch_alter(array &$batch) : void {
1212
// Better preservation messsaging.
1313
foreach ($batch['sets'] as &$set) {
1414
if (!isset($set['operations'])) {
@@ -59,24 +59,24 @@ function islandora_drush_utils_batch_alter(&$batch) {
5959
if (is_string($callable)) {
6060
return trim($callable);
6161
}
62-
elseif (is_array($callable)) {
62+
63+
if (is_array($callable)) {
6364
if (is_object($callable[0])) {
6465
return sprintf("(%s instance)::%s", get_class($callable[0]),
6566
trim($callable[1]));
6667
}
67-
else {
68-
return sprintf("%s::%s", trim($callable[0]), trim($callable[1]));
69-
}
68+
69+
return sprintf("%s::%s", trim($callable[0]), trim($callable[1]));
7070
}
71-
elseif ($callable instanceof Closure) {
71+
72+
if ($callable instanceof Closure) {
7273
return 'closure';
7374
}
74-
else {
75-
return 'unknown';
76-
}
75+
76+
return 'unknown';
7777
};
7878

79-
$wrap_op = function ($op) use ($user, $logger, $namer) {
79+
$wrap_op = static function ($op) use ($user, $logger, $namer) {
8080
$func = reset($op);
8181
if ($func !== '_islandora_drush_utils_user_wrapped_batch_op') {
8282
$logger->debug('Wrapping @func with @wrapper to maintain the user (@uid).',
@@ -93,9 +93,8 @@ function islandora_drush_utils_batch_alter(&$batch) {
9393
],
9494
];
9595
}
96-
else {
97-
return $op;
98-
}
96+
97+
return $op;
9998
};
10099

101100
foreach ($batch['sets'] as &$set) {
@@ -108,7 +107,7 @@ function islandora_drush_utils_batch_alter(&$batch) {
108107
/**
109108
* Wrap batch op with user.
110109
*/
111-
function _islandora_drush_utils_user_wrapped_batch_op($id, $info, &$context) {
110+
function _islandora_drush_utils_user_wrapped_batch_op(int|string $id, array $info, array &$context) {
112111
$switcher = \Drupal::service('account_switcher');
113112
try {
114113
$user = \Drupal::service('entity_type.manager')

src/Drush/Commands/UserWrapperCommands.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
use Drupal\Core\Entity\EntityTypeManagerInterface;
1010
use Drupal\Core\Session\AccountInterface;
1111
use Drupal\Core\Session\AccountSwitcherInterface;
12-
use Psr\Log\LoggerAwareInterface;
13-
use Psr\Log\LoggerAwareTrait;
12+
use Drush\Commands\AutowireTrait;
13+
use Psr\Log\LoggerInterface;
1414
use Symfony\Component\Console\Command\Command;
1515
use Symfony\Component\Console\Input\InputOption;
16+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1617
use Symfony\Component\DependencyInjection\ContainerInterface;
1718

1819
/**
@@ -23,9 +24,9 @@
2324
* "@islandora_drush_utils-user-wrap" annotation to use it where we want
2425
* to.
2526
*/
26-
class UserWrapperCommands implements LoggerAwareInterface, ContainerInjectionInterface {
27+
class UserWrapperCommands implements ContainerInjectionInterface {
2728

28-
use LoggerAwareTrait;
29+
use AutowireTrait;
2930

3031
/**
3132
* The user to which we will switch.
@@ -42,6 +43,8 @@ class UserWrapperCommands implements LoggerAwareInterface, ContainerInjectionInt
4243
public function __construct(
4344
protected AccountSwitcherInterface $switcher,
4445
protected EntityTypeManagerInterface $entityTypeManager,
46+
#[Autowire(service: 'logger.islandora_drush_utils')]
47+
protected LoggerInterface $logger,
4548
protected $debug = FALSE,
4649
) {
4750
}
@@ -53,6 +56,7 @@ public static function create(ContainerInterface $container) {
5356
return new static(
5457
$container->get('account_switcher'),
5558
$container->get('entity_type.manager'),
59+
$container->get('logger.islandora_drush_utils'),
5660
FALSE,
5761
);
5862
}

0 commit comments

Comments
 (0)