Skip to content

Instantly share code, notes, and snippets.

David Buchmann dbu

View GitHub Profile
@dbu
dbu / ValidateServiceDefinitions.php
Created Apr 18, 2019
symfony compiler pass to detect invalid classes on service configurations
View ValidateServiceDefinitions.php
<?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 Nov 5, 2018
Elasticsearch: Find all documents that have invalid entries in an array
View _README.md

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 Sep 12, 2018
Load environment specific configuration in symfony 4 kernel
View Kernel.php
<?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 Aug 7, 2018
why does the anchor folder not exist in the docker image?
View Dockerfile
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
View fly.php
use League\Flysystem\Util;
public function read($path)
{
$path = Util::normalizePath($path);
if (! ($object = $this->flysystem->getAdapter()->read($path))) {
return false;
}
@dbu
dbu / README.md
Last active Oct 2, 2018
Convert NelmioApiDocBundle annotations to Swagger PHP
View README.md

A Symfony command to convert from NelmioApiDocBundle annotations to Swagger-PHP annotations.

This code is provided as is. Make sure to have your code committed to version control before running the command. Check if things work out and if not, use version control to reset the automated changes and fix the command.

@dbu
dbu / TagHandler.php
Last active Feb 8, 2016
channel neutral tag handling for FOSHttpCache
View TagHandler.php
<?php
/**
* Handle tagging. Both request and response are passed,
* so that tags can either be added to the response, or
* recorded with the request information.
*/
interface TagHandler
{
/**
@dbu
dbu / AppKernel.php
Created May 15, 2015
symfony as a microframework
View AppKernel.php
<?php
namespace App;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class AppKernel extends HttpKernel
@dbu
dbu / README.md
Last active Jan 13, 2016
Symfony2: Role Hierarchy check independent of firewall
View README.md

We needed to decide whether a user loaded from FOSUserBundle is granted a specific role. Because of the role hierarchy, this is not as simple as doing in_array($role, $user->getRoles()). The user model only knows about its roles, not about what other roles those roles grant it.

The only thing that handles this situation that i found is the SecurityContext::isGranted method. But the problem of that is that its a check about the role of the "current" user. We needed this information in a command that generates a file and needs to know which user has permission for a specific role.

The RoleHierarchy service can not do decisions but only explode roles into all roles granted through the tree. The RoleHiararchyVoter is part of the security manager. Both are private service and thus not intended to be reused in application code.

The simplest we could come up with is this code, which we use like this:

$roleHierarchy = $this->getContainer()->get('acme_demo.security.role_hierarchy_checker');
@dbu
dbu / CacheInvalidationSubscriber.php
Last active Aug 29, 2015
CMF FOSHttpCache integration
View CacheInvalidationSubscriber.php
<?php
namespace Symfony\Cmf\CoreBundle\Listener;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Symfony\Cmf\CoreBundle\Cache\CmsInvalidator;
/**
* Doctrine listener to invalidate cached urls on changes.
*/
You can’t perform that action at this time.