Skip to content

Instantly share code, notes, and snippets.

@ostrolucky
ostrolucky / Makefile
Created August 27, 2022 16:05
Makefile with dynamic jobs (separate job for each subdir). Great for monorepo
SUBDIRS := $(wildcard fe-*)
PROXIED_COMPOSER_COMMANDS := install build lint test
MAKEFLAGS := --jobs=$(words $(SUBDIRS))
define make-composer-dot-rules
$1.$2::
cd $1 && composer $2
endef
@ostrolucky
ostrolucky / ColumnHydrator.php
Last active January 3, 2022 11:53
Doctrine ColumnHydrator
<?php
declare(strict_types=1);
namespace App\ORM\Hydration;
use Doctrine\ORM\Internal\Hydration\ArrayHydrator;
/**
* Returns one-dimensional scalar array from query: mixed[][] => mixed[]
@ostrolucky
ostrolucky / AddDocumentEvent.php
Created July 7, 2021 13:58
Make FOSElasticsearchbundle working without doctrine
<?php
declare(strict_types=1);
namespace Elektronik\Bl\DomainBundle\Infrastructure\Elasticsearch\Event;
use Symfony\Contracts\EventDispatcher\Event;
class AddDocumentEvent extends Event
{
private string $index;
@ostrolucky
ostrolucky / DataConnectionWrapper.php
Created August 23, 2017 22:02 — forked from Xymanek/DataConnectionWrapper.php
Symfony multi-tenant database
<?php
namespace AppBundle\Doctrine;
use AppBundle\Entity\Organisation;
use Doctrine\DBAL\Connection;
use Acme\Common\DatabaseNameResolver;
class DataConnectionWrapper extends Connection
{
/**
@ostrolucky
ostrolucky / transcript.txt
Created April 20, 2019 13:47
Using lldb to debug PHP code
Guilhermes-MacBook-Pro:orm guilhermeblanco$ lldb /usr/local/Cellar/php/7.3.2/bin/php
(lldb) target create "/usr/local/Cellar/php/7.3.2/bin/php"
Current executable set to '/usr/local/Cellar/php/7.3.2/bin/php' (x86_64).
(lldb) run ./vendor/bin/phpunit tests/Doctrine/Tests/ORM/Functional/PaginationTest.php --filter testIterateWithOutputWalkersWithRegularJoinWithComplexOrderByReferencingJoined
Process 99416 launched: '/usr/local/Cellar/php/7.3.2/bin/php' (x86_64)
PHPUnit 7.5.6 by Sebastian Bergmann and contributors.
Runtime: PHP 7.3.2 with Xdebug 2.7.0RC2
Configuration: /Users/guilhermeblanco/doctrine/orm/phpunit.xml.dist
@ostrolucky
ostrolucky / UniqueDTO.php
Created December 17, 2017 01:20
UniqueDTOValidator
<?php
declare(strict_types=1);
namespace App\Validator\Constraints;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Checks if entity associatied to DTO is unique.
<?php
function array_select(array $array, array $filter): array
{
return array_map(function (array $object) use ($filter) {
$object = array_intersect_key($object, $filter);
foreach ($object as $key => $value) {
if (is_array($filter[$key])) {
$object[$key] = array_select($object[$key], $filter[$key]);
@ostrolucky
ostrolucky / macflife.d
Created March 25, 2019 13:03
This script is from the DTrace book, and traces the creation and deletion of files
#!/usr/sbin/dtrace -s
/*
* maclife.d
*
* Example script from Chapter 5 of the book: DTrace: Dynamic Tracing in
* Oracle Solaris, Mac OS X, and FreeBSD", by Brendan Gregg and Jim Mauro,
* Prentice Hall, 2011. ISBN-10: 0132091518. http://dtracebook.com.
*
* See the book for the script description and warnings. Many of these are
* provided as example solutions, and will need changes to work on your OS.
@ostrolucky
ostrolucky / conflict-free-merge.sh
Created December 15, 2018 21:17
Apply only changes which are mergeable to upper branch without conflicts
UPPERBRANCH=master
BOTTOMBRANCH=3.4
DIR="src"
git checkout $BOTTOMBRANCH
php-cs-fixer fix $DIR --rules='{"array_syntax": {"syntax": "short"}}'
git diff > patch.patch && splitpatch patch.patch && rm patch.patch
git checkout $UPPERBRANCH --force
mkdir -p nonworkingpatches workingpatches && rm -rf nonworkingpatches/* workingpatches/*
for patchfile in *.patch*; do
@ostrolucky
ostrolucky / array_flatten.php
Created October 20, 2017 22:24
Array flatten algorithms benchmark
<?php
$array = [1, 2, 3, 4, [], [5, 6], 7, 8, [9], [10, [11, 12], 13, [14, 15, 16, [17, [18, [19, 20, 21]]]]], 22, 23, 24, [25], 26, [27, [28], [29, [30, 31], [32], [33, [34, [35, 36], 37, 38, [39, 40]]]]]];
function laravel_flatten($array, $depth = INF)
{
return array_reduce($array, function ($result, $item) use ($depth) {
if (! is_array($item)) {
return array_merge($result, [$item]);
} elseif ($depth === 1) {