@@ -36,11 +36,11 @@ public function initialize(
3636 ExtensionConfiguration $ extensionConfiguration = null ,
3737 TypoScriptFrontendController $ typoScriptFrontendController = null ,
3838 ): void {
39- if (!$ typoScriptFrontendController ) {
39+ if (!$ typoScriptFrontendController instanceof \ TYPO3 \ CMS \ Frontend \ Controller \TypoScriptFrontendController ) {
4040 $ typoScriptFrontendController = $ this ->getTypoScriptFrontendController ();
4141 }
4242
43- if (!$ extensionConfiguration ) {
43+ if (!$ extensionConfiguration instanceof \ TYPO3 \ CMS \ Core \ Configuration \ExtensionConfiguration ) {
4444 $ extensionConfiguration = GeneralUtility::makeInstance (ExtensionConfiguration::class);
4545 }
4646
@@ -54,19 +54,17 @@ public function initialize(
5454 /**
5555 * Iterates over a structure (array, object) and replaces markers in every string found.
5656 *
57- * @param mixed $structure
5857 *
59- * @return void
6058 * @throws \Exception
6159 */
6260 public function replaceMarkersInStructureAndAdjustCaching (
6361 mixed &$ structure
6462 ): void {
65- if ($ this ->markerCollection === null ) {
63+ if (! $ this ->markerCollection instanceof \ Sinso \ Variables \ Domain \ Model \MarkerCollection ) {
6664 throw new \Exception ('Markers not initialized. Please run initialize() first. ' , 1726241619 );
6765 }
6866 $ this ->replaceMarkersInStructure ($ structure );
69- $ this ->setCacheTagsAndLifetimeInTsfe ();
67+ $ this ->setCacheTagsInTsfe ();
7068 }
7169
7270 /**
@@ -111,12 +109,18 @@ protected function replaceMarkersInText(string &$text): void
111109 }
112110
113111 // Assign a cache key associated with the marker
114- $ this ->cacheTags ->add (CacheKeyUtility::getCacheKey ($ marker ->getMarkerWithBrackets ()));
112+ $ this ->cacheTags ->add (
113+ CacheKeyUtility::getCacheKey (
114+ $ marker ->getMarkerWithBrackets ()
115+ )
116+ );
115117 $ this ->usedMarkerKeys [] = $ marker ->key ;
116118 $ text = $ newContent ;
117119 }
118120 }
119121
122+ $ this ->usedMarkerKeys = array_unique ($ this ->usedMarkerKeys );
123+
120124 // Remove all markers (avoids empty entries)
121125 if ($ this ->extensionConfiguration ->get ('variables ' , 'removeUnreplacedMarkers ' )) {
122126 $ text = preg_replace ('/{{.*?}}/ ' , '' , $ text );
@@ -132,8 +136,8 @@ protected function getMarkers(): MarkerCollection
132136 return $ page ['uid ' ];
133137 }, $ this ->typoScriptFrontendController ->rootLine );
134138
135- if (!empty ($ this -> typoScriptFrontendController -> tmpl -> setup ['plugin. ' ]['tx_variables. ' ]['persistence. ' ]['storagePid ' ])) {
136- $ pids [] = (int )$ this -> typoScriptFrontendController -> tmpl -> setup ['plugin. ' ]['tx_variables. ' ]['persistence. ' ]['storagePid ' ];
139+ if (!empty ($ GLOBALS [ ' TYPO3_REQUEST ' ]-> getAttribute ( ' frontend.typoscript ' )-> getSetupArray () ['plugin. ' ]['tx_variables. ' ]['persistence. ' ]['storagePid ' ])) {
140+ $ pids [] = (int )$ GLOBALS [ ' TYPO3_REQUEST ' ]-> getAttribute ( ' frontend.typoscript ' )-> getSetupArray () ['plugin. ' ]['tx_variables. ' ]['persistence. ' ]['storagePid ' ];
137141 }
138142
139143 $ table = 'tx_variables_marker ' ;
@@ -169,36 +173,40 @@ protected function getMarkers(): MarkerCollection
169173 return $ markers ;
170174 }
171175
172- protected function setCacheTagsAndLifetimeInTsfe (): void
176+ protected function setCacheTagsInTsfe (): void
173177 {
174- $ this ->usedMarkerKeys = array_unique ($ this ->usedMarkerKeys );
175-
176- $ minLifetime = min (
177- $ this ->getSmallestLifetimeForMarkers ($ this ->usedMarkerKeys ),
178- $ this ->typoScriptFrontendController ->page ['cache_timeout ' ] ?: PHP_INT_MAX
179- );
180-
181- $ this ->typoScriptFrontendController ->page ['cache_timeout ' ] = $ minLifetime ;
182-
183178 if (count ($ this ->cacheTags ) > 0 ) {
184179 $ this ->typoScriptFrontendController ->addCacheTags ($ this ->cacheTags ->toArray ());
185180 }
186181 }
187182
188- public function getSmallestLifetimeForMarkers ( array $ usedMarkerKeys ): int
183+ public function getLifetime ( ): int
189184 {
185+ return $ this ->getNearestTimestampForMarkers ($ this ->usedMarkerKeys ) - \TYPO3 \CMS \Core \Utility \GeneralUtility::makeInstance (\TYPO3 \CMS \Core \Context \Context::class)->getPropertyFromAspect ('date ' , 'timestamp ' );
186+ }
187+
188+ /**
189+ * Get the nearest timestamp in the future when changes for Markers should happen.
190+ * This respects starttime and endtime.
191+ * The result will be used to calculate the maximal caching duration
192+ *
193+ * @throws \Doctrine\DBAL\Exception
194+ */
195+ public function getNearestTimestampForMarkers (array $ usedMarkerKeys ): int
196+ {
197+ // Max value possible to keep an int \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController->realPageCacheContent ($timeOutTime = $GLOBALS['EXEC_TIME'] + $cacheTimeout;)
198+ $ result = PHP_INT_MAX ;
199+
190200 $ tableName = 'tx_variables_marker ' ;
191201 $ queryBuilder = GeneralUtility::makeInstance (ConnectionPool::class)->getConnectionForTable ($ tableName )->createQueryBuilder ();
192202 $ queryBuilder ->getRestrictions ()->removeAll ()
193203 ->add (GeneralUtility::makeInstance (DeletedRestriction::class));
194204
195205 // Code heavily inspired by:
196206 // \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController->getFirstTimeValueForRecord
197- $ now = (int )$ GLOBALS ['ACCESS_TIME ' ];
198- // Max value possible to keep an int \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController->realPageCacheContent ($timeOutTime = $GLOBALS['EXEC_TIME'] + $cacheTimeout;)
199- $ result = PHP_INT_MAX - $ GLOBALS ['EXEC_TIME ' ];
207+ $ now = (int )\TYPO3 \CMS \Core \Utility \GeneralUtility::makeInstance (\TYPO3 \CMS \Core \Context \Context::class)->getPropertyFromAspect ('date ' , 'timestamp ' );
200208 $ timeFields = [];
201- $ timeConditions = $ queryBuilder ->expr ()->orX ();
209+ $ timeConditions = $ queryBuilder ->expr ()->or ();
202210 foreach (['starttime ' , 'endtime ' ] as $ field ) {
203211 if (isset ($ GLOBALS ['TCA ' ][$ tableName ]['ctrl ' ]['enablecolumns ' ][$ field ])) {
204212 $ timeFields [$ field ] = $ GLOBALS ['TCA ' ][$ tableName ]['ctrl ' ]['enablecolumns ' ][$ field ];
@@ -212,7 +220,7 @@ public function getSmallestLifetimeForMarkers(array $usedMarkerKeys): int
212220 . ' THEN NULL ELSE ' . $ queryBuilder ->quoteIdentifier ($ timeFields [$ field ]) . ' END '
213221 . ') AS ' . $ queryBuilder ->quoteIdentifier ($ timeFields [$ field ])
214222 );
215- $ timeConditions ->add (
223+ $ timeConditions ->with (
216224 $ queryBuilder ->expr ()->gt (
217225 $ timeFields [$ field ],
218226 $ queryBuilder ->createNamedParameter ($ now , \PDO ::PARAM_INT )
@@ -222,7 +230,7 @@ public function getSmallestLifetimeForMarkers(array $usedMarkerKeys): int
222230 }
223231
224232 // if starttime or endtime are defined, evaluate them
225- if (! empty ( $ timeFields) ) {
233+ if ($ timeFields !== [] ) {
226234 // find the timestamp, when the current page's content changes the next time
227235 $ queryBuilder
228236 ->from ($ tableName )
@@ -235,7 +243,7 @@ public function getSmallestLifetimeForMarkers(array $usedMarkerKeys): int
235243 ->fetch ();
236244
237245 if ($ row ) {
238- foreach ($ timeFields as $ timeField => $ _ ) {
246+ foreach (array_keys ( $ timeFields) as $ timeField ) {
239247 // if a MIN value is found, take it into account for the
240248 // cache lifetime we have to filter out start/endtimes < $now,
241249 // as the SQL query also returns rows with starttime < $now
0 commit comments