Completed
Push — master ( e7445d...6e98ac )
by Timo
307:07 queued 304:17
created

Util::getConfigurationFromExistingTSFE()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 43
ccs 30
cts 30
cp 1
rs 8.8337
c 0
b 0
f 0
cc 6
nc 24
nop 3
crap 6
1
<?php
2
namespace ApacheSolrForTypo3\Solr;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
28
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache;
29
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager;
30
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationPageResolver;
31
use ApacheSolrForTypo3\Solr\System\Configuration\ExtensionConfiguration;
32
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
33
use ApacheSolrForTypo3\Solr\System\TCA\TCAService;
34
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
35
use ApacheSolrForTypo3\Solr\System\Mvc\Frontend\Controller\OverriddenTypoScriptFrontendController;
36
use TYPO3\CMS\Backend\Utility\BackendUtility;
37
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
38
use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
39
use TYPO3\CMS\Core\Utility\GeneralUtility;
40
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
41
use TYPO3\CMS\Frontend\Page\PageRepository;
42
43
/**
44
 * Utility class for tx_solr
45
 *
46
 * @author Ingo Renner <[email protected]>
47
 */
48
class Util
49
{
50
51
    /**
52
     * Generates a document id for documents representing page records.
53
     *
54
     * @param int $uid The page's uid
55
     * @param int $typeNum The page's typeNum
56
     * @param int $language the language id, defaults to 0
57
     * @param string $accessGroups comma separated list of uids of groups that have access to that page
58
     * @param string $mountPointParameter The mount point parameter that is used to access the page.
59
     * @return string The document id for that page
60
     */
61 68
    public static function getPageDocumentId($uid, $typeNum = 0, $language = 0, $accessGroups = '0,-1', $mountPointParameter = '')
62
    {
63 68
        $additionalParameters = $typeNum . '/' . $language . '/' . $accessGroups;
64
65 68
        if ((string)$mountPointParameter !== '') {
66 2
            $additionalParameters = $mountPointParameter . '/' . $additionalParameters;
67
        }
68
69 68
        $documentId = self::getDocumentId('pages', $uid, $uid, $additionalParameters);
70
71 68
        return $documentId;
72
    }
73
74
    /**
75
     * Generates a document id in the form $siteHash/$type/$uid.
76
     *
77
     * @param string $table The records table name
78
     * @param int $rootPageId The record's site root id
79
     * @param int $uid The record's uid
80
     * @param string $additionalIdParameters Additional ID parameters
81
     * @return string A document id
82
     */
83 89
    public static function getDocumentId($table, $rootPageId, $uid, $additionalIdParameters = '')
84
    {
85 89
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
86 89
        $site = $siteRepository->getSiteByPageId($rootPageId);
87 89
        $siteHash = $site->getSiteHash();
88
89 89
        $documentId = $siteHash . '/' . $table . '/' . $uid;
90 89
        if (!empty($additionalIdParameters)) {
91 68
            $documentId .= '/' . $additionalIdParameters;
92
        }
93
94 89
        return $documentId;
95
    }
96
97
    /**
98
     * Shortcut to retrieve the TypoScript configuration for EXT:solr
99
     * (plugin.tx_solr) from TSFE.
100
     *
101
     * @return TypoScriptConfiguration
102
     */
103 203
    public static function getSolrConfiguration()
104
    {
105 203
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
106 203
        return $configurationManager->getTypoScriptConfiguration();
107
    }
108
109
    /**
110
     * Gets the Solr configuration for a specific root page id.
111
     * To be used from the backend.
112
     *
113
     * @param int $pageId Id of the (root) page to get the Solr configuration from.
114
     * @param bool $initializeTsfe Optionally initializes a full TSFE to get the configuration, defaults to FALSE
115
     * @param int $language System language uid, optional, defaults to 0
116
     * @return TypoScriptConfiguration The Solr configuration for the requested tree.
117
     */
118 166
    public static function getSolrConfigurationFromPageId($pageId, $initializeTsfe = false, $language = 0)
119
    {
120 166
        $rootPath = '';
121 166
        return self::getConfigurationFromPageId($pageId, $rootPath, $initializeTsfe, $language);
122
    }
123
124
    /**
125
     * Loads the TypoScript configuration for a given page id and language.
126
     * Language usage may be disabled to get the default TypoScript
127
     * configuration.
128
     *
129
     * @param int $pageId Id of the (root) page to get the Solr configuration from.
130
     * @param string $path The TypoScript configuration path to retrieve.
131
     * @param bool $initializeTsfe Optionally initializes a full TSFE to get the configuration, defaults to FALSE
132
     * @param int $language System language uid, optional, defaults to 0
133
     * @param bool $useTwoLevelCache Flag to enable the two level cache for the typoscript configuration array
134
     * @return TypoScriptConfiguration The Solr configuration for the requested tree.
135
     */
136 170
    public static function getConfigurationFromPageId($pageId, $path, $initializeTsfe = false, $language = 0, $useTwoLevelCache = true)
137
    {
138 170
        $pageId = self::getConfigurationPageIdToUse($pageId);
139
140 170
        static $configurationObjectCache = [];
141 170
        $cacheId = md5($pageId . '|' . $path . '|' . $language . '|' . ($initializeTsfe ? '1' : '0'));
142 170
        if (isset($configurationObjectCache[$cacheId])) {
143 75
            if ($initializeTsfe) {
144 5
                self::initializeTsfe($pageId, $language);
145
            }
146 75
            return $configurationObjectCache[$cacheId];
147
        }
148
149
        // If we're on UID 0, we cannot retrieve a configuration currently.
150
        // getRootline() below throws an exception (since #typo3-60 )
151
        // as UID 0 cannot have any parent rootline by design.
152 170
        if ($pageId == 0) {
153 3
            return $configurationObjectCache[$cacheId] = self::buildTypoScriptConfigurationFromArray([], $pageId, $language, $path);
154
        }
155
156 168
        if ($useTwoLevelCache) {
157
            /** @var $cache TwoLevelCache */
158 168
            $cache = GeneralUtility::makeInstance(TwoLevelCache::class, /** @scrutinizer ignore-type */ 'tx_solr_configuration');
159 168
            $configurationArray = $cache->get($cacheId);
160
        }
161
162 168
        if (!empty($configurationArray)) {
163
            // we have a cache hit and can return it.
164
            if ($initializeTsfe) {
165
                self::initializeTsfe($pageId, $language);
166
            }
167
            return $configurationObjectCache[$cacheId] = self::buildTypoScriptConfigurationFromArray($configurationArray, $pageId, $language, $path);
168
        }
169
170
        // we have nothing in the cache. We need to build the configurationToUse
171 168
        $configurationArray = self::buildConfigurationArray($pageId, $path, $initializeTsfe, $language);
172
173 168
        if ($useTwoLevelCache && isset($cache)) {
174 168
            $cache->set($cacheId, $configurationArray);
175
        }
176
177 168
        return $configurationObjectCache[$cacheId] = self::buildTypoScriptConfigurationFromArray($configurationArray, $pageId, $language, $path);
178
    }
179
180
    /**
181
     * This method retrieves the closest pageId where a configuration is located, when this
182
     * feature is enabled.
183
     *
184
     * @param int $pageId
185
     * @return int
186
     */
187 170
    protected static function getConfigurationPageIdToUse($pageId)
188
    {
189 170
        $extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class);
190 170
        if ($extensionConfiguration->getIsUseConfigurationFromClosestTemplateEnabled()) {
191
            /** @var $configurationPageResolve ConfigurationPageResolver */
192
            $configurationPageResolver = GeneralUtility::makeInstance(ConfigurationPageResolver::class);
193
            $pageId = $configurationPageResolver->getClosestPageIdWithActiveTemplate($pageId);
194
            return $pageId;
195
        }
196 170
        return $pageId;
197
    }
198
199
    /**
200
     * Initializes a TSFE, if required and builds an configuration array, containing the solr configuration.
201
     *
202
     * @param integer $pageId
203
     * @param string $path
204
     * @param boolean $initializeTsfe
205
     * @param integer $language
206
     * @return array
207
     */
208 168
    protected static function buildConfigurationArray($pageId, $path, $initializeTsfe, $language)
209
    {
210 168
        if ($initializeTsfe) {
211 22
            self::initializeTsfe($pageId, $language);
212 22
            $configurationToUse = self::getConfigurationFromInitializedTSFE($path);
213
        } else {
214 166
            $configurationToUse = self::getConfigurationFromExistingTSFE($pageId, $path, $language);
215
        }
216
217 168
        return is_array($configurationToUse) ? $configurationToUse : [];
218
    }
219
220
    /**
221
     * Builds the configuration object from a config array and returns it.
222
     *
223
     * @param array $configurationToUse
224
     * @param int $pageId
225
     * @param int $languageId
226
     * @param string $typoScriptPath
227
     * @return TypoScriptConfiguration
228
     */
229 170
    protected static function buildTypoScriptConfigurationFromArray(array $configurationToUse, $pageId, $languageId, $typoScriptPath)
230
    {
231 170
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
232 170
        return $configurationManager->getTypoScriptConfiguration($configurationToUse, $pageId, $languageId, $typoScriptPath);
233
    }
234
235
    /**
236
     * This function is used to retrieve the configuration from a previous initialized TSFE
237
     * (see: getConfigurationFromPageId)
238
     *
239
     * @param string $path
240
     * @return mixed
241
     */
242 22
    private static function getConfigurationFromInitializedTSFE($path)
243
    {
244
        /** @var $tmpl ExtendedTemplateService */
245 22
        $tmpl = GeneralUtility::makeInstance(ExtendedTemplateService::class);
246 22
        $configuration = $tmpl->ext_getSetup($GLOBALS['TSFE']->tmpl->setup, $path);
247 22
        $configurationToUse = $configuration[0];
248 22
        return $configurationToUse;
249
    }
250
251
    /**
252
     * This function is used to retrieve the configuration from an existing TSFE instance
253
     *
254
     * @param $pageId
255
     * @param $path
256
     * @param $language
257
     * @return mixed
258
     */
259 166
    private static function getConfigurationFromExistingTSFE($pageId, $path, $language)
260
    {
261 166
        if (is_int($language)) {
262 166
            GeneralUtility::_GETset($language, 'L');
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Core\Utility\GeneralUtility::_GETset() has been deprecated: since TYPO3 v9 LTS, will be removed in TYPO3 v10.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

262
            /** @scrutinizer ignore-deprecated */ GeneralUtility::_GETset($language, 'L');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
263
        }
264
265
            /** @var $pageSelect PageRepository */
266 166
        $pageSelect = GeneralUtility::makeInstance(PageRepository::class);
267 166
        $rootLine = $pageSelect->getRootLine($pageId);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Frontend\Page\...pository::getRootLine() has been deprecated: since TYPO3 v9, will be removed in TYPO3 v10.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

267
        $rootLine = /** @scrutinizer ignore-deprecated */ $pageSelect->getRootLine($pageId);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
268
269 166
        $initializedTsfe = false;
270 166
        $initializedPageSelect = false;
271 166
        if (empty($GLOBALS['TSFE']->sys_page)) {
272 91
            if (empty($GLOBALS['TSFE'])) {
273 91
                $GLOBALS['TSFE'] = new \stdClass();
274 91
                $GLOBALS['TSFE']->tmpl = new \stdClass();
275 91
                $GLOBALS['TSFE']->tmpl->rootLine = $rootLine;
276 91
                $GLOBALS['TSFE']->sys_page = $pageSelect;
277 91
                $GLOBALS['TSFE']->id = $pageId;
278 91
                $GLOBALS['TSFE']->tx_solr_initTsfe = 1;
279 91
                $initializedTsfe = true;
280
            }
281
282 91
            $GLOBALS['TSFE']->sys_page = $pageSelect;
283 91
            $initializedPageSelect = true;
284
        }
285
            /** @var $tmpl ExtendedTemplateService */
286 166
        $tmpl = GeneralUtility::makeInstance(ExtendedTemplateService::class);
287 166
        $tmpl->tt_track = false; // Do not log time-performance information
288 166
        $tmpl->init();
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Core\TypoScript\TemplateService::init() has been deprecated: since TYPO3 v9, will be removed in TYPO3 v10.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

288
        /** @scrutinizer ignore-deprecated */ $tmpl->init();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
289 166
        $tmpl->runThroughTemplates($rootLine); // This generates the constants/config + hierarchy info for the template.
290 166
        $tmpl->generateConfig();
291
292 166
        $getConfigurationFromInitializedTSFEAndWriteToCache = $tmpl->ext_getSetup($tmpl->setup, $path);
293 166
        $configurationToUse = $getConfigurationFromInitializedTSFEAndWriteToCache[0];
294
295 166
        if ($initializedPageSelect) {
296 91
            $GLOBALS['TSFE']->sys_page = null;
297
        }
298 166
        if ($initializedTsfe) {
299 91
            unset($GLOBALS['TSFE']);
300
        }
301 166
        return $configurationToUse;
302
    }
303
304
    /**
305
     * Initializes the TSFE for a given page ID and language.
306
     *
307
     * @param int $pageId The page id to initialize the TSFE for
308
     * @param int $language System language uid, optional, defaults to 0
309
     * @param bool $useCache Use cache to reuse TSFE
310
     * @todo When we drop TYPO3 8 support we should use a middleware stack to initialize a TSFE for our needs
311
     * @return void
312
     */
313 27
    public static function initializeTsfe($pageId, $language = 0, $useCache = true)
314
    {
315 27
        static $tsfeCache = [];
316
317
        // resetting, a TSFE instance with data from a different page Id could be set already
318 27
        unset($GLOBALS['TSFE']);
319
320 27
        $cacheId = $pageId . '|' . $language;
321
322 27
        if (!is_object($GLOBALS['TT'])) {
323 26
            $GLOBALS['TT'] = GeneralUtility::makeInstance(TimeTracker::class, false);
324
        }
325
326 27
        if (!isset($tsfeCache[$cacheId]) || !$useCache) {
327 27
            GeneralUtility::_GETset($language, 'L');
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Core\Utility\GeneralUtility::_GETset() has been deprecated: since TYPO3 v9 LTS, will be removed in TYPO3 v10.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

327
            /** @scrutinizer ignore-deprecated */ GeneralUtility::_GETset($language, 'L');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
328
329
330 27
            $GLOBALS['TSFE'] = GeneralUtility::makeInstance(OverriddenTypoScriptFrontendController::class, $GLOBALS['TYPO3_CONF_VARS'], $pageId, 0);
331
332
            // for certain situations we need to trick TSFE into granting us
333
            // access to the page in any case to make getPageAndRootline() work
334
            // see http://forge.typo3.org/issues/42122
335 27
            $pageRecord = BackendUtility::getRecord('pages', $pageId, 'fe_group');
336 27
            $groupListBackup = $GLOBALS['TSFE']->gr_list;
337 27
            $GLOBALS['TSFE']->gr_list = $pageRecord['fe_group'];
338
339 27
            $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance(PageRepository::class);
340 27
            self::getPageAndRootlineOfTSFE($pageId);
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...PageAndRootlineOfTSFE() has been deprecated: This is only implemented to provide compatibility for TYPO3 8 and 9 when we drop TYPO3 8 support this should changed to use a middleware stack ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

340
            /** @scrutinizer ignore-deprecated */ self::getPageAndRootlineOfTSFE($pageId);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
341
342
            // restore gr_list
343 26
            $GLOBALS['TSFE']->gr_list = $groupListBackup;
344
345 26
            $GLOBALS['TSFE']->initTemplate();
346 26
            $GLOBALS['TSFE']->forceTemplateParsing = true;
347 26
            $GLOBALS['TSFE']->initFEuser();
348 26
            $GLOBALS['TSFE']->initUserGroups();
349
            //  $GLOBALS['TSFE']->getCompressedTCarray(); // seems to cause conflicts sometimes
350
351 26
            $GLOBALS['TSFE']->no_cache = true;
352 26
            $GLOBALS['TSFE']->tmpl->start($GLOBALS['TSFE']->rootLine);
353 26
            $GLOBALS['TSFE']->no_cache = false;
354 26
            $GLOBALS['TSFE']->getConfigArray();
355 26
            $GLOBALS['TSFE']->settingLanguage();
356 26
            if (!$useCache) {
357
                $GLOBALS['TSFE']->settingLocale();
358
            }
359
360 26
            $GLOBALS['TSFE']->newCObj();
361 26
            $GLOBALS['TSFE']->absRefPrefix = self::getAbsRefPrefixFromTSFE($GLOBALS['TSFE']);
362 26
            $GLOBALS['TSFE']->calculateLinkVars();
363
364 26
            if ($useCache) {
365 26
                $tsfeCache[$cacheId] = $GLOBALS['TSFE'];
366
            }
367
        }
368
369 26
        if ($useCache) {
370 26
            $GLOBALS['TSFE'] = $tsfeCache[$cacheId];
371 26
            $GLOBALS['TSFE']->settingLocale();
372
        }
373 26
    }
374
375
    /**
376
     * @deprecated This is only implemented to provide compatibility for TYPO3 8 and 9 when we drop TYPO3 8 support this
377
     * should changed to use a middleware stack
378
     * @param integer $pageId
379
     */
380 27
    private static function getPageAndRootlineOfTSFE($pageId)
381
    {
382
        //@todo When we drop the support of TYPO3 8 we should use the frontend middleware stack instead of initializing this on our own
383
        /** @var $siteRepository SiteRepository */
384 27
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
385 27
        $site = $siteRepository->getSiteByPageId($pageId);
386 27
        $GLOBALS['TSFE']->getPageAndRootlineWithDomain($site->getRootPageId());
387 26
    }
388
389
    /**
390
     * Check if record ($table, $uid) is a workspace record
391
     *
392
     * @param string $table The table the record belongs to
393
     * @param int $uid The record's uid
394
     * @return bool TRUE if the record is in a draft workspace, FALSE if it's a LIVE record
395
     */
396 42
    public static function isDraftRecord($table, $uid)
397
    {
398 42
        $isWorkspaceRecord = false;
399
400 42
        if ((ExtensionManagementUtility::isLoaded('workspaces')) && (BackendUtility::isTableWorkspaceEnabled($table))) {
401
            $record = BackendUtility::getRecord($table, $uid, 'pid, t3ver_state');
402
403
            if ($record['pid'] == '-1' || $record['t3ver_state'] > 0) {
404
                $isWorkspaceRecord = true;
405
            }
406
        }
407
408 42
        return $isWorkspaceRecord;
409
    }
410
411
    /**
412
     * Check if the page type of a page record is allowed
413
     *
414
     * @param array $pageRecord The pages database row
415
     * @param string $configurationName The name of the configuration to use.
416
     *
417
     * @return bool TRUE if the page type is allowed, otherwise FALSE
418
     */
419 31
    public static function isAllowedPageType(array $pageRecord, $configurationName = 'pages')
420
    {
421 31
        $isAllowedPageType = false;
422 31
        $configurationName = $configurationName ?? 'pages';
423 31
        $allowedPageTypes = self::getAllowedPageTypes($pageRecord['uid'], $configurationName);
424
425 31
        if (in_array($pageRecord['doktype'], $allowedPageTypes)) {
426 30
            $isAllowedPageType = true;
427
        }
428
429 31
        return $isAllowedPageType;
430
    }
431
432
    /**
433
     * Get allowed page types
434
     *
435
     * @param int $pageId Page ID
436
     * @param string $configurationName The name of the configuration to use.
437
     *
438
     * @return array Allowed page types to compare to a doktype of a page record
439
     */
440 31
    public static function getAllowedPageTypes($pageId, $configurationName = 'pages')
441
    {
442 31
        $rootPath = '';
443 31
        $configuration = self::getConfigurationFromPageId($pageId, $rootPath);
444 31
        return $configuration->getIndexQueueAllowedPageTypesArrayByConfigurationName($configurationName);
445
    }
446
447
    /**
448
     * Resolves the configured absRefPrefix to a valid value and resolved if absRefPrefix
449
     * is set to "auto".
450
     *
451
     * @param TypoScriptFrontendController $TSFE
452
     * @return string
453
     */
454 26
    public static function getAbsRefPrefixFromTSFE(TypoScriptFrontendController $TSFE)
455
    {
456 26
        $absRefPrefix = '';
457 26
        if (empty($TSFE->config['config']['absRefPrefix'])) {
458 23
            return $absRefPrefix;
459
        }
460
461 3
        $absRefPrefix = trim($TSFE->config['config']['absRefPrefix']);
462 3
        if ($absRefPrefix === 'auto') {
463 1
            $absRefPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
464
        }
465
466 3
        return $absRefPrefix;
467
    }
468
469
470
    /**
471
     * This function can be used to check if one of the strings in needles is
472
     * contained in the haystack.
473
     *
474
     *
475
     * Example:
476
     *
477
     * haystack: the brown fox
478
     * needles: ['hello', 'world']
479
     * result: false
480
     *
481
     * haystack: the brown fox
482
     * needles: ['is', 'fox']
483
     * result: true
484
     *
485
     * @param string $haystack
486
     * @param array $needles
487
     * @return bool
488
     */
489 54
    public static function containsOneOfTheStrings($haystack, array $needles)
490
    {
491 54
        foreach ($needles as $needle) {
492 54
            $position = strpos($haystack, $needle);
493 54
            if ($position !== false) {
494 3
                return true;
495
            }
496
        }
497
498 51
        return false;
499
    }
500
}
501