Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

PHP Standard Recommendation

Slide 3

Slide 3 text

What is a PSR?

Slide 4

Slide 4 text

accepted PSR-1: Basic Coding Standard

Slide 5

Slide 5 text

accepted PSR-2: Coding Style Guide

Slide 6

Slide 6 text

draft PSR-12: Extended Coding Style Guide

Slide 7

Slide 7 text

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 { // ... }

Slide 8

Slide 8 text

Who is behind these standards?

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

How these standards are written?

Slide 12

Slide 12 text

Member Project Project Representative Core Committee

Slide 13

Slide 13 text

Working Group = Editor + Sponsor + Contributors

Slide 14

Slide 14 text

Pre draft >> Draft >> Review >> Accepted >> Dreprecate

Slide 15

Slide 15 text

Let’s go!

Slide 16

Slide 16 text

Autoloading

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Security issues

Slide 22

Slide 22 text

draft PSR-9: Security Reporting Process [email protected]

Slide 23

Slide 23 text

draft PSR-10: Security Disclosure Publication

Slide 24

Slide 24 text

Why knowing existing standards is useful?

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Day-to-day tools

Slide 27

Slide 27 text

accepted PSR-3: Logger Interface

Slide 28

Slide 28 text

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());

Slide 29

Slide 29 text

Why a standard when there is already “one”?

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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);

Slide 32

Slide 32 text

accepted PSR-11: Container Interface

Slide 33

Slide 33 text

Why a standard when there are already so many libraries?

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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();

Slide 36

Slide 36 text

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');

Slide 37

Slide 37 text

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 { // ... }

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Web

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

What standards about so low levels topics can bring?

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Why those standards?

Slide 50

Slide 50 text

Why those standards? Promote interoperability.

Slide 51

Slide 51 text

PSR-8: Huggable Interface draft

Slide 52

Slide 52 text

Only members can vote...

Slide 53

Slide 53 text

...but everybody can participate :)

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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