[TASK] General code cleanup in ext:sv
[Packages/TYPO3.CMS.git] / typo3 / sysext / sv / Classes / Report / ServicesListReport.php
1 <?php
2 namespace TYPO3\CMS\Sv\Report;
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 use TYPO3\CMS\Core\Exception;
18 use TYPO3\CMS\Core\Utility\CommandUtility;
19 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Reports\Controller\ReportController;
22 use TYPO3\CMS\Reports\ReportInterface;
23
24 /**
25 * This class provides a report displaying a list of all installed services
26 * Code inspired by EXT:dam/lib/class.tx_dam_svlist.php by René Fritz
27 *
28 * @author Francois Suter <francois@typo3.org>
29 */
30 class ServicesListReport implements ReportInterface {
31
32 /**
33 * Back-reference to the calling reports module
34 *
35 * @var ReportController
36 */
37 protected $reportsModule;
38
39 /**
40 * Constructor
41 *
42 * @param ReportController $reportsModule Back-reference to the calling reports module
43 */
44 public function __construct(ReportController $reportsModule) {
45 $this->reportsModule = $reportsModule;
46 $this->getLanguageService()->includeLLFile('EXT:sv/Resources/Private/Language/locallang.xlf');
47 }
48
49 /**
50 * This method renders the report
51 *
52 * @return string The status report as HTML
53 */
54 public function getReport() {
55 $content = '';
56 $content .= $this->renderHelp();
57 $content .= $this->renderServicesList();
58 $content .= $this->renderExecutablesSearchPathList();
59 return $content;
60 }
61
62 /**
63 * Renders the help comments at the top of the module.
64 *
65 * @return string The help content for this module.
66 */
67 protected function renderHelp() {
68 $help = '<p class="lead">' . $this->getLanguageService()->getLL('report_explanation') . '</p>';
69 $help .= '<p class="help">' . $this->getLanguageService()->getLL('externals_explanation') . '</p>';
70 return $help;
71 }
72
73 /**
74 * This method assembles a list of all installed services
75 *
76 * @return string HTML to display
77 */
78 protected function renderServicesList() {
79 $servicesList = '';
80 $services = $this->getInstalledServices();
81 foreach ($services as $serviceType => $installedServices) {
82 $servicesList .= $this->renderServiceTypeList($serviceType, $installedServices);
83 }
84 return $servicesList;
85 }
86
87 /**
88 * Renders the services list for a single service type.
89 *
90 * @param string $serviceType The service type to render the installed services list for
91 * @param array $services List of services for the given type
92 * @return string Service list as HTML for one service type
93 */
94 protected function renderServiceTypeList($serviceType, $services) {
95 $lang = $this->getLanguageService();
96 $header = '<h3>' . sprintf($lang->getLL('service_type'), $serviceType) . '</h3>';
97 $serviceList = '
98 <table class="table table-striped table-hover tx_sv_reportlist">
99 <thead>
100 <tr>
101 <td style="width: 35%">' . $lang->getLL('service') . '</td>
102 <td>' . $lang->getLL('priority') . '</td>
103 <td>' . $lang->getLL('quality') . '</td>
104 <td style="width: 35%">' . $lang->getLL('subtypes') . '</td>
105 <td>' . $lang->getLL('os') . '</td>
106 <td>' . $lang->getLL('externals') . '</td>
107 <td>' . $lang->getLL('available') . '</td>
108 </tr>
109 </thead>
110 <tbody>';
111
112 foreach ($services as $serviceKey => $serviceInformation) {
113 $serviceList .= $this->renderServiceRow($serviceKey, $serviceInformation);
114 }
115
116 $serviceList .= '
117 </tbody>
118 </table>
119 ';
120 return $header . $serviceList;
121 }
122
123 /**
124 * Renders a single service's row.
125 *
126 * @param string $serviceKey The service key to access the service.
127 * @param array $serviceInformation registration information of the service.
128 * @return string HTML row for the service.
129 */
130 protected function renderServiceRow($serviceKey, $serviceInformation) {
131 $serviceDescription = '
132 <p class="service-header">
133 <span class="service-title">' . $serviceInformation['title'] . '</span> (' . $serviceInformation['extKey'] . ': ' . $serviceKey . ')
134 </p>';
135 if (!empty($serviceInformation['description'])) {
136 $serviceDescription .= '<p class="service-description">' . $serviceInformation['description'] . '</p>';
137 }
138 $serviceSubtypes = $serviceInformation['serviceSubTypes'] ? implode(', ', $serviceInformation['serviceSubTypes']) : '-';
139 $serviceOperatingSystem = $serviceInformation['os'] ?: $this->getLanguageService()->getLL('any');
140 $serviceRequiredExecutables = $serviceInformation['exec'] ?: '-';
141 $serviceAvailabilityClass = 'danger';
142 $serviceAvailable = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_common.xlf:no');
143 try {
144 $serviceDetails = ExtensionManagementUtility::findServiceByKey($serviceKey);
145 if ($serviceDetails['available']) {
146 $serviceAvailabilityClass = 'success';
147 $serviceAvailable = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_common.xlf:yes');
148 }
149 } catch (Exception $e) {
150
151 }
152 $serviceRow = '
153 <tr class="service ' . $serviceAvailabilityClass . '">
154 <td class="first-cell">' . $serviceDescription . '</td>
155 <td class="cell">' . $serviceInformation['priority'] . '</td>
156 <td class="cell">' . $serviceInformation['quality'] . '</td>
157 <td class="cell">' . $serviceSubtypes . '</td>
158 <td class="cell">' . $serviceOperatingSystem . '</td>
159 <td class="cell">' . $serviceRequiredExecutables . '</td>
160 <td class="last-cell">' . $serviceAvailable . '</td>
161 </tr>';
162 return $serviceRow;
163 }
164
165 /**
166 * This method assembles a list of all defined executables search paths
167 *
168 * @return string HTML to display
169 */
170 protected function renderExecutablesSearchPathList() {
171 $searchPaths = CommandUtility::getPaths(TRUE);
172 $content = '<h3>' . $this->getLanguageService()->getLL('search_paths') . '</h3>';
173 if (count($searchPaths) === 0) {
174 $content .= '<p>' . $this->getLanguageService()->getLL('no_search_paths') . '</p>';
175 } else {
176 $content .= '
177 <table class="table table-striped table-hover tx_sv_reportlist">
178 <thead>
179 <tr>
180 <td>' . $this->getLanguageService()->getLL('path') . '</td>
181 <td>' . $this->getLanguageService()->getLL('valid') . '</td>
182 </tr>
183 </thead>
184 <tbody>';
185 foreach ($searchPaths as $path => $isValid) {
186 $pathAccessibleClass = 'danger';
187 $pathAccessible = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_common.xlf:no');
188 if ($isValid) {
189 $pathAccessibleClass = 'success';
190 $pathAccessible = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_common.xlf:yes');
191 }
192 $content .= '
193 <tr class="' . $pathAccessibleClass . '">
194 <td class="first-cell">' . GeneralUtility::fixWindowsFilePath($path) . '</td>
195 <td class="last-cell">' . $pathAccessible . '</td>
196 </tr>';
197 }
198 $content .= '
199 </tbody>
200 </table>';
201 }
202 return $content;
203 }
204
205 /**
206 * This method filters the $T3_SERVICES global array to return a relevant,
207 * ordered list of installed services.
208 *
209 * Every installed service appears twice in $T3_SERVICES: once as a service key
210 * for a given service type, and once a service type all by itself
211 * The list of services to display must avoid these duplicates
212 *
213 * Furthermore, inside each service type, installed services must be
214 * ordered by priority and quality
215 *
216 * @return array List of filtered and ordered services
217 */
218 protected function getInstalledServices() {
219 $filteredServices = array();
220 foreach ($GLOBALS['T3_SERVICES'] as $serviceType => $serviceList) {
221 // If the (first) key of the service list is not the same as the service type,
222 // it's a "true" service type. Keep it and sort it.
223 if (key($serviceList) !== $serviceType) {
224 uasort($serviceList, array($this, 'sortServices'));
225 $filteredServices[$serviceType] = $serviceList;
226 }
227 }
228 return $filteredServices;
229 }
230
231 /**
232 * Utility method used to sort services according to their priority and
233 * quality.
234 *
235 * @param array $a First service to compare
236 * @param array $b Second service to compare
237 * @return int 1, 0 or -1 if a is smaller, equal or greater than b, respectively
238 */
239 protected function sortServices(array $a, array $b) {
240 $result = 0;
241 // If priorities are the same, test quality
242 if ($a['priority'] == $b['priority']) {
243 if ($a['quality'] != $b['quality']) {
244 // Service with highest quality should come first,
245 // thus it must be marked as smaller
246 $result = $a['quality'] > $b['quality'] ? -1 : 1;
247 }
248 } else {
249 // Service with highest priority should come first,
250 // thus it must be marked as smaller
251 $result = $a['priority'] > $b['priority'] ? -1 : 1;
252 }
253 return $result;
254 }
255
256 /**
257 * Returns LanguageService
258 *
259 * @return \TYPO3\CMS\Lang\LanguageService
260 */
261 protected function getLanguageService() {
262 return $GLOBALS['LANG'];
263 }
264
265 }