nerdfisch: DevBits

Kleine, aber feine Code-Snippets, nützliche Tweaks und elegante Lösungsansätze aus dem Entwickler-Alltag

13.11.2025 | Peter Majmesku

Drush 12+: Simplified Drush commands with PHP 8 attributes

HelloWorldCommands.php
<?php

declare(strict_types=1);

namespace Drupal\nice_module\Drush\Commands;

use Drupal\Component\DependencyInjection\ContainerInterface;
use Drupal\nice_module\HelloWorldService;
use Drush\Attributes as CLI;
use Drush\Commands\DrushCommands;

/**
 * A Drush command class for saying "hello" to the world.
 */
final class HelloWorldCommands extends DrushCommands {

  public function __construct(
    private readonly HelloWorldService $helloWorldService,
  ) {
    parent::__construct();
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container): self {
    return new static($container->get('nice_module.hello_world_service'));
  }

  #[CLI\Command(name: 'nice_module:say-hello', aliases: ['smci'])]
  #[CLI\Argument(name: 'userName', description: 'The name of the user.')]
  public function sayHello(?string $userName = NULL): void {
    $this->helloWorldService->sayHello($userName, $this->output());
    $this->logger()->success('I said "hello" to the user.');
  }

}
drush
php
13.11.2025 | Peter Majmesku

Install the Latest GitFlow Version on macOS

sh
13.11.2025 | Peter Majmesku

Autowire services with the Autowire attribute in Drupal

Example.php
<?php

declare(strict_types=1);

namespace Drupal\my_module;

use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

class Example {

  public function __construct(
    #[Autowire(service: 'current_user')]
    private readonly AccountInterface $currentUser,
  ) {
  }

  public function getCurrentUser(): AccountInterface {
    return $this->currentUser;
  }

}

my_module.services.yml
services:
  my_module.example:
    class: Drupal\my_module\Service\Example
    autowire: true
  Drupal\my_module\Service\Example: '@my_module.example'

php
services
yml / yaml
13.11.2025 | Marc Hitscherich

Calculate next suitable version tag from git

calc-release.sh
#!/bin/bash

latest=$(git tag -l | sort -V | tail -n1)
semver_parts=(${latest//./ })
major=${semver_parts[0]}
minor=${semver_parts[1]}
patch=${semver_parts[2]}

patch_version=${major}.${minor}.$((patch+1))
minor_version=${major}.$((minor+1)).0
major_version=$((major+1)).0.0

echo "latest:        $latest"
echo "patch release: $patch_version"
echo "minor release: $minor_version"
echo "major release: $major_version"
sh
gitlab
git
version control
13.11.2025 | Marc Hitscherich

Simple domain redirect in Kubernetes with nginx ingress

redirect-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: old-domain-redirect
  namespace: my-namespace
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt
    nginx.ingress.kubernetes.io/permanent-redirect: https://new-domain.com$request_uri
    nginx.ingress.kubernetes.io/ssl-redirect: 'true'
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: old-domain.com
  tls:
  - hosts:
    - old-domain.com
    secretName: old-domain.com-cert

yaml
kubernetes
infrastructure
nginx
copy-paste