AddComplexElement.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup\Declaration\Schema\Operations;
  7. use Magento\Framework\Setup\Declaration\Schema\Db\DbSchemaWriterInterface;
  8. use Magento\Framework\Setup\Declaration\Schema\Db\DefinitionAggregator;
  9. use Magento\Framework\Setup\Declaration\Schema\Dto\ElementInterface;
  10. use Magento\Framework\Setup\Declaration\Schema\Dto\TableElementInterface;
  11. use Magento\Framework\Setup\Declaration\Schema\ElementHistory;
  12. use Magento\Framework\Setup\Declaration\Schema\OperationInterface;
  13. /**
  14. * Add complex element operation.
  15. *
  16. * Adds element that has various dependencies, like foreign key that has dependencies to another table.
  17. */
  18. class AddComplexElement implements OperationInterface
  19. {
  20. /**
  21. * Operation name.
  22. */
  23. const OPERATION_NAME = 'add_complex_element';
  24. /**
  25. * @var DefinitionAggregator
  26. */
  27. private $definitionAggregator;
  28. /**
  29. * @var DbSchemaWriterInterface
  30. */
  31. private $dbSchemaWriter;
  32. /**
  33. * Constructor.
  34. *
  35. * @param DefinitionAggregator $definitionAggregator
  36. * @param DbSchemaWriterInterface $dbSchemaWriter
  37. */
  38. public function __construct(
  39. DefinitionAggregator $definitionAggregator,
  40. DbSchemaWriterInterface $dbSchemaWriter
  41. ) {
  42. $this->definitionAggregator = $definitionAggregator;
  43. $this->dbSchemaWriter = $dbSchemaWriter;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function getOperationName()
  49. {
  50. return self::OPERATION_NAME;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function isOperationDestructive()
  56. {
  57. return false;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function doOperation(ElementHistory $elementHistory)
  63. {
  64. /**
  65. * @var TableElementInterface | ElementInterface $element
  66. */
  67. $element = $elementHistory->getNew();
  68. $definition = $this->definitionAggregator->toDefinition($element);
  69. $statement = $this->dbSchemaWriter->addElement(
  70. $element->getName(),
  71. $element->getTable()->getResource(),
  72. $element->getTable()->getName(),
  73. $definition,
  74. $element->getType()
  75. );
  76. return [$statement];
  77. }
  78. }