TYPO3 CMS  TYPO3_8-7
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 
21 
28 class SqlReader
29 {
34 
40  {
41  $this->signalSlotDispatcher = $signalSlotDispatcher ?: GeneralUtility::makeInstance(Dispatcher::class);
42  }
43 
53  public function getTablesDefinitionString(bool $withStatic = false): string
54  {
55  $sqlString = [];
56 
57  // Find all ext_tables.sql of loaded extensions
58  foreach ((array)$GLOBALS['TYPO3_LOADED_EXT'] as $extensionConfiguration) {
59  if (!is_array($extensionConfiguration) && !$extensionConfiguration instanceof \ArrayAccess) {
60  continue;
61  }
62  if ($extensionConfiguration['ext_tables.sql']) {
63  $sqlString[] = file_get_contents($extensionConfiguration['ext_tables.sql']);
64  }
65  if ($withStatic && $extensionConfiguration['ext_tables_static+adt.sql']) {
66  $sqlString[] = file_get_contents($extensionConfiguration['ext_tables_static+adt.sql']);
67  }
68  }
69 
70  $sqlString = $this->emitTablesDefinitionIsBeingBuiltSignal($sqlString);
71 
72  return implode(LF . LF, $sqlString);
73  }
74 
84  public function getStatementArray(string $dumpContent, string $queryRegex = null): array
85  {
86  $statementArray = [];
87  $statementArrayPointer = 0;
88  foreach (explode(LF, $dumpContent) as $lineContent) {
89  $lineContent = trim($lineContent);
90 
91  // Skip empty lines and comments
92  if ($lineContent === '' || $lineContent[0] === '#' || strpos($lineContent, '--') === 0) {
93  continue;
94  }
95 
96  $statementArray[$statementArrayPointer] .= $lineContent;
97 
98  if (substr($lineContent, -1) === ';') {
99  $statement = trim($statementArray[$statementArrayPointer]);
100  if (!$statement || ($queryRegex && !preg_match('/' . $queryRegex . '/i', $statement))) {
101  unset($statementArray[$statementArrayPointer]);
102  }
103  $statementArrayPointer++;
104  } else {
105  $statementArray[$statementArrayPointer] .= ' ';
106  }
107  }
108 
109  return $statementArray;
110  }
111 
118  public function getInsertStatementArray(string $dumpContent): array
119  {
120  return $this->getStatementArray($dumpContent, '^INSERT');
121  }
122 
129  public function getCreateTableStatementArray(string $dumpContent): array
130  {
131  return $this->getStatementArray($dumpContent, '^CREATE TABLE');
132  }
133 
143  protected function emitTablesDefinitionIsBeingBuiltSignal(array $sqlString): array
144  {
145  // Using the old class name from the install tool here to keep backwards compatibility.
146  $signalReturn = $this->signalSlotDispatcher->dispatch(
147  SqlExpectedSchemaService::class,
148  'tablesDefinitionIsBeingBuilt',
149  [$sqlString]
150  );
151 
152  // This is important to support old associated returns
153  $signalReturn = array_values($signalReturn);
154  $sqlString = $signalReturn[0];
155  if (!is_array($sqlString)) {
157  sprintf(
158  'The signal %s of class %s returned a value of type %s, but array was expected.',
159  'tablesDefinitionIsBeingBuilt',
160  __CLASS__,
161  gettype($sqlString)
162  ),
163  1382351456
164  );
165  }
166 
167  return $sqlString;
168  }
169 }
getInsertStatementArray(string $dumpContent)
Definition: SqlReader.php:118
getCreateTableStatementArray(string $dumpContent)
Definition: SqlReader.php:129
getTablesDefinitionString(bool $withStatic=false)
Definition: SqlReader.php:53
getStatementArray(string $dumpContent, string $queryRegex=null)
Definition: SqlReader.php:84
static makeInstance($className,... $constructorArguments)
emitTablesDefinitionIsBeingBuiltSignal(array $sqlString)
Definition: SqlReader.php:143
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
__construct(Dispatcher $signalSlotDispatcher=null)
Definition: SqlReader.php:39