first commit

This commit is contained in:
Sven Wappler
2021-04-17 00:26:33 +02:00
commit 866c63cc63
813 changed files with 100696 additions and 0 deletions

View File

@@ -0,0 +1,257 @@
<?php
namespace WapplerSystems\Meilisearch\System\Util;
/***************************************************************
* Copyright notice
*
* (c) 2010-2016 Timo Schmidt <timo.schmidt@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!
***************************************************************/
/**
* Class ArrayAccessor
*
* LowLevel class to access nested associative arrays with
* a path.
*
* Example:
*
* $data = [];
* $data['foo']['bar'] = 'bla';
*
* $accessor = new ArrayAccesor($data);
* $value = $accessor->get('foo.bar');
*
* echo $value;
*
* the example above will output "bla"
*
*/
class ArrayAccessor
{
/**
* @var array
*/
protected $data;
/**
* @var string
*/
protected $pathSeparator = ':';
/**
* @var bool
*/
protected $includePathSeparatorInKeys = false;
/**
* @param array $data
* @param string $pathSeparator
* @param bool $includePathSeparatorInKeys
*/
public function __construct(array $data = [], $pathSeparator = ':', $includePathSeparatorInKeys = false)
{
$this->data = $data;
$this->pathSeparator = $pathSeparator;
$this->includePathSeparatorInKeys = $includePathSeparatorInKeys;
}
/**
* @param mixed $data
*/
public function setData($data)
{
$this->data = $data;
}
/**
* @return array
*/
public function getData()
{
return $this->data;
}
/**
* @param $path
* @param mixed $defaultIfEmpty
* @return array|null
*/
public function get($path, $defaultIfEmpty = null)
{
$pathArray = $this->getPathAsArray($path);
$pathSegmentCount = count($pathArray);
switch ($pathSegmentCount) {
// direct access for small paths
case 1:
return isset($this->data[$pathArray[0]]) ?
$this->data[$pathArray[0]] : $defaultIfEmpty;
case 2:
return isset($this->data[$pathArray[0]][$pathArray[1]]) ?
$this->data[$pathArray[0]][$pathArray[1]] : $defaultIfEmpty;
case 3:
return isset($this->data[$pathArray[0]][$pathArray[1]][$pathArray[2]]) ?
$this->data[$pathArray[0]][$pathArray[1]][$pathArray[2]] : $defaultIfEmpty;
default:
// when we have a longer path we use a loop to get the element
$loopResult = $this->getDeepElementWithLoop($pathArray);
return $loopResult !== null ? $loopResult : $defaultIfEmpty;
}
}
/**
* @param $pathArray
* @return array|null
*/
protected function getDeepElementWithLoop($pathArray)
{
$currentElement = $this->data;
foreach ($pathArray as $key => $pathSegment) {
// if the current path segment was not found we can stop searching
if (!isset($currentElement[$pathSegment])) {
break;
}
$currentElement = $currentElement[$pathSegment];
unset($pathArray[$key]);
}
// if segments are left the item does not exist
if (count($pathArray) > 0) {
return null;
}
// if no items left, we've found the last element
return $currentElement;
}
/**
* @param $path
* @return bool
*/
public function has($path)
{
return $this->get($path) !== null;
}
/**
* @param $path
* @param $value
*/
public function set($path, $value)
{
$pathArray = $this->getPathAsArray($path);
$pathSegmentCount = count($pathArray);
switch ($pathSegmentCount) {
// direct access for small paths
case 1:
$this->data[$pathArray[0]] = $value;
return;
case 2:
$this->data[$pathArray[0]][$pathArray[1]] = $value;
return;
default:
$this->setDeepElementWithLoop($pathArray, $value);
}
}
/**
* @param $pathArray
* @param mixed $value
*/
protected function setDeepElementWithLoop($pathArray, $value)
{
$currentElement = &$this->data;
foreach ($pathArray as $key => $pathSegment) {
if (!isset($currentElement[$pathSegment])) {
$currentElement[$pathSegment] = [];
}
unset($pathArray[$key]);
// if segments are left the item does not exist
if (count($pathArray) === 0) {
$currentElement[$pathSegment] = $value;
return;
}
$currentElement = &$currentElement[$pathSegment];
}
}
/**
* @param string $path
*/
public function reset($path)
{
$pathArray = $this->getPathAsArray($path);
$pathSegmentCount = count($pathArray);
switch ($pathSegmentCount) {
// direct access for small paths
case 1:
unset($this->data[$pathArray[0]]);
return;
case 2:
unset($this->data[$pathArray[0]][$pathArray[1]]);
return;
default:
$this->resetDeepElementWithLoop($pathArray);
}
}
/**
* @param array $pathArray
*/
protected function resetDeepElementWithLoop($pathArray)
{
$currentElement = &$this->data;
foreach ($pathArray as $key => $pathSegment) {
unset($pathArray[$key]);
// if segments are left the item does not exist
if (count($pathArray) === 0) {
unset($currentElement[$pathSegment]);
// when the element is empty after unsetting the path segment, we can remove it completely
if (empty($currentElement)) {
unset($currentElement);
}
} else {
$currentElement = &$currentElement[$pathSegment];
}
}
}
/**
* @param string $path
* @return array
*/
protected function getPathAsArray($path)
{
if (!$this->includePathSeparatorInKeys) {
$pathArray = explode($this->pathSeparator, $path);
return $pathArray;
}
$substitutedPath = str_replace($this->pathSeparator, $this->pathSeparator . '@@@', trim($path));
$pathArray = array_filter(explode('@@@', $substitutedPath));
return $pathArray;
}
}

View File

@@ -0,0 +1,185 @@
<?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;
}
/**
* This method is used to retrieve the connection configuration from the TYPO3 site configuration.
*
* Note: Language context properties have precedence over global settings.
*
* The configuration is done in the globals configuration of a site, and be extended in the language specific configuration
* of a site.
*
* Typically everything except the core name is configured on the global level and the core name differs for each language.
*
* In addition every property can be defined for the ```read``` and ```write``` scope.
*
* The convention for property keys is "solr_{propertyName}_{scope}". With the configuration "solr_host_read" you define the host
* for the solr read connection.
*
* @param Site $typo3Site
* @param string $property
* @param int $languageId
* @param string $scope
* @param string $defaultValue
* @return string
*/
public static function getConnectionProperty(Site $typo3Site, string $property, int $languageId, string $scope, string $defaultValue = null): string
{
$value = self::getConnectionPropertyOrFallback($typo3Site, $property, $languageId, $scope);
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
* @param int $languageId
* @param string $scope
* @return mixed
*/
protected static function getConnectionPropertyOrFallback(Site $typo3Site, string $property, int $languageId, string $scope)
{
if ($scope === 'write' && !self::writeConnectionIsEnabled($typo3Site, $languageId)) {
$scope = 'read';
}
// convention key solr_$property_$scope
$keyToCheck = 'solr_' . $property . '_' . $scope;
// convention fallback key solr_$property_read
$fallbackKey = 'solr_' . $property . '_read';
// try to find language specific setting if found return it
$languageSpecificConfiguration = $typo3Site->getLanguageById($languageId)->toArray();
$value = self::getValueOrFallback($languageSpecificConfiguration, $keyToCheck, $fallbackKey);
if ($value !== null) {
return $value;
}
// if not found check global configuration
$siteBaseConfiguration = $typo3Site->getConfiguration();
return self::getValueOrFallback($siteBaseConfiguration, $keyToCheck, $fallbackKey);
}
/**
* Checks whether write connection is enabled.
* Language context properties have precedence over global settings.
*
* @param Site $typo3Site
* @param int $languageId
* @return bool
*/
protected static function writeConnectionIsEnabled(Site $typo3Site, int $languageId): bool
{
$languageSpecificConfiguration = $typo3Site->getLanguageById($languageId)->toArray();
$value = self::getValueOrFallback($languageSpecificConfiguration, 'solr_use_write_connection', 'solr_use_write_connection');
if ($value !== null) {
return $value;
}
$siteBaseConfiguration = $typo3Site->getConfiguration();
$value = self::getValueOrFallback($siteBaseConfiguration, 'solr_use_write_connection', 'solr_use_write_connection');
if ($value !== null) {
return $value;
}
return false;
}
/**
* @param array $data
* @param string $keyToCheck
* @param string $fallbackKey
* @return string|bool|null
*/
protected static function getValueOrFallback(array $data, string $keyToCheck, string $fallbackKey)
{
$value = $data[$keyToCheck] ?? null;
if ($value === '0' || $value === 0 || !empty($value)) {
return self::evaluateConfigurationData($value);
}
return self::evaluateConfigurationData($data[$fallbackKey] ?? null);
}
/**
* 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;
} elseif ($value === 'false') {
return false;
}
return $value;
}
}