Skip to content

Commit

Permalink
[TASK] fix PhpStan errors for TYPO3 13
Browse files Browse the repository at this point in the history
  • Loading branch information
dkd-kaehm committed Jul 5, 2024
1 parent 2f9b5d0 commit e865edc
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
use ApacheSolrForTypo3\Solr\IndexQueue\Item;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
Expand All @@ -39,7 +41,7 @@ public function __construct(
private readonly Item $indexQueueItem,
private readonly array $record,
private readonly TypoScriptFrontendController $tsfe,
private readonly TypoScriptConfiguration $configuration
private readonly TypoScriptConfiguration $configuration,
) {}

public function getDocument(): Document
Expand All @@ -62,14 +64,20 @@ public function getIndexingConfigurationName(): string
return $this->indexQueueItem->getIndexingConfigurationName();
}

/**
* @throws ContentRenderingException
*/
public function getSite(): Site
{
return $this->tsfe->getSite();
return clone $this->tsfe->cObj->getRequest()->getAttribute('site');
}

/**
* @throws ContentRenderingException
*/
public function getSiteLanguage(): SiteLanguage
{
return $this->tsfe->getLanguage();
return $this->getSite()->getAttribute('language');
}

public function getRecord(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
Expand All @@ -45,14 +46,20 @@ public function __construct(
$this->documents[] = $this->document;
}

/**
* @throws ContentRenderingException
*/
public function getSite(): Site
{
return $this->tsfe->getSite();
return clone $this->tsfe->cObj->getRequest()->getAttribute('site');
}

/**
* @throws ContentRenderingException
*/
public function getSiteLanguage(): SiteLanguage
{
return $this->tsfe->getLanguage();
return clone $this->getSite()->getAttribute('language');
}

public function getIndexQueueItem(): Item
Expand Down
11 changes: 9 additions & 2 deletions Classes/Event/Indexing/BeforeDocumentsAreIndexedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
Expand All @@ -39,14 +40,20 @@ public function __construct(
private readonly TypoScriptFrontendController $tsfe,
) {}

/**
* @throws ContentRenderingException
*/
public function getSite(): Site
{
return $this->tsfe->getSite();
return clone $this->tsfe->cObj->getRequest()->getAttribute('site');
}

/**
* @throws ContentRenderingException
*/
public function getSiteLanguage(): SiteLanguage
{
return $this->tsfe->getLanguage();
return clone $this->getSite()->getAttribute('language');
}

public function getIndexQueueItem(): Item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

use ApacheSolrForTypo3\Solr\IndexQueue\Item;
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
Expand All @@ -29,59 +28,6 @@
* Previously used with
* $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['Indexer']['indexPageAddDocuments']
*/
class BeforePageDocumentIsProcessedForIndexingEvent
class BeforePageDocumentIsProcessedForIndexingEvent extends BeforeDocumentIsProcessedForIndexingEvent
{
/**
* @var Document[]
*/
private array $documents = [];

public function __construct(
private readonly Document $document,
private readonly Item $indexQueueItem,
private readonly TypoScriptFrontendController $tsfe,
) {
$this->documents[] = $this->document;
}

public function getSite(): Site
{
return $this->tsfe->getSite();
}

public function getSiteLanguage(): SiteLanguage
{
return $this->tsfe->getLanguage();
}

public function getIndexQueueItem(): Item
{
return $this->indexQueueItem;
}

public function getDocument(): Document
{
return $this->document;
}

/**
* @param Document[] $documents
*/
public function addDocuments(array $documents): void
{
$this->documents = array_merge($this->documents, $documents);
}

/**
* @return Document[]
*/
public function getDocuments(): array
{
return $this->documents;
}

public function getTsfe(): TypoScriptFrontendController
{
return clone $this->tsfe;
}
}
12 changes: 8 additions & 4 deletions Classes/IndexQueue/AbstractIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
use ApacheSolrForTypo3\Solr\ContentObject\Relation;
use ApacheSolrForTypo3\Solr\FrontendEnvironment\Tsfe;
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
use ApacheSolrForTypo3\Solr\System\Util\ArrayAccessor;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\TypoScript\FrontendTypoScript;
use TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
Expand Down Expand Up @@ -120,7 +122,7 @@ protected function resolveFieldValue(
array $indexingConfiguration,
string $solrFieldName,
array $data,
TypoScriptFrontendController $tsfe
TypoScriptFrontendController $tsfe,
): mixed {
if (isset($indexingConfiguration[$solrFieldName . '.'])) {
// configuration found => need to resolve a cObj
Expand Down Expand Up @@ -156,10 +158,12 @@ protected function resolveFieldValue(
1
));

// @todo: this must be solved differently
$typoScriptParser = GeneralUtility::makeInstance(TypoScriptParser::class);
/** @var ?FrontendTypoScript $frontendTypoScript */
$frontendTypoScript = $tsfe->cObj->getRequest()->getAttribute('frontend.typoscript');
$configurationAccess = new ArrayAccessor($frontendTypoScript?->getSetupArray(), '.', true);
// $name and $conf is loaded with the referenced values.
[$name, $conf] = $typoScriptParser->getVal($referencedTsPath, $GLOBALS['TYPO3_REQUEST']->getAttribute('frontend.typoscript')?->getSetupArray());
$name = $configurationAccess->get($referencedTsPath);
$conf = $configurationAccess->get($referencedTsPath . '.');

// need to change directory to make IMAGE content objects work in BE context
// see http://blog.netzelf.de/lang/de/tipps-und-tricks/tslib_cobj-image-im-backend
Expand Down
46 changes: 40 additions & 6 deletions Classes/IndexQueue/FrontendHelper/PageIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@
use ApacheSolrForTypo3\Solr\Event\Indexing\AfterPageDocumentIsCreatedForIndexingEvent;
use ApacheSolrForTypo3\Solr\Event\Indexing\BeforeDocumentsAreIndexedEvent;
use ApacheSolrForTypo3\Solr\Event\Indexing\BeforePageDocumentIsProcessedForIndexingEvent;
use ApacheSolrForTypo3\Solr\Exception;
use ApacheSolrForTypo3\Solr\Exception as SolrException;
use ApacheSolrForTypo3\Solr\FieldProcessor\Service;
use ApacheSolrForTypo3\Solr\IndexQueue\Item;
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerRequest;
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerResponse;
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use ApacheSolrForTypo3\Solr\System\Logging\DebugWriter;
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
use ApacheSolrForTypo3\Solr\Util;
use Doctrine\DBAL\Exception as DBALException;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LogLevel;
Expand Down Expand Up @@ -200,9 +202,16 @@ public function __invoke(AfterCacheableContentIsGeneratedEvent $event): void

/**
* Index item
*
* @throws DBALException
* @throws SolrException
* @throws Throwable
*/
protected function index(Item $indexQueueItem, ServerRequestInterface $request, TypoScriptFrontendController $tsfe): void
{
protected function index(
Item $indexQueueItem,
ServerRequestInterface $request,
TypoScriptFrontendController $tsfe,
): void {
/** @var PageArguments $pageArguments */
$pageArguments = $request->getAttribute('routing');
$pageInformation = $request->getAttribute('frontend.page.information');
Expand All @@ -218,7 +227,12 @@ protected function index(Item $indexQueueItem, ServerRequestInterface $request,
$this->getAccessRootline(),
$pageInformation->getMountPoint()
);
$document = $this->substitutePageDocument($document, $pageInformation->getPageRecord(), $indexQueueItem, $tsfe);
$document = $this->substitutePageDocument(
$document,
$pageInformation->getPageRecord(),
$indexQueueItem,
$tsfe,
);

$this->responseData['pageIndexed'] = (int)$this->indexPage($document, $indexQueueItem, $tsfe);
$this->responseData['originalPageDocument'] = (array)$document;
Expand All @@ -236,14 +250,19 @@ protected function index(Item $indexQueueItem, ServerRequestInterface $request,
/**
* Gets the solr connection to use for indexing the page based on the
* Index Queue item's properties.
*
* @throws DBALException
* @throws NoSolrConnectionFoundException
* @throws SolrException
* @throws Throwable
*/
protected function getSolrConnection(Item $indexQueueItem, SiteLanguage $siteLanguage, bool $logExceptions): SolrConnection
{
$connectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
try {
$solrConnection = $connectionManager->getConnectionByRootPageId($indexQueueItem->getRootPageUid(), $siteLanguage->getLanguageId());
if (!$solrConnection->getWriteService()->ping()) {
throw new Exception(
throw new SolrException(
'Could not connect to Solr server.',
1323946472
);
Expand All @@ -269,9 +288,12 @@ protected function getSolrConnection(Item $indexQueueItem, SiteLanguage $siteLan

/**
* This method retrieves the item from the index queue, that is indexed in this request.
*
* @throws DBALException
*/
protected function getIndexQueueItem(): ?Item
{
/** @var Queue $indexQueue */
$indexQueue = GeneralUtility::makeInstance(Queue::class);
return $indexQueue->getItem($this->request->getParameter('item'));
}
Expand All @@ -289,7 +311,13 @@ protected function substitutePageDocument(
Item $indexQueueItem,
TypoScriptFrontendController $tsfe,
): Document {
$event = new AfterPageDocumentIsCreatedForIndexingEvent($pageDocument, $indexQueueItem, $pageRecord, $tsfe, $this->configuration);
$event = new AfterPageDocumentIsCreatedForIndexingEvent(
$pageDocument,
$indexQueueItem,
$pageRecord,
$tsfe,
$this->configuration,
);
$event = $this->getEventDispatcher()->dispatch($event);
return $event->getDocument();
}
Expand All @@ -309,6 +337,9 @@ protected function getPageDocument(TypoScriptFrontendController $tsfe, PageInfor
* Indexes a page.
*
* @return bool TRUE after successfully indexing the page, FALSE on error
*
* @throws DBALException
* @throws SolrException
*/
protected function indexPage(
Document $pageDocument,
Expand Down Expand Up @@ -336,6 +367,9 @@ protected function indexPage(
* care of manipulating fields as defined in the field's configuration.
*
* @param Document[] $documents An array of documents to manipulate
*
* @throws SolrException
* @throws DBALException
*/
protected function processDocuments(array $documents): void
{
Expand Down
4 changes: 2 additions & 2 deletions Classes/IndexQueue/PageIndexerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,11 @@ protected function getUrl(string $url, array $headers, float $timeout): Response
'options' => $options,
]
);
} finally {
} /* @todo: fix that properly or remove */ /*finally {
if (isset($originalBackendUser)) {
$GLOBALS['BE_USER'] = $originalBackendUser;
}
}
}*/
$response->getBody()->rewind();
return $response;
}
Expand Down
2 changes: 1 addition & 1 deletion Classes/Middleware/SolrRoutingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ protected function retrievePageInformation(UriInterface $uri, Site $site): array
sprintf(
$message,
$path,
$this->language->getTwoLetterIsoCode(),
$this->language->getLocale()->getLanguageCode(),
$uri->getPath()
)
);
Expand Down
11 changes: 2 additions & 9 deletions Classes/Report/SolrConfigurationStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,15 @@ protected function getRootPagesWithIndexingOff(): array
if ($solrIsEnabledAndIndexingDisabled) {
$rootPagesWithIndexingOff[] = $rootPage;
}
/** @phpstan-ignore-next-line */
} catch (RuntimeException) {
$rootPagesWithIndexingOff[] = $rootPage;
/** @phpstan-ignore-next-line */
} catch (ServiceUnavailableException $sue) {
} /** @noinspection PhpRedundantCatchClauseInspection */ catch (ServiceUnavailableException $sue) {
if ($sue->getCode() == 1294587218) {
// No TypoScript template found, continue with next site
$rootPagesWithIndexingOff[] = $rootPage;
continue;
}
/** @phpstan-ignore-next-line */
} catch (SiteNotFoundException $sue) {
} /** @noinspection PhpRedundantCatchClauseInspection */ catch (SiteNotFoundException $sue) {
if ($sue->getCode() == 1521716622) {
// No site found, continue with next site
$rootPagesWithIndexingOff[] = $rootPage;
Expand Down Expand Up @@ -188,8 +185,6 @@ protected function getRootPages(): array

/**
* Checks if the solr plugin is enabled with plugin.tx_solr.enabled.
*
* @throws DBALException
*/
protected function getIsSolrEnabled(int $pageUid): bool
{
Expand All @@ -198,8 +193,6 @@ protected function getIsSolrEnabled(int $pageUid): bool

/**
* Checks if the indexing is enabled with config.index_enable
*
* @throws DBALException
*/
protected function getIsIndexingEnabled(int $pageUid): bool
{
Expand Down
Loading

0 comments on commit e865edc

Please sign in to comment.