Seit
TYPO3 4.1 gibt es ein neues Command Line Interface!
Leider ist dieses nirgendwo dokumentiert.
Vorab, ich habe eine Beispiel Extension geschrieben die Ihr hier finden könnt:
http://typo3.org/extensions/repository/view/cli_example/1.0.0/
Und los gehts ...
Schritt 1:
Legt einen neuen Backend User an.
Er sollte mit _cli_ beginnen also zB _cli_user, das Passwort ist egal da das CLI sich nicht ins Backend einloggt.
Schritt 2:
ext_localconf.php bearbeiten bzw anlegen und folgenden Inhalt rein kopieren
| 0: |
<?php if (TYPO3_MODE=='BE') {
// Setting up scripts that can be run from the cli_dispatch.phpsh script.
$TYPO3_CONF_VARS['SC_OPTIONS']['GLOBAL']['cliKeys'][$_EXTKEY] = array('EXT:'.$_EXTKEY.'/cli/class.cli_example.php','_CLI_user');
} ?>
|
| 1: |
| 2: |
| 3: |
| 4: |
| 5: |
|
| Hier koennen Sie sich den Quellcode kopieren.
|
Schritt 3:
Einen neuen Ordner cli in euerer Extension anlegen
Eine neue Datei class.cli_example.php anlegen. Wenn Ihr der Datei einen anderen Namen gebt müsst Ihr den Namen auch in der ext_localconf.php anpassen.
Schritt 4:
Öffnet die neu angelegte Datei class.cli_example.php und kopiert folgendes Grundgerüst hinein
| 0: |
<?php
/***************************************************************
* Copyright notice
*
* (c) 2008 Julian Kleinhans (jk@web-factory.de)
* All rights reserved
*
* [...]
*
*/
if (!defined('TYPO3_cliMode')) die('You cannot run this script directly!');
// Include basis cli class
require_once(PATH_t3lib.'class.t3lib_cli.php');
class tx_cliexample_cli extends t3lib_cli {
/**
* Constructor
*/
function tx_mfcarticletocontent_cli () {
// Running parent class constructor
parent::t3lib_cli();
// Setting help texts:
$this->cli_help['name'] = 'Name of script';
$this->cli_help['synopsis'] = '###OPTIONS###';
$this->cli_help['description'] = 'Class with basic functionality for CLI scripts';
$this->cli_help['examples'] = '/.../cli_dispatch.phpsh EXTKEY TASK';
$this->cli_help['author'] = 'Julian Kleinhans, (c) 2008';
}
/**
* CLI engine
*
* @param array Command line arguments
* @return string
*/
function cli_main($argv) {
// get task (function)
$task = (string)$this->cli_args['_DEFAULT'][1];
if (!$task){
$this->cli_validateArgs();
$this->cli_help();
exit;
}
if ($task == 'myFunction') {
$this->myFunction();
}
/**
* Or other tasks
* Which task shoud be called can you define in the shell command
* /www/typo3/cli_dispatch.phpsh cli_example otherTask
*/
if ($task == 'otherTask') {
// ...
}
}
/**
* myFunction which is called over cli
*
*/
function myFunction(){
// Output
$this->cli_echo('Whats your name:');
// Input
$input = $this->cli_keyboardInput();
$this->cli_echo('Hi '.$input.', your CLI script works :)');
// Input yes/no
$input = $this->cli_keyboardInput_yes('You want money?');
if($b){
$this->cli_echo('nHaha.. go working! :)');
}else{
$this->cli_echo('nOh ok.. are you ill?');
}
}
}
// Call the functionality
$cleanerObj = t3lib_div::makeInstance('tx_cliexample_cli');
$cleanerObj->cli_main($_SERVER['argv']);
?>
|
| 1: |
| 2: |
| 3: |
| 4: |
| 5: |
| 6: |
| 7: |
| 8: |
| 9: |
| 10: |
| 11: |
| 12: |
| 13: |
| 14: |
| 15: |
| 16: |
| 17: |
| 18: |
| 19: |
| 20: |
| 21: |
| 22: |
| 23: |
| 24: |
| 25: |
| 26: |
| 27: |
| 28: |
| 29: |
| 30: |
| 31: |
| 32: |
| 33: |
| 34: |
| 35: |
| 36: |
| 37: |
| 38: |
| 39: |
| 40: |
| 41: |
| 42: |
| 43: |
| 44: |
| 45: |
| 46: |
| 47: |
| 48: |
| 49: |
| 50: |
| 51: |
| 52: |
| 53: |
| 54: |
| 55: |
| 56: |
| 57: |
| 58: |
| 59: |
| 60: |
| 61: |
| 62: |
| 63: |
| 64: |
| 65: |
| 66: |
| 67: |
| 68: |
| 69: |
| 70: |
| 71: |
| 72: |
| 73: |
| 74: |
| 75: |
| 76: |
| 77: |
| 78: |
| 79: |
| 80: |
| 81: |
| 82: |
| 83: |
| 84: |
| 85: |
| 86: |
| 87: |
| 88: |
| 89: |
| 90: |
| 91: |
| 92: |
| 93: |
|
| Hier koennen Sie sich den Quellcode kopieren.
|
Ich denke der Quellcode ist soweit selbsterklärend, wenn nein stellt hier euere Fragen ;)
Aufgerufen wird das ganze dann so
PFAD/www/typo3/cli_dispatch.phpsh EXTKEY TASK
|
Beispiel:
/server/www/typo3/cli_dispatch.phpsh cli_example myFunction
|
Somit wird die Funktion myFunction aufgerufen!
Ihr könnt euer Script somit mit x weiteren Tasks/funktionen erweitern und diese dann per Shellcommando ausführen lassen..
Vor
TYPO3 Version 4.1 musste man ein CLI Script anders bauen. Dazu schaut euch folgendes Tutorial an:
http://www.typo3-tutorials.org/tutorials/entwicklung/cli-shell-scripte.html