public function register(Container $app)
 {
     $app->extend('data_collectors', function ($collectors, $app) {
         $collectors['db'] = function ($app) {
             $collector = new DoctrineDataCollector($app['dbs']);
             $timeLogger = new DbalLogger($app['logger'], $app['stopwatch']);
             foreach ($app['dbs.options'] as $name => $params) {
                 /** @var Connection $db */
                 $db = $app['dbs'][$name];
                 $loggerChain = new LoggerChain();
                 $logger = new DebugStack();
                 $loggerChain->addLogger($logger);
                 $loggerChain->addLogger($timeLogger);
                 $db->getConfiguration()->setSQLLogger($loggerChain);
                 $collector->addLogger($name, $logger);
             }
             return $collector;
         };
         return $collectors;
     });
     $app['data_collector.templates'] = $app->extend('data_collector.templates', function ($templates) {
         $templates[] = array('db', '@DoctrineBundle/Collector/db.html.twig');
         return $templates;
     });
     $app['twig.loader.filesystem'] = $app->extend('twig.loader.filesystem', function ($loader) {
         /** @var \Twig_Loader_Filesystem $loader */
         $loader->addPath(dirname(__DIR__) . '/Resources/views', 'DoctrineBundle');
         return $loader;
     });
 }
Example #2
0
 public function addLoggerTo($em)
 {
     if (null !== $em->getConfiguration()->getSQLLogger()) {
         $logger = new LoggerChain();
         $logger->addLogger($this);
         $logger->addLogger($em->getConfiguration()->getSQLLogger());
         $em->getConfiguration()->setSQLLogger($logger);
     } else {
         $em->getConfiguration()->setSQLLogger($this);
     }
 }
 public function onFlush(OnFlushEventArgs $args)
 {
     $em = $args->getEntityManager();
     $uow = $em->getUnitOfWork();
     // extend the sql logger
     $this->old = $em->getConnection()->getConfiguration()->getSQLLogger();
     $new = new LoggerChain();
     $new->addLogger(new AuditLogger(function () use($em) {
         $this->flush($em);
     }));
     if ($this->old instanceof SQLLogger) {
         $new->addLogger($this->old);
     }
     $em->getConnection()->getConfiguration()->setSQLLogger($new);
     foreach ($uow->getScheduledEntityUpdates() as $entity) {
         $this->updated[] = [$entity, $uow->getEntityChangeSet($entity)];
     }
     foreach ($uow->getScheduledEntityInsertions() as $entity) {
         $this->inserted[] = [$entity, $ch = $uow->getEntityChangeSet($entity)];
     }
     foreach ($uow->getScheduledEntityDeletions() as $entity) {
         $uow->initializeObject($entity);
         $this->removed[] = [$entity, $this->id($em, $entity)];
     }
     foreach ($uow->getScheduledCollectionUpdates() as $collection) {
         $mapping = $collection->getMapping();
         if (!$mapping['isOwningSide'] || $mapping['type'] !== ClassMetadataInfo::MANY_TO_MANY) {
             continue;
             // ignore inverse side or one to many relations
         }
         foreach ($collection->getInsertDiff() as $entity) {
             $this->associated[] = [$collection->getOwner(), $entity, $mapping];
         }
         foreach ($collection->getDeleteDiff() as $entity) {
             $this->dissociated[] = [$collection->getOwner(), $entity, $this->id($em, $entity), $mapping];
         }
     }
     foreach ($uow->getScheduledCollectionDeletions() as $collection) {
         $mapping = $collection->getMapping();
         if (!$mapping['isOwningSide'] || $mapping['type'] !== ClassMetadataInfo::MANY_TO_MANY) {
             continue;
             // ignore inverse side or one to many relations
         }
         foreach ($collection->toArray() as $entity) {
             $this->dissociated[] = [$collection->getOwner(), $entity, $this->id($em, $entity), $mapping];
         }
     }
 }
 /**
  * {@inheritDoc}
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     /** @var $options \DoctrineORMModule\Options\SQLLoggerCollectorOptions */
     $options = $this->getOptions($serviceLocator);
     // @todo always ask the serviceLocator instead? (add a factory?)
     if ($options->getSqlLogger()) {
         $debugStackLogger = $serviceLocator->get($options->getSqlLogger());
     } else {
         $debugStackLogger = new DebugStack();
     }
     /* @var $configuration \Doctrine\ORM\Configuration */
     $configuration = $serviceLocator->get($options->getConfiguration());
     if (null !== $configuration->getSQLLogger()) {
         $logger = new LoggerChain();
         $logger->addLogger($debugStackLogger);
         $logger->addLogger($configuration->getSQLLogger());
         $configuration->setSQLLogger($logger);
     } else {
         $configuration->setSQLLogger($debugStackLogger);
     }
     return new SQLLoggerCollector($debugStackLogger, $options->getName());
 }
 /**
  * {@inheritDoc}
  */
 public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
 {
     /** @var $options \DoctrineORMModule\Options\SQLLoggerCollectorOptions */
     $options = $this->getOptions($container);
     // @todo always ask the serviceLocator instead? (add a factory?)
     if ($options->getSqlLogger()) {
         $debugStackLogger = $container->get($options->getSqlLogger());
     } else {
         $debugStackLogger = new DebugStack();
     }
     /* @var $configuration \Doctrine\ORM\Configuration */
     $configuration = $container->get($options->getConfiguration());
     if (null !== $configuration->getSQLLogger()) {
         $logger = new LoggerChain();
         $logger->addLogger($debugStackLogger);
         $logger->addLogger($configuration->getSQLLogger());
         $configuration->setSQLLogger($logger);
     } else {
         $configuration->setSQLLogger($debugStackLogger);
     }
     return new SQLLoggerCollector($debugStackLogger, 'doctrine.sql_logger_collector.' . $options->getName());
 }