Skip to content

Commit

Permalink
fixed issues with haste 5
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Nov 23, 2023
1 parent 9b7bfc5 commit 4e18aa1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 58 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## [1.16.1] - 2023-11-23
- Fixed: compatibility issues with haste 5
- Fixed: infinite loop in StringUtil

## [1.16.0] - 2023-11-08
- Added: support for haste 5
- Added: dynamically load either StringUtil or StringParser methods based on which version of contao-haste is installed
Expand Down
118 changes: 61 additions & 57 deletions library/Haste/Util/StringUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace HeimrichHannot\Haste\Util;

use Codefog\HasteBundle\StringParser;
use Contao\System;
use Soundasleep\Html2Text;

class StringUtil
Expand All @@ -23,63 +25,6 @@ class StringUtil
const NUMBERS_NONAMBIGUOUS = '23456789';


/**
* Recursively replace simple tokens and insert tags
*
* @param string $text
* @param array $tokens Array of Tokens
* @param int $textFlags Filters the tokens and the text for a given set of options
*
* @return string
*/
public static function recursiveReplaceTokensAndTags($text, $tokens, $textFlags = 0)
{
if (class_exists('\Haste\Util\StringUtil')) {
$strBuffer = \Haste\Util\StringUtil::recursiveReplaceTokensAndTags($text, $tokens, $textFlags);
} else {
$container = \Contao\System::getContainer();
$strBuffer = $container->get(\Codefog\HasteBundle\StringParser::class)->recursiveReplaceTokensAndTags($text, $tokens, $textFlags);
}
return $strBuffer;
}

/**
* Convert the given array or string to plain text using given options
*
* @param mixed $value
* @param int $options
*
* @return mixed
*/
public static function convertToText($value, $options)
{
if (class_exists('\Haste\Util\StringUtil')) {
$value = \Haste\Util\StringUtil::convertToText($value, $options);
} else {
$container = \Contao\System::getContainer();
$value = $container->get(\Codefog\HasteBundle\StringParser::class)->convertToText($value, $options);
}
return $value;
}

/**
* Flatten input data, Simple Tokens can't handle arrays
*
* @param mixed $value
* @param string $key
* @param array $data
* @param string $pattern
*/
public static function flatten($value, $key, array & $data, $pattern = ', ')
{
if (class_exists('\Haste\Util\StringUtil')) {
\Haste\Util\StringUtil::flatten($value, $key, $data, $pattern);
} else {
$container = \Contao\System::getContainer();
$container->get(\Codefog\HasteBundle\StringParser::class)->flatten($value, $key, $data, $pattern);
}
}

/**
* Convert new line or br with <p> tags
* @param $text
Expand Down Expand Up @@ -436,5 +381,64 @@ public static function replaceNonXmlEntities($xml)
$replace = ["&#xA0;", "&#x2014;"];
return str_replace($search, $replace, $xml);
}




/**
* Recursively replace simple tokens and insert tags
*
* @param string $text
* @param array $tokens Array of Tokens
* @param int $textFlags Filters the tokens and the text for a given set of options
*
* @return string
*/
public static function recursiveReplaceTokensAndTags($text, $tokens, $textFlags = 0)
{
return static::mapCalls('recursiveReplaceTokensAndTags', $text, ...func_get_args());
}

/**
* Convert the given array or string to plain text using given options
*
* @param mixed $value
* @param int $options
*
* @return mixed
*/
public static function convertToText($value, $options)
{
return static::mapCalls('convertToText', $value, ...func_get_args());

}

/**
* Flatten input data, Simple Tokens can't handle arrays
*
* @param mixed $value
* @param string $key
* @param array $data
* @param string $pattern
*/
public static function flatten($value, $key, array & $data, $pattern = ', ')
{
return static::mapCalls('flatten', $value, ...func_get_args());
}

private static function mapCalls(string $method, $default)
{
$arguments = func_get_args();
array_shift($arguments);
array_shift($arguments);
$container = System::getContainer();

if (class_exists(StringParser::class) && $container->has(StringParser::class)) {
return $container->get(StringParser::class)->{$method}(...$arguments);
} elseif (class_exists(\Haste\Util\StringUtil::class)) {
return \Haste\Util\StringUtil::{$method}(...$arguments);
}
return $default;
}
}

39 changes: 38 additions & 1 deletion library/Haste/Util/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace HeimrichHannot\Haste\Util;

class Url extends \Haste\Util\Url
use Codefog\HasteBundle\UrlParser;
use Contao\System;

class Url
{
/**
* Check an url for existing scheme and add if it is missing
Expand Down Expand Up @@ -373,5 +376,39 @@ public static function redirect($strLocation, $intStatus=303)

exit;
}

public static function addQueryString($strQuery, $varUrl = null)
{
return static::mapCalls('addQueryString', $strQuery, ...func_get_args());
}

public static function removeQueryString(array $arrParams, $varUrl = null)
{
return static::mapCalls('removeQueryString', $varUrl, ...func_get_args());
}

public static function removeQueryStringCallback(callable $callback, $varUrl = null)
{
return static::mapCalls('removeQueryStringCallback', $varUrl, ...func_get_args());
}

protected static function prepareUrl($varUrl)
{
return static::mapCalls('prepareUrl', $varUrl, ...func_get_args());
}

private static function mapCalls(string $method, $default)
{
$arguments = func_get_args();
array_shift($arguments);
array_shift($arguments);
$container = System::getContainer();
if (class_exists(UrlParser::class) && $container->has(UrlParser::class)) {
return $container->get(UrlParser::class)->{$method}(...$arguments);
} elseif (class_exists(\Haste\Util\Url::class)) {
return \Haste\Util\Url::{$method}(...func_get_args());
}
return $default;
}
}

0 comments on commit 4e18aa1

Please sign in to comment.