Skip to content

Commit

Permalink
feat: drop support for PHP < 8.0
Browse files Browse the repository at this point in the history
drop support for twig < 2.15.6
bump min versions
add PHP strong types
  • Loading branch information
Chris8934 authored and debesha committed Jun 21, 2024
1 parent f148ec4 commit f6da29a
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 125 deletions.
24 changes: 6 additions & 18 deletions DataCollector/HydrationDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,14 @@

class HydrationDataCollector extends DataCollector
{
/**
* @var HydrationLogger
*/
private $hydrationLogger;
private HydrationLogger $hydrationLogger;

public function __construct(EntityManagerInterface $manager)
{
$this->hydrationLogger = $manager->getConfiguration()->getHydrationLogger();
}

/**
* @return void
*/
public function collect(Request $request, Response $response, \Throwable $exception = null)
public function collect(Request $request, Response $response, \Throwable $exception = null): void
{
$this->data['hydrations'] = $this->hydrationLogger->hydrations;
}
Expand All @@ -44,12 +38,12 @@ public function getHydrations()
return $this->data['hydrations'];
}

public function getHydrationsCount()
public function getHydrationsCount(): int
{
return count($this->data['hydrations']);
}

public function getTime()
public function getTime(): int
{
$time = 0;
foreach ($this->data['hydrations'] as $hydration) {
Expand All @@ -61,18 +55,12 @@ public function getTime()
return $time;
}

/**
* @return string
*/
public function getName()
public function getName(): string
{
return 'hydrations';
}

/**
* @return void
*/
public function reset()
public function reset(): void
{
$this->data = [];
$this->hydrationLogger->hydrations = [];
Expand Down
9 changes: 1 addition & 8 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@
*/
class Configuration implements ConfigurationInterface
{
/**
* @return TreeBuilder
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.

return new TreeBuilder('debesha_doctrine_profile_extra');
}
}
5 changes: 1 addition & 4 deletions DependencyInjection/DebeshaDoctrineProfileExtraExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
*/
class DebeshaDoctrineProfileExtraExtension extends Extension
{
/**
* @return void
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
if (!$container->hasParameter('doctrine.orm.entity_manager.class')) {
throw new \InvalidArgumentException('You must include DoctrineBundle/DoctrineBundle before DebeshaDoctrineProfileExtraBundle in your AppKerner.php');
Expand Down
27 changes: 6 additions & 21 deletions ORM/HydrationLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,24 @@ class HydrationLogger
{
/**
* Executed hydrations
*
* @var array
*/
public $hydrations = [];
public array $hydrations = [];

/**
* If Debug Stack is enabled (log queries) or not.
*
* @var bool
*/
public $enabled = true;
public bool $enabled = true;

/**
* @var float|null
*/
public $start = null;
public ?float $start = null;

/**
* @var int
*/
public $currentHydration = 0;
public int $currentHydration = 0;

/**
* Marks a hydration as started. Timing is started
*
* @param string $type type of hydration
*/
public function start($type)
public function start(string $type): void
{
if ($this->enabled) {
$this->start = microtime(true);
Expand All @@ -58,13 +48,8 @@ public function start($type)
/**
* Marks a hydration as stopped. Number of hydrated entities and alias map is
* passed to method.
*
* @param int $resultNum
* @param array $aliasMap
*
* @return void
*/
public function stop($resultNum, $aliasMap)
public function stop(int $resultNum, array $aliasMap): void
{
if ($this->enabled) {
$this->hydrations[$this->currentHydration]['executionMS'] = microtime(true) - $this->start;
Expand Down
21 changes: 4 additions & 17 deletions ORM/LoggingConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,13 @@ public function __construct()
$hydrationLogger = new HydrationLogger();
$this->setHydrationLogger($hydrationLogger);
}
/**
* Gets the hydration logger.
*
* @return HydrationLogger|null
*/
public function getHydrationLogger()

public function getHydrationLogger(): ?HydrationLogger
{
return isset($this->_attributes['hydrationLogger'])
? $this->_attributes['hydrationLogger']
: null;
return $this->_attributes['hydrationLogger'] ?? null;
}

/**
* Sets the hydration logger.
*
* @param HydrationLogger $ns
*
* @return void
*/
public function setHydrationLogger(HydrationLogger $logger)
public function setHydrationLogger(HydrationLogger $logger): void
{
$this->_attributes['hydrationLogger'] = $logger;
}
Expand Down
53 changes: 19 additions & 34 deletions ORM/LoggingEntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
namespace Debesha\DoctrineProfileExtraBundle\ORM;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Exception;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\Query;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;

Expand All @@ -31,52 +32,36 @@ class LoggingEntityManager extends EntityManager
*/
public function newHydrator($hydrationMode): AbstractHydrator
{
switch ($hydrationMode) {
case Query::HYDRATE_OBJECT:
return new LoggingObjectHydrator($this);

case Query::HYDRATE_ARRAY:
return new LoggingArrayHydrator($this);

case Query::HYDRATE_SCALAR:
return new LoggingScalarHydrator($this);

case Query::HYDRATE_SINGLE_SCALAR:
return new LoggingSingleScalarHydrator($this);

case Query::HYDRATE_SIMPLEOBJECT:
return new LoggingSimpleObjectHydrator($this);
default:
return parent::newHydrator($hydrationMode);
}
}

/**
* @return Configuration
*/
public function getConfiguration()
{
return parent::getConfiguration();
return match ($hydrationMode) {
AbstractQuery::HYDRATE_OBJECT => new LoggingObjectHydrator($this),
AbstractQuery::HYDRATE_ARRAY => new LoggingArrayHydrator($this),
AbstractQuery::HYDRATE_SCALAR => new LoggingScalarHydrator($this),
AbstractQuery::HYDRATE_SINGLE_SCALAR => new LoggingSingleScalarHydrator($this),
AbstractQuery::HYDRATE_SIMPLEOBJECT => new LoggingSimpleObjectHydrator($this),
default => parent::newHydrator($hydrationMode),
};
}

/**
* {@inheritdoc}
*
* @throws Exception|ORMException
*/
public static function create($conn, Configuration $config, EventManager $eventManager = null): EntityManager
public static function create($connection, Configuration $config, EventManager $eventManager = null): EntityManager
{
if (!$config->getMetadataDriverImpl()) {
throw ORMException::missingMappingDriverImpl();
}

switch (true) {
case is_array($conn):
$conn = \Doctrine\DBAL\DriverManager::getConnection(
$conn, $config, ($eventManager ?: new EventManager())
case is_array($connection):
$connection = \Doctrine\DBAL\DriverManager::getConnection(
$connection, $config, ($eventManager ?: new EventManager())
);
break;

case $conn instanceof Connection:
if ($eventManager !== null && $conn->getEventManager() !== $eventManager) {
case $connection instanceof Connection:
if ($eventManager !== null && $connection->getEventManager() !== $eventManager) {
throw ORMException::mismatchedEventManager();
}
break;
Expand All @@ -85,6 +70,6 @@ public static function create($conn, Configuration $config, EventManager $eventM
throw new \InvalidArgumentException('Invalid argument');
}

return new self($conn, $config, $conn->getEventManager());
return new self($connection, $config, $connection->getEventManager());
}
}
14 changes: 6 additions & 8 deletions ORM/LoggingHydratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,22 @@
namespace Debesha\DoctrineProfileExtraBundle\ORM;

use Countable;
use Doctrine\DBAL\Result;
use Doctrine\ORM\Internal\Hydration\ArrayHydrator;
use Doctrine\ORM\Internal\Hydration\ObjectHydrator;
use Doctrine\ORM\Internal\Hydration\ScalarHydrator;
use Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator;
use Doctrine\ORM\Internal\Hydration\SingleScalarHydrator;
use Doctrine\ORM\Query\ResultSetMapping;

trait LoggingHydratorTrait
{
/**
* Hydrates all rows returned by the passed statement instance at once.
*
* @param \Doctrine\DBAL\Result $stmt
* @param \Doctrine\ORM\Query\ResultSetMapping $resultSetMapping
* @param array $hints
*
* @return array
* @param array $hints
*/
public function hydrateAll($stmt, $resultSetMapping, array $hints = [])
public function hydrateAll(Result $stmt, ResultSetMapping $resultSetMapping, array $hints = []): Countable|array
{
if ($logger = $this->_em->getConfiguration()->getHydrationLogger()) {
$type = null;
Expand All @@ -54,8 +52,8 @@ public function hydrateAll($stmt, $resultSetMapping, array $hints = [])
$result = parent::hydrateAll($stmt, $resultSetMapping, $hints);

if ($logger) {
if (is_array($result) || $result instanceof Countable) {
$logger->stop(sizeof($result), $resultSetMapping->getAliasMap());
if (is_countable($result)) {
$logger->stop(count($result), $resultSetMapping->getAliasMap());
}
}

Expand Down
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ Adds a section to web profile which lists all doctrine hydrations performed duri

## Installation

Add this in your `composer.json`

```php
"require-dev": {
[...]
"debesha/doctrine-hydration-profiler-bundle" : "~1.0@dev",
},
```sh
composer require "debesha/doctrine-hydration-profiler-bundle"
```

Then run `php composer.phar update `
Expand Down
17 changes: 9 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
"extra",
"bundle",
"doctrine",
"symfony"
"symfony",
"dev"
],
"homepage": "http://github.com/debesha/DoctrineProfileExtraBundle",
"homepage": "https://github.com/debesha/DoctrineProfileExtraBundle",
"license": "MIT",
"authors": [
{
Expand All @@ -19,12 +20,12 @@
}
],
"require": {
"php": ">=7.0",
"doctrine/orm": "^2.2",
"doctrine/doctrine-bundle": "^1.2|^2.0",
"symfony/framework-bundle": "^2.1|^3.0|^4.0|^5.0|^6.0",
"symfony/twig-bundle": "^2.0|^3.0|^4.0|^5.0|^6.0",
"twig/twig": "^1.12|^2.0|^3.0"
"php": ">=8.0",
"doctrine/orm": "^2.19",
"doctrine/doctrine-bundle": "^2.11",
"symfony/framework-bundle": "^5.4|^6.0|^7.0",
"symfony/twig-bundle": "^5.4|^6.0|^7.0",
"twig/twig": "^2.15.6|^3.8"
},
"require-dev": {
"roave/security-advisories": "dev-latest"
Expand Down

0 comments on commit f6da29a

Please sign in to comment.