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

Silex - Symfony goes micro

Silex - Symfony goes micro

Talk on the Silex microframework as given at symfonyday 2011.

Igor Wiedler

October 21, 2011
Tweet

More Decks by Igor Wiedler

Other Decks in Programming

Transcript

  1. What? • Bare bones • Routes mapped to controllers •

    The ‘C’ of ‘MVC’ • REST • Single file app
  2. <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /some/path RewriteCond %{REQUEST_FILENAME} !-f RewriteCond

    %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] </IfModule>
  3. server { location / { if (-f $request_filename) { break;

    } rewrite ^(.*) /index.php last; } location ~ index\.php$ { fastcgi_pass /var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } }
  4. nested $f = function () { return function () {

    return true; }; }; $g = $f(); $value = $g();
  5. scope $outer = 'world'; $f = function () use ($outer)

    { $inner = 'hello'; return "$inner $outer"; }; => "hello world"
  6. scope $helloWorld = function () { $outer = 'world'; $f

    = function () use ($outer) { $inner = 'hello'; return "$inner $outer"; }; return $f(); }
  7. passing $output = function ($info) { echo $info."\n"; }; $doStuff

    = function ($output) { $output('doing some magic'); doMagic(); $output('did some magic'); };
  8. factory $userFactory = function ($name) { return new User($name); };

    // ... $user = $userFactory($_POST['name']);
  9. convert $app->get('/blog/{post}', function (Post $post) { ... }) ->convert('post', function

    ($post) use ($app) { $id = (int) $post; return $app['posts']->find($id); });
  10. $app->after(function ( Request $request, Response $response, ) use ($app) {

    $response->headers->set('x-csrf-token', $app['csrf_token']); });
  11. Protected $app['lambda_parameter'] = $app->protect( function ($a, $b) { return $a

    + $b; }); // will not execute the lambda $add = $app['lambda_parameter']; // calling it now echo $add(2, 3);
  12. Exposed Services • debug • request • autoloader • routes

    • controllers • dispatcher • resolver • kernel
  13. Core Service Providers • doctrine • form • http cache

    • monolog • session • swiftmailer • symfony bridges • translation • twig • url generator • validator
  14. 3rd Party • doctrine orm • pomm (postgres) • predis

    • mongo • KyotoTycoon • memcache • rest • markdown • gravatar • buzz • config • solr • profiler • ...
  15. • src • app.php • web • index.php • tests

    • bootstrap.php • YourTest.php
  16. use Silex\WebTestCase; class YourTest extends WebTestCase { public function createApp()

    { return require __DIR__.'/../src/app.php'; } // tests... } tests/YourTest.php
  17. public function testAbout() { $client = $this->createClient(); $client->request('GET', '/about'); $response

    = $client->getResponse(); $this->assertTrue($response->isOk()); $this->assertContains('trashbin', $response->getContent()); $this->assertContains('github', $response->getContent()); $this->assertContains('igorw', $response->getContent()); }
  18. phpunit.xml.dist <?xml version="1.0" encoding="UTF-8"?> <phpunit backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true"

    convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" bootstrap="tests/bootstrap.php" > <testsuites> <testsuite name="YourApp Test Suite"> <directory>./tests/</directory> </testsuite> </testsuites> </phpunit>
  19. Ω