[FEATURE] Run CommandController commands through Scheduler
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Scheduler / TaskExecutor.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Claus Due, Wildside A/S <claus@wildside.dk>
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 * Task Executor
27 *
28 * Takes a Tx_Extbase_Scheduler_Task and executes the CommandController command
29 * defined therein.
30 *
31 * @package Extbase
32 * @subpackage Scheduler
33 */
34 class Tx_Extbase_Scheduler_TaskExecutor implements t3lib_Singleton {
35
36 /**
37 * @var Tx_Extbase_Object_ObjectManagerInterface
38 */
39 protected $objectManager;
40
41 /**
42 * @var Tx_Extbase_MVC_CLI_CommandManager
43 */
44 protected $commandManager;
45
46 /**
47 * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
48 */
49 protected $configurationManager;
50
51 /**
52 * @param Tx_Extbase_Object_ObjectManager $objectManager
53 */
54 public function injectObjectManager(Tx_Extbase_Object_ObjectManager $objectManager) {
55 $this->objectManager = $objectManager;
56 }
57
58 /**
59 * @param Tx_Extbase_MVC_CLI_CommandManager $commandManager
60 */
61 public function injectCommandManager(Tx_Extbase_MVC_CLI_CommandManager $commandManager) {
62 $this->commandManager = $commandManager;
63 }
64
65 /**
66 * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
67 */
68 public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
69 $this->configurationManager = $configurationManager;
70 }
71
72 /**
73 * Initializes configuration manager, object container and reflection service
74 *
75 * @param array $configuration
76 * @return void
77 */
78 protected function initialize(array $configuration) {
79 // initialize configuration
80 $this->configurationManager->setContentObject(t3lib_div::makeInstance('tslib_cObj'));
81 $this->configurationManager->setConfiguration($configuration);
82
83 // configure object container
84 $typoScriptSetup = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
85 if (isset($typoScriptSetup['config.']['tx_extbase.']['objects.'])) {
86 $objectContainer = t3lib_div::makeInstance('Tx_Extbase_Object_Container_Container');
87 foreach ($typoScriptSetup['config.']['tx_extbase.']['objects.'] as $classNameWithDot => $classConfiguration) {
88 if (isset($classConfiguration['className'])) {
89 $originalClassName = rtrim($classNameWithDot, '.');
90 $objectContainer->registerImplementation($originalClassName, $classConfiguration['className']);
91 }
92 }
93 }
94
95 // initialize reflection
96 $reflectionService = $this->objectManager->get('Tx_Extbase_Reflection_Service');
97 $reflectionService->setDataCache($GLOBALS['typo3CacheManager']->getCache('extbase_reflection'));
98 if (!$reflectionService->isInitialized()) {
99 $reflectionService->initialize();
100 }
101 }
102
103 /**
104 * Execute Task
105 *
106 * If errors occur during Task execution they are thrown as Exceptions which
107 * must be caught manually if you manually execute Tasks through your code.
108 *
109 * @param Tx_Extbase_Scheduler_Task $task the task to execute
110 * @return void
111 */
112 public function execute(Tx_Extbase_Scheduler_Task $task) {
113 $commandIdentifier = $task->getCommandIdentifier();
114 list ($extensionKey, $controllerName, $commandName) = explode(':', $commandIdentifier);
115 $extensionName = t3lib_div::underscoredToUpperCamelCase($extensionKey);
116 $this->initialize(array('extensionName' => $extensionName));
117
118 $request = $this->objectManager->create('Tx_Extbase_MVC_CLI_Request');
119 $dispatcher = $this->objectManager->get('Tx_Extbase_MVC_Dispatcher');
120 $response = $this->objectManager->create('Tx_Extbase_MVC_CLI_Response');
121
122 // execute command
123 $command = $this->commandManager->getCommandByIdentifier($commandIdentifier);
124 $request->setControllerObjectName($command->getControllerClassName());
125 $request->setControllerCommandName($command->getControllerCommandName());
126 $request->setArguments($task->getArguments());
127 $dispatcher->dispatch($request, $response);
128
129 $this->shutdown();
130 }
131
132 /**
133 * Resets framework singletons
134 *
135 * @return void
136 */
137 protected function shutdown() {
138 // shutdown
139 $persistenceManager = $this->objectManager->get('Tx_Extbase_Persistence_Manager');
140 $persistenceManager->persistAll();
141 $reflectionService = $this->objectManager->get('Tx_Extbase_Reflection_Service');
142 $reflectionService->shutdown();
143 }
144
145 }
146
147 ?>