‪TYPO3CMS  10.4
UpdateScriptUtility.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 
27 {
34  public function ‪executeUpdateIfNeeded($extensionKey)
35  {
36  $className = $this->‪requireUpdateScript($extensionKey);
37  $scriptObject = GeneralUtility::makeInstance($className);
38 
39  // old em always assumed the method exist, we do so too.
40  // @TODO: Make this smart, let scripts implement interfaces
41  // @TODO: With current ext_update construct it is impossible to enforce some type of return
42  return $scriptObject->access() ? $scriptObject->main() : null;
43  }
44 
54  protected function ‪requireUpdateScript($extensionKey)
55  {
56  if (class_exists('ext_update', false)) {
58  'class ext_update for this run does already exist, requiring impossible',
59  1359748085
60  );
61  }
62 
63  $className = $this->‪determineUpdateClassName($extensionKey);
64  if ($className === '') {
66  'Requested update script of extension does not exist',
67  1359747976
68  );
69  }
70  return $className;
71  }
72 
81  public function ‪checkUpdateScriptExists($extensionKey)
82  {
83  $className = $this->‪determineUpdateClassName($extensionKey);
84  if ($className !== '') {
85  $updater = GeneralUtility::makeInstance($className);
86  return $updater->access();
87  }
88  return false;
89  }
90 
98  protected function ‪determineUpdateClassName($extensionKey)
99  {
100  $updateScript = GeneralUtility::getFileAbsFileName('EXT:' . $extensionKey . '/class.ext_update.php');
101  if (!file_exists($updateScript)) {
102  return '';
103  }
104 
105  // get script contents
106  $scriptSourceCode = ‪GeneralUtility::getUrl($updateScript);
107  // check if it has a namespace
108  if (!preg_match('/<\?php.*namespace\s+([^;]+);.*class/is', $scriptSourceCode, $matches)) {
109  // if no, rename the class with a unique name
110  $className = 'ext_update' . md5($extensionKey . $scriptSourceCode);
111  $temporaryFileName = ‪Environment::getVarPath() . '/transient/' . $className . '.php';
112  if (!file_exists(GeneralUtility::getFileAbsFileName($temporaryFileName))) {
113  $scriptSourceCode = preg_replace('/^\s*class\s+ext_update\s+/m', 'class ' . $className . ' ', $scriptSourceCode);
114  ‪GeneralUtility::writeFileToTypo3tempDir($temporaryFileName, $scriptSourceCode);
115  }
116  $updateScript = $temporaryFileName;
117  } else {
118  $className = $matches[1] . '\ext_update';
119  }
120  include_once $updateScript;
121  if (!class_exists($className, false)) {
123  sprintf('class.ext_update.php of extension "%s" did not declare ext_update class', $extensionKey),
124  1428176468
125  );
126  }
127 
128  return $className;
129  }
130 }
‪TYPO3\CMS\Extensionmanager\Utility
‪TYPO3\CMS\Extensionmanager\Utility\UpdateScriptUtility\determineUpdateClassName
‪string determineUpdateClassName($extensionKey)
Definition: UpdateScriptUtility.php:98
‪TYPO3\CMS\Core\Utility\GeneralUtility\getUrl
‪static mixed getUrl($url, $includeHeader=0, $requestHeaders=null, &$report=null)
Definition: GeneralUtility.php:1748
‪TYPO3\CMS\Extensionmanager\Utility\UpdateScriptUtility\requireUpdateScript
‪string requireUpdateScript($extensionKey)
Definition: UpdateScriptUtility.php:54
‪TYPO3\CMS\Extensionmanager\Utility\UpdateScriptUtility
Definition: UpdateScriptUtility.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility\writeFileToTypo3tempDir
‪static string writeFileToTypo3tempDir($filepath, $content)
Definition: GeneralUtility.php:1928
‪TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
Definition: ExtensionManagerException.php:24
‪TYPO3\CMS\Extensionmanager\Utility\UpdateScriptUtility\executeUpdateIfNeeded
‪mixed executeUpdateIfNeeded($extensionKey)
Definition: UpdateScriptUtility.php:34
‪TYPO3\CMS\Extensionmanager\Utility\UpdateScriptUtility\checkUpdateScriptExists
‪bool checkUpdateScriptExists($extensionKey)
Definition: UpdateScriptUtility.php:81
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:192