Slide 1

Slide 1 text

Silex Anatomy

Slide 2

Slide 2 text

Simon Child, from The Noun Project

Slide 3

Slide 3 text

Solène Troussé, from The Noun Project

Slide 4

Slide 4 text

“Framework” Collection of tools Structural base Set of conventions

Slide 5

Slide 5 text

“Library” Specific tool Does one thing Allows for configuration

Slide 6

Slide 6 text

“Convention” Not opposite of configuration Sensible defaults Implicit inference

Slide 7

Slide 7 text

“Configuration” Static vs runtime Hardcoding? Globals? Dependency Inversion

Slide 8

Slide 8 text

DI is not user friendly!

Slide 9

Slide 9 text

use Doctrine\Common\Cache\ArrayCache; use Guzzle\Common\Cache\DoctrineCacheAdapter; use Guzzle\Http\Client; use Guzzle\Log\MessageFormatter; use Guzzle\Log\Zf1LogAdapter; use Guzzle\Plugin\Cache\CachePlugin; use Guzzle\Plugin\Log\LogPlugin; $client = new Client('https://igor.io/'); $client->addSubscriber(new LogPlugin( new Zf1LogAdapter( new \Zend_Log(new \Zend_Log_Writer_Stream('php://output')) ), MessageFormatter::DEBUG_FORMAT )); $client->addSubscriber(new CachePlugin(array( 'adapter' => new DoctrineCacheAdapter(new ArrayCache()), ))); $response = $client->get('/')->send();

Slide 10

Slide 10 text

+ response object + logging + caching $data = file_get_contents('https://igor.io');

Slide 11

Slide 11 text

FactoryFactory

Slide 12

Slide 12 text

find src | grep Factory

Slide 13

Slide 13 text

Security\Factory\SecurityFactoryInterface Security\UserProvider\UserProviderFactoryInterface Form\FormFactoryBuilderInterface Form\FormFactoryInterface Form\ResolvedFormTypeFactoryInterface Validator\Mapping\ClassMetadataFactoryAdapter Validator\Mapping\ClassMetadataFactoryInterface

Slide 14

Slide 14 text

Cache\Service\StorageCacheFactory Db\Adapter\AdapterServiceFactory Mvc\Service\AbstractPluginManagerFactory Mvc\Service\DiAbstractServiceFactoryFactory Mvc\Service\DiFactory Mvc\Service\DiStrictAbstractServiceFactoryFactory Mvc\Service\SerializerAdapterPluginManagerFactory ServiceManager\AbstractFactoryInterface ServiceManager\Di\DiAbstractServiceFactory

Slide 15

Slide 15 text

Service Container

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

ContainerFactory

Slide 18

Slide 18 text

class Application extends Pimple { ... }

Slide 19

Slide 19 text

class Application extends Pimple { public function __construct() { $this['kernel'] = $this->share(function ($app) { return new HttpKernel($app['dispatcher'], $app['resolver']); }); ... } }

Slide 20

Slide 20 text

$app = new Silex\Application();

Slide 21

Slide 21 text

$app = new Silex\Application(); $app['debug'] = true; $app['resolver'] = $app->share(function ($app) { return new MuchBetterControllerResolver($app); });

Slide 22

Slide 22 text

Silex\Application 500+ LOC

Slide 23

Slide 23 text

1900+ LOC Sinatra

Slide 24

Slide 24 text

class Application extends Pimple implements HttpKernelInterface * route builder * container * http-kernel * utility functions

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

“Swag” The most used word in the whole fucking universe. Douche bags use it, your kids use it, your mail man uses it, and your fucking dog uses it. If you got swag, you generally wear those shitty hats side way, and your ass hanging out like a fucking goof cause your pants are half way down your white ass legs.

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

UNIX Philosophy

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

symfony - http-kernel - http-foundation - event-dispatcher - routing - dependency-injection

Slide 35

Slide 35 text

=== Framework User Interface

Slide 36

Slide 36 text

HttpKernel EventDispatcher ControllerResolver Listener A Listener B Request Response

Slide 37

Slide 37 text

HttpKernel Request Response request controller response

Slide 38

Slide 38 text

request controller RouterListener UrlMatcher

Slide 39

Slide 39 text

$kernel = new HttpKernel( $dispatcher, new ControllerResolver() ); use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\HttpKernel\Controller\ControllerResolver;

Slide 40

Slide 40 text

use Symfony\Component\EventDispatcher\EventDispatcher; $dispatcher = new EventDispatcher();

Slide 41

Slide 41 text

use Symfony\Component\HttpKernel\EventListener\RouterListener; $dispatcher->addSubscriber( new RouterListener($matcher) );

Slide 42

Slide 42 text

use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\RequestContext; $matcher = new UrlMatcher( $routes, new RequestContext() );

Slide 43

Slide 43 text

use Symfony\Component\Routing\RouteCollection; $routes = new RouteCollection();

Slide 44

Slide 44 text

use Symfony\Component\Routing\Route; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; $route = new Route('/foo', [ '_controller' => function (Request $request) { return new Response('Hello foo!'); }, ]); $routes->add('foo', $route);

Slide 45

Slide 45 text

$route = (new Route('/foo')) ->setDefault('_controller', ...) ->setRequirement('_method', 'POST');

Slide 46

Slide 46 text

use Symfony\Component\HttpFoundation\Request; $request = Request::createFromGlobals(); $response = $kernel->handle($request); $response->send();

Slide 47

Slide 47 text

use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\HttpKernel\Controller\ControllerResolver; use Symfony\Component\HttpKernel\EventListener\RouterListener; use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; $routes = new RouteCollection(); $routes->add('foo', new Route('/foo', [ '_controller' => function (Request $request) { return new Response('foo!'); }, ])); $dispatcher = new EventDispatcher(); $dispatcher->addSubscriber(new RouterListener( new UrlMatcher($routes, new RequestContext()) )); $kernel = new HttpKernel($dispatcher, new ControllerResolver()); $request = Request::createFromGlobals(); $response = $kernel->handle($request); $response->send();

Slide 48

Slide 48 text

Tedious bootstrap. Do not want!

Slide 49

Slide 49 text

services: routes: class: Symfony\Component\Routing\RouteCollection dispatcher: class: Symfony\Component\EventDispatcher\EventDispatcher calls: - ['addSubscriber', [@listener.router]] listener.router: class: Symfony\Component\HttpKernel\EventListener\RouterListener arguments: [@url_matcher] url_matcher: class: Symfony\Component\Routing\Matcher\UrlMatcher arguments: [@routes, @request_context] request_context: class: Symfony\Component\Routing\RequestContext http_kernel: class: Symfony\Component\HttpKernel\HttpKernel arguments: [@dispatcher, @controller_resolver] controller_resolver: class: Symfony\Component\HttpKernel\Controller\ControllerResolver

Slide 50

Slide 50 text

$container = Yolo\createContainer();

Slide 51

Slide 51 text

use Symfony\Component\Routing\Route; $routes = $container->get('routes'); $routes->add('foo', new Route('/foo', [ '_controller' => ..., ]));

Slide 52

Slide 52 text

$kernel = $container->get('http_kernel');

Slide 53

Slide 53 text

Annoying API. Do not want!

Slide 54

Slide 54 text

$builder = $container->get('route_builder'); $builder->get('/', function ($request) { return 'Look at me! I was built!'; });

Slide 55

Slide 55 text

class RouteBuilder { private $index = 0; private $routes; public function __construct(RouteCollection $routes) { $this->routes = $routes; } public function get($path, $controller) { return $this->match($path, $controller, 'GET'); } ... public function match($path, $controller, $method = null) { $name = $this->index++; $requirements = $method ? ['_method' => $method] : []; $route = new Route($path, ['_controller' => $controller], $requirements); $this->routes->add($name, $route); return $route; } }

Slide 56

Slide 56 text

$container = Yolo\createContainer(); $builder = $container->get('route_builder'); $builder->get('/', function ($request) { return 'Hi'; }); $front = $container->get('front_controller'); $front->run();

Slide 57

Slide 57 text

Application Facade

Slide 58

Slide 58 text

$container = Yolo\createContainer(); $app = new Yolo\Application($container); $app->get('/', function ($request) { return 'App does not mean anything!'; }); $app->run();

Slide 59

Slide 59 text

class Application { private $container; public function __construct(ContainerInterface $container = null) { $this->container = $container ?: createContainer(); } public function get($path, $controller) { return $this->container->get('route_builder')->get($path, $controller); } ... public function run() { $front = $this->container->get('front_controller'); $front->run(); } }

Slide 60

Slide 60 text

$app = new Yolo\Application(); $app->get('/', function ($request) { return 'Hi'; }); $app->run();

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

* joind.in/8387 * yolophp.com * github.com/igorw/yolo * @igorwesome ____ /. \__ /_ \_/ \ // \ ___ |\ |_| |_| ____ /. \__ /_ \_/ \ // \ ___ |\ |_| |_| ____ /. \__ /_ \_/ \ // \ ___ |\ |_| |_|

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Configuration Parameters Extensions

Slide 65

Slide 65 text

$container = Yolo\createContainer( [ 'debug' => true, ], [ new MonologExtension(), ] ); use Yolo\DependencyInjection\MonologExtension;

Slide 66

Slide 66 text

Exception Handling kernel.error event ExceptionListener Application::error()

Slide 67

Slide 67 text

use Symfony\Component\HttpKernel\KernelEvents; class Application { ... public function error($listener, $priority = 0) { $this->container ->get('dispatcher') ->addListener(KernelEvents::EXCEPTION, $listener, $priority); } ... }

Slide 68

Slide 68 text

use Symfony\Component\HttpFoundation\Response; $app->error(function ($event) { $e = $event->getException(); $message = sprintf("Had problem '%s'.\n", $e->getMessage()); $event->setResponse(new Response($message)); });

Slide 69

Slide 69 text

Service Controllers

Slide 70

Slide 70 text

class HelloController { private $name; public function __construct($name) { $this->name = $name; } public function worldAction($request) { return "Hello, I'm {$this->name}."; } }

Slide 71

Slide 71 text

$container = Yolo\createContainer( [ 'hello.name' => 'the amazing app', ], [ new ServiceControllerExtension(), new CallableExtension( 'controller', function ($configs, $container) { $container->register('hello.controller') ->setClass('HelloController') ->addArgument('%hello.name%'); } ), ] );

Slide 72

Slide 72 text

$app->get('/', 'hello.controller:worldAction');

Slide 73

Slide 73 text

"Hello, I'm the amazing app."

Slide 74

Slide 74 text

Implicit Request/Response ControllerResolver RequestParameterListener StringResponseListener

Slide 75

Slide 75 text

class RequestParameterListener implements EventSubscriberInterface { public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); if (!$request->attributes->has('request')) { $request->attributes->set('request', $request); } } public static function getSubscribedEvents() { return [ KernelEvents::REQUEST => ['onKernelRequest'], ]; } }

Slide 76

Slide 76 text

class StringResponseListener implements EventSubscriberInterface { public function onKernelView(GetResponseForControllerResultEvent $event) { $result = $event->getControllerResult(); $event->setResponse(new Response((string) $result)); } public static function getSubscribedEvents() { return [ KernelEvents::VIEW => [['onKernelView', -512]], ]; } }

Slide 77

Slide 77 text

$app->get('/', function (Request $request) { return new Response('Response object here.'); });

Slide 78

Slide 78 text

$app->get('/', function (Request $request) { return new Response('Response object here.'); });

Slide 79

Slide 79 text

fin