Wednesday, August 23, 2017

Symfony 3.2 brings support of PHP constants in YAML

A nice improvement of Symfony 3.2 is support of PHP constants in YAML files.

This allows to define a service from a class and some constructor parameters defined as PHP constants, all in a services.yml file (no PHP code involved, just inject it !).

Let's try it with a PSR-7 HTTP client :


        # Concrete implementation of HTTP client
            class: Http\Client\Curl\Client
                - "@app.api_client.message_factory"
                - "@app.api_client.stream_factory"
                - !php/const:CURLOPT_CONNECTTIMEOUT: 4
                  !php/const:CURLOPT_TIMEOUT: 10
        # Concrete implementation of message factory
            class: Http\Message\MessageFactory\GuzzleMessageFactory
        # Concrete implementation of stream factory
            class: Http\Message\StreamFactory\GuzzleStreamFactory

        # Use the fully YAML configured HttpClient in your service !
            class: App\Api\Client
                - "@app.api_client.http_client"

This example is based on PHP-HTTP PSR7 compliant library and documentation (

Thursday, February 11, 2016

Access static methods/properties from Twig

Classic stuff we need while using Twig : access static properties or call static methods of any class.

Reflection Extension

Downloadable from my Github repository

namespace SiteBundle\Twig\Extension;

class Reflection extends \Twig_Extension
     * {@inheritdoc}
    public function getFunctions()
        return array(
            new \Twig_SimpleFunction('call_static', [$this, 'callStaticMethod']),
            new \Twig_SimpleFunction('get_static', [$this, 'getStaticProperty']),

    public function callStaticMethod($class, $method, array $args = [])
        $refl = new \reflectionClass($class);

        // Check that method is static AND public
        if ($refl->hasMethod($method) && $refl->getMethod($method)->isStatic() && $refl->getMethod($method)->isPublic()) {
            return call_user_func_array($class.'::'.$method, $args);

        throw new \RuntimeException(sprintf('Invalid static method call for class %s and method %s', $class, $method));

    public function getStaticProperty($class, $property)
        $refl = new \reflectionClass($class);

        // Check that property is static AND public
        if ($refl->hasProperty($property) && $refl->getProperty($property)->isStatic() && $refl->getProperty($property)->isPublic()) {
            return $refl->getProperty($property)->getValue();

        throw new \RuntimeException(sprintf('Invalid static property get for class %s and property %s', $class, $property));

     * {@inheritdoc}
    public function getName()
        return 'reflection';

Twig extension definition

        class: SiteBundle\Twig\Extension\Reflection
            - { name: twig.extension }

Friday, January 30, 2015


A short post to promote a new tool developed by my friend Alain Tiemblo : TwigFiddle. You can now test your templates, macros, and share your Twig code examples with this tool.

This is really useful when you post or answer a question about a Twig issue on Stackoverflow (or any other dev forum).

So test it and make feedback if you discover an issue or just have any suggestion...

About Alain Tiemblo :

profile for Alain Tiemblo at Stack Overflow, Q&A for professional and enthusiast programmers