Skip to content

Instantly share code, notes, and snippets.

@dbu
dbu / OAuthEsmtpTransportFactoryDecorator.php
Last active August 6, 2024 11:25
Send emails with Symfony Mailer through Outlook / office365 with OAuth
<?php
declare(strict_types=1);
namespace App\Infrastructure\Email;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\Smtp\Auth\AuthenticatorInterface;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
@dbu
dbu / 2021-04-04-pictures.md.raw
Last active April 6, 2021 15:19
image gallery for sculpin with rokka.io and bootstrap. example for the article at https://www.liip.ch/en/blog/integrating-rokka-io-with-the-sculpin-static-site-generator
# the file is markdown, but gist renders markdown. remove ".raw" and this line
---
title: Pictures example post
images:
-
file: TestImage.jpg
caption: This is a test
note: Additional stuff to print next to the image
-
file: TestImage2.jpg
@dbu
dbu / DoctrineEncryptListener.php
Last active February 10, 2024 11:18
encrypting doctrine fields
<?php
namespace App\EventListener;
use App\Doctrine\Encryption\EncryptingEntityInterface;
use App\Doctrine\Encryption\EncryptorInterface;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Events;
@dbu
dbu / CacheWarmerCompilerPass.php
Last active March 4, 2024 15:13
Conditionally tag a service in Symfony
<?php
// src/DependencyInjection/CacheWarmerCompilerPass.php
namespace App\DependencyInjection;
use App\Cache\MyCacheWarmer;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
@dbu
dbu / CommandHelper.php
Created May 9, 2019 11:08
typed options and arguments for symfony console
abstract class CommandHelper
{
public static function getStringOption(InputInterface $input, string $name): ?string
{
$optionData = $input->getOption($name);
if (null !== $optionData && !\is_string($optionData)) {
throw new \InvalidArgumentException(sprintf('Invalid data provided for --%s', $name));
}
@dbu
dbu / ValidateServiceDefinitions.php
Created April 18, 2019 12:02
symfony compiler pass to detect invalid classes on service configurations
<?php
declare(strict_types=1);
namespace Infrastructure\Symfony\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
@dbu
dbu / _README.md
Last active November 5, 2018 17:02
Elasticsearch: Find all documents that have invalid entries in an array

We had a problem where invalid data got stored in elasticsearch. An array of objects had some objects placed in it that are missing a mandatory field. After fixing the mistake, we wanted to update all offending entires. For this, we need to get the IDs of affected items.

The "obvious" query would be _exists_:general_information AND !(_exists_:general_information.value). But as soon as there is any array element with a value, the second condition will consider the value existing. If there are any valid entries in the array, the query will not work as expected.

The solution we found was to use an ES script that loops over the elements in the source document and returns 1 if it finds one that has no data. To our positive surprise, running this on an index with over 1M entries only took a couple of seconds. Definitely not something for a routine query, but an acceptable time for a one-off query to fix a problem.

@dbu
dbu / Kernel.php
Last active September 12, 2018 09:25
Load environment specific configuration in symfony 4 kernel
<?php
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
@dbu
dbu / Dockerfile
Created August 7, 2018 06:15
why does the anchor folder not exist in the docker image?
FROM mprasil/dokuwiki
RUN apt-get update && \
apt-get install -y unzip curl && \
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN curl -O -L "https://github.com/turnermm/ckgedit/archive/master.zip" && \
unzip master.zip -d /dokuwiki/lib/plugins/ && \
mv /dokuwiki/lib/plugins/ckgedit-master /dokuwiki/lib/plugins/ckgedit && \
rm -f master.zip
RUN curl -O -L "https://trello-attachments.s3.amazonaws.com/5af4815352fa15728c62aaae/5afe9c8a66d239ee43a6f068/92c13557957b1d6893779951e94ef181/anchor.zip"
RUN unzip anchor.zip -d /dokuwiki/lib/plugins
use League\Flysystem\Util;
public function read($path)
{
$path = Util::normalizePath($path);
if (! ($object = $this->flysystem->getAdapter()->read($path))) {
return false;
}