[TASK] Deprecate Extbase CommandControllers and @cli annotation
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Scheduler / TaskExecutor.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Scheduler;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Task Executor
19 *
20 * Takes a \TYPO3\CMS\Extbase\Scheduler\Task and executes the CommandController command
21 * defined therein.
22 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.0. Use symfony/console commands instead.
23 */
24 class TaskExecutor implements \TYPO3\CMS\Core\SingletonInterface
25 {
26 /**
27 * @var \TYPO3\CMS\Extbase\Mvc\Cli\Request
28 */
29 protected $request;
30
31 /**
32 * @var \TYPO3\CMS\Extbase\Mvc\Cli\Response
33 */
34 protected $response;
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Mvc\Dispatcher
38 */
39 protected $dispatcher;
40
41 /**
42 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
43 */
44 protected $objectManager;
45
46 /**
47 * @var \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager
48 */
49 protected $commandManager;
50
51 /**
52 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
53 */
54 protected $configurationManager;
55
56 /**
57 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
58 */
59 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
60 {
61 $this->objectManager = $objectManager;
62 }
63
64 /**
65 * @param \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager $commandManager
66 */
67 public function injectCommandManager(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager $commandManager)
68 {
69 $this->commandManager = $commandManager;
70 }
71
72 /**
73 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
74 */
75 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
76 {
77 $this->configurationManager = $configurationManager;
78 }
79
80 /**
81 * Initialize Dispatcher
82 */
83 public function initializeObject()
84 {
85 $this->dispatcher = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Dispatcher::class);
86 }
87
88 /**
89 * Initializes configuration manager, object container and reflection service
90 *
91 * @param array $configuration
92 */
93 protected function initialize(array $configuration)
94 {
95 // initialize unconsumed Request and Response
96 $this->request = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\Request::class);
97 $this->response = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\Response::class);
98 // initialize configuration
99 $this->configurationManager->setContentObject(\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class));
100 $this->configurationManager->setConfiguration($configuration);
101 // configure object container
102 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
103 if (isset($frameworkConfiguration['objects'])) {
104 $objectContainer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\Container\Container::class);
105 foreach ($frameworkConfiguration['objects'] as $classNameWithDot => $classConfiguration) {
106 if (isset($classConfiguration['className'])) {
107 $originalClassName = rtrim($classNameWithDot, '.');
108 $objectContainer->registerImplementation($originalClassName, $classConfiguration['className']);
109 }
110 }
111 }
112 }
113
114 /**
115 * Execute Task
116 *
117 * If errors occur during Task execution they are thrown as Exceptions which
118 * must be caught manually if you manually execute Tasks through your code.
119 *
120 * @param \TYPO3\CMS\Extbase\Scheduler\Task $task the task to execute
121 */
122 public function execute(\TYPO3\CMS\Extbase\Scheduler\Task $task)
123 {
124 $commandIdentifier = $task->getCommandIdentifier();
125 list($extensionKey, $controllerName, $commandName) = explode(':', $commandIdentifier);
126 $extensionName = \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($extensionKey);
127 $this->initialize(['extensionName' => $extensionName]);
128 // execute command
129 $command = $this->commandManager->getCommandByIdentifier($commandIdentifier);
130 $this->request->setControllerObjectName($command->getControllerClassName());
131 $this->request->setControllerCommandName($command->getControllerCommandName());
132 $this->request->setArguments($task->getArguments());
133 $this->dispatcher->dispatch($this->request, $this->response);
134 $this->shutdown();
135 }
136
137 /**
138 * Resets framework singletons
139 */
140 protected function shutdown()
141 {
142 $persistenceManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
143 $persistenceManager->persistAll();
144 }
145 }