Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PSRs: what, why and how?

PSRs: what, why and how?

While increasingly important in our ecosystem, the PHP Standards Recommendations remain too little known and somewhat mystical for many of us. In this talk, I propose to understand what is a PSR and then to briefly review all the existing recommendations, whether validated, accepted or draft. We will also come back to the importance and purpose of these standards as well as their development process.

Julien Janvier

May 20, 2017
Tweet

More Decks by Julien Janvier

Other Decks in Programming

Transcript

  1. PSRs: what,
    why and how?
    ___
    Julien Janvier
    @jujanvier
    https://joind.in/talk/ebe72

    View Slide

  2. PHP
    Standard
    Recommendation

    View Slide

  3. What is a PSR?

    View Slide

  4. accepted
    PSR-1: Basic Coding Standard

    View Slide

  5. accepted
    PSR-2: Coding Style Guide

    View Slide

  6. draft
    PSR-12: Extended Coding Style Guide

    View Slide

  7. draft
    PSR-5: PHPDoc Standard
    /**
    * @method int MyMagicMethod(string $argument1) {
    * This is the summary for MyMagicMethod.
    *
    * @param string $argument1 Description for argument 1.
    *
    * @return int
    * }
    */
    class MyMagicClass
    {
    // ...
    }

    View Slide

  8. Who is behind these standards?

    View Slide

  9. Framework Interoperability Group
    www.php-fig.org
    www.github.com/php-fig/fig-standards
    @phpfig

    View Slide

  10. ...and many more...
    ...and many more...

    View Slide

  11. How these standards are written?

    View Slide

  12. Member Project
    Project Representative
    Core Committee

    View Slide

  13. Working Group =
    Editor + Sponsor +
    Contributors

    View Slide

  14. Pre draft >> Draft >>
    Review >> Accepted >>
    Dreprecate

    View Slide

  15. Let’s go!

    View Slide

  16. Autoloading

    View Slide

  17. PSR-0: Autoloading Standard
    Nelmio\Alice\ => src/
    class Nelmio\Alice\Loader\Yaml
    src/Nelmio/Alice/Loader/Yaml.php
    class Nelmio_Alice_Loader_Yaml
    deprecated

    View Slide

  18. PSR-0: Autoloading Standard
    Nelmio\Alice\ => src/
    class Nelmio\Alice\Loader\Yaml
    src/Nelmio/Alice/Loader/Yaml.php
    accepted
    src/Loader/Yaml.php
    class Nelmio_Alice_Loader_Yaml
    PSR-4: Autoloader
    deprecated

    View Slide

  19. What prompted the
    FIG to write the first
    standards back in
    2009?

    View Slide

  20. What prompted the
    FIG to write the first
    standards back in
    2009?
    Almost impossible to
    reuse others’ code.

    View Slide

  21. Security issues

    View Slide

  22. draft
    PSR-9: Security Reporting Process

    [email protected]

    View Slide

  23. draft
    PSR-10: Security Disclosure Publication

    View Slide

  24. Why knowing
    existing standards
    is useful?

    View Slide

  25. Why knowing
    existing standards
    is useful?
    Let us take
    advantage of others’
    researches.

    View Slide

  26. Day-to-day tools

    View Slide

  27. accepted
    PSR-3: Logger Interface

    View Slide

  28. accepted
    PSR-3: Logger Interface
    namespace Psr\Log;
    interface LoggerInterface
    {
    /**
    * Action must be taken immediately.
    * Example: Entire website down, database unavailable, etc.
    * This should trigger the SMS alerts and wake you up.
    *
    * @param string $message
    * @param array $context
    *
    * @return void
    */
    public function alert($message, array $context = array());

    View Slide

  29. Why a standard
    when there
    is already “one”?

    View Slide

  30. Why a standard
    when there
    is already “one”?
    Our needs evolve.

    View Slide

  31. accepted
    PSR-11: Container Interface
    namespace Psr\Container;
    interface ContainerInterface
    {
    /**
    * Finds an entry of the container by its identifier and returns it.
    *
    * @param string $id Identifier of the entry to look for.
    *
    * @throws NotFoundExceptionInterface No entry was found for this id.
    * @throws ContainerExceptionInterface Error while retrieving the entry.
    *
    * @return mixed Entry.
    */
    public function get($id);

    View Slide

  32. accepted
    PSR-11: Container Interface

    View Slide

  33. Why a standard
    when there are
    already so many
    libraries?

    View Slide

  34. Why a standard
    when there are
    already so many
    libraries?
    Offer us a world of
    new possibilities.

    View Slide

  35. accepted
    PSR-6: Caching Interface
    // $pool is an instance of Psr\Cache\CacheItemPoolInterface
    // $item is an instance of Psr\Cache\CacheItemInterface
    $item = $pool->getItem('foo');
    if (!$item->isHit()) {
    $item->set('bar');
    $item->expiresAfter(3600);
    $pool->save($item);
    }
    $value = $item->get();

    View Slide

  36. accepted
    PSR-16: Simple Cache
    // $cache is an instance of Psr\SimpleCache\CacheInterface
    if (null === $cache->get('foo')) {
    $cache->set('foo', 'bar', 3600);
    }
    $value = $cache->get('foo');

    View Slide

  37. draft
    PSR-14: Event Manager
    namespace Psr\EventManager;
    interface EventManagerInterface
    {
    public function attach($event, $callback, $priority = 0);
    public function detach($event, $callback);
    public function clearListeners($event);
    public function trigger($event, $target = null, $argv =
    array());
    }
    interface EventInterface
    {
    // ...
    }

    View Slide

  38. Why a standard for
    “problems” that
    already have been
    resolved by our
    framework?

    View Slide

  39. Why a standard for
    “problems” that
    already have been
    resolved by our
    framework?
    Avoid to couple our
    business code to a
    vendor library.

    View Slide

  40. Web

    View Slide

  41. accepted
    PSR-7: HTTP Message Interface
    POST /path HTTP/1.1
    Host: example.com
    foo=bar&baz=bat
    HTTP/1.1 200 OK
    Content-Type: text/plain
    This is the response body

    View Slide

  42. accepted
    PSR-7: HTTP Message Interface
    Request Response
    Message
    ServerRequest
    Stream
    Uri
    UploadedFile
    getBody()
    getUploadedFiles()

    View Slide

  43. PSR-17: HTTP Factories
    namespace Psr\Http\Message;
    use Psr\Http\Message\StreamInterface;
    interface StreamFactoryInterface
    {
    public function createStream($content = '');
    public function createStreamFromFile($filename, $mode = 'r');
    public function createStreamFromResource($resource);
    }
    review

    View Slide

  44. accepted
    PSR-13: Hypermedia Links
    namespace Psr\Link;
    interface LinkInterface
    {
    public function getHref();
    public function isTemplated();
    public function getRels();
    public function getAttributes();
    }

    View Slide

  45. What standards
    about so low levels
    topics can bring?

    View Slide

  46. What standards
    about so low levels
    topics can bring?
    Maybe it opens the
    gates of PHP native
    implementations.

    View Slide

  47. PSR-15: HTTP Middlewares
    Working with Slim Middleware, by Timothy Boronczyk
    vote

    View Slide

  48. PSR-15: HTTP Middlewares
    use Psr\Http\ServerMiddleware\DelegateInterface;
    use Psr\Http\ServerMiddleware\MiddlewareInterface;
    use Psr\Http\Message\ServerRequestInterface;
    class CacheMiddleware implements MiddlewareInterface
    {
    public function process(
    ServerRequestInterface $request, DelegateInterface $delegate
    ) {
    if (null === $response = $this->cache->get(/**a key**/)) {
    $response = $delegate->process($request);
    $this->cache->set(/**a key**/, $response);
    }
    return $response;
    }
    vote

    View Slide

  49. Why those
    standards?

    View Slide

  50. Why those
    standards?
    Promote
    interoperability.

    View Slide

  51. PSR-8: Huggable Interface
    draft

    View Slide

  52. Only members can vote...

    View Slide

  53. ...but everybody can participate :)

    View Slide

  54. www.php-fig.org/get-involved/

    View Slide

  55. Thanks :)
    ___
    Julien Janvier
    @jujanvier
    https://joind.in/talk/ebe72

    View Slide

  56. Webography
    ● FIG / FIG 3.0
    ○ The PHP-FIG: Past, Present & Future - podcast with a few FIG members
    ○ Why PHP-FIG matters - by Evert Pot
    ○ FIG 3.0 Proposal - by Larry Garfield
    ○ The TL;DR of FIG 3.0 - by Michael Cullum
    ○ Michael Cullum Talks About PHP FIG, PSR & Symfony - with Michael Cullum
    ○ Phil Sturgeon Talks About API Development, PHP-FIG, PHP Books And The Future Of PHP -
    with Phil Sturgeon
    ○ PHP FIG: Breaking down the Boundaries - by Michael Cullum (video here)
    ● PSR-0
    ○ Is PSR-0 Shortsighted, or are you? - by Phil Sturgeon
    ○ Composer and PSR-0: Friends, Not Relatives - by Phil Sturgeon

    View Slide

  57. Webography
    ● PSR 6
    ○ An Open Letter To PHP-FIG - by Anthony Ferrara
    ○ PSR-6 Has Serious Problems - by Andrew Carter
    ○ PSR-6 Is About To Pass And Still Has Serious Problems - by Andrew Carter
    ○ [VOTE][Accept] PSR-6 Caching Interfaces - by Paul Dragoonis
    ● PSR 9 / PSR 10
    ○ Call for volunteers - by Michael Hess
    ● PSR-11
    ○ [PSR-11+] An overview of interoperable PHP modules - by David Négrier
    ○ [PSR-11+] An update on standard service providers - by David Négrier

    View Slide

  58. Webography
    ● PSR-7 / Middlewares
    ○ Two things to know about PSR-7 - by Rob Allen
    ○ Middleware concept - by Slim framework
    ○ Writing PSR-7 middleware - by Rob Allen
    ○ HTTP, PSR-7 and Middleware - by Rob Allen
    ○ PSR-7 And Middleware, The Future Of PHP - by Matthew Weier O'Phinney
    ○ All About Middleware - problems about PRS-7 by Anthony Ferrara
    ○ All About PSR-7 Middleware - answer about Anthony Ferrara’s post by Woody Gilk
    ○ Dependency Inversion and PSR-7 Bodies - by Woody Gilk
    ○ PSR-7 Objects Are Not Immutable - by Andrew Carter
    ○ Working with Slim Middleware - by Timothy Boronczyk
    ● PSR-15 / Middlewares
    ○ Using Anonymous Classes to Write Middleware - by Matthew Weier O'Phinney
    ○ Stack middlewares - by Igor Wiedler

    View Slide