meilisearch/Classes/System/Util/SiteUtility.php

132 lines
3.8 KiB
PHP
Raw Permalink Normal View History

2021-04-17 00:26:33 +02:00
<?php
namespace WapplerSystems\Meilisearch\System\Util;
/***************************************************************
* Copyright notice
*
* (c) 2019 Timo Hund <timo.hund@dkd.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* This class contains related functions for the new site management that was introduced with TYPO3 9.
*/
class SiteUtility
{
/**
* Determines if the site where the page belongs to is managed with the TYPO3 site management.
*
* @param int $pageId
* @return boolean
*/
public static function getIsSiteManagedSite(int $pageId): bool
{
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
try {
/* @var SiteFinder $siteFinder */
$site = $siteFinder->getSiteByPageId($pageId);
} catch (SiteNotFoundException $e) {
return false;
}
return $site instanceof Site;
}
/**
*
*
* @param Site $typo3Site
* @param string $property
* @param string $defaultValue
* @return string
*/
2021-04-24 04:44:44 +02:00
public static function getConnectionProperty(Site $typo3Site, string $property, string $defaultValue = null): string
2021-04-17 00:26:33 +02:00
{
2021-04-24 04:44:44 +02:00
$value = self::getConnectionPropertyOrFallback($typo3Site, $property);
2021-04-17 00:26:33 +02:00
if ($value === null) {
return $defaultValue;
}
return $value;
}
/**
* Resolves site configuration properties.
* Language context properties have precedence over global settings.
*
* @param Site $typo3Site
* @param string $property
* @return mixed
*/
2021-04-24 04:44:44 +02:00
protected static function getConnectionPropertyOrFallback(Site $typo3Site, string $property)
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
// convention key meilisearch_$property_$scope
2021-04-24 04:44:44 +02:00
$keyToCheck = 'meilisearch_' . $property;
2021-04-17 00:26:33 +02:00
// if not found check global configuration
$siteBaseConfiguration = $typo3Site->getConfiguration();
2021-04-24 04:44:44 +02:00
return self::getValueOrFallback($siteBaseConfiguration, $keyToCheck);
2021-04-17 00:26:33 +02:00
}
/**
* @param array $data
* @param string $keyToCheck
* @return string|bool|null
*/
2021-04-24 04:44:44 +02:00
protected static function getValueOrFallback(array $data, string $keyToCheck)
2021-04-17 00:26:33 +02:00
{
$value = $data[$keyToCheck] ?? null;
if ($value === '0' || $value === 0 || !empty($value)) {
return self::evaluateConfigurationData($value);
}
2021-04-24 04:44:44 +02:00
return null;
2021-04-17 00:26:33 +02:00
}
/**
* Evaluate configuration data
*
* Setting boolean values via environment variables
* results in strings like 'false' that may be misinterpreted
* thus we check for boolean values in strings.
*
* @param string|bool|null $value
* @return string|bool|null
*/
protected static function evaluateConfigurationData($value)
{
if ($value === 'true') {
return true;
2021-04-24 04:44:44 +02:00
}
if ($value === 'false') {
2021-04-17 00:26:33 +02:00
return false;
}
return $value;
}
}