TYPO3 / Surf

Easy and powerful PHP deployment tool
https://docs.typo3.org/other/typo3/surf/master/en-us/
GNU General Public License v3.0
98 stars 53 forks source link

How to inject Logger in custom tasks? #777

Closed jonaseberle closed 1 year ago

jonaseberle commented 1 year ago

Expected Behavior

When extending TYPO3\Surf\Domain\Model\Task (abstract class Task implements LoggerAwareInterface {...}), $this->logger is injected and can be used.

Actual Behavior

For custom tasks, the

    $services->set(LoggerInterface::class)
        ->factory([service(LoggerFactory::class), 'createLogger']);

is not enabled thus the Logger is not injected.

I have patched Surf's services.php (in my case my custom tasks are in ./src on the same level as composer.json)

diff --git a/Resources/services.php b/Resources/services.php
--- a/Resources/services.php
+++ b/Resources/services.php
@@ -60,6 +60,8 @@
     $services->set(OutputInterface::class)
         ->factory([service(ConsoleOutputFactory::class), 'createOutput']);

+    $services->load('Custom\\', __DIR__ . '/../../../../src');
+
     $services->load('TYPO3\Surf\\', __DIR__ . '/../src')
         ->exclude([__DIR__ . '/../src/{Cli,Application,Exception,Domain/Model,Domain/Enum,DeprecationMessageFactory.php,Exception.php,functions.php}']);

I don't know how this could be solved for all custom tasks though in a way that does not rely on the specific path.

Steps to Reproduce the Problem

  1. Create a custom task that uses $this->logger->...
  2. Add it to a workflow

Specifications

Reference: https://docs.typo3.org/other/typo3/surf/main/en-us/Tasks/Index.html

sabbelasichon commented 1 year ago

@jonaseberle Good point. I have implemented the common LoggerAwareInterface check for custom task. If the task implements the interface the logger gets injected. This should do the trick, right?

jonaseberle commented 1 year ago

https://github.com/TYPO3/Surf/commit/5afa1ed6c8e7796807c843352808a60052af99bf looks good, thank you.