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