[FEATURE] Allow closures in CLI scripts 63/42363/3
authorBenjamin Mack <benni@typo3.org>
Fri, 7 Aug 2015 10:52:03 +0000 (12:52 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Fri, 7 Aug 2015 12:40:06 +0000 (14:40 +0200)
In order to minimize entry points for CLI scripts, the CLI key
now accepts a closure as well as a script.

Resolves: #68837
Releases: master
Change-Id: Id61f610d7b130dce7966fa94df6c296148fd1cb9
Reviewed-on: http://review.typo3.org/42363
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Susanne Moog <typo3@susannemoog.de>
Tested-by: Susanne Moog <typo3@susannemoog.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Console/CliRequestHandler.php
typo3/sysext/core/Documentation/Changelog/master/Feature-68837-ClosuresForCommandLineScripts.rst [new file with mode: 0644]
typo3/sysext/extbase/Scripts/CommandLineLauncher.php [deleted file]
typo3/sysext/extbase/ext_localconf.php
typo3/sysext/lowlevel/Resources/PHP/admin_cli.php [deleted file]
typo3/sysext/lowlevel/Resources/PHP/cleaner_cli.php [deleted file]
typo3/sysext/lowlevel/Resources/PHP/refindex_cli.php [deleted file]
typo3/sysext/lowlevel/ext_localconf.php
typo3/sysext/scheduler/Resources/PHP/scheduler_cli_dispatch.php [deleted file]
typo3/sysext/scheduler/ext_localconf.php

index c325eba..6eeee9c 100644 (file)
@@ -62,8 +62,15 @@ class CliRequestHandler implements RequestHandlerInterface {
                        list($commandLineScript, $commandLineName) = $this->getIncludeScriptByCommandLineKey($command);
                        $this->boot($commandLineName);
 
-                       // include the CLI script
-                       include($commandLineScript);
+                       if (is_callable($commandLineScript)) {
+                               if ($commandLineScript instanceof \Closure) {
+                                       $commandLineScript->bindTo($this);
+                               }
+                               call_user_func($commandLineScript);
+                       } else {
+                               // include the CLI script
+                               include($commandLineScript);
+                       }
 
                } catch (\InvalidArgumentException $e) {
                        $output->writeln('<error>Oops, an error occurred: ' . $e->getMessage() . '</error>');
@@ -138,10 +145,12 @@ class CliRequestHandler implements RequestHandlerInterface {
         */
        protected function getIncludeScriptByCommandLineKey($cliKey) {
                list($commandLineScript, $commandLineName) = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][$cliKey];
-               $commandLineScript = GeneralUtility::getFileAbsFileName($commandLineScript);
-               // Note: These constants are not in use anymore, and marked for deprecation and will be removed in TYPO3 CMS 8
-               define('TYPO3_cliKey', $cliKey);
-               define('TYPO3_cliInclude', $commandLineScript);
+               if (!is_callable($commandLineScript)) {
+                       $commandLineScript = GeneralUtility::getFileAbsFileName($commandLineScript);
+                       // Note: These constants are not in use anymore, and marked for deprecation and will be removed in TYPO3 CMS 8
+                       define('TYPO3_cliKey', $cliKey);
+                       define('TYPO3_cliInclude', $commandLineScript);
+               }
                // This is a compatibility layer: Some cli scripts rely on this, like ext:phpunit cli
                // This layer will be removed in TYPO3 CMS 8
                $GLOBALS['temp_cliScriptPath'] = array_shift($_SERVER['argv']);
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-68837-ClosuresForCommandLineScripts.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-68837-ClosuresForCommandLineScripts.rst
new file mode 100644 (file)
index 0000000..d036046
--- /dev/null
@@ -0,0 +1,21 @@
+===================================================
+Feature: #68837 - Closures for Command Line Scripts
+===================================================
+
+Description
+===========
+
+For registering new command line scripts through the CLI API ("cliKey"), it is now possible to use PHP closures
+instead of reference to PHP scripts.
+
+Example usage inside ext_localconf.php:
+
+.. code-block:: php
+
+       $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']['myclikey'] = array(
+               function() {
+                       $controller = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\Acme\MyExtension\CommandLineTool::class);
+                       $controller->main();
+               },
+               '_CLI_lowlevel'
+       );
\ No newline at end of file
diff --git a/typo3/sysext/extbase/Scripts/CommandLineLauncher.php b/typo3/sysext/extbase/Scripts/CommandLineLauncher.php
deleted file mode 100644 (file)
index d0c0aa9..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-<?php
-/*
- * This file is part of the TYPO3 CMS project.
- *
- * It is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License, either version 2
- * of the License, or any later version.
- *
- * For the full copyright and license information, please read the
- * LICENSE.txt file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
-
-/**
- * Launcher for the bootstrap when executing via cli.
- *
- * ONLY USED INTERNALLY, MIGHT CHANGE WITHOUT NOTICE!
- */
-require_once \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('extbase', 'Classes/Core/Bootstrap.php');
-$bootstrap = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Core\Bootstrap::class);
-echo $bootstrap->run('', array());
index 946a78c..4132489 100644 (file)
@@ -34,7 +34,10 @@ unset($extbaseObjectContainer);
 if (TYPO3_MODE === 'BE') {
        // registers Extbase at the cli_dispatcher with key "extbase".
        $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']['extbase'] = array(
-               'EXT:extbase/Scripts/CommandLineLauncher.php',
+               function() {
+                       $bootstrap = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Core\Bootstrap::class);
+                       echo $bootstrap->run('', array());
+               },
                '_CLI_lowlevel'
        );
        // register help command
diff --git a/typo3/sysext/lowlevel/Resources/PHP/admin_cli.php b/typo3/sysext/lowlevel/Resources/PHP/admin_cli.php
deleted file mode 100644 (file)
index 2e01900..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<?php
-if (!defined('TYPO3_cliMode')) {
-       die('You cannot run this script directly!');
-}
-
-// Call the functionality
-$adminObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Lowlevel\AdminCommand::class);
-$adminObj->cli_main($_SERVER['argv']);
diff --git a/typo3/sysext/lowlevel/Resources/PHP/cleaner_cli.php b/typo3/sysext/lowlevel/Resources/PHP/cleaner_cli.php
deleted file mode 100644 (file)
index f1bc7d3..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<?php
-if (!defined('TYPO3_cliMode')) {
-       die('You cannot run this script directly!');
-}
-
-// Call the functionality
-$cleanerObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Lowlevel\CleanerCommand::class);
-$cleanerObj->cli_main($_SERVER['argv']);
diff --git a/typo3/sysext/lowlevel/Resources/PHP/refindex_cli.php b/typo3/sysext/lowlevel/Resources/PHP/refindex_cli.php
deleted file mode 100644 (file)
index 2975046..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-<?php
-if (!defined('TYPO3_cliMode')) {
-       die('You cannot run this script directly!');
-}
-// Call the functionality
-if (in_array('-e', $_SERVER['argv']) || in_array('-c', $_SERVER['argv'])) {
-       $testOnly = in_array('-c', $_SERVER['argv']);
-       $refIndexObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ReferenceIndex::class);
-       list($headerContent, $bodyContent) = $refIndexObj->updateIndex($testOnly, !in_array('-s', $_SERVER['argv']));
-       $bodyContent = str_replace('##LF##', LF, $bodyContent);
-} else {
-       echo '
-Options:
--c = Check refindex
--e = Update refindex
--s = Silent
-';
-       die;
-}
index 4d9e0f3..7c3f5b6 100644 (file)
@@ -3,9 +3,40 @@ defined('TYPO3_MODE') or die();
 
 if (TYPO3_MODE === 'BE') {
        // Setting up scripts that can be run from the cli_dispatch.phpsh script.
-       $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']['lowlevel_refindex'] = array('EXT:lowlevel/Resources/PHP/refindex_cli.php', '_CLI_lowlevel');
-       $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']['lowlevel_cleaner'] = array('EXT:lowlevel/Resources/PHP/cleaner_cli.php', '_CLI_lowlevel');
-       $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']['lowlevel_admin'] = array('EXT:lowlevel/Resources/PHP/admin_cli.php', '_CLI_lowlevel');
+       $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']['lowlevel_refindex'] = array(
+               function() {
+                       // Call the functionality
+                       if (in_array('-e', $_SERVER['argv']) || in_array('-c', $_SERVER['argv'])) {
+                               $testOnly = in_array('-c', $_SERVER['argv']);
+                               $refIndexObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ReferenceIndex::class);
+                               list($headerContent, $bodyContent) = $refIndexObj->updateIndex($testOnly, !in_array('-s', $_SERVER['argv']));
+                               $bodyContent = str_replace('##LF##', LF, $bodyContent);
+                       } else {
+                               echo '
+                       Options:
+                       -c = Check refindex
+                       -e = Update refindex
+                       -s = Silent
+                       ';
+                               die;
+                       }
+               },
+               '_CLI_lowlevel'
+       );
+       $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']['lowlevel_cleaner'] = array(
+               function() {
+                       $cleanerObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Lowlevel\CleanerCommand::class);
+                       $cleanerObj->cli_main($_SERVER['argv']);
+               },
+               '_CLI_lowlevel'
+       );
+       $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']['lowlevel_admin'] = array(
+               function() {
+                       $adminObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Lowlevel\AdminCommand::class);
+                       $adminObj->cli_main($_SERVER['argv']);
+               },
+               '_CLI_lowlevel'
+       );
        $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lowlevel']['cleanerModules']['missing_files'] = array('TYPO3\\CMS\\Lowlevel\\MissingFilesCommand');
        $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lowlevel']['cleanerModules']['missing_relations'] = array('TYPO3\\CMS\\Lowlevel\\MissingRelationsCommand');
        $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lowlevel']['cleanerModules']['double_files'] = array('TYPO3\\CMS\\Lowlevel\\DoubleFilesCommand');
diff --git a/typo3/sysext/scheduler/Resources/PHP/scheduler_cli_dispatch.php b/typo3/sysext/scheduler/Resources/PHP/scheduler_cli_dispatch.php
deleted file mode 100644 (file)
index dfc726b..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-<?php
-/*
- * This file is part of the TYPO3 CMS project.
- *
- * It is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License, either version 2
- * of the License, or any later version.
- *
- * For the full copyright and license information, please read the
- * LICENSE.txt file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
-
-/**
- * Starts all due tasks, used by the command line interface
- * This script must be included by the "CLI module dispatcher"
- */
-
-if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI && basename(PATH_thisScript) === 'cli_dispatch.phpsh') {
-       $schedulerCliController = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Scheduler\Controller\SchedulerCliController::class);
-       $schedulerCliController->run();
-} else {
-       die('This script must be included by the "CLI module dispatcher"');
-}
index 85e291b..965eda9 100644 (file)
@@ -3,7 +3,10 @@ defined('TYPO3_MODE') or die();
 
 // Register the Scheduler as a possible key for CLI calls
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']['scheduler'] = array(
-       'EXT:scheduler/Resources/PHP/scheduler_cli_dispatch.php',
+       function() {
+               $schedulerCliController = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Scheduler\Controller\SchedulerCliController::class);
+               $schedulerCliController->run();
+       },
        '_CLI_scheduler'
 );
 // Get the extensions's configuration