Skip to content

Commit

Permalink
Leverage LazyProxyTrait in LazySchemaDiffProvider (#1273)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas authored Dec 9, 2022
1 parent 248282c commit 7fa9d14
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 56 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ jobs:
strategy:
matrix:
php-version:
- "7.4"
- "8.0"
- "8.1"
- "8.2"
dependencies:
- "highest"
include:
- dependencies: "lowest"
php-version: "7.4"
php-version: "8.1"

steps:
- name: "Checkout"
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ jobs:
strategy:
matrix:
php-version:
- "7.4"
- "8.1"

steps:
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
],
"homepage": "https://www.doctrine-project.org/projects/migrations.html",
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.1",
"composer-runtime-api": "^2",
"doctrine/dbal": "^3.5.1",
"doctrine/deprecations": "^0.5.3 || ^1",
"doctrine/event-manager": "^1.0",
"friendsofphp/proxy-manager-lts": "^1.0",
"psr/log": "^1.1.3 || ^2 || ^3",
"symfony/console": "^4.4.16 || ^5.4 || ^6.0",
"symfony/stopwatch": "^4.4 || ^5.4 || ^6.0"
"symfony/stopwatch": "^4.4 || ^5.4 || ^6.0",
"symfony/var-exporter": "^6.2"
},
"require-dev": {
"ext-pdo_sqlite": "*",
Expand All @@ -45,7 +45,7 @@
"phpstan/phpstan-phpunit": "^1.1",
"phpstan/phpstan-strict-rules": "^1.1",
"phpstan/phpstan-symfony": "^1.1",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^9.5.24",
"symfony/cache": "^4.4 || ^5.4 || ^6.0",
"symfony/process": "^4.4 || ^5.4 || ^6.0",
"symfony/yaml": "^4.4 || ^5.4 || ^6.0"
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/Migrations/DependencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public function getDiffGenerator(): DiffGenerator
public function getSchemaDiffProvider(): SchemaDiffProvider
{
return $this->getDependency(SchemaDiffProvider::class, function (): LazySchemaDiffProvider {
return LazySchemaDiffProvider::fromDefaultProxyFactoryConfiguration(
return new LazySchemaDiffProvider(
new DBALSchemaDiffProvider(
$this->getConnection()->createSchemaManager(),
$this->getConnection()->getDatabasePlatform()
Expand Down
16 changes: 16 additions & 0 deletions lib/Doctrine/Migrations/Provider/LazySchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Provider;

use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\VarExporter\LazyProxyTrait;

/**
* @internal
*/
class LazySchema extends Schema
{
use LazyProxyTrait;
}
46 changes: 5 additions & 41 deletions lib/Doctrine/Migrations/Provider/LazySchemaDiffProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
namespace Doctrine\Migrations\Provider;

use Doctrine\DBAL\Schema\Schema;
use ProxyManager\Configuration;
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
use ProxyManager\Proxy\LazyLoadingInterface;

/**
* The LazySchemaDiffProvider is responsible for lazily generating the from schema when diffing two schemas
Expand All @@ -18,59 +14,27 @@
*/
class LazySchemaDiffProvider implements SchemaDiffProvider
{
private LazyLoadingValueHolderFactory $proxyFactory;

private SchemaDiffProvider $originalSchemaManipulator;

public function __construct(
LazyLoadingValueHolderFactory $proxyFactory,
SchemaDiffProvider $originalSchemaManipulator
) {
$this->proxyFactory = $proxyFactory;
$this->originalSchemaManipulator = $originalSchemaManipulator;
}

public static function fromDefaultProxyFactoryConfiguration(
SchemaDiffProvider $originalSchemaManipulator
): LazySchemaDiffProvider {
$proxyConfig = new Configuration();
$proxyConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
$proxyFactory = new LazyLoadingValueHolderFactory($proxyConfig);

return new LazySchemaDiffProvider($proxyFactory, $originalSchemaManipulator);
}

public function createFromSchema(): Schema
{
$originalSchemaManipulator = $this->originalSchemaManipulator;

return $this->proxyFactory->createProxy(
Schema::class,
static function (&$wrappedObject, $proxy, $method, array $parameters, &$initializer) use ($originalSchemaManipulator): bool {
$initializer = null;

$wrappedObject = $originalSchemaManipulator->createFromSchema();

return true;
}
);
return LazySchema::createLazyProxy(static fn () => $originalSchemaManipulator->createFromSchema());
}

public function createToSchema(Schema $fromSchema): Schema
{
$originalSchemaManipulator = $this->originalSchemaManipulator;

if ($fromSchema instanceof LazyLoadingInterface && ! $fromSchema->isProxyInitialized()) {
return $this->proxyFactory->createProxy(
Schema::class,
static function (&$wrappedObject, $proxy, $method, array $parameters, &$initializer) use ($originalSchemaManipulator, $fromSchema): bool {
$initializer = null;

$wrappedObject = $originalSchemaManipulator->createToSchema($fromSchema);

return true;
}
);
if ($fromSchema instanceof LazySchema && ! $fromSchema->isLazyObjectInitialized()) {
return LazySchema::createLazyProxy(static fn () => $originalSchemaManipulator->createToSchema($fromSchema));
}

return $this->originalSchemaManipulator->createToSchema($fromSchema);
Expand All @@ -80,8 +44,8 @@ static function (&$wrappedObject, $proxy, $method, array $parameters, &$initiali
public function getSqlDiffToMigrate(Schema $fromSchema, Schema $toSchema): array
{
if (
$toSchema instanceof LazyLoadingInterface
&& ! $toSchema->isProxyInitialized()
$toSchema instanceof LazySchema
&& ! $toSchema->isLazyObjectInitialized()
) {
return [];
}
Expand Down
6 changes: 0 additions & 6 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ parameters:
-
message: '~^Variable property access on SimpleXMLElement\.$~'
path: lib/Doctrine/Migrations/Configuration/Migration/XmlFile.php

-
message: '~^Call to function is_bool\(\) with bool will always evaluate to true\.$~'
path: lib/Doctrine/Migrations/InlineParameterFormatter.php
Expand All @@ -24,11 +23,6 @@ parameters:
message: '~^Method Doctrine\\Migrations\\Tests\\Stub\\DoctrineRegistry::getService\(\) should return Doctrine\\Persistence\\ObjectManager but returns Doctrine\\DBAL\\Connection\|Doctrine\\ORM\\EntityManager~'
path: tests/Doctrine/Migrations/Tests/Stub/DoctrineRegistry.php

# We cannot document the closure as precise as PHPStan wants us to.
-
message: '~^Parameter #2 \$initializer of method ProxyManager\\Factory\\LazyLoadingValueHolderFactory\:\:createProxy\(\) expects Closure\(.*\)\: bool, Closure\(.*\)\: true given\.$~'
path: lib/Doctrine/Migrations/Provider/LazySchemaDiffProvider.php

# https://github.com/phpstan/phpstan/issues/5982
-
message: '~^Cannot call method getWrappedConnection\(\) on class-string\|object\.~'
Expand Down

0 comments on commit 7fa9d14

Please sign in to comment.