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

Микрофреймворк Silex. Берем в дорогу только необходимое

Dd3f18c87b851137000c7427d7bd5d32?s=47 fwdays
October 18, 2013

Микрофреймворк Silex. Берем в дорогу только необходимое

Dd3f18c87b851137000c7427d7bd5d32?s=128

fwdays

October 18, 2013
Tweet

More Decks by fwdays

Other Decks in Programming

Transcript

  1. Silex Берем только необходимое Дубина Владимир Speroteck Twitter: vovikha Gmail:

    vovikha@gmail.com
  2. Background Experience  7 Years with PHP  5 Years

    with Zend Framework  3 Years with Magento  3 Years as Team Lead Interests  Virtual environment. (Vagrant, LXC Containers, Chef)  Continuous Integration. (Jenkins, Behat, Functional testing)  Agile. (Code review, Tests, Pair programming)  Building Software. (Phing, Composer)
  3. Insights  We produce less new code for sale 

    Mostly integration issues  Less Code – Less Bugs
  4. Complexity

  5. Complexity

  6. Frameworks and Libraries “A library is essentially a set of

    functions that you can call, these days usually organized into classes.” “A framework embodies some abstract design, with more behavior built in. In order to use it you need to insert your behavior into various places in the framework either by subclassing or by plugging in your own classes. The framework's code then calls your code at these points.” Martin Fowler http://martinfowler.com/bliki/InversionOfContr ol.html
  7. Frameworks by Igor Wiedler Conventions  Codding standard  Directory

    layout  Community Libraries  Solved problems  Reusable code Wiring  Base Config  Library glue  Bootstrap
  8. Micro Frameworks Conventions  Codding standard  Directory layout 

    Community Libraries  Solved problems  Reusable code Wiring  Base Config  Library glue  Bootstrap
  9. Micro frameworks “Use Silex if you are comfortable with making

    all of your own architecture decisions and full stack Symfony2 if not.” Dustin Whittle
  10. Silex overview

  11. Silex overview

  12. Silex overview

  13. Silex is largest micro framework in the world  16831

    NCLOC in 280 classes  4018 NCLOC for “Hello World” application Dependencies  pimple/pimple  psr/log  symfony/debug  symfony/event-dispatcher  symfony/http-kernel  symfony/http-foundation  symfony/routing
  14. Glorified Router <?php ... $app = new \Silex\Application(); $app->get('/hello/{name}', function($name)

    use ($app) { return 'Hello ' . $name; }); $app->run();
  15. Dependency Injection Container Pimple - simple Dependency Injection Container for

    PHP 5.3 50 lines of code!
  16. Pimple. Lazy loading. <?php require_once 'vendor/autoload.php'; class Service { public

    function __construct() { echo __CLASS__; } } $c = new \Pimple(); $c['service'] = function () { return new Service(); }; $service = $c['service'];
  17. Pimple. Lazy loading.

  18. Pimple. Dependency Injection. <?php $container['session_storage_class'] = 'SessionStorage'; $container['session_storage'] = function

    ($c) { return new $c['session_storage_class']($c['cookie_name']); }; $container['session_storage']();
  19. Pimple. Singletons <?php $app['service'] = $app->share(function () { return new

    Service(); }); // Pimple. Share services. public static function share($callable) { return function ($c) use ($callable) { static $object; if (null === $object) { $object = $callable($c) } return $object; } }
  20. Restful application with Silex Requirements:  QA can create/view/edit/delete Project

     QA can create/view/edit/delete User Stories for Project  QA can create/view/edit/delete Test Cases for User Story
  21. Codding horror

  22. Init Application # Init Repository $ git init # Add

    Some documentation $ echo Reqman 'Application' > README.md $ composer init # Answer questions and define Reqman application # Require Silex micro framework $ composer require silex/silex # Init Endpoint $ mkdir web && cd touch api.php
  23. Endpoint <?php // web/api.php require_once __DIR__.'/../vendor/autoload.php'; $app = new \Silex\Application();

    $app['debug'] = true; // Turn on errors $app->get('/', function() use($app) { return "Home Page"; }); $app->run();
  24. Restful Routes <?php // web/api.php ... $app->get("/project", function() use ($app)

    { return $app->json( array( array(‘name’ => ‘Project1’) ) ); }); $app->run(); // /api.php/project/ => [{‘name’ => ‘Project1’}]
  25. Restful Routes $app->get(‘/project’, function () { ... }); // View

    all $app->get(‘/project/{id}’, function () { ... }); // View one $app->post(‘/project’, function (Request $request) { ... }); // Create one $app->put(‘/project/{id}’, function (Request $request) { ... }); // Update one $app->delete(‘/project/{id}’, function (Request $request) { ... }); // Delete one
  26. Database $app->register(new \Silex\Provider\DoctrineServiceProvider(), array( 'db.options' => array( 'driver' => 'pdo_mysql',

    'dbname' => 'reqman', 'host' => 'localhost', 'user' => 'root', 'password' => '' ), )); /** @var \Doctrine\DBAL\Connection $dbAdapter */ $dbAdapter = $app['db'];
  27. Provider Interface Class MyServiceProvider implements ServiceProviderInterface { public function register(Application

    $app) { $app['service'] = $app->share(function () use ($app) { $service = new Service(); $service->setName($app[’name']); return $service }); } }
  28. Organizing Controllers $app->mount( '/project', new \Reqman\ProjectBundle\Controller\ProjectController() ); // Interface for

    custom controllers interface ControllerProviderInterface { public function connect(Application $app); }
  29. Project Controller <?php class ProjectController implements ControllerProviderInterface { public function

    connect(Application $app) { $controller = $app['controllers_factory']; $controller->get("/", function() use ($app) { $dbAdapter = $app['db']; $sql = "SELECT * FROM `project`"; $result = $dbAdapter->fetchAll($sql); return $app->json($result); }); } }
  30. Project Controller <?php class ProjectController implements ControllerProviderInterface { public function

    connect(Application $app) { $controller = $app['controllers_factory']; $controller->post("/", function(Request $request) use ($app) { $dbAdapter = $app['db']; $params = $request->request->all(); $dbAdapter->insert('project', $params); return $app->json("", 201); }); } }
  31. Debug

  32. Testing # Add testing framework $ composer require --dev behat/behat

    # Unfortunately we need much more dependencies "require-dev":{ "symfony/browser-kit" : "2.1.*", "behat/behat": "*", "behat/mink": "*", "behat/mink-extension": "*", "behat/mink-goutte-driver": "*", "behat/mink-selenium2-driver": "*", "behat/mink-browserkit-driver": "*", "behat/common-contexts": "*", "kriswallsmith/buzz": "*", "phpunit/phpunit": "*", "phpunit/dbunit": "*" }
  33. Migrations # Add Migration library $ composer require --dev knplabs/migration-service-provider

    $ composer require --dev knplabs/console-service-provider class ProjectMigration extends AbstractMigration { public function schemaUp(Schema $schema) { $projectTable = $schema->createTable('project'); $projectTable->addColumn('id', 'integer', array( 'unsigned' => true, 'autoincrement' => true )); $projectTable->addColumn('name', 'string'); $projectTable->setPrimaryKey(array('id')); } }
  34. Behat for API testing # Add extra contexts to test

    Restful Api $ composer require --dev behat/common-contexts Feature: Project As a QA I should be able to manage projects Scenario: List of projects When I send a GET request to "/project/" Then the response code should be 200 And response should contain json: """ [ {"id": “1", "name": "project 1”}, {"id": “2", "name": "project 2”} ] """
  35. Behat and fixtures # Add own context in behat.yml default:

    context: class: FeatureContext <?php // Customize context to load data fixtures right before tests class FeatureContext extends BehatContext { /** @BeforeScenario */ public function before($event) { //load data into database } /** @AfterScenario */ public function after($event) { // clean data } ?>
  36. Fixtures # PHPUnit_Extensions_Database # PHPUnit_Extensions_Database_DefaultTester $data = new \PHPUnit_Extensions_Database_DataSet_YamlDataSet( dirname(__FILE__)

    . "fixtures/data.yml"); // fixtures/data.yml project: - id: 1 name: project 1 - id: 2 name: project 2 # Bug with foreign keys in innodb. Solution is to rewrite: # PHPUnit_Extensions_Database_Operation_Truncate
  37. Continuous Integration with Travis $ touch .travis.yml $ cat .travis.xml

    language: php php: - 5.3 env: - DB=mysql before_script: # Install apache and configure host ... # Install packages - composer self-update && composer install –dev # Apply migrations - mysql -e 'create database test_reqman;' - php console.php knp:migration:migrate script: - ./bin/behat
  38. Thank you Questions