‪TYPO3CMS  10.4
SqlReader.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 Psr\EventDispatcher\EventDispatcherInterface;
22 use TYPO3\CMS\Core\Package\PackageManager;
23 
31 {
35  protected ‪$eventDispatcher;
36 
40  protected ‪$packageManager;
41 
47  public function ‪__construct(EventDispatcherInterface ‪$eventDispatcher, PackageManager ‪$packageManager)
48  {
49  $this->eventDispatcher = ‪$eventDispatcher;
50  $this->packageManager = ‪$packageManager;
51  }
52 
59  public function ‪getTablesDefinitionString(bool $withStatic = false): string
60  {
61  $sqlString = [];
62 
63  // Find all ext_tables.sql of loaded extensions
64  foreach ($this->packageManager->getActivePackages() as $package) {
65  $packagePath = $package->getPackagePath();
66  if (@file_exists($packagePath . 'ext_tables.sql')) {
67  $sqlString[] = (string)file_get_contents($packagePath . 'ext_tables.sql');
68  }
69  if ($withStatic && @file_exists($packagePath . 'ext_tables_static+adt.sql')) {
70  $sqlString[] = (string)file_get_contents($packagePath . 'ext_tables_static+adt.sql');
71  }
72  }
73 
75  $event = $this->eventDispatcher->dispatch(new ‪AlterTableDefinitionStatementsEvent($sqlString));
76  $sqlString = $event->getSqlData();
77 
78  return implode(LF . LF, $sqlString);
79  }
80 
90  public function ‪getStatementArray(string $dumpContent, string $queryRegex = null): array
91  {
92  $statementArray = [];
93  $statementArrayPointer = 0;
94  $isInMultilineComment = false;
95  foreach (explode(LF, $dumpContent) as $lineContent) {
96  $lineContent = trim($lineContent);
97 
98  // Skip empty lines and comments
99  if ($lineContent === '' || $lineContent[0] === '#' || strpos($lineContent, '--') === 0 ||
100  strpos($lineContent, '/*') === 0 || substr($lineContent, -2) === '*/' || $isInMultilineComment
101  ) {
102  // skip c style multiline comments
103  if (strpos($lineContent, '/*') === 0 && substr($lineContent, -2) !== '*/') {
104  $isInMultilineComment = true;
105  }
106  if (substr($lineContent, -2) === '*/') {
107  $isInMultilineComment = false;
108  }
109  continue;
110  }
111 
112  $statementArray[$statementArrayPointer] = ($statementArray[$statementArrayPointer] ?? '') . $lineContent;
113 
114  if (substr($lineContent, -1) === ';') {
115  $statement = trim($statementArray[$statementArrayPointer]);
116  if (!$statement || ($queryRegex && !preg_match('/' . $queryRegex . '/i', $statement))) {
117  unset($statementArray[$statementArrayPointer]);
118  }
119  $statementArrayPointer++;
120  } else {
121  $statementArray[$statementArrayPointer] .= ' ';
122  }
123  }
124 
125  return $statementArray;
126  }
127 
134  public function ‪getInsertStatementArray(string $dumpContent): array
135  {
136  return $this->‪getStatementArray($dumpContent, '^INSERT');
137  }
138 
145  public function ‪getCreateTableStatementArray(string $dumpContent): array
146  {
147  return $this->‪getStatementArray($dumpContent, '^CREATE TABLE');
148  }
149 }
‪TYPO3\CMS\Core\Database\Schema\SqlReader\$packageManager
‪PackageManager $packageManager
Definition: SqlReader.php:38
‪TYPO3\CMS\Core\Database\Schema
Definition: Comparator.php:18
‪TYPO3\CMS\Core\Database\Schema\SqlReader
Definition: SqlReader.php:31
‪TYPO3\CMS\Core\Database\Schema\SqlReader\getStatementArray
‪array getStatementArray(string $dumpContent, string $queryRegex=null)
Definition: SqlReader.php:88
‪TYPO3\CMS\Core\Database\Schema\SqlReader\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: SqlReader.php:34
‪TYPO3\CMS\Core\Database\Schema\SqlReader\getInsertStatementArray
‪array getInsertStatementArray(string $dumpContent)
Definition: SqlReader.php:132
‪TYPO3\CMS\Core\Database\Schema\SqlReader\__construct
‪__construct(EventDispatcherInterface $eventDispatcher, PackageManager $packageManager)
Definition: SqlReader.php:45
‪TYPO3\CMS\Core\Database\Schema\SqlReader\getCreateTableStatementArray
‪array getCreateTableStatementArray(string $dumpContent)
Definition: SqlReader.php:143
‪TYPO3\CMS\Core\Database\Event\AlterTableDefinitionStatementsEvent
Definition: AlterTableDefinitionStatementsEvent.php:24
‪TYPO3\CMS\Core\Database\Schema\SqlReader\getTablesDefinitionString
‪string getTablesDefinitionString(bool $withStatic=false)
Definition: SqlReader.php:57