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 :

    services:

        # Concrete implementation of HTTP client
        app.api_client.http_client:
            class: Http\Client\Curl\Client
            arguments:
                - "@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
        app.api_client.message_factory:
            class: Http\Message\MessageFactory\GuzzleMessageFactory
        # Concrete implementation of stream factory
        app.api_client.stream_factory:
            class: Http\Message\StreamFactory\GuzzleStreamFactory

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

This example is based on PHP-HTTP PSR7 compliant library and documentation (http://php-http.readthedocs.io/en/latest/clients/curl-client.html)

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

services:
    site.twig.reflection_extension:
        class: SiteBundle\Twig\Extension\Reflection
        tags:
            - { name: twig.extension }

Friday, January 30, 2015

TwigFiddle

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...

twigfiddle.com



About Alain Tiemblo :

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