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+ ?>
0 commit comments