LazySchemaDiffProvider.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations\Provider;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use ProxyManager\Configuration;
  6. use ProxyManager\Factory\LazyLoadingValueHolderFactory;
  7. use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
  8. use ProxyManager\Proxy\LazyLoadingInterface;
  9. /**
  10. * The LazySchemaDiffProvider is responsible for lazily generating the from schema when diffing two schemas
  11. * to produce a migration.
  12. *
  13. * @internal
  14. */
  15. class LazySchemaDiffProvider implements SchemaDiffProvider
  16. {
  17. /** @var LazyLoadingValueHolderFactory */
  18. private $proxyFactory;
  19. /** @var SchemaDiffProvider */
  20. private $originalSchemaManipulator;
  21. public function __construct(
  22. LazyLoadingValueHolderFactory $proxyFactory,
  23. SchemaDiffProvider $originalSchemaManipulator
  24. ) {
  25. $this->proxyFactory = $proxyFactory;
  26. $this->originalSchemaManipulator = $originalSchemaManipulator;
  27. }
  28. public static function fromDefaultProxyFactoryConfiguration(
  29. SchemaDiffProvider $originalSchemaManipulator
  30. ): LazySchemaDiffProvider {
  31. $proxyConfig = new Configuration();
  32. $proxyConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
  33. $proxyFactory = new LazyLoadingValueHolderFactory($proxyConfig);
  34. return new LazySchemaDiffProvider($proxyFactory, $originalSchemaManipulator);
  35. }
  36. public function createFromSchema(): Schema
  37. {
  38. $originalSchemaManipulator = $this->originalSchemaManipulator;
  39. return $this->proxyFactory->createProxy(
  40. Schema::class,
  41. static function (&$wrappedObject, $proxy, $method, array $parameters, &$initializer) use ($originalSchemaManipulator): bool {
  42. $initializer = null;
  43. $wrappedObject = $originalSchemaManipulator->createFromSchema();
  44. return true;
  45. }
  46. );
  47. }
  48. public function createToSchema(Schema $fromSchema): Schema
  49. {
  50. $originalSchemaManipulator = $this->originalSchemaManipulator;
  51. if ($fromSchema instanceof LazyLoadingInterface && ! $fromSchema->isProxyInitialized()) {
  52. return $this->proxyFactory->createProxy(
  53. Schema::class,
  54. static function (&$wrappedObject, $proxy, $method, array $parameters, &$initializer) use ($originalSchemaManipulator, $fromSchema): bool {
  55. $initializer = null;
  56. $wrappedObject = $originalSchemaManipulator->createToSchema($fromSchema);
  57. return true;
  58. }
  59. );
  60. }
  61. return $this->originalSchemaManipulator->createToSchema($fromSchema);
  62. }
  63. /** @return string[] */
  64. public function getSqlDiffToMigrate(Schema $fromSchema, Schema $toSchema): array
  65. {
  66. if (
  67. $toSchema instanceof LazyLoadingInterface
  68. && ! $toSchema->isProxyInitialized()
  69. ) {
  70. return [];
  71. }
  72. return $this->originalSchemaManipulator->getSqlDiffToMigrate($fromSchema, $toSchema);
  73. }
  74. }