LazySchemaDiffProvider.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Migrations\Provider;
  20. use Doctrine\DBAL\Schema\Schema;
  21. use ProxyManager\Configuration;
  22. use ProxyManager\Factory\LazyLoadingValueHolderFactory;
  23. use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
  24. use ProxyManager\Proxy\LazyLoadingInterface;
  25. class LazySchemaDiffProvider implements SchemaDiffProviderInterface
  26. {
  27. /** @var LazyLoadingValueHolderFactory */
  28. private $proxyFactory;
  29. /** @var SchemaDiffProviderInterface */
  30. private $originalSchemaManipulator;
  31. public function __construct(LazyLoadingValueHolderFactory $proxyFactory, SchemaDiffProviderInterface $originalSchemaManipulator)
  32. {
  33. $this->proxyFactory = $proxyFactory;
  34. $this->originalSchemaManipulator = $originalSchemaManipulator;
  35. }
  36. public static function fromDefaultProxyFacyoryConfiguration(SchemaDiffProviderInterface $originalSchemaManipulator)
  37. {
  38. $proxyConfig = new Configuration();
  39. $proxyConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
  40. $proxyFactory = new LazyLoadingValueHolderFactory($proxyConfig);
  41. return new LazySchemaDiffProvider($proxyFactory, $originalSchemaManipulator);
  42. }
  43. /**
  44. * @return Schema
  45. */
  46. public function createFromSchema()
  47. {
  48. $originalSchemaManipulator = $this->originalSchemaManipulator;
  49. return $this->proxyFactory->createProxy(
  50. Schema::class,
  51. function (& $wrappedObject, $proxy, $method, array $parameters, & $initializer) use ($originalSchemaManipulator) {
  52. $initializer = null;
  53. $wrappedObject = $originalSchemaManipulator->createFromSchema();
  54. return true;
  55. }
  56. );
  57. }
  58. /**
  59. * @param Schema $fromSchema
  60. * @return Schema
  61. */
  62. public function createToSchema(Schema $fromSchema)
  63. {
  64. $originalSchemaManipulator = $this->originalSchemaManipulator;
  65. if ($fromSchema instanceof LazyLoadingInterface && ! $fromSchema->isProxyInitialized()) {
  66. return $this->proxyFactory->createProxy(
  67. Schema::class,
  68. function (& $wrappedObject, $proxy, $method, array $parameters, & $initializer) use ($originalSchemaManipulator, $fromSchema) {
  69. $initializer = null;
  70. $wrappedObject = $originalSchemaManipulator->createToSchema($fromSchema);
  71. return true;
  72. }
  73. );
  74. }
  75. return $this->originalSchemaManipulator->createToSchema($fromSchema);
  76. }
  77. /**
  78. * @param Schema $fromSchema
  79. * @param Schema $toSchema
  80. *
  81. * @return string[]
  82. */
  83. public function getSqlDiffToMigrate(Schema $fromSchema, Schema $toSchema)
  84. {
  85. if (
  86. $toSchema instanceof LazyLoadingInterface
  87. && ! $toSchema->isProxyInitialized()
  88. ) {
  89. return [];
  90. }
  91. return $this->originalSchemaManipulator->getSqlDiffToMigrate($fromSchema, $toSchema);
  92. }
  93. }