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+ /**
38+ * @var string service
39+ */
40+ protected $ service = '' ;
41+
42+ /**
43+ * @var string command
44+ */
45+ protected $ command = '' ;
46+
47+ /**
48+ * Constructor with basic checks
49+ *
50+ * @return void
51+ */
52+ public function __construct () {
53+ if (!isset ($ _SERVER ['argv ' ][1 ])) {
54+ $ this ->error ('No service defined ' );
55+ }
56+
57+ $ split = explode (': ' , $ _SERVER ['argv ' ][1 ]);
58+ if (count ($ split ) === 1 ) {
59+ $ this ->error ('CLI calls need to be like coreapi cache:clearallcaches ' );
60+ } elseif (count ($ split ) !== 2 ) {
61+ $ this ->error ('Only one : is allowed in first argument ' );
62+ }
63+
64+ $ this ->service = strtolower ($ split [0 ]);
65+ $ this ->command = strtolower ($ split [1 ]);
66+ }
67+
68+
69+ public function start () {
70+ try {
71+ switch ($ this ->service ) {
72+ case 'cache ' :
73+ $ this ->cacheApi ();
74+ break ;
75+ case 'database ' :
76+ $ this ->databaseApi ();
77+ break ;
78+ case 'extension ' :
79+ $ this ->extensionApi ();
80+ break ;
81+ case 'site ' :
82+ $ this ->siteApi ();
83+ break ;
84+ default :
85+ $ this ->error (sprintf ('Service "%s" not supported ' , $ this ->service ));
86+ }
87+ } catch (Exception $ e ) {
88+ $ errorMessage = sprintf ('ERROR: Error in service "%s" and command "%s"": %s! ' , $ this ->service , $ this ->command , $ e ->getMessage ());
89+ $ this ->outputLine ($ errorMessage );
90+ }
91+ }
92+
93+ protected function cacheApi () {
94+ $ cacheApiService = t3lib_div::makeInstance ('Tx_Coreapi_Service_CacheApiService ' );
95+ $ cacheApiService ->initializeObject ();
96+
97+ switch ($ this ->command ) {
98+ case 'clearallcaches ' :
99+ $ cacheApiService ->clearAllCaches ();
100+ $ this ->outputLine ('All caches cleared ' );
101+ break ;
102+ case 'clearconfigurationcache ' :
103+ $ cacheApiService ->clearConfigurationCache ();
104+ $ this ->outputLine ('Configuration cache cleared ' );
105+ break ;
106+ case 'clearpagecache ' :
107+ $ cacheApiService ->clearPageCache ();
108+ $ this ->outputLine ('Page cache cleared ' );
109+ break ;
110+ default :
111+ $ this ->error (sprintf ('Command "%s" not supported ' , $ this ->command ));
112+ }
113+ }
114+
115+ protected function databaseApi () {
116+ $ databaseApiService = t3lib_div::makeInstance ('Tx_Coreapi_Service_DatabaseApiService ' );
117+
118+ switch ($ this ->command ) {
119+ case 'databasecompare ' :
120+ if ($ _SERVER ['argv ' ][2 ] === 'help ' ) {
121+ $ actions = $ databaseApiService ->databaseCompareAvailableActions ();
122+ $ this ->outputTable ($ actions );
123+ } else {
124+ $ databaseApiService ->databaseCompare ($ _SERVER ['argv ' ][2 ]);
125+ }
126+ break ;
127+ default :
128+ $ this ->error (sprintf ('Command "%s" not supported ' , $ this ->command ));
129+ }
130+ }
131+
132+ /**
133+ * Implement the extensionapi service commands
134+ *
135+ * @return void
136+ */
137+ protected function extensionApi () {
138+ $ extensionApiService = t3lib_div::makeInstance ('Tx_Coreapi_Service_ExtensionApiService ' );
139+
140+ switch ($ this ->command ) {
141+ case 'info ' :
142+ // @todo: remove duplicated code
143+ $ data = $ extensionApiService ->getExtensionInformation ($ _SERVER ['argv ' ][2 ]);
144+ $ this ->outputLine ('' );
145+ $ this ->outputLine ('EXTENSION "%s": %s %s ' , array (strtoupper ($ _SERVER ['argv ' ][2 ]), $ data ['em_conf ' ]['version ' ], $ data ['em_conf ' ]['state ' ]));
146+ $ this ->outputLine (str_repeat ('- ' , self ::MAXIMUM_LINE_LENGTH ));
147+
148+ $ outputInformation = array ();
149+ $ outputInformation ['is installed ' ] = ($ data ['is_installed ' ] ? 'yes ' : 'no ' );
150+ foreach ($ data ['em_conf ' ] as $ emConfKey => $ emConfValue ) {
151+ // Skip empty properties
152+ if (empty ($ emConfValue )) {
153+ continue ;
154+ }
155+ // Skip properties which are already handled
156+ if ($ emConfKey === 'title ' || $ emConfKey === 'version ' || $ emConfKey === 'state ' ) {
157+ continue ;
158+ }
159+ $ outputInformation [$ emConfKey ] = $ emConfValue ;
160+ }
161+
162+ foreach ($ outputInformation as $ outputKey => $ outputValue ) {
163+ $ description = '' ;
164+ if (is_array ($ outputValue )) {
165+ foreach ($ outputValue as $ additionalKey => $ additionalValue ) {
166+ if (is_array ($ additionalValue )) {
167+
168+ if (empty ($ additionalValue )) {
169+ continue ;
170+ }
171+ $ description .= LF . str_repeat (' ' , 28 ) . $ additionalKey ;
172+ $ description .= LF ;
173+ foreach ($ additionalValue as $ ak => $ av ) {
174+ $ description .= str_repeat (' ' , 30 ) . $ ak . ': ' . $ av . LF ;
175+ }
176+ } else {
177+ $ description .= LF . str_repeat (' ' , 28 ) . $ additionalKey . ': ' . $ additionalValue ;
178+ }
179+ }
180+ } else {
181+ $ description = wordwrap ($ outputValue , self ::MAXIMUM_LINE_LENGTH - 28 , PHP_EOL . str_repeat (' ' , 28 ), TRUE );
182+ }
183+ $ this ->outputLine ('%-2s%-25s %s ' , array (' ' , $ outputKey , $ description ));
184+ }
185+ break ;
186+ case 'updatelist ' :
187+ $ extensionApiService ->updateMirrors ();
188+ break ;
189+ case 'listinstalled ' :
190+ $ extensions = $ extensionApiService ->getInstalledExtensions ($ _SERVER ['argv ' ][2 ]);
191+ $ out = array ();
192+
193+ foreach ($ extensions as $ key => $ details ) {
194+ $ title = $ key . ' - ' . $ details ['version ' ] . '/ ' . $ details ['state ' ];
195+ $ out [$ title ] = $ details ['title ' ];
196+ }
197+ $ this ->outputTable ($ out );
198+ break ;
199+ default :
200+ $ this ->error (sprintf ('Command "%s" not supported ' , $ this ->command ));
201+ }
202+
203+ }
204+
205+ /**
206+ * Implement the siteapi service commands
207+ *
208+ * @return void
209+ */
210+ protected function siteApi () {
211+ $ siteApiService = t3lib_div::makeInstance ('Tx_Coreapi_Service_SiteApiService ' );
212+
213+ switch ($ this ->command ) {
214+ case 'info ' :
215+ $ infos = $ siteApiService ->getSiteInfo ();
216+ $ this ->outputTable ($ infos );
217+ break ;
218+ case 'createsysnews ' :
219+ $ siteApiService ->createSysNews ($ _SERVER ['argv ' ][2 ], $ _SERVER ['argv ' ][3 ]);
220+ break ;
221+ default :
222+ $ this ->error (sprintf ('Command "%s" not supported ' , $ this ->command ));
223+ }
224+ }
225+
226+ /**
227+ * Output a single line
228+ *
229+ * @param string $text text
230+ * @param array $arguments optional arguments
231+ * @return void
232+ */
233+ protected function outputLine ($ text , array $ arguments = array ()) {
234+ if ($ arguments !== array ()) {
235+ $ text = vsprintf ($ text , $ arguments );
236+ }
237+ echo $ text . PHP_EOL ;
238+ }
239+
240+ /**
241+ * Output a whole table, maximum 2 cols
242+ *
243+ * @param array $input input table
244+ * @return void
245+ */
246+ protected function outputTable (array $ input ) {
247+ $ this ->outputLine (str_repeat ('- ' , self ::MAXIMUM_LINE_LENGTH ));
248+ foreach ($ input as $ key => $ value ) {
249+ $ line = wordwrap ($ value , self ::MAXIMUM_LINE_LENGTH - 43 , PHP_EOL . str_repeat (' ' , 43 ), TRUE );
250+ $ this ->outputLine ('%-2s%-40s %s ' , array (' ' , $ key , $ line ));
251+ }
252+ $ this ->outputLine (str_repeat ('- ' , self ::MAXIMUM_LINE_LENGTH ));
253+ }
254+
255+ /**
256+ * End call
257+ *
258+ * @param string $message Error message
259+ * @return void
260+ */
261+ protected function error ($ message ) {
262+ die ('ERROR: ' . $ message );
263+ }
264+
265+ }
266+
267+ if ((TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI ) && basename (PATH_thisScript) == 'cli_dispatch.phpsh ' ) {
268+ $ dispatcher = t3lib_div::makeInstance ('Tx_Coreapi_Cli_Dispatcher ' );
269+ $ dispatcher ->start ();
270+ } else {
271+ die ('This script must be included by the "CLI module dispatcher" ' );
272+ }
273+
274+ ?>
0 commit comments