Commit 3e48a344 authored by Christian Kuhn's avatar Christian Kuhn Committed by Oliver Bartsch
Browse files

[TASK] Use dependency injection in ext:sys_note

Configure classes in Services.yaml towards dependency injection.

Also rename NoteController to NoteRenderer as it's not really a
controller that handles requests, but just a renderer used as
dependency in events. Controllers are usually public DI services,
renderes not.

Resolves: #96528
Releases: main
Change-Id: Idfadc75763ad0a7a84fd572473affb9040575b54
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/73006

Tested-by: Benjamin Franzke's avatarBenjamin Franzke <bfr@qbus.de>
Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
Reviewed-by: Benjamin Franzke's avatarBenjamin Franzke <bfr@qbus.de>
Reviewed-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
parent 681e71a0
......@@ -17,9 +17,7 @@ declare(strict_types=1);
namespace TYPO3\CMS\SysNote\Domain\Repository;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Sys_note repository
......@@ -31,20 +29,24 @@ class SysNoteRepository
const SYS_NOTE_POSITION_BOTTOM = 0;
const SYS_NOTE_POSITION_TOP = 1;
protected ConnectionPool $connectionPool;
public function __construct(ConnectionPool $connectionPool)
{
$this->connectionPool = $connectionPool;
}
/**
* Find notes by given pids and author
* Find notes by given pid and author
*
* @param string $pids Single PID or comma separated list of PIDs
* @param int $author author uid
* @param int $pid Single pid
* @param int $author Author uid
* @param int|null $position null for no restriction, integer for defined position
* @return array
*/
public function findByPidsAndAuthorId($pids, int $author, int $position = null): array
public function findByPidAndAuthorId(int $pid, int $author, int $position = null): array
{
$pids = GeneralUtility::intExplode(',', (string)$pids);
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('sys_note');
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_note');
$queryBuilder->getRestrictions()->removeAll();
$res = $queryBuilder
->select(
......@@ -63,7 +65,7 @@ class SysNoteRepository
)
->where(
$queryBuilder->expr()->eq('sys_note.deleted', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)),
$queryBuilder->expr()->in('sys_note.pid', $queryBuilder->createNamedParameter($pids, Connection::PARAM_INT_ARRAY)),
$queryBuilder->expr()->eq('sys_note.pid', $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)),
$queryBuilder->expr()->orX(
$queryBuilder->expr()->eq('sys_note.personal', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)),
$queryBuilder->expr()->eq('sys_note.cruser', $queryBuilder->createNamedParameter($author, \PDO::PARAM_INT))
......
......@@ -18,8 +18,7 @@ declare(strict_types=1);
namespace TYPO3\CMS\SysNote\Hook;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\SysNote\Controller\NoteController;
use TYPO3\CMS\SysNote\Renderer\NoteRenderer;
/**
* Hook for the info module.
......@@ -28,6 +27,13 @@ use TYPO3\CMS\SysNote\Controller\NoteController;
*/
class InfoModuleHook
{
protected NoteRenderer $noteRenderer;
public function __construct(NoteRenderer $noteRenderer)
{
$this->noteRenderer = $noteRenderer;
}
/**
* Add sys_notes as additional content to the footer of the info module
*/
......@@ -35,9 +41,8 @@ class InfoModuleHook
{
/** @var ServerRequestInterface $request */
$request = $params['request'];
$controller = GeneralUtility::makeInstance(NoteController::class);
$id = (int)($request->getQueryParams()['id'] ?? 0);
$returnUrl = $request->getAttribute('normalizedParams')->getRequestUri();
return $controller->listAction($id, null, $returnUrl);
return $this->noteRenderer->renderList($id, null, $returnUrl);
}
}
......@@ -18,8 +18,8 @@ declare(strict_types=1);
namespace TYPO3\CMS\SysNote\Provider;
use TYPO3\CMS\Backend\Controller\Event\ModifyPageLayoutContentEvent;
use TYPO3\CMS\SysNote\Controller\NoteController;
use TYPO3\CMS\SysNote\Domain\Repository\SysNoteRepository;
use TYPO3\CMS\SysNote\Renderer\NoteRenderer;
/**
* Event listener to render notes in the page module.
......@@ -28,11 +28,11 @@ use TYPO3\CMS\SysNote\Domain\Repository\SysNoteRepository;
*/
final class PageModuleProvider
{
protected NoteController $noteController;
protected NoteRenderer $noteRenderer;
public function __construct(NoteController $noteController)
public function __construct(NoteRenderer $noteRenderer)
{
$this->noteController = $noteController;
$this->noteRenderer = $noteRenderer;
}
/**
......@@ -43,7 +43,7 @@ final class PageModuleProvider
$request = $event->getRequest();
$id = (int)($request->getQueryParams()['id'] ?? 0);
$returnUrl = $request->getAttribute('normalizedParams')->getRequestUri();
$event->addHeaderContent($this->noteController->listAction($id, SysNoteRepository::SYS_NOTE_POSITION_TOP, $returnUrl));
$event->addFooterContent($this->noteController->listAction($id, SysNoteRepository::SYS_NOTE_POSITION_BOTTOM, $returnUrl));
$event->addHeaderContent($this->noteRenderer->renderList($id, SysNoteRepository::SYS_NOTE_POSITION_TOP, $returnUrl));
$event->addFooterContent($this->noteRenderer->renderList($id, SysNoteRepository::SYS_NOTE_POSITION_BOTTOM, $returnUrl));
}
}
......@@ -18,8 +18,8 @@ declare(strict_types=1);
namespace TYPO3\CMS\SysNote\Provider;
use TYPO3\CMS\Recordlist\Event\RenderAdditionalContentToRecordListEvent;
use TYPO3\CMS\SysNote\Controller\NoteController;
use TYPO3\CMS\SysNote\Domain\Repository\SysNoteRepository;
use TYPO3\CMS\SysNote\Renderer\NoteRenderer;
/**
* Render existing notes within list module.
......@@ -28,11 +28,11 @@ use TYPO3\CMS\SysNote\Domain\Repository\SysNoteRepository;
*/
class RecordListProvider
{
protected NoteController $noteController;
protected NoteRenderer $noteRenderer;
public function __construct(NoteController $noteController)
public function __construct(NoteRenderer $noteRenderer)
{
$this->noteController = $noteController;
$this->noteRenderer = $noteRenderer;
}
public function __invoke(RenderAdditionalContentToRecordListEvent $event): void
......@@ -40,7 +40,7 @@ class RecordListProvider
$request = $event->getRequest();
$pid = (int)($event->getRequest()->getParsedBody()['id'] ?? $event->getRequest()->getQueryParams()['id'] ?? 0);
$returnUrl = $request->getAttribute('normalizedParams')->getRequestUri();
$event->addContentAbove($this->noteController->listAction($pid, SysNoteRepository::SYS_NOTE_POSITION_TOP, $returnUrl));
$event->addContentBelow($this->noteController->listAction($pid, SysNoteRepository::SYS_NOTE_POSITION_BOTTOM, $returnUrl));
$event->addContentAbove($this->noteRenderer->renderList($pid, SysNoteRepository::SYS_NOTE_POSITION_TOP, $returnUrl));
$event->addContentBelow($this->noteRenderer->renderList($pid, SysNoteRepository::SYS_NOTE_POSITION_BOTTOM, $returnUrl));
}
}
......@@ -15,7 +15,7 @@ declare(strict_types=1);
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\SysNote\Controller;
namespace TYPO3\CMS\SysNote\Renderer;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
......@@ -29,14 +29,14 @@ use TYPO3\CMS\SysNote\Domain\Repository\SysNoteRepository;
*
* @internal
*/
class NoteController
class NoteRenderer
{
protected SysNoteRepository $notesRepository;
protected array $pagePermissionCache = [];
public function __construct()
public function __construct(SysNoteRepository $sysNoteRepository)
{
$this->notesRepository = GeneralUtility::makeInstance(SysNoteRepository::class);
$this->notesRepository = $sysNoteRepository;
}
/**
......@@ -47,7 +47,7 @@ class NoteController
* @param string $returnUrl Url to return to when editing and closing a notes record again
* @return string
*/
public function listAction(int $pid, int $position = null, string $returnUrl = ''): string
public function renderList(int $pid, int $position = null, string $returnUrl = ''): string
{
$backendUser = $this->getBackendUser();
if ($pid <= 0
......@@ -57,7 +57,7 @@ class NoteController
return '';
}
$notes = $this->notesRepository->findByPidsAndAuthorId($pid, (int)$backendUser->user[$backendUser->userid_column], $position);
$notes = $this->notesRepository->findByPidAndAuthorId($pid, (int)$backendUser->user[$backendUser->userid_column], $position);
if (!$notes) {
return '';
}
......
......@@ -6,6 +6,10 @@ services:
TYPO3\CMS\SysNote\:
resource: '../Classes/*'
TYPO3\CMS\SysNote\Hook\InfoModuleHook:
public: true
TYPO3\CMS\SysNote\Provider\RecordListProvider:
tags:
- name: event.listener
......
......@@ -19,7 +19,6 @@ namespace TYPO3\CMS\SysNote\Tests\Functional\Tca;
use TYPO3\CMS\Backend\Tests\Functional\Form\FormTestService;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
class NoteVisibleFieldsTest extends FunctionalTestCase
......@@ -45,9 +44,9 @@ class NoteVisibleFieldsTest extends FunctionalTestCase
public function noteFormContainsExpectedFields(): void
{
$this->setUpBackendUserFromFixture(1);
$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageServiceFactory::class)->create('default');
$GLOBALS['LANG'] = $this->getContainer()->get(LanguageServiceFactory::class)->create('default');
$formEngineTestService = GeneralUtility::makeInstance(FormTestService::class);
$formEngineTestService = new FormTestService();
$formResult = $formEngineTestService->createNewRecordForm('sys_note');
foreach (static::$noteFields as $expectedField) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment