Skip to content

Commit

Permalink
Fix up logger of user switching.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-vessey committed Feb 26, 2025
1 parent 318d245 commit 50252a1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"extra": {
"drush": {
"services": {
"drush.11.6-12.5.yml": ">=11.6",
"drush.10.services.yml": "^10 || ^11"
}
}
Expand Down
1 change: 1 addition & 0 deletions drush.10.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ services:
arguments:
- '@account_switcher'
- '@entity_type.manager'
- '@logger.islandora_drush_utils'
tags:
- name: drush.command
islandora_drush_utils.command.user_wrapping_alterer:
Expand Down
6 changes: 6 additions & 0 deletions drush.11.6-12.5.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
# XXX: Command files are expected to make use of newer methods of instantiation,
# such as:
# - create() method as of Drush 11.6; or,
# - autowiring as of Drush 12.5
services: {}
27 changes: 13 additions & 14 deletions islandora_drush_utils.module
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Implements hook_batch_alter().
*/
function islandora_drush_utils_batch_alter(&$batch) {
function islandora_drush_utils_batch_alter(array &$batch) : void {
// Better preservation messsaging.
foreach ($batch['sets'] as &$set) {
if (!isset($set['operations'])) {
Expand Down Expand Up @@ -59,24 +59,24 @@ function islandora_drush_utils_batch_alter(&$batch) {
if (is_string($callable)) {
return trim($callable);
}
elseif (is_array($callable)) {

if (is_array($callable)) {
if (is_object($callable[0])) {
return sprintf("(%s instance)::%s", get_class($callable[0]),
trim($callable[1]));
}
else {
return sprintf("%s::%s", trim($callable[0]), trim($callable[1]));
}

return sprintf("%s::%s", trim($callable[0]), trim($callable[1]));
}
elseif ($callable instanceof Closure) {

if ($callable instanceof Closure) {
return 'closure';
}
else {
return 'unknown';
}

return 'unknown';
};

$wrap_op = function ($op) use ($user, $logger, $namer) {
$wrap_op = static function ($op) use ($user, $logger, $namer) {
$func = reset($op);
if ($func !== '_islandora_drush_utils_user_wrapped_batch_op') {
$logger->debug('Wrapping @func with @wrapper to maintain the user (@uid).',
Expand All @@ -93,9 +93,8 @@ function islandora_drush_utils_batch_alter(&$batch) {
],
];
}
else {
return $op;
}

return $op;
};

foreach ($batch['sets'] as &$set) {
Expand All @@ -108,7 +107,7 @@ function islandora_drush_utils_batch_alter(&$batch) {
/**
* Wrap batch op with user.
*/
function _islandora_drush_utils_user_wrapped_batch_op($id, $info, &$context) {
function _islandora_drush_utils_user_wrapped_batch_op(int|string $id, array $info, array &$context) {
$switcher = \Drupal::service('account_switcher');
try {
$user = \Drupal::service('entity_type.manager')
Expand Down
10 changes: 8 additions & 2 deletions src/Drush/Commands/UserWrapperCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drush\Commands\AutowireTrait;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand All @@ -23,9 +26,9 @@
* "@islandora_drush_utils-user-wrap" annotation to use it where we want
* to.
*/
class UserWrapperCommands implements LoggerAwareInterface, ContainerInjectionInterface {
class UserWrapperCommands implements ContainerInjectionInterface {

use LoggerAwareTrait;
use AutowireTrait;

/**
* The user to which we will switch.
Expand All @@ -42,6 +45,8 @@ class UserWrapperCommands implements LoggerAwareInterface, ContainerInjectionInt
public function __construct(
protected AccountSwitcherInterface $switcher,
protected EntityTypeManagerInterface $entityTypeManager,
#[Autowire(service: 'logger.islandora_drush_utils')]
protected LoggerInterface $logger,
protected $debug = FALSE,
) {
}
Expand All @@ -53,6 +58,7 @@ public static function create(ContainerInterface $container) {
return new static(
$container->get('account_switcher'),
$container->get('entity_type.manager'),
$container->get('@logger.islandora_drush_utils'),
FALSE,
);
}
Expand Down

0 comments on commit 50252a1

Please sign in to comment.