Skip to content

Instantly share code, notes, and snippets.

@lossendae
Last active March 2, 2018 12:41
Show Gist options
  • Save lossendae/2be5bc441b47079511c0e4a0dfe7fe6c to your computer and use it in GitHub Desktop.
Save lossendae/2be5bc441b47079511c0e4a0dfe7fe6c to your computer and use it in GitHub Desktop.
Pomm auto tagging model & model layer, use custom session builder for query logs
pomm:
configuration:
whatever:
dsn: "%env(DATABASE_URL)%"
pomm:default: true
logger:
service: "@logger"
# model will be auto tagged but we still need to use our own Session builder
pomm.model_manager.session_builder:
class: 'App\Model\SessionBuilder'
# auto tag Model and ModelLayer
_instanceof:
PommProject\ModelManager\Model\Model:
public: true
tags: ['pomm.model']
PommProject\ModelManager\ModelLayer\ModelLayer:
public: true
tags: ['pomm.model_layer']
<?php
namespace App\Model;
use PommProject\Foundation\Converter\ConverterHolder;
use PommProject\Foundation\Session\Session;
use PommProject\ModelManager\SessionBuilder as ModelManagerSessionBuilder;
/**
* Class SessionBuilder
*
* @package App\Model
*/
class SessionBuilder extends ModelManagerSessionBuilder
{
/**
* initializeConverterHolder
*
* @param ConverterHolder $converter_holder
*
* @return $this|\PommProject\Foundation\Session\SessionBuilder|void
*/
protected function initializeConverterHolder(ConverterHolder $converter_holder)
{
parent::initializeConverterHolder($converter_holder);
}
/**
* postConfigure
*
* @param Session $session
*
* @return ModelManagerSessionBuilder|void
*/
protected function postConfigure(Session $session)
{
parent::postConfigure($session);
$session
->getListener('query')
->attachAction(function ($name, $data, $session) {
if ($session->hasLogger()) {
if ($name == 'query:pre') {
// log query...
$session->getLogger()->debug(
sprintf(
'[%s] -- %s',
$data['session_stamp'],
$data['sql']
),
['_channel' => 'pomm']
);
if (count($data['parameters']) > 0) {
$data['parameters']['_channel'] = 'pomm';
$session->getLogger()->debug('Parameters', $data['parameters']);
}
} elseif ($name == 'query:post') {
$session->getLogger()->debug(
sprintf(
'Result count : %d - Time : %s ms',
$data['result_count'],
$data['time_ms']
),
['_channel' => 'pomm']
);
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment