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

Silex Anatomy (phpne)

Silex Anatomy (phpne)

Igor Wiedler

March 19, 2013
Tweet

More Decks by Igor Wiedler

Other Decks in Programming

Transcript

  1. 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();
  2. class Application extends Pimple { public function __construct() { $this['kernel']

    = $this->share(function ($app) { return new HttpKernel($app['dispatcher'], $app['resolver']); }); ... } }
  3. $app = new Silex\Application(); $app['debug'] = true; $app['resolver'] = $app->share(function

    ($app) { return new MuchBetterControllerResolver($app); });
  4. “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.
  5. 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);
  6. 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();
  7. 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
  8. 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; } }
  9. 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(); } }
  10. * joind.in/8387 * yolophp.com * github.com/igorw/yolo * @igorwesome ____ /.

    \__ /_ \_/ \ // \ ___ |\ |_| |_| ____ /. \__ /_ \_/ \ // \ ___ |\ |_| |_| ____ /. \__ /_ \_/ \ // \ ___ |\ |_| |_|
  11. $container = Yolo\createContainer( [ 'debug' => true, ], [ new

    MonologExtension(), ] ); use Yolo\DependencyInjection\MonologExtension;
  12. use Symfony\Component\HttpKernel\KernelEvents; class Application { ... public function error($listener, $priority

    = 0) { $this->container ->get('dispatcher') ->addListener(KernelEvents::EXCEPTION, $listener, $priority); } ... }
  13. 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)); });
  14. class HelloController { private $name; public function __construct($name) { $this->name

    = $name; } public function worldAction($request) { return "Hello, I'm {$this->name}."; } }
  15. $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%'); } ), ] );
  16. 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'], ]; } }
  17. 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]], ]; } }