[TASK] Re-work/simplify copyright header in PHP files - Part 9
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / UpdateScriptUtility.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility;
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
17 /**
18 * Utility to find and execute class.ext_update.php scripts of extensions
19 *
20 * @author Christian Kuhn <lolli@schwarzbu.ch>
21 */
22 class UpdateScriptUtility {
23
24 /**
25 * Returns true, if ext_update class says it wants to run.
26 *
27 * @param string $extensionKey extension key
28 * @return mixed NULL, if update is not available, else update script return
29 */
30 public function executeUpdateIfNeeded($extensionKey) {
31 $this->requireUpdateScript($extensionKey);
32 $scriptObject = new \ext_update;
33 // old em always assumed the method exist, we do so too.
34 // @TODO: Make this smart, let scripts implement interfaces
35 // @TODO: Enforce different update class script names per extension
36 // @TODO: With different class names it would be easily possible to check for updates in list view.
37 $accessReturnValue = $scriptObject->access();
38
39 $result = NULL;
40 if ($accessReturnValue === TRUE) {
41 // @TODO: With current ext_update construct it is impossible
42 // @TODO: to enforce some type of return
43 $result = $scriptObject->main();
44 }
45
46 return $result;
47 }
48
49 /**
50 * Require update script.
51 * Throws exception if update script does not exist, so checkUpdateScriptExists()
52 * should be called before
53 *
54 * @param string $extensionKey
55 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
56 */
57 protected function requireUpdateScript($extensionKey) {
58 if (class_exists('ext_update')) {
59 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(
60 'class ext_update for this run does already exist, requiring impossible',
61 1359748085
62 );
63 }
64
65 $fileLocation = $this->getUpdateFileLocation($extensionKey);
66
67 if (!$this->checkUpdateScriptExists($extensionKey)) {
68 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(
69 'Requested update script of extension does not exist',
70 1359747976
71 );
72
73 }
74 require $fileLocation;
75
76 if (!class_exists('ext_update')) {
77 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(
78 'class.ext_update.php of extension did not declare ext_update class',
79 1359748132
80 );
81 }
82 }
83
84 /**
85 * Checks if an update class file exists.
86 * Does not check if some update is needed.
87 *
88 * @param string $extensionKey Extension key
89 * @return boolean True, if there is some update script
90 */
91 public function checkUpdateScriptExists($extensionKey) {
92 $updateScriptFileExists = FALSE;
93 if (file_exists($this->getUpdateFileLocation($extensionKey))) {
94 $updateScriptFileExists = TRUE;
95 }
96 return $updateScriptFileExists;
97 }
98
99 /**
100 * Determines location of update file.
101 * Does not check if the file exists.
102 *
103 * @param string $extensionKey Extension key
104 * @return string Absolute path to possible update file of extension
105 */
106 protected function getUpdateFileLocation($extensionKey) {
107 return \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(
108 'EXT:' . $extensionKey . '/class.ext_update.php',
109 FALSE
110 );
111 }
112 }