Skip to content
This repository was archived by the owner on Aug 15, 2023. It is now read-only.

Commit b6f29d8

Browse files
author
Georg Ringer
committed
[FEATURE] Support custom cli dispatcher
As the CommandController is not available in the current LTS version 4.5, an own CLI dispatcher is used to call the commands. This is still work in progress to reduce code duplications
1 parent 084d43e commit b6f29d8

File tree

6 files changed

+244
-5
lines changed

6 files changed

+244
-5
lines changed

Classes/Cli/Dispatcher.php

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?php
2+
/***************************************************************
3+
* Copyright notice
4+
*
5+
* (c) 2012 Georg Ringer ([email protected])
6+
* All rights reserved
7+
*
8+
* This script is part of the TYPO3 project. The TYPO3 project is
9+
* free software; you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation; either version 2 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* The GNU General Public License can be found at
15+
* http://www.gnu.org/copyleft/gpl.html.
16+
*
17+
* This script is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU General Public License for more details.
21+
*
22+
* This copyright notice MUST APPEAR in all copies of the script!
23+
***************************************************************/
24+
25+
/**
26+
* Starts all due tasks, used by the command line interface
27+
* This script must be included by the "CLI module dispatcher"
28+
*
29+
* @author Georg Ringer <[email protected]>
30+
* @package TYPO3
31+
* @subpackage tx_coreapi
32+
*/
33+
class Tx_Coreapi_Cli_Dispatcher {
34+
35+
const MAXIMUM_LINE_LENGTH = 79;
36+
37+
protected $service = '';
38+
protected $command = '';
39+
40+
41+
public function __construct() {
42+
if (!isset($_SERVER['argv'][1])) {
43+
die('ERROR: No service defined');
44+
}
45+
$this->service = strtolower($_SERVER['argv'][1]);
46+
47+
if (!isset($_SERVER['argv'][2])) {
48+
die('ERROR: No command defined');
49+
}
50+
$this->command = strtolower($_SERVER['argv'][2]);
51+
}
52+
53+
54+
public function start() {
55+
try {
56+
switch ($this->service) {
57+
case 'databaseapi':
58+
$this->databaseApi();
59+
break;
60+
case 'extensionapi':
61+
$this->extensionApi();
62+
break;
63+
case 'siteapi':
64+
$this->siteApi();
65+
break;
66+
default:
67+
die(sprintf('ERROR: Service "%s" not supported', $this->service));
68+
}
69+
} catch (Exception $e) {
70+
$errorMessage = sprintf('ERROR: Error in service "%s" and command "%s"": %s!', $this->service, $this->command, $e->getMessage());
71+
$this->outputLine($errorMessage);
72+
}
73+
}
74+
75+
protected function databaseApi() {
76+
$databaseApiService = t3lib_div::makeInstance('Tx_Coreapi_Service_DatabaseApiService');
77+
switch ($this->command) {
78+
case 'databasecompare':
79+
// todo
80+
break;
81+
default:
82+
die(sprintf('ERROR: Command "%s" not supported', $this->command));
83+
}
84+
}
85+
86+
/**
87+
* Implement the extensionapi service commands
88+
*
89+
* @return void
90+
*/
91+
protected function extensionApi() {
92+
$extensionApiService = t3lib_div::makeInstance('Tx_Coreapi_Service_ExtensionApiService');
93+
94+
switch ($this->command) {
95+
case 'info':
96+
// @todo: remove duplicated code
97+
$data = $extensionApiService->getExtensionInformation($_SERVER['argv'][3]);
98+
$this->outputLine('');
99+
$this->outputLine('EXTENSION "%s": %s %s', array(strtoupper($_SERVER['argv'][3]), $data['em_conf']['version'], $data['em_conf']['state']));
100+
$this->outputLine(str_repeat('-', self::MAXIMUM_LINE_LENGTH));
101+
102+
$outputInformation = array();
103+
$outputInformation['is installed'] = ($data['is_installed'] ? 'yes' : 'no');
104+
foreach($data['em_conf'] as $emConfKey => $emConfValue) {
105+
// Skip empty properties
106+
if (empty($emConfValue)) {
107+
continue;
108+
}
109+
// Skip properties which are already handled
110+
if ($emConfKey === 'title' || $emConfKey === 'version' || $emConfKey === 'state') {
111+
continue;
112+
}
113+
$outputInformation[$emConfKey] = $emConfValue;
114+
}
115+
116+
foreach ($outputInformation as $outputKey => $outputValue) {
117+
$description = '';
118+
if (is_array($outputValue)) {
119+
foreach ($outputValue as $additionalKey => $additionalValue) {
120+
if (is_array($additionalValue)) {
121+
122+
if (empty($additionalValue)) {
123+
continue;
124+
}
125+
$description .= LF . str_repeat(' ', 28) . $additionalKey;
126+
$description .= LF;
127+
foreach ($additionalValue as $ak => $av) {
128+
$description .= str_repeat(' ', 30) . $ak . ': ' . $av . LF;
129+
}
130+
} else {
131+
$description .= LF . str_repeat(' ', 28) . $additionalKey . ': '. $additionalValue;
132+
}
133+
}
134+
} else {
135+
$description = wordwrap($outputValue, self::MAXIMUM_LINE_LENGTH - 28, PHP_EOL . str_repeat(' ', 28), TRUE);
136+
}
137+
$this->outputLine('%-2s%-25s %s', array(' ', $outputKey, $description));
138+
}
139+
break;
140+
case 'updatelist':
141+
$extensionApiService->updateMirrors();
142+
break;
143+
case 'listinstalled':
144+
$extensions = $extensionApiService->getInstalledExtensions($_SERVER['argv'][3]);
145+
$out = array();
146+
147+
foreach($extensions as $key => $details) {
148+
$title = $key . ' - ' . $details['version'] . '/' . $details['state'];
149+
$out[$title] = $details['title'];
150+
}
151+
$this->outputTable($out);
152+
break;
153+
default:
154+
die(sprintf('ERROR: Command "%s" not supported', $this->command));
155+
}
156+
157+
}
158+
159+
/**
160+
* Implement the siteapi service commands
161+
*
162+
* @return void
163+
*/
164+
protected function siteApi() {
165+
$siteApiService = t3lib_div::makeInstance('Tx_Coreapi_Service_SiteApiService');
166+
167+
switch ($this->command) {
168+
case 'info':
169+
$infos = $siteApiService->getSiteInfo();
170+
$this->outputTable($infos);
171+
break;
172+
case 'createsysnews':
173+
$siteApiService->createSysNews($_SERVER['argv'][3], $_SERVER['argv'][4]);
174+
break;
175+
default:
176+
die(sprintf('ERROR: Command "%s" not supported', $this->command));
177+
}
178+
}
179+
180+
/**
181+
* Output a single line
182+
*
183+
* @param string $text text
184+
* @param array $arguments optional arguments
185+
* @return void
186+
*/
187+
protected function outputLine($text, array $arguments = array()) {
188+
if ($arguments !== array()) {
189+
$text = vsprintf($text, $arguments);
190+
}
191+
echo $text . PHP_EOL;
192+
}
193+
194+
/**
195+
* Output a whole table, maximum 2 cols
196+
*
197+
* @param array $input input table
198+
* @return void
199+
*/
200+
protected function outputTable(array $input) {
201+
$this->outputLine(str_repeat('-', self::MAXIMUM_LINE_LENGTH));
202+
foreach($input as $key => $value) {
203+
$line = wordwrap($value, self::MAXIMUM_LINE_LENGTH - 43, PHP_EOL . str_repeat(' ', 43), TRUE);
204+
$this->outputLine('%-2s%-40s %s', array(' ', $key, $line));
205+
}
206+
$this->outputLine(str_repeat('-', self::MAXIMUM_LINE_LENGTH));
207+
}
208+
}
209+
210+
211+
if ((TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) && basename(PATH_thisScript) == 'cli_dispatch.phpsh') {
212+
$dispatcher = t3lib_div::makeInstance('Tx_Coreapi_Cli_Dispatcher');
213+
$dispatcher->start();
214+
} else {
215+
die('This script must be included by the "CLI module dispatcher"');
216+
}
217+
218+
?>

Classes/Service/ExtensionApiService.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ class Tx_Coreapi_Service_ExtensionApiService {
4747
* @return array
4848
*/
4949
public function getExtensionInformation($key) {
50+
if (strlen($key) === 0) {
51+
throw new InvalidArgumentException('No extension key given!');
52+
}
5053
if (!$GLOBALS['TYPO3_LOADED_EXT'][$key]) {
51-
throw new Exception(sprintf('Extension "%s" not found.', $key));
54+
throw new InvalidArgumentException(sprintf('Extension "%s" not found!', $key));
5255
}
5356

5457
include_once(t3lib_extMgm::extPath($key) . 'ext_emconf.php');
@@ -61,6 +64,11 @@ public function getExtensionInformation($key) {
6164
}
6265

6366
public function getInstalledExtensions($type = '') {
67+
$type = strtoupper($type);
68+
if (!empty($type) && $type !== 'L' && $type !== 'G' && $type !== 'S') {
69+
throw new InvalidArgumentException('Only "L", "S" and "G" are supported as type (or nothing)');
70+
}
71+
6472
$extensions = $GLOBALS['TYPO3_LOADED_EXT'];
6573

6674
$list = array();

Classes/Service/SiteApiService.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public function getSiteInfo() {
5555
* @return void
5656
*/
5757
public function createSysNews($header, $text) {
58+
if (strlen($header) === 0) {
59+
throw new InvalidArgumentException('No header given');
60+
}
5861
$GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_news', array(
5962
'title' => $header,
6063
'content' => $text,

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Checkout the project website at forge.typo3.org:
1515
* clearConfigurationCache
1616

1717
### CLI call: ###
18+
If you are using TYPO3 4.7+, you can use the awesome CommandController of Extbase
19+
1820
This will show you all available calls
1921
./typo3/cli_dispatch.phpsh extbase help
2022

23+
If you are using 4.5 or 4.6, you can still use the extension with a call like
24+
./typo3/cli_dispatch.phpsh coreapi extensionapi info coreapi

ext_emconf.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
'title' => 'coreapi',
55
'description' => 'coreapi',
66
'category' => 'plugin',
7-
'author' => 'Tobias Liebig',
8-
'author_email' => '[email protected]',
7+
'author' => 'Tobias Liebig,Georg Ringer',
8+
99
'author_company' => '',
1010
'shy' => '',
1111
'priority' => '',
1212
'module' => '',
13-
'state' => 'alpha',
13+
'state' => 'beta',
1414
'internal' => '',
15-
'uploadfolder' => '1',
15+
'uploadfolder' => '0',
1616
'createDirs' => '',
1717
'modify_tables' => '',
1818
'clearCacheOnLoad' => 0,

ext_localconf.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@
99
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = 'Tx_Coreapi_Command_ExtensionApiCommandController';
1010
}
1111

12+
13+
// Register the CLI dispatcher
14+
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][$_EXTKEY] = array(
15+
'EXT:' . $_EXTKEY . '/Classes/Cli/Dispatcher.php', '_CLI_lowlevel'
16+
);
17+
1218
?>

0 commit comments

Comments
 (0)