Skip to content

Instantly share code, notes, and snippets.

View nicolas-grekas's full-sized avatar
✔️
Green-keeping the Symfony CI

Nicolas Grekas nicolas-grekas

✔️
Green-keeping the Symfony CI
View GitHub Profile
<?php
// composer require blackfire/php-sdk
// search for XXX in the snippet below
while (isset($_SERVER['HTTP_X_BLACKFIRE_QUERY'])) {
$probe = \BlackfireProbe::getMainInstance();
if (!$probe->disable()) {
break;
}
<?php
// A proposal for a future PHP version: builtin lazy variables
class foo
{
function __construct(PDO $db) {
$this->db = $db;
}
@nicolas-grekas
nicolas-grekas / TraceableAdapter.php
Last active December 28, 2016 16:21
TraceableAdapter.php
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
# example 1: add a tag or a setter based on implemented interfaces:
services:
_instanceof:
Psr\Log\LoggerAwareInterface:
calls:
- [setLogger, ['@logger']]
Symfony\Component\EventDispatcher\EventSubscriberInterface:
tags: [kernel.event_subscriber]
--- a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
+++ b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
@@ -157,6 +157,16 @@ class YamlFileLoader extends FileLoader
if (!is_array($content['services'])) {
throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
}
+
+ if (isset($content['services']['_instanceof'])) {
+ if (!is_array($types = $content['services']['_instanceof'])) {
+ throw new InvalidArgumentException(sprintf('The "_instanceof" key must contain an array, "%s" given in "%s".', gettype($types), $file));
diff --git a/src/Symfony/Component/VarDumper/Caster/DateCaster.php b/src/Symfony/Component/VarDumper/Caster/DateCaster.php
index 61d24b1..b078ecd 100644
--- a/src/Symfony/Component/VarDumper/Caster/DateCaster.php
+++ b/src/Symfony/Component/VarDumper/Caster/DateCaster.php
@@ -23,17 +23,15 @@ class DateCaster
public static function castDate(\DateTimeInterface $d, array $a, Stub $stub, $isNested, $filter)
{
$prefix = Caster::PREFIX_VIRTUAL;
+ $location = $d->getTimezone()->getLocation();
+ $fromNow = (new \DateTime())->diff($d);
@nicolas-grekas
nicolas-grekas / interceptor.php
Last active September 11, 2017 15:02
Wondering about dynamic proxies in PHP
<?php
// Very early draft. Not applicable yet.
// TODO: specify behavior for __destruct()
// Only public methods/properties can be intercepted, final ones also
// Magic methods are intercepted as regular ones
// Undefined properties/methods access are excluded from interception
// method_exists()/property_exists()/isset() are excluded from interception
@nicolas-grekas
nicolas-grekas / message-bus.php
Last active October 18, 2018 07:21
A middleware stack that doesn't double the stack size
<?php
error_reporting(-1);
require 'vendor/autoload.php';
/**
* This is the core interface. It already allows building dispatchers *and middlewareHandlers*.
*/
interface MessageBusInterface
{
@nicolas-grekas
nicolas-grekas / AutowirePass.diff
Created November 8, 2018 10:54
Autowiring aliases for named parameters
--- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
+++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
@@ -193,7 +193,7 @@ class AutowirePass extends AbstractRecursivePass
continue;
}
- $type = ProxyHelper::getTypeHint($reflectionMethod, $parameter, true);
+ $type = ltrim(ProxyHelper::getTypeHint($reflectionMethod, $parameter, false), '\\');
if (!$type) {
@nicolas-grekas
nicolas-grekas / secrets.yaml
Created April 6, 2019 13:08
secrets.yaml
parameters:
env(SECRETS_FILE): .secrets.php
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$someSecret: '%env(key:KEY3:require:SECRETS_FILE)%'