‪TYPO3CMS  9.5
SqlReader.php
Go to the documentation of this file.
1 <?php
2 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 
18 use TYPO3\CMS\Core\Package\PackageManager;
21 
29 {
33  protected ‪$signalSlotDispatcher;
34 
38  protected ‪$packageManager;
39 
45  public function ‪__construct(‪Dispatcher ‪$signalSlotDispatcher = null, PackageManager ‪$packageManager = null)
46  {
47  $this->signalSlotDispatcher = ‪$signalSlotDispatcher ?: GeneralUtility::makeInstance(Dispatcher::class);
48  $this->packageManager = ‪$packageManager ?? GeneralUtility::makeInstance(PackageManager::class);
49  }
50 
60  public function ‪getTablesDefinitionString(bool $withStatic = false): string
61  {
62  $sqlString = [];
63 
64  // Find all ext_tables.sql of loaded extensions
65  foreach ($this->packageManager->getActivePackages() as $package) {
66  $packagePath = $package->getPackagePath();
67  if (@file_exists($packagePath . 'ext_tables.sql')) {
68  $sqlString[] = file_get_contents($packagePath . 'ext_tables.sql');
69  }
70  if ($withStatic && @file_exists($packagePath . 'ext_tables_static+adt.sql')) {
71  $sqlString[] = file_get_contents($packagePath . 'ext_tables_static+adt.sql');
72  }
73  }
74 
75  $sqlString = $this->‪emitTablesDefinitionIsBeingBuiltSignal($sqlString);
76 
77  return implode(LF . LF, $sqlString);
78  }
79 
89  public function ‪getStatementArray(string $dumpContent, string $queryRegex = null): array
90  {
91  $statementArray = [];
92  $statementArrayPointer = 0;
93  $isInMultilineComment = false;
94  foreach (explode(LF, $dumpContent) as $lineContent) {
95  $lineContent = trim($lineContent);
96 
97  // Skip empty lines and comments
98  if ($lineContent === '' || $lineContent[0] === '#' || strpos($lineContent, '--') === 0 ||
99  strpos($lineContent, '/*') === 0 || substr($lineContent, -2) === '*/' || $isInMultilineComment
100  ) {
101  // skip c style multiline comments
102  if (strpos($lineContent, '/*') === 0 && substr($lineContent, -2) !== '*/') {
103  $isInMultilineComment = true;
104  }
105  if (substr($lineContent, -2) === '*/') {
106  $isInMultilineComment = false;
107  }
108  continue;
109  }
110 
111  $statementArray[$statementArrayPointer] = ($statementArray[$statementArrayPointer] ?? '') . $lineContent;
112 
113  if (substr($lineContent, -1) === ';') {
114  $statement = trim($statementArray[$statementArrayPointer]);
115  if (!$statement || ($queryRegex && !preg_match('/' . $queryRegex . '/i', $statement))) {
116  unset($statementArray[$statementArrayPointer]);
117  }
118  $statementArrayPointer++;
119  } else {
120  $statementArray[$statementArrayPointer] .= ' ';
121  }
122  }
123 
124  return $statementArray;
125  }
126 
133  public function ‪getInsertStatementArray(string $dumpContent): array
134  {
135  return $this->‪getStatementArray($dumpContent, '^INSERT');
136  }
137 
144  public function ‪getCreateTableStatementArray(string $dumpContent): array
145  {
146  return $this->‪getStatementArray($dumpContent, '^CREATE TABLE');
147  }
148 
158  protected function ‪emitTablesDefinitionIsBeingBuiltSignal(array $sqlString): array
159  {
160  // Using the old class name from the install tool here to keep backwards compatibility.
161  $signalReturn = $this->signalSlotDispatcher->dispatch(
162  'TYPO3\\CMS\\Install\\Service\\SqlExpectedSchemaService',
163  'tablesDefinitionIsBeingBuilt',
164  [$sqlString]
165  );
166 
167  // This is important to support old associated returns
168  $signalReturn = array_values($signalReturn);
169  $sqlString = $signalReturn[0];
170  if (!is_array($sqlString)) {
172  sprintf(
173  'The signal %s of class %s returned a value of type %s, but array was expected.',
174  'tablesDefinitionIsBeingBuilt',
175  __CLASS__,
176  gettype($sqlString)
177  ),
178  1382351456
179  );
180  }
181 
182  return $sqlString;
183  }
184 }
‪TYPO3\CMS\Core\Database\Schema\SqlReader\emitTablesDefinitionIsBeingBuiltSignal
‪array emitTablesDefinitionIsBeingBuiltSignal(array $sqlString)
Definition: SqlReader.php:156
‪TYPO3\CMS\Core\Database\Schema\SqlReader\$packageManager
‪PackageManager $packageManager
Definition: SqlReader.php:36
‪TYPO3\CMS\Core\Database\Schema
Definition: Comparator.php:3
‪TYPO3\CMS\Core\Database\Schema\SqlReader
Definition: SqlReader.php:29
‪TYPO3\CMS\Core\Database\Schema\SqlReader\getStatementArray
‪array getStatementArray(string $dumpContent, string $queryRegex=null)
Definition: SqlReader.php:87
‪TYPO3\CMS\Core\Database\Schema\SqlReader\__construct
‪__construct(Dispatcher $signalSlotDispatcher=null, PackageManager $packageManager=null)
Definition: SqlReader.php:43
‪TYPO3\CMS\Core\Database\Schema\SqlReader\getInsertStatementArray
‪array getInsertStatementArray(string $dumpContent)
Definition: SqlReader.php:131
‪TYPO3\CMS\Core\Database\Schema\SqlReader\getCreateTableStatementArray
‪array getCreateTableStatementArray(string $dumpContent)
Definition: SqlReader.php:142
‪TYPO3\CMS\Core\Database\Schema\Exception\UnexpectedSignalReturnValueTypeException
Definition: UnexpectedSignalReturnValueTypeException.php:23
‪TYPO3\CMS\Core\Database\Schema\SqlReader\getTablesDefinitionString
‪string getTablesDefinitionString(bool $withStatic=false)
Definition: SqlReader.php:58
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Database\Schema\SqlReader\$signalSlotDispatcher
‪Dispatcher $signalSlotDispatcher
Definition: SqlReader.php:32
‪TYPO3\CMS\Extbase\SignalSlot\Dispatcher
Definition: Dispatcher.php:28