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. 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 { // ... }
  2. 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
  3. What prompted the FIG to write the first standards back

    in 2009? Almost impossible to reuse others’ code.
  4. 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());
  5. 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);
  6. Why a standard when there are already so many libraries?

    Offer us a world of new possibilities.
  7. 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();
  8. 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');
  9. 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 { // ... }
  10. Why a standard for “problems” that already have been resolved

    by our framework? Avoid to couple our business code to a vendor library.
  11. Web

  12. 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
  13. 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
  14. accepted PSR-13: Hypermedia Links namespace Psr\Link; interface LinkInterface { public

    function getHref(); public function isTemplated(); public function getRels(); public function getAttributes(); }
  15. What standards about so low levels topics can bring? Maybe

    it opens the gates of PHP native implementations.
  16. 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
  17. 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
  18. 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
  19. 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