Schema.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Dto;
  7. use Magento\Framework\App\ResourceConnection;
  8. /**
  9. * Schema DTO element.
  10. *
  11. * Aggregation root for all structural elements. Provides access to tables by their names.
  12. */
  13. class Schema
  14. {
  15. /**
  16. * Resource connection.
  17. *
  18. * @var ResourceConnection
  19. */
  20. private $resourceConnection;
  21. /**
  22. * Schema tables.
  23. *
  24. * @var Table[]
  25. */
  26. private $tables;
  27. /**
  28. * Schema constructor.
  29. *
  30. * @param ResourceConnection $resourceConnection
  31. */
  32. public function __construct(ResourceConnection $resourceConnection)
  33. {
  34. $this->resourceConnection = $resourceConnection;
  35. $this->tables = [];
  36. }
  37. /**
  38. * Retrieve all tables, that are presented in schema.
  39. *
  40. * @return Table[]
  41. */
  42. public function getTables()
  43. {
  44. return $this->tables;
  45. }
  46. /**
  47. * Add table by name key to tables registry.
  48. *
  49. * @param Table $table
  50. * @return $this
  51. */
  52. public function addTable(Table $table)
  53. {
  54. $this->tables[$table->getName()] = $table;
  55. return $this;
  56. }
  57. /**
  58. * Retrieve table by it name.
  59. *
  60. * Return false if table is not present in schema.
  61. *
  62. * @param string $name
  63. * @return bool|Table
  64. */
  65. public function getTableByName($name)
  66. {
  67. $name = $this->resourceConnection->getTableName($name);
  68. return $this->tables[$name] ?? false;
  69. }
  70. }