Compare commits

...

3 Commits

Author SHA1 Message Date
Clemens Schwaighofer
8bd14b4385 Add parseCharacterRanges function to Strings.php and tests 2026-01-14 10:42:16 +09:00
Clemens Schwaighofer
cfb6f14c09 Add color output for phan 2026-01-07 13:21:17 +09:00
Clemens Schwaighofer
969aa684d9 Indent fixes for publish script 2026-01-07 12:50:37 +09:00
5 changed files with 203 additions and 78 deletions

View File

@@ -56,6 +56,8 @@ return [
// Automatically inferred from composer.json requirement for "php" of ">=8.2"
'target_php_version' => '8.2',
"minimum_target_php_version" => "8.2",
// turn color on (-C)
"color_issue_messages_if_supported" => true,
// If enabled, missing properties will be created when
// they are first seen. If false, we'll report an

View File

@@ -3,7 +3,7 @@
BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/";
PACKAGE_DOWNLOAD="${BASE_FOLDER}package-download/";
if [ ! -d "${PACKAGE_DOWNLOAD}" ]; then
mkdir "${PACKAGE_DOWNLOAD}";
mkdir "${PACKAGE_DOWNLOAD}";
fi;
VERSION=$(git tag --list | sort -V | tail -n1 | sed -e "s/^v//");
file_last_published="${BASE_FOLDER}last.published";
@@ -11,84 +11,84 @@ go_flag="$1";
function gitea_publish
{
_GITEA_PUBLISH="${1}"
_GITEA_UPLOAD_FILENAME="${2}"
_GITEA_URL_DL="${3}"
_GITEA_URL_PUSH="${4}"
_GITEA_USER="${5}"
_GITEA_TOKEN="${6}"
_PACKAGE_DOWNLOAD="${7}"
_VERSION="${8}"
_file_last_published="${9}"
_GITEA_PUBLISH="${1}"
_GITEA_UPLOAD_FILENAME="${2}"
_GITEA_URL_DL="${3}"
_GITEA_URL_PUSH="${4}"
_GITEA_USER="${5}"
_GITEA_TOKEN="${6}"
_PACKAGE_DOWNLOAD="${7}"
_VERSION="${8}"
_file_last_published="${9}"
if [ -z "${_GITEA_PUBLISH}" ]; then
return
fi;
if [ -n "${_GITEA_UPLOAD_FILENAME}" ] &&
[ -n "${_GITEA_URL_DL}" ] && [ -n "${_GITEA_URL_PUSH}" ] &&
[ -n "${_GITEA_USER}" ] && [ -n "${_GITEA_TOKEN}" ]; then
echo "> Publish ${_GITEA_UPLOAD_FILENAME} with ${_VERSION} to: ${_GITEA_URL_PUSH}";
if [ ! -f "${_PACKAGE_DOWNLOAD}${_GITEA_UPLOAD_FILENAME}-v${_VERSION}.zip" ]; then
echo "> Download: ${_GITEA_UPLOAD_FILENAME}-v${_VERSION}.zip";
curl -LJO \
--output-dir "${_PACKAGE_DOWNLOAD}" \
"${_GITEA_URL_DL}"/v"${_VERSION}".zip;
fi;
if [ ! -f "${_PACKAGE_DOWNLOAD}${_GITEA_UPLOAD_FILENAME}-v${_VERSION}.zip" ]; then
echo "[!] Package file does not exist for version: ${_VERSION}";
else
response=$(curl --user "${_GITEA_USER}":"${_GITEA_TOKEN}" \
--upload-file "${_PACKAGE_DOWNLOAD}${_GITEA_UPLOAD_FILENAME}-v${_VERSION}.zip" \
"${_GITEA_URL_PUSH}"?version="${_VERSION}");
status=$(echo "${response}" | jq .errors[].status);
message=$(echo "${response}" | jq .errors[].message);
if [ -n "${status}" ]; then
echo "[!] Error ${status}: ${message}";
else
echo "> Publish completed";
fi;
echo "${_VERSION}" > "${_file_last_published}";
fi;
else
echo "[!] Missing either GITEA_UPLOAD_FILENAME, GITEA_URL_DL, GITEA_URL_PUSH, GITEA_USER or GITEA_TOKEN environment variable";
fi;
if [ -z "${_GITEA_PUBLISH}" ]; then
return
fi;
if [ -n "${_GITEA_UPLOAD_FILENAME}" ] &&
[ -n "${_GITEA_URL_DL}" ] && [ -n "${_GITEA_URL_PUSH}" ] &&
[ -n "${_GITEA_USER}" ] && [ -n "${_GITEA_TOKEN}" ]; then
echo "> Publish ${_GITEA_UPLOAD_FILENAME} with ${_VERSION} to: ${_GITEA_URL_PUSH}";
if [ ! -f "${_PACKAGE_DOWNLOAD}${_GITEA_UPLOAD_FILENAME}-v${_VERSION}.zip" ]; then
echo "> Download: ${_GITEA_UPLOAD_FILENAME}-v${_VERSION}.zip";
curl -LJO \
--output-dir "${_PACKAGE_DOWNLOAD}" \
"${_GITEA_URL_DL}"/v"${_VERSION}".zip;
fi;
if [ ! -f "${_PACKAGE_DOWNLOAD}${_GITEA_UPLOAD_FILENAME}-v${_VERSION}.zip" ]; then
echo "[!] Package file does not exist for version: ${_VERSION}";
else
response=$(curl --user "${_GITEA_USER}":"${_GITEA_TOKEN}" \
--upload-file "${_PACKAGE_DOWNLOAD}${_GITEA_UPLOAD_FILENAME}-v${_VERSION}.zip" \
"${_GITEA_URL_PUSH}"?version="${_VERSION}");
status=$(echo "${response}" | jq .errors[].status);
message=$(echo "${response}" | jq .errors[].message);
if [ -n "${status}" ]; then
echo "[!] Error ${status}: ${message}";
else
echo "> Publish completed";
fi;
echo "${_VERSION}" > "${_file_last_published}";
fi;
else
echo "[!] Missing either GITEA_UPLOAD_FILENAME, GITEA_URL_DL, GITEA_URL_PUSH, GITEA_USER or GITEA_TOKEN environment variable";
fi;
}
function gitlab_publish
{
_GITLAB_PUBLISH="${1}";
_GITLAB_URL="${2}";
_GITLAB_DEPLOY_TOKEN="${3}";
_PACKAGE_DOWNLOAD="${4}"
_VERSION="${5}"
_file_last_published="${6}"
if [ -z "${GITLAB_PUBLISH}" ]; then
return;
fi;
if [ -n "${_GITLAB_URL}" ] && [ -n "${_GITLAB_DEPLOY_TOKEN}" ]; then
curl --data tag=v"${_VERSION}" \
--header "Deploy-Token: ${_GITLAB_DEPLOY_TOKEN}" \
"${_GITLAB_URL}";
curl --data branch=master \
--header "Deploy-Token: ${_GITLAB_DEPLOY_TOKEN}" \
"${_GITLAB_URL}";
echo "${_VERSION}" > "${_file_last_published}";
else
echo "[!] Missing GITLAB_URL or GITLAB_DEPLOY_TOKEN environment variable";
fi;
_GITLAB_PUBLISH="${1}";
_GITLAB_URL="${2}";
_GITLAB_DEPLOY_TOKEN="${3}";
_PACKAGE_DOWNLOAD="${4}"
_VERSION="${5}"
_file_last_published="${6}"
if [ -z "${_GITLAB_PUBLISH}" ]; then
return;
fi;
if [ -n "${_GITLAB_URL}" ] && [ -n "${_GITLAB_DEPLOY_TOKEN}" ]; then
curl --data tag=v"${_VERSION}" \
--header "Deploy-Token: ${_GITLAB_DEPLOY_TOKEN}" \
"${_GITLAB_URL}";
curl --data branch=master \
--header "Deploy-Token: ${_GITLAB_DEPLOY_TOKEN}" \
"${_GITLAB_URL}";
echo "${_VERSION}" > "${_file_last_published}";
else
echo "[!] Missing GITLAB_URL or GITLAB_DEPLOY_TOKEN environment variable";
fi;
}
if [ -z "${VERSION}" ]; then
echo "[!] Version must be set in the form x.y.z without any leading characters";
exit;
echo "[!] Version must be set in the form x.y.z without any leading characters";
exit;
fi;
# compare version, if different or newer, deploy
if [ -f "${file_last_published}" ]; then
LAST_PUBLISHED_VERSION=$(cat "${file_last_published}");
if dpkg --compare-versions "${VERSION}" le "${LAST_PUBLISHED_VERSION}"; then
echo "[!] git tag version ${VERSION} is not newer than previous published version ${LAST_PUBLISHED_VERSION}";
fi;
LAST_PUBLISHED_VERSION=$(cat "${file_last_published}");
if dpkg --compare-versions "${VERSION}" le "${LAST_PUBLISHED_VERSION}"; then
echo "[!] git tag version ${VERSION} is not newer than previous published version ${LAST_PUBLISHED_VERSION}";
fi;
fi;
# read in the .env.deploy file and we must have
@@ -105,8 +105,8 @@ fi;
# GITLAB_TOKEN
# GITLAB_URL
if [ ! -f "${BASE_FOLDER}.env.deploy" ]; then
echo "[!] Deploy enviroment file .env.deploy is missing";
exit;
echo "[!] Deploy enviroment file .env.deploy is missing";
exit;
fi;
set -o allexport;
cd "${BASE_FOLDER}" || exit
@@ -116,10 +116,10 @@ cd - >/dev/null 2>&1 || exit;
set +o allexport;
if [ "${go_flag}" != "go" ]; then
echo "[!] No go flag given";
echo "> Would publish ${VERSION}";
echo "[END]";
exit;
echo "[!] No go flag given";
echo "> Would publish ${VERSION}";
echo "[END]";
exit;
fi;
echo "[START]";

View File

@@ -268,6 +268,51 @@ class Strings
));
}
/**
* Split up character ranges in format A-Z, a-z, 0-9
*
* @param string $input
* @return string[]
*/
public static function parseCharacterRanges(string $input): array
{
// if not alphanumeric, throw value error
if (!preg_match("/^[A-Za-z0-9\-\s]+$/u", $input)) {
throw new \InvalidArgumentException(
"The input string contains invalid characters, "
. "only alphanumeric, dash (-), space and 'or' are allowed: "
. $input
);
}
// Remove all spaces
$input = str_replace(' ', '', $input);
$result = [];
// Find all patterns like "A-Z" (character-dash-character)
preg_match_all('/(.)-(.)/u', $input, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$start = $match[1];
$end = $match[2];
// Get ASCII/Unicode values
$startOrd = ord($start[0]);
$endOrd = ord($end[0]);
// make sure start is before end
if ($startOrd > $endOrd) {
[$startOrd, $endOrd] = [$endOrd, $startOrd];
}
// Generate range of characters
for ($i = $startOrd; $i <= $endOrd; $i++) {
$char = chr($i);
if (!in_array($char, $result)) {
$result[] = $char;
}
}
}
// make the result unique
$result = array_unique($result);
return $result;
}
/**
* Check if a regex is valid. Does not return the detail regex parser error
*

View File

@@ -27,7 +27,7 @@ class RandomKey
/** @var int character count in they key character range */
private static int $key_character_range_length = 0;
/** @var int default key lenghth */
/** @deprecated Will be removed */
/** @deprecated Will be removed, as setting has moved to randomKeyGen */
private static int $key_length = 4;
/**
@@ -107,7 +107,7 @@ class RandomKey
* @param int $key_length key length
* @return bool true for valid, false for invalid length
*/
private static function validateRandomKeyLenght(int $key_length): bool
private static function validateRandomKeyLength(int $key_length): bool
{
if (
$key_length > 0 &&
@@ -125,12 +125,12 @@ class RandomKey
*
* @param int $key_length key length
* @return bool true/false for set status
* @deprecated This function does no longer set the key length, the randomKeyGen parameter has to b used
* @deprecated This function does no longer set the key length, the randomKeyGen parameter has to be used
*/
public static function setRandomKeyLength(int $key_length): bool
{
// only if valid int key with valid length
if (self::validateRandomKeyLenght($key_length) === true) {
if (self::validateRandomKeyLength($key_length) === true) {
self::$key_length = $key_length;
return true;
} else {
@@ -176,7 +176,7 @@ class RandomKey
$key_character_range_length = self::getRandomKeyDataLength();
}
// if not valid key length, fallback to default
if (!self::validateRandomKeyLenght($key_length)) {
if (!self::validateRandomKeyLength($key_length)) {
$key_length = self::KEY_LENGTH_DEFAULT;
}
// create random string

View File

@@ -698,6 +698,84 @@ final class CoreLibsConvertStringsTest extends TestCase
'Cannot match last preg error string'
);
}
/**
* Undocumented function
*
* @return array
*/
public function parseCharacterRangesProvider(): array
{
return [
'simple a-z' => [
['a-z'],
implode('', range('a', 'z')),
null,
],
'simple A-Z' => [
['A-Z'],
implode('', range('A', 'Z')),
null,
],
'simple 0-9' => [
['0-9'],
implode('', range('0', '9')),
null,
],
'mixed ranges' => [
['a-c', 'X-Z', '3-5'],
'abcXYZ345',
null,
],
'reverse ranges' => [
['z-a'],
'abcdefghijklmnopqrstuvwxyz',
null,
],
'overlapping ranges' => [
['a-f', 'd-j'],
'abcdefghij',
null,
],
'mixed valid and overlap ranges' => [
['a-f', 'z-a', '0-3'],
'abcdefghijklmnopqrstuvwxyz0123',
null,
],
'invalid ranges' => [
['a-あ', 'A-あ', '0-あ'],
'',
\InvalidArgumentException::class,
],
];
}
/**
* Undocumented function
*
* @covers ::parseCharacterRanges
* @dataProvider parseCharacterRangesProvider
* @testdox parseCharacterRanges $input to $expected [$_dataName]
*
* @param array $input
* @param string $expected
* @param string|null $expected_exception
* @return void
*/
public function testParseCharacterRanges(
array $input,
string $expected,
?string $expected_exception
): void {
if ($expected_exception !== null) {
$this->expectException($expected_exception);
}
$this->assertEquals(
$expected,
implode('', \CoreLibs\Convert\Strings::parseCharacterRanges(implode('', $input)))
);
}
}
// __END__