‪TYPO3CMS  ‪main
SchemaInformation.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Doctrine\DBAL\Connection;
21 use Doctrine\DBAL\Schema\Schema;
22 use Doctrine\DBAL\Schema\Table;
24 
32 {
33  private string ‪$connectionIdentifier;
34 
35  public function ‪__construct(
36  private readonly Connection $connection,
37  private readonly ‪FrontendInterface $cache
38  ) {
39  $this->connectionIdentifier = sprintf(
40  '%s-%s',
41  str_replace(
42  ['.', ':', '/', '\\', '!', '?'],
43  '_',
44  (string)($connection->getParams()['dbname'] ?? 'generic')
45  ),
46  // hash connection params, which holds various information like host,
47  // port etc. to get a descriptive hash for this connection.
48  hash('xxh3', serialize($connection->getParams()))
49  );
50  }
51 
58  public function ‪listTableNames(): array
59  {
60  $tableNames = [];
61  $tables = $this->‪introspectSchema()->getTables();
62  array_walk($tables, static function (Table $table) use (&$tableNames): void {
63  $tableNames[] = $table->getName();
64  });
65  return $tableNames;
66  }
67 
74  public function ‪introspectSchema(): Schema
75  {
76  ‪$identifier = $this->connectionIdentifier . '-schema';
77  $schema = $this->cache->get(‪$identifier);
78  if ($schema instanceof Schema) {
79  return $schema;
80  }
81  $schema = $this->connection->createSchemaManager()->introspectSchema();
82  $this->cache->set(‪$identifier, $schema);
83  return $schema;
84  }
85 
92  public function ‪introspectTable(string $tableName): Table
93  {
94  ‪$identifier = $this->connectionIdentifier . '-table-' . $tableName;
95  $table = $this->cache->get(‪$identifier);
96  if ($table instanceof Table) {
97  return $table;
98  }
99  $table = $this->connection->createSchemaManager()->introspectTable($tableName);
100  $this->cache->set(‪$identifier, $table);
101  return $table;
102  }
103 }
‪TYPO3\CMS\Core\Database\Schema\SchemaInformation\introspectSchema
‪introspectSchema()
Definition: SchemaInformation.php:74
‪TYPO3\CMS\Core\Database\Schema\SchemaInformation
Definition: SchemaInformation.php:32
‪TYPO3\CMS\Core\Database\Schema
Definition: ColumnDiff.php:18
‪TYPO3\CMS\Core\Database\Schema\SchemaInformation\$connectionIdentifier
‪string $connectionIdentifier
Definition: SchemaInformation.php:33
‪TYPO3\CMS\Core\Database\Schema\SchemaInformation\listTableNames
‪string[] listTableNames()
Definition: SchemaInformation.php:58
‪TYPO3\CMS\Core\Database\Schema\SchemaInformation\introspectTable
‪introspectTable(string $tableName)
Definition: SchemaInformation.php:92
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Database\Schema\SchemaInformation\__construct
‪__construct(private readonly Connection $connection, private readonly FrontendInterface $cache)
Definition: SchemaInformation.php:35
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37