TYPO3 CMS  TYPO3_8-7
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 
19 
24 {
31  public function executeUpdateIfNeeded($extensionKey)
32  {
33  $className = $this->requireUpdateScript($extensionKey);
34  $scriptObject = GeneralUtility::makeInstance($className);
35 
36  // old em always assumed the method exist, we do so too.
37  // @TODO: Make this smart, let scripts implement interfaces
38  // @TODO: With current ext_update construct it is impossible to enforce some type of return
39  return $scriptObject->access() ? $scriptObject->main() : null;
40  }
41 
51  protected function requireUpdateScript($extensionKey)
52  {
53  if (class_exists('ext_update', false)) {
54  throw new ExtensionManagerException(
55  'class ext_update for this run does already exist, requiring impossible',
56  1359748085
57  );
58  }
59 
60  $className = $this->determineUpdateClassName($extensionKey);
61  if ($className === '') {
62  throw new ExtensionManagerException(
63  'Requested update script of extension does not exist',
64  1359747976
65  );
66  }
67  return $className;
68  }
69 
78  public function checkUpdateScriptExists($extensionKey)
79  {
80  $className = $this->determineUpdateClassName($extensionKey);
81  if ($className !== '') {
82  $updater = GeneralUtility::makeInstance($className);
83  return $updater->access();
84  }
85  return false;
86  }
87 
95  protected function determineUpdateClassName($extensionKey)
96  {
97  $updateScript = GeneralUtility::getFileAbsFileName('EXT:' . $extensionKey . '/class.ext_update.php');
98  if (!file_exists($updateScript)) {
99  return '';
100  }
101 
102  // get script contents
103  $scriptSourceCode = GeneralUtility::getUrl($updateScript);
104  // check if it has a namespace
105  if (!preg_match('/<\?php.*namespace\s+([^;]+);.*class/is', $scriptSourceCode, $matches)) {
106  // if no, rename the class with a unique name
107  $className = 'ext_update' . md5($extensionKey . $scriptSourceCode);
108  $temporaryFileName = PATH_site . 'typo3temp/var/transient/' . $className . '.php';
109  if (!file_exists(GeneralUtility::getFileAbsFileName($temporaryFileName))) {
110  $scriptSourceCode = preg_replace('/^\s*class\s+ext_update\s+/m', 'class ' . $className . ' ', $scriptSourceCode);
111  GeneralUtility::writeFileToTypo3tempDir($temporaryFileName, $scriptSourceCode);
112  }
113  $updateScript = $temporaryFileName;
114  } else {
115  $className = $matches[1] . '\ext_update';
116  }
117  include_once $updateScript;
118  if (!class_exists($className, false)) {
119  throw new ExtensionManagerException(
120  sprintf('class.ext_update.php of extension "%s" did not declare ext_update class', $extensionKey),
121  1428176468
122  );
123  }
124 
125  return $className;
126  }
127 }
static writeFileToTypo3tempDir($filepath, $content)
static getFileAbsFileName($filename, $_=null, $_2=null)
static makeInstance($className,... $constructorArguments)