Skip to content

Instantly share code, notes, and snippets.

@mikemix
mikemix / AbstractTypedDataMapper.php
Last active December 28, 2022 18:47
Symfony Form Data Mapper helper
<?php
declare(strict_types=1);
namespace App\Form\DataMapper;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\FormInterface;
<?php
final class RandomImageSource implements ImageSourceInterface
{
/** {@inheritDoc} */
public function __invoke(string $imageId, string $imageFormat): ImageInterface
{
return new ImmutableImage($imageId, $imageFormat, static function () use ($imageId): string {
$randomImageUrl = \sprintf('https://picsum.photos/seed/%s/1000/1000', \sha1($imageId));
$randomImageData = \file_get_contents($randomImageUrl);
<?php
final class UrlGenerateCommand extends Command
{
private readonly RouterInterface $router;
public function __construct(
RouterInterface $router,
) {
parent::__construct();
<?php
final class IndexAction
{
private const YEAR = 31536000;
public function __construct(
private readonly ChecksumValidatorInterface $checksumValidator,
private readonly SupportedImagesInterface $supportedFormats,
private readonly SizeFactoryInterface $sizeFactory,
@mikemix
mikemix / thumbgun.www-data.dockerfile
Created August 28, 2022 19:32
Recreating www-data user in Alpine
ARG USER_UID=82
ARG USER_GID=82
# Recreate www-data user with user id matching the host
RUN deluser --remove-home www-data && \
addgroup -S -g ${USER_GID} www-data && \
adduser -u ${USER_UID} -D -S -G www-data www-data
<?php
declare(strict_types=1);
namespace App\User\Notification;
use App\Notification\EmailNotificationInterface;
use App\Notification\NotificationManagerInterface;
use App\Events\PostRegistrationEvent;
use App\User\Notification\WelcomeGreetingEmailNotificationFactory;
<?php
/** @var LRUCacheInterface<User> $cache */
$cache = new LRUCache(50);
foreach ($taskRepository->findAll() as $task) {
// get the user from cache
// phpStorm will correctly resolve the $user as the User class
$user = $cache->get($task->getUserId());
<?php
declare(strict_types=1);
namespace App\Cache;
/**
* @template TCacheItem
* @template-implements LRUCacheInterface<TCacheItem>
*/
<?php
declare(strict_types=1);
namespace App\Cache;
/**
* @template TObject
*/
interface LRUCacheInterface
<?php
declare(strict_types=1);
namespace App\User\Notification;
use App\Notification\EmailNotificationInterface;
use App\Notification\NotificationManagerInterface;
use App\Events\PostRegistrationEvent;
use App\Events\EventSubscriberInterface;