index.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. use Symfony\Component\HttpFoundation\JsonResponse;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider;
  5. use Silex\Provider\DoctrineServiceProvider;
  6. error_reporting(-1);
  7. $loader = require_once __DIR__.'/../vendor/autoload.php';
  8. \Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
  9. $app = new Silex\Application();
  10. $dbh = new PDO('mysql:host=192.168.100.102;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
  11. PDO::ATTR_PERSISTENT => true
  12. ));
  13. $app->register(new Silex\Provider\DoctrineServiceProvider(), array(
  14. 'db.options' => array(
  15. 'pdo' => $dbh
  16. ),
  17. ));
  18. $app->register(new DoctrineOrmServiceProvider, array(
  19. 'orm.proxies_dir' => __DIR__.'/../proxies', // Not sure how or if this needs handling...
  20. 'orm.em.options' => array(
  21. 'mappings' => array(
  22. array(
  23. 'type' => 'annotation',
  24. 'namespace' => 'Entity',
  25. 'path' => __DIR__.'/../src/Entity',
  26. 'use_simple_annotation_reader' => false,
  27. ),
  28. ),
  29. 'metadata_cache' => 'apc'
  30. ),
  31. ));
  32. $app->get('/json', function() {
  33. return new JsonResponse(array('message' => 'Hello, World!'));
  34. });
  35. $app->get('/db', function(Request $request) use ($app) {
  36. $queries = $request->query->getInt('queries', 1);
  37. // possibility for micro enhancement could be the use of SplFixedArray -> http://php.net/manual/de/class.splfixedarray.php
  38. $worlds = array();
  39. $repo = $app['orm.em']->getRepository('Entity\World');
  40. for ($i = 0; $i < $queries; ++$i) {
  41. $worlds[] = $repo->find(mt_rand(1, 10000));
  42. }
  43. if ($queries == 1) {
  44. $worlds = $worlds[0];
  45. }
  46. return new JsonResponse($worlds);
  47. });
  48. $app->run();