Schema.php

Same filename in this branch
  1. main core/modules/sqlite/src/Driver/Database/sqlite/Schema.php
  2. main core/modules/mysql/src/Driver/Database/mysql/Schema.php
  3. main core/modules/pgsql/src/Driver/Database/pgsql/Schema.php
  4. main core/tests/Drupal/Tests/Core/Database/Stub/Driver/Schema.php
  5. main core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Schema.php
  6. main core/lib/Drupal/Core/Database/Schema.php
Same filename and directory in other branches
  1. 11.x core/modules/sqlite/src/Driver/Database/sqlite/Schema.php
  2. 11.x core/modules/mysql/src/Driver/Database/mysql/Schema.php
  3. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Schema.php
  4. 11.x core/tests/Drupal/Tests/Core/Database/Stub/Driver/Schema.php
  5. 11.x core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Schema.php
  6. 11.x core/lib/Drupal/Core/Database/Schema.php
  7. 10 core/modules/sqlite/src/Driver/Database/sqlite/Schema.php
  8. 10 core/modules/mysql/src/Driver/Database/mysql/Schema.php
  9. 10 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php
  10. 10 core/tests/Drupal/Tests/Core/Database/Stub/Driver/Schema.php
  11. 10 core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Schema.php
  12. 10 core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php
  13. 10 core/lib/Drupal/Core/Database/Driver/mysql/Schema.php
  14. 10 core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
  15. 10 core/lib/Drupal/Core/Database/Schema.php
  16. 9 core/modules/sqlite/src/Driver/Database/sqlite/Schema.php
  17. 9 core/modules/mysql/src/Driver/Database/mysql/Schema.php
  18. 9 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql/Schema.php
  19. 9 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysqlDeprecatedVersion/Schema.php
  20. 9 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestPgsql/Schema.php
  21. 9 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php
  22. 9 core/tests/Drupal/Tests/Core/Database/Stub/Driver/Schema.php
  23. 9 core/tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses/Schema.php
  24. 9 core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php
  25. 9 core/lib/Drupal/Core/Database/Driver/mysql/Schema.php
  26. 9 core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
  27. 9 core/lib/Drupal/Core/Database/Schema.php
  28. 8.9.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql/Schema.php
  29. 8.9.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestPgsql/Schema.php
  30. 8.9.x core/tests/Drupal/Tests/Core/Database/Stub/Driver/Schema.php
  31. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php
  32. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Schema.php
  33. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
  34. 8.9.x core/lib/Drupal/Core/Database/Schema.php
  35. 11.x core/lib/Drupal/Core/Database/SchemaDefinition/Schema.php

Namespace

Drupal\Core\Database\SchemaDefinition

File

core/lib/Drupal/Core/Database/SchemaDefinition/Schema.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Core\Database\SchemaDefinition;

use Drupal\Component\Assertion\Inspector;
use Drupal\Core\Database\Exception\SchemaDefinitionException;

/**
 * Describes a database schema as a collection of database objects.
 */
final class Schema implements SchemaDefinitionInterface {
  
  /**
   * Constructor.
   *
   * @param SchemaDefinitionType $type
   *   The type of Drupal feature providing the schema.
   * @param string $name
   *   The schema name.
   * @param list<Table> $tables
   *   A list of Table objects.
   */
  public function __construct(public readonly SchemaDefinitionType $type, public readonly string $name, public readonly array $tables) {
    $this->validate();
  }
  
  /**
   * Validates the properties of the value object.
   *
   * @throws \Drupal\Core\Database\Exception\SchemaDefinitionException
   */
  private function validate() : void {
    if ($this->tables && !Inspector::assertAllObjects($this->tables, Table::class)) {
      throw new SchemaDefinitionException("All members of the 'tables' argument of a Schema must be Table objects");
    }
  }
  
  /**
   * Returns the names of the tables.
   *
   * @return list<string>
   *   The names of the tables.
   */
  public function tableNames() : array {
    return array_map(fn(Table $table): string => $table->name, $this->tables);
  }
  
  /**
   * Gets a specific table definition.
   *
   * @param string $name
   *   The name of the Table object to return.
   *
   * @return Table
   *   The Table object.
   *
   * @throws \Drupal\Core\Database\Exception\SchemaDefinitionException
   *   When the requested table is missing.
   */
  public function getTableDefinition(string $name) : Table {
    $ret = array_find($this->tables, function (Table $table) use ($name) : bool {
      return $table->name === $name;
    });
    if (!$ret) {
      throw new SchemaDefinitionException("Schema {$this->name} does not define a table named {$name}");
    }
    return $ret;
  }
  
  /**
   * {@inheritdoc}
   */
  public function toArray() : array {
    $spec = [];
    foreach ($this->tables as $table) {
      $spec[$table->name] = $table->toArray();
    }
    return $spec;
  }

}

Classes

Title Deprecated Summary
Schema Describes a database schema as a collection of database objects.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.