Skip to content

Commit

Permalink
add LazySchemaHasher (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
bloep committed Jun 26, 2023
1 parent a3d626b commit 9058bc9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
35 changes: 35 additions & 0 deletions src/DbSchema/LazySchemaHasher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace staabm\PHPStanDba\DbSchema;

class LazySchemaHasher implements SchemaHasher
{
/**
* @var callable():SchemaHasher
*/
private $schemaHasherFactory;

/**
* @var SchemaHasher|null
*/
private $schemaHasher;

/**
* @param callable():SchemaHasher $schemaHasherFactory
*/
public function __construct(callable $schemaHasherFactory)
{
$this->schemaHasherFactory = $schemaHasherFactory;
}

public function hashDb(): string
{
if (null === $this->schemaHasher) {
$this->schemaHasher = ($this->schemaHasherFactory)();
}

return $this->schemaHasher->hashDb();
}
}
10 changes: 10 additions & 0 deletions src/DbSchema/SchemaHasher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace staabm\PHPStanDba\DbSchema;

interface SchemaHasher
{
public function hashDb(): string;
}
2 changes: 1 addition & 1 deletion src/DbSchema/SchemaHasherMysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PDO;
use PHPStan\ShouldNotHappenException;

final class SchemaHasherMysql
final class SchemaHasherMysql implements SchemaHasher
{
/**
* @var PDO|mysqli
Expand Down
1 change: 0 additions & 1 deletion src/QueryReflection/PdoMysqlQueryReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PDO;
use PDOException;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Type;
use staabm\PHPStanDba\TypeMapping\MysqlTypeMapper;
use staabm\PHPStanDba\TypeMapping\TypeMapper;

Expand Down
6 changes: 3 additions & 3 deletions src/QueryReflection/ReplayAndRecordingQueryReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace staabm\PHPStanDba\QueryReflection;

use PHPStan\Type\Type;
use staabm\PHPStanDba\DbSchema\SchemaHasherMysql;
use staabm\PHPStanDba\DbSchema\SchemaHasher;
use staabm\PHPStanDba\Error;

final class ReplayAndRecordingQueryReflector implements QueryReflector, RecordingReflector
Expand All @@ -31,11 +31,11 @@ final class ReplayAndRecordingQueryReflector implements QueryReflector, Recordin
private $reflectionCache;

/**
* @var SchemaHasherMysql
* @var SchemaHasher
*/
private $schemaHasher;

public function __construct(ReflectionCache $reflectionCache, QueryReflector $queryReflector, SchemaHasherMysql $schemaHasher)
public function __construct(ReflectionCache $reflectionCache, QueryReflector $queryReflector, SchemaHasher $schemaHasher)
{
$this->replayReflector = new ReplayQueryReflector($reflectionCache);

Expand Down

0 comments on commit 9058bc9

Please sign in to comment.