vendor/codeages/biz-framework/src/Provider/DoctrineServiceProvider.php line 120

Open in your IDE?
  1. <?php
  2. /*
  3.  * 此文件来自 Silex 项目(https://github.com/silexphp/Silex).
  4.  *
  5.  * 版权信息请看 LICENSE.SILEX
  6.  */
  7. namespace Codeages\Biz\Framework\Provider;
  8. use Codeages\Biz\Framework\Util\Lock;
  9. use Pimple\Container;
  10. use Pimple\ServiceProviderInterface;
  11. use Doctrine\DBAL\DriverManager;
  12. use Doctrine\DBAL\Configuration;
  13. use Doctrine\Common\EventManager;
  14. use Symfony\Bridge\Doctrine\Logger\DbalLogger;
  15. /**
  16.  * Doctrine DBAL Provider.
  17.  *
  18.  * @author Fabien Potencier <fabien@symfony.com>
  19.  */
  20. class DoctrineServiceProvider implements ServiceProviderInterface
  21. {
  22.     /**
  23.      * @SuppressWarnings(PHPMD.NPathComplexity)
  24.      * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  25.      */
  26.     public function register(Container $app)
  27.     {
  28.         $app['db.default_options'] = array(
  29.             'driver' => 'pdo_mysql',
  30.             'dbname' => null,
  31.             'host' => 'localhost',
  32.             'user' => 'root',
  33.             'password' => null,
  34.             'wrapperClass' => 'Codeages\Biz\Framework\Dao\Connection',
  35.         );
  36.         $app['dbs.options.initializer'] = $app->protect(function () use ($app) {
  37.             static $initialized false;
  38.             if ($initialized) {
  39.                 return;
  40.             }
  41.             $initialized true;
  42.             if (!isset($app['dbs.options'])) {
  43.                 $app['dbs.options'] = array('default' => isset($app['db.options']) ? $app['db.options'] : array());
  44.             }
  45.             $tmp $app['dbs.options'];
  46.             foreach ($tmp as $name => &$options) {
  47.                 $options array_replace($app['db.default_options'], $options);
  48.                 if (!isset($app['dbs.default'])) {
  49.                     $app['dbs.default'] = $name;
  50.                 }
  51.             }
  52.             $app['dbs.options'] = $tmp;
  53.         });
  54.         $app['dbs'] = function ($app) {
  55.             $app['dbs.options.initializer']();
  56.             $dbs = new Container();
  57.             foreach ($app['dbs.options'] as $name => $options) {
  58.                 if ($app['dbs.default'] === $name) {
  59.                     // we use shortcuts here in case the default has been overridden
  60.                     $config $app['db.config'];
  61.                     $manager $app['db.event_manager'];
  62.                 } else {
  63.                     $config $app['dbs.config'][$name];
  64.                     $manager $app['dbs.event_manager'][$name];
  65.                 }
  66.                 $dbs[$name] = function () use ($options$config$manager) {
  67.                     return DriverManager::getConnection($options$config$manager);
  68.                 };
  69.             }
  70.             return $dbs;
  71.         };
  72.         $app['dbs.config'] = function ($app) {
  73.             $app['dbs.options.initializer']();
  74.             $configs = new Container();
  75.             $addLogger = isset($app['logger']) && null !== $app['logger'] && class_exists('Symfony\Bridge\Doctrine\Logger\DbalLogger');
  76.             foreach ($app['dbs.options'] as $name => $options) {
  77.                 $configs[$name] = new Configuration();
  78.                 if ($addLogger) {
  79.                     $configs[$name]->setSQLLogger(new DbalLogger($app['logger'], isset($app['stopwatch']) ? $app['stopwatch'] : null));
  80.                 }
  81.             }
  82.             return $configs;
  83.         };
  84.         $app['dbs.event_manager'] = function ($app) {
  85.             $app['dbs.options.initializer']();
  86.             $managers = new Container();
  87.             foreach ($app['dbs.options'] as $name => $options) {
  88.                 $managers[$name] = new EventManager();
  89.             }
  90.             return $managers;
  91.         };
  92.         $this->registerShortcutForFirstDb($app);
  93.         $this->registerLock($app);
  94.     }
  95.     private function registerShortcutForFirstDb($app)
  96.     {
  97.         $app['db'] = function ($app) {
  98.             $dbs $app['dbs'];
  99.             return $dbs[$app['dbs.default']];
  100.         };
  101.         $app['db.config'] = function ($app) {
  102.             $dbs $app['dbs.config'];
  103.             return $dbs[$app['dbs.default']];
  104.         };
  105.         $app['db.event_manager'] = function ($app) {
  106.             $dbs $app['dbs.event_manager'];
  107.             return $dbs[$app['dbs.default']];
  108.         };
  109.     }
  110.     private function registerLock($app)
  111.     {
  112.         $app['lock'] = function ($app) {
  113.             return new Lock($app);
  114.         };
  115.     }
  116. }