public function createEntityManager(Application $app)
    {
        $ems = new Pimple;

        foreach ($app['ems.options'] as $name => $options) {
            $ems[$name] = $ems->share(
                function () use ($app, $name, $options) {
                    if (false === isset($options['db'])) {
                        $options['db'] = $name;
                    }

                    if (isset($app['dbs'][$options['db']])) {
                        $em = EntityManager::create(
                            $app['dbs'][$options['db']],
                            $app['ems.config'][$name],
                            $app['dbs.event_manager'][$options['db']]
                        );
                    } else {
                        $em = EntityManager::create(
                            $app['db'],
                            $app['ems.config'][$name],
                            $app['db.event_manager']
                        );
                    }

                    if ($options['flush_on_terminate']) {
                        $app['dispatcher']->addListener(
                            SilexEvents::FINISH,
                            function () use ($em) {
                                $em->flush();
                                // @codeCoverageIgnoreStart
                            }
                            // @codeCoverageIgnoreEnd
                        );
                    }

                    return $em;
                }
            );
        }

        return $ems;
    }