Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PSR7 - interoperabilità HTTP

423 views

Published on

Talk dell'incontro di aprile 2018 del PUG Roma. Video disponibile su https://www.youtube.com/watch?v=U0TQ0iehXYU

Published in: Technology
  • Be the first to comment

  • Be the first to like this

PSR7 - interoperabilità HTTP

  1. 1. PSR7 interoperabilità HTTP Massimiliano Arione PUG Roma 24 aprile 2018
  2. 2. PSR? PHP Standard Recommendations
  3. 3. FIG https://www.php-fig.org/
  4. 4. Status # Title Status # Title X 0 Autoloading Standard B 10 Security Reporting Process A 1 Basic Coding Standard A 11 Container Interface A 2 Coding Style Guide R 12 Extended Coding Style Guide A 3 Logger Interface A 13 Hypermedia Links A 4 Autoloading Standard B 14 Event Manager B 5 PHPDoc Standard A 15 HTTP Handlers A 6 Caching Interface A 16 Simple Cache A 7 HTTP Message Interface D 17 HTTP Factories B 8 Huggable Interface D 18 HTTP Client B 9 Security Advisories
  5. 5. Status # Title Status # Title X 0 Autoloading Standard B 10 Security Reporting Process A 1 Basic Coding Standard A 11 Container Interface A 2 Coding Style Guide R 12 Extended Coding Style Guide A 3 Logger Interface A 13 Hypermedia Links A 4 Autoloading Standard B 14 Event Manager B 5 PHPDoc Standard A 15 HTTP Handlers A 6 Caching Interface A 16 Simple Cache A 7 HTTP Message Interface D 17 HTTP Factories B 8 Huggable Interface D 18 HTTP Client B 9 Security Advisories
  6. 6. Interfacce
  7. 7. <?php namespace DoctrineCommonPersistence; interface ObjectManager { public function find($className, $id); public function persist($object); public function remove($object); /* … */ }
  8. 8. <?php namespace DoctrineORM; use DoctrineCommonPersistenceObjectManager; interface EntityManagerInterface extends ObjectManager { public function getCache(); public function getConnection(); public function beginTransaction(); public function commit(); public function rollback(); /* ... */ }
  9. 9. <?php namespace PUGRoma; use DoctrineORMEntityManagerInterface; class Pippo implements EntityManagerInterface, ArrayAccess { /* tutti i metodi delle interfacce implementate ... */ }
  10. 10. interfacce predefinite ● Traversable ● Iterator ● IteratorAggregate ● Throwable ● ArrayAccess ● Serializable ● ● Countable ● OuterIterator ● RecursiveIterator ● SeekableIterator ● ● DateTimeInterface ● JsonSerializable
  11. 11. S.O.L.I.D. Single responsibility Open/closed Liskov substitution Interface segregation Dependency inversion http://wall-skills.com/wp-content/uploads/2013/12/solid-OOP_wall-skills.pdf
  12. 12. Interfacce PSR ● PSR3 Logging ● PSR6 Caching ● PSR7 HTTP Message ● PSR11 Container ● PSR13 Hypermedia links ● PSR15 HTTP Handlers ● PSR16 SimpleCache
  13. 13. HTTP
  14. 14. HTTP
  15. 15. PSR7 PsrHttpMessageRequestInterface PsrHttpMessageResponseInterface
  16. 16. PSR7 gerarchia MessageInterface RequestInterface ResponseInterface ServerRequestInterface
  17. 17. PSR7 altre interfacce ● StreamInterface ● UploadedFileInterface ● UriInterface
  18. 18. <?php namespace AppController; use ApplicationCommandCreaElementoCommand; use PsrHttpMessageServerRequestInterface; use SimpleBusMessageBusMessageBus; use SymfonyComponentHttpFoundationRequest; use SymfonyComponentHttpFoundationResponse; use SymfonyBundleFrameworkBundleControllerAbstractController final class PippoController extends AbstractController { public function creaElemento(MessageBus $bus, Request $request): Response { $command = CreaElementoCommand::fromRequest($request); $bus->handle($command); return $this->json([], Response::HTTP_CREATED); } }
  19. 19. <?php namespace ApplicationCommand; use SymfonyComponentHttpFoundationRequest; class CreaElementoCommand { public $nome; public $email; public $telefono; /* ... */ public static function fromRequest(Request $request): self { $params = $request->request->all(); $command = new static(); foreach ($params as $name => $value) { if (property_exists(self::class, $name) { $command->$name = $value; } } return $command; } }
  20. 20. <?php namespace ApplicationCommand; use PsrHttpMessageServerRequestInterface; class CreaElementoCommand { public $nome; public $email; public $telefono; /* ... */ public static function fromRequest(ServerRequestInterface $request): self { $params = $request->getParsedBody(); $command = new static(); foreach ($params as $name => $value) { if (property_exists(self::class, $name) { $command->$name = $value; } } return $command; } }
  21. 21. $ composer require symfony/psr-http-message-bridge sensio/framework-extra-bundle zendframework/zend-diactoros
  22. 22. <?php namespace AppController; use ApplicationCommandCreaElementoCommand; use SimpleBusMessageBusMessageBus; use SymfonyComponentHttpFoundationRequest; use SymfonyComponentHttpFoundationResponse; use SymfonyBundleFrameworkBundleControllerAbstractController final class PippoController extends AbstractController { public function creaElemento(MessageBus $bus, Request $request): Response { $command = CreaElementoCommand::fromRequest($request); $bus->handle($command); return $this->json([], Response::HTTP_CREATED); } }
  23. 23. <?php namespace AppController; use ApplicationCommandCreaElementoCommand; use PsrHttpMessageServerRequestInterface; use SimpleBusMessageBusMessageBus; use SymfonyComponentHttpFoundationResponse; use SymfonyBundleFrameworkBundleControllerAbstractController final class PippoController extends AbstractController { public function creaElemento(MessageBus $bus, ServerRequestInterface $request): Response { $command = CreaElementoCommand::fromRequest($request); $bus->handle($command); return $this->json([], Response::HTTP_CREATED); } }
  24. 24. https://packagist.org/providers/psr/http-message-implementation
  25. 25. Happy coding!

×