Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace removed DebugStack with LoggingMiddleware #58

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/Logging/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Webfactory\Doctrine\Logging;

use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement as StatementInterface;

class Connection extends AbstractConnectionMiddleware
{
public function __construct(ConnectionInterface $wrappedConnection, private readonly QueryCollection $queries)
{
parent::__construct($wrappedConnection);
}

public function prepare(string $sql): StatementInterface
{
return new Statement(parent::prepare($sql), $this->queries, $sql);
}

public function exec(string $sql): int
{
$start = microtime(true);
try {
return parent::exec($sql);
} finally {
$this->queries->addQuery(new Query($sql, [], microtime(true) - $start));
}
}

public function query(string $sql): Result
{
$start = microtime(true);
try {
return parent::query($sql);
} finally {
$this->queries->addQuery(new Query($sql, [], microtime(true) - $start));
}
}

public function beginTransaction(): void
{
$start = microtime(true);
try {
parent::beginTransaction();
} finally {
$this->queries->addQuery(new Query('"BEGIN TRANSACTION"', [], microtime(true) - $start));
}
}

public function commit(): void
{
$start = microtime(true);
try {
parent::commit();
} finally {
$this->queries->addQuery(new Query('"COMMIT"', [], microtime(true) - $start));
}
}

public function rollBack(): void
{
$start = microtime(true);
try {
parent::rollBack();
} finally {
$this->queries->addQuery(new Query('"ROLLBACK"', [], microtime(true) - $start));
}
}
}
20 changes: 20 additions & 0 deletions src/Logging/Driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Webfactory\Doctrine\Logging;

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;

class Driver extends AbstractDriverMiddleware
{
public function __construct(DriverInterface $wrappedDriver, private readonly QueryCollection $queries)
{
parent::__construct($wrappedDriver);
}

public function connect(array $params): DriverConnection
{
return new Connection(parent::connect($params), $this->queries);
}
}
38 changes: 38 additions & 0 deletions src/Logging/Middleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Webfactory\Doctrine\Logging;

use Doctrine\DBAL\Driver as DriverInterface;

class Middleware implements DriverInterface\Middleware
{
private readonly QueryCollection $queries;

public function __construct()
{
$this->queries = new QueryCollection();
}

public function wrap(DriverInterface $driver): DriverInterface
{
return new Driver($driver, $this->queries);
}

/**
* @return Query[]
*/
public function getQueries(): array
{
return $this->queries->queries;
}

public function isEnabled(): bool
{
return $this->queries->enabled;
}

public function setEnabled(bool $enabled): void
{
$this->queries->enabled = $enabled;
}
}
48 changes: 16 additions & 32 deletions src/ORMTestInfrastructure/Query.php → src/Logging/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,44 @@
* file that was distributed with this source code.
*/

namespace Webfactory\Doctrine\ORMTestInfrastructure;
namespace Webfactory\Doctrine\Logging;

/**
* Represents a query that has been executed.
*/
class Query
{
/**
* The SQL query.
*
* @var string
*/
protected $sql = null;

/**
* The assigned parameters.
*
* @var mixed[]
*/
protected $params = null;

/**
* Currently not used:
* - types
*
* @param string $sql - sql
* @param mixed[] $params - params
*/
public function __construct($sql, array $params)
{
$this->sql = $sql;
$this->params = $params;
public function __construct(
protected readonly string $sql,
protected readonly array $params,
protected readonly float $executionTimeInSeconds,
) {
}

/**
* Returns the SQL of the query.
*
* @return string
*/
public function getSql()
public function getSql(): string
{
return $this->sql;
}

/**
* Returns a list of parameters that have been assigned to the statement.
*
* @return mixed[]
*/
public function getParams()
public function getParams(): array
{
return $this->params;
}

/**
* Returns the execution time of the query in seconds.
*/
public function getExecutionTimeInSeconds(): float
{
return $this->executionTimeInSeconds;
}

/**
* Returns a string representation of the query and its params.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Logging/QueryCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Webfactory\Doctrine\Logging;

class QueryCollection
{
/** @var array<Query> */
public array $queries = [];

public bool $enabled = true;

public function addQuery(Query $query): void
{
if ($this->enabled) {
$this->queries[] = $query;
}
}
}
38 changes: 38 additions & 0 deletions src/Logging/Statement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Webfactory\Doctrine\Logging;

use Doctrine\DBAL\Driver\Middleware\AbstractStatementMiddleware;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement as StatementInterface;
use Doctrine\DBAL\ParameterType;

class Statement extends AbstractStatementMiddleware
{
private array $params = [];

public function __construct(
StatementInterface $wrappedStatement,
private readonly QueryCollection $queries,
private readonly string $sql
) {
parent::__construct($wrappedStatement);
}

public function bindValue($param, $value, $type = ParameterType::STRING): void
{
$this->params[$param] = $value;

parent::bindValue($param, $value, $type);
}

public function execute($params = []): Result
{
$start = microtime(true);
try {
return parent::execute();
} finally {
$this->queries->addQuery(new Query($this->sql, $this->params, microtime(true) - $start));
}
}
}
21 changes: 10 additions & 11 deletions src/ORMTestInfrastructure/ORMInfrastructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\ObjectRepository;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Mapping\DefaultNamingStrategy;
use Doctrine\ORM\Mapping\NamingStrategy;
use Doctrine\ORM\Tools\ResolveTargetEntityListener;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Common\EventSubscriber;
use Webfactory\Doctrine\Config\ConnectionConfiguration;
use Webfactory\Doctrine\Config\ExistingConnectionConfiguration;
use Webfactory\Doctrine\Logging\Middleware;
use Webfactory\Doctrine\Logging\Query;

/**
* Helper class that creates the database infrastructure for a defined set of entity classes.
Expand Down Expand Up @@ -92,7 +91,7 @@ class ORMInfrastructure
/**
* The query logger that is used.
*
* @var DebugStack
* @var Middleware
*/
protected $queryLogger = null;

Expand Down Expand Up @@ -197,7 +196,7 @@ private function __construct($entityClasses, ?ConnectionConfiguration $connectio
}
$this->entityClasses = $entityClasses;
$this->connectionConfiguration = $connectionConfiguration;
$this->queryLogger = new QueryLogger();
$this->queryLogger = new Middleware();
$this->namingStrategy = new DefaultNamingStrategy();
$this->mappingDriver = $mappingDriver;
$this->resolveTargetListener = new ResolveTargetEntityListener();
Expand Down Expand Up @@ -255,11 +254,11 @@ public function getQueries()
*/
public function import($dataSource)
{
$loggerWasEnabled = $this->queryLogger->enabled;
$this->queryLogger->enabled = false;
$loggerWasEnabled = $this->queryLogger->isEnabled();
$this->queryLogger->setEnabled(false);
$importer = new Importer($this->copyEntityManager());
$importer->import($dataSource);
$this->queryLogger->enabled = $loggerWasEnabled;
$this->queryLogger->setEnabled($loggerWasEnabled);
}

/**
Expand All @@ -270,14 +269,14 @@ public function import($dataSource)
public function getEntityManager()
{
if ($this->entityManager === null) {
$loggerWasEnabled = $this->queryLogger->enabled;
$this->queryLogger->enabled = false;
$loggerWasEnabled = $this->queryLogger->isEnabled();
$this->queryLogger->setEnabled(false);
$this->entityManager = $this->createEntityManager();
$this->setupEventSubscribers();
if ($this->createSchema) {
$this->createSchemaForSupportedEntities();
}
$this->queryLogger->enabled = $loggerWasEnabled;
$this->queryLogger->setEnabled($loggerWasEnabled);
}
return $this->entityManager;
}
Expand Down