351 lines
16 KiB
HTML
351 lines
16 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>JSDoc: Source: services/php-version.js</title>
|
|
|
|
<script src="scripts/prettify/prettify.js"> </script>
|
|
<script src="scripts/prettify/lang-css.js"> </script>
|
|
<!--[if lt IE 9]>
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="main">
|
|
|
|
<h1 class="page-title">Source: services/php-version.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
<article>
|
|
<pre class="prettyprint source linenums"><code>/**
|
|
* Utilities relating to PHP version numbers. This compares version numbers
|
|
* using the algorithm followed by Composer (see
|
|
* https://getcomposer.org/doc/04-schema.md#version).
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import { fetch } from '../core/base-service/got.js'
|
|
import { getCachedResource } from '../core/base-service/resource-cache.js'
|
|
import { listCompare } from './version.js'
|
|
import { omitv } from './text-formatters.js'
|
|
|
|
/**
|
|
* Return a negative value if v1 < v2,
|
|
* zero if v1 = v2, a positive value otherwise.
|
|
*
|
|
* @param {string} v1 - First version for comparison
|
|
* @param {string} v2 - Second version for comparison
|
|
* @returns {number} Comparison result (-1, 0 or 1)
|
|
*/
|
|
function asciiVersionCompare(v1, v2) {
|
|
if (v1 < v2) {
|
|
return -1
|
|
} else if (v1 > v2) {
|
|
return 1
|
|
} else {
|
|
return 0
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Take a version without the starting v.
|
|
* eg, '1.0.x-beta'
|
|
* Return { numbers: [1,0,something big], modifier: 2, modifierCount: 1 }
|
|
*
|
|
* @param {string} version - Version number string
|
|
* @returns {object} Object containing version details
|
|
*/
|
|
function numberedVersionData(version) {
|
|
// A version has a numbered part and a modifier part
|
|
// (eg, 1.0.0-patch, 2.0.x-dev).
|
|
const parts = version.split('-')
|
|
const numbered = parts[0]
|
|
|
|
// Aliases that get caught here.
|
|
if (numbered === 'dev') {
|
|
return {
|
|
numbers: parts[1],
|
|
modifier: 5,
|
|
modifierCount: 1,
|
|
}
|
|
}
|
|
|
|
let modifierLevel = 3
|
|
let modifierLevelCount = 0
|
|
|
|
// Normalization based on
|
|
// https://github.com/composer/semver/blob/1.5.0/src/VersionParser.php
|
|
|
|
if (parts.length > 1) {
|
|
const modifier = parts[parts.length - 1]
|
|
const firstLetter = modifier.charCodeAt(0)
|
|
let modifierLevelCountString
|
|
|
|
// Modifiers: alpha < beta < RC < normal < patch < dev
|
|
if (firstLetter === 97 || firstLetter === 65) {
|
|
// a / A
|
|
modifierLevel = 0
|
|
if (/^alpha/i.test(modifier)) {
|
|
modifierLevelCountString = +modifier.slice(5)
|
|
} else {
|
|
modifierLevelCountString = +modifier.slice(1)
|
|
}
|
|
} else if (firstLetter === 98 || firstLetter === 66) {
|
|
// b / B
|
|
modifierLevel = 1
|
|
if (/^beta/i.test(modifier)) {
|
|
modifierLevelCountString = +modifier.slice(4)
|
|
} else {
|
|
modifierLevelCountString = +modifier.slice(1)
|
|
}
|
|
} else if (firstLetter === 82 || firstLetter === 114) {
|
|
// R / r
|
|
modifierLevel = 2
|
|
modifierLevelCountString = +modifier.slice(2)
|
|
} else if (firstLetter === 112) {
|
|
// p
|
|
modifierLevel = 4
|
|
if (/^patch/.test(modifier)) {
|
|
modifierLevelCountString = +modifier.slice(5)
|
|
} else {
|
|
modifierLevelCountString = +modifier.slice(1)
|
|
}
|
|
} else if (firstLetter === 100) {
|
|
// d
|
|
modifierLevel = 5
|
|
if (/^dev/.test(modifier)) {
|
|
modifierLevelCountString = +modifier.slice(3)
|
|
} else {
|
|
modifierLevelCountString = +modifier.slice(1)
|
|
}
|
|
}
|
|
|
|
// If we got the empty string, it defaults to a modifier count of 1.
|
|
if (!modifierLevelCountString) {
|
|
modifierLevelCount = 1
|
|
} else {
|
|
modifierLevelCount = +modifierLevelCountString
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Try to convert to a list of numbers.
|
|
*
|
|
* @param {string} s - Version number string
|
|
* @returns {number} Version number integer
|
|
*/
|
|
function toNum(s) {
|
|
let n = +s
|
|
if (Number.isNaN(n)) {
|
|
n = 0xffffffff
|
|
}
|
|
return n
|
|
}
|
|
const numberList = numbered.split('.').map(toNum)
|
|
|
|
return {
|
|
numbers: numberList,
|
|
modifier: modifierLevel,
|
|
modifierCount: modifierLevelCount,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Compares two versions and return an integer based on the result.
|
|
* See https://getcomposer.org/doc/04-schema.md#version
|
|
* and https://github.com/badges/shields/issues/319#issuecomment-74411045
|
|
*
|
|
* @param {string} v1 - First version
|
|
* @param {string} v2 - Second version
|
|
* @returns {number} Negative value if v1 < v2, zero if v1 = v2, else a positive value
|
|
*/
|
|
function compare(v1, v2) {
|
|
// Omit the starting `v`.
|
|
const rawv1 = omitv(v1)
|
|
const rawv2 = omitv(v2)
|
|
let v1data, v2data
|
|
try {
|
|
v1data = numberedVersionData(rawv1)
|
|
v2data = numberedVersionData(rawv2)
|
|
} catch (e) {
|
|
return asciiVersionCompare(rawv1, rawv2)
|
|
}
|
|
|
|
// Compare the numbered part (eg, 1.0.0 < 2.0.0).
|
|
const numbersCompare = listCompare(v1data.numbers, v2data.numbers)
|
|
if (numbersCompare !== 0) {
|
|
return numbersCompare
|
|
}
|
|
|
|
// Compare the modifiers (eg, alpha < beta).
|
|
if (v1data.modifier < v2data.modifier) {
|
|
return -1
|
|
} else if (v1data.modifier > v2data.modifier) {
|
|
return 1
|
|
}
|
|
|
|
// Compare the modifier counts (eg, alpha1 < alpha3).
|
|
if (v1data.modifierCount < v2data.modifierCount) {
|
|
return -1
|
|
} else if (v1data.modifierCount > v2data.modifierCount) {
|
|
return 1
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
/**
|
|
* Determines the latest version from a list of versions.
|
|
*
|
|
* @param {string[]} versions - List of versions
|
|
* @returns {string} Latest version
|
|
*/
|
|
function latest(versions) {
|
|
let latest = versions[0]
|
|
for (let i = 1; i < versions.length; i++) {
|
|
if (compare(latest, versions[i]) < 0) {
|
|
latest = versions[i]
|
|
}
|
|
}
|
|
return latest
|
|
}
|
|
|
|
/**
|
|
* Determines if a version is stable or not.
|
|
*
|
|
* @param {string} version - Version number
|
|
* @returns {boolean} true if version is stable, else false
|
|
*/
|
|
function isStable(version) {
|
|
const rawVersion = omitv(version)
|
|
let versionData
|
|
try {
|
|
versionData = numberedVersionData(rawVersion)
|
|
} catch (e) {
|
|
return false
|
|
}
|
|
// normal or patch
|
|
return versionData.modifier === 3 || versionData.modifier === 4
|
|
}
|
|
|
|
/**
|
|
* Checks if a version is valid and returns the minor version.
|
|
*
|
|
* @param {string} version - Version number
|
|
* @returns {string} Minor version
|
|
*/
|
|
function minorVersion(version) {
|
|
const result = version.match(/^(\d+)(?:\.(\d+))?(?:\.(\d+))?/)
|
|
|
|
if (result === null) {
|
|
return ''
|
|
}
|
|
|
|
return `${result[1]}.${result[2] ? result[2] : '0'}`
|
|
}
|
|
|
|
/**
|
|
* Reduces the list of php versions that intersect with release versions to a version range (for eg. '5.4 - 7.1', '>= 5.5').
|
|
*
|
|
* @param {string[]} versions - List of php versions
|
|
* @param {string[]} phpReleases - List of php release versions
|
|
* @returns {string[]} Reduced Version Range (for eg. ['5.4 - 7.1'], ['>= 5.5'])
|
|
*/
|
|
function versionReduction(versions, phpReleases) {
|
|
if (!versions.length) {
|
|
return []
|
|
}
|
|
|
|
// versions intersect
|
|
versions = Array.from(new Set(versions))
|
|
.filter(n => phpReleases.includes(n))
|
|
.sort()
|
|
|
|
// nothing to reduction
|
|
if (versions.length < 2) {
|
|
return versions
|
|
}
|
|
|
|
const first = phpReleases.indexOf(versions[0])
|
|
const last = phpReleases.indexOf(versions[versions.length - 1])
|
|
|
|
// no missed versions
|
|
if (first + versions.length - 1 === last) {
|
|
if (last === phpReleases.length - 1) {
|
|
return [`>= ${versions[0][2] === '0' ? versions[0][0] : versions[0]}`] // 7.0 -> 7
|
|
}
|
|
|
|
return [`${versions[0]} - ${versions[versions.length - 1]}`]
|
|
}
|
|
|
|
return versions
|
|
}
|
|
|
|
/**
|
|
* Fetches the PHP release versions from cache if exists, else fetch from the source url and save in cache.
|
|
*
|
|
* @async
|
|
* @param {object} githubApiProvider - Github API provider
|
|
* @returns {Promise<*>} Promise that resolves to parsed response
|
|
*/
|
|
async function getPhpReleases(githubApiProvider) {
|
|
return getCachedResource({
|
|
url: '/repos/php/php-src/git/refs/tags',
|
|
scraper: tags =>
|
|
Array.from(
|
|
new Set(
|
|
tags
|
|
// only releases
|
|
.filter(
|
|
tag => tag.ref.match(/^refs\/tags\/php-\d+\.\d+\.\d+$/) != null,
|
|
)
|
|
// get minor version of release
|
|
.map(tag => tag.ref.match(/^refs\/tags\/php-(\d+\.\d+)\.\d+$/)[1]),
|
|
),
|
|
),
|
|
requestFetcher: githubApiProvider.fetch.bind(githubApiProvider, fetch),
|
|
})
|
|
}
|
|
|
|
export {
|
|
compare,
|
|
latest,
|
|
isStable,
|
|
minorVersion,
|
|
versionReduction,
|
|
getPhpReleases,
|
|
}
|
|
</code></pre>
|
|
</article>
|
|
</section>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<nav>
|
|
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-badge-maker.html">badge-maker</a></li><li><a href="module-badge-maker_lib_xml.html">badge-maker/lib/xml</a></li><li><a href="module-core_base-service_base.html">core/base-service/base</a></li><li><a href="module-core_base-service_base-graphql.html">core/base-service/base-graphql</a></li><li><a href="module-core_base-service_base-json.html">core/base-service/base-json</a></li><li><a href="module-core_base-service_base-svg-scraping.html">core/base-service/base-svg-scraping</a></li><li><a href="module-core_base-service_base-toml.html">core/base-service/base-toml</a></li><li><a href="module-core_base-service_base-xml.html">core/base-service/base-xml</a></li><li><a href="module-core_base-service_base-yaml.html">core/base-service/base-yaml</a></li><li><a href="module-core_base-service_errors.html">core/base-service/errors</a></li><li><a href="module-core_base-service_graphql.html">core/base-service/graphql</a></li><li><a href="module-core_base-service_openapi.html">core/base-service/openapi</a></li><li><a href="module-core_base-service_resource-cache.html">core/base-service/resource-cache</a></li><li><a href="module-core_base-service_service-definitions.html">core/base-service/service-definitions</a></li><li><a href="module-core_server_server.html">core/server/server</a></li><li><a href="module-core_service-test-runner_create-service-tester.html">core/service-test-runner/create-service-tester</a></li><li><a href="module-core_service-test-runner_icedfrisby-shields.html">core/service-test-runner/icedfrisby-shields</a></li><li><a href="module-core_service-test-runner_runner.html">core/service-test-runner/runner</a></li><li><a href="module-core_service-test-runner_service-tester.html">core/service-test-runner/service-tester</a></li><li><a href="module-core_service-test-runner_services-for-title.html">core/service-test-runner/services-for-title</a></li><li><a href="module-core_token-pooling_token-pool.html">core/token-pooling/token-pool</a></li><li><a href="module-services_build-status.html">services/build-status</a></li><li><a href="module-services_color-formatters.html">services/color-formatters</a></li><li><a href="module-services_contributor-count.html">services/contributor-count</a></li><li><a href="module-services_date.html">services/date</a></li><li><a href="module-services_downloads.html">services/downloads</a></li><li><a href="module-services_dynamic-common.html">services/dynamic-common</a></li><li><a href="module-services_dynamic_json-path.html">services/dynamic/json-path</a></li><li><a href="module-services_endpoint-common.html">services/endpoint-common</a></li><li><a href="module-services_licenses.html">services/licenses</a></li><li><a href="module-services_package-json-helpers.html">services/package-json-helpers</a></li><li><a href="module-services_php-version.html">services/php-version</a></li><li><a href="module-services_pipenv-helpers.html">services/pipenv-helpers</a></li><li><a href="module-services_route-builder.html">services/route-builder</a></li><li><a href="module-services_size.html">services/size</a></li><li><a href="module-services_steam_steam-base.html">services/steam/steam-base</a></li><li><a href="module-services_text-formatters.html">services/text-formatters</a></li><li><a href="module-services_validators.html">services/validators</a></li><li><a href="module-services_version.html">services/version</a></li><li><a href="module-services_website-status.html">services/website-status</a></li><li><a href="module-services_winget_version.html">services/winget/version</a></li></ul><h3>Classes</h3><ul><li><a href="BaseThunderstoreService.html">BaseThunderstoreService</a></li><li><a href="module-badge-maker_lib_xml-ElementList.html">ElementList</a></li><li><a href="module-badge-maker_lib_xml-XmlElement.html">XmlElement</a></li><li><a href="module-core_base-service_base-graphql-BaseGraphqlService.html">BaseGraphqlService</a></li><li><a href="module-core_base-service_base-json-BaseJsonService.html">BaseJsonService</a></li><li><a href="module-core_base-service_base-svg-scraping-BaseSvgScrapingService.html">BaseSvgScrapingService</a></li><li><a href="module-core_base-service_base-toml-BaseTomlService.html">BaseTomlService</a></li><li><a href="module-core_base-service_base-xml-BaseXmlService.html">BaseXmlService</a></li><li><a href="module-core_base-service_base-yaml-BaseYamlService.html">BaseYamlService</a></li><li><a href="module-core_base-service_base-BaseService.html">BaseService</a></li><li><a href="module-core_base-service_errors-Deprecated.html">Deprecated</a></li><li><a href="module-core_base-service_errors-ImproperlyConfigured.html">ImproperlyConfigured</a></li><li><a href="module-core_base-service_errors-Inaccessible.html">Inaccessible</a></li><li><a href="module-core_base-service_errors-InvalidParameter.html">InvalidParameter</a></li><li><a href="module-core_base-service_errors-InvalidResponse.html">InvalidResponse</a></li><li><a href="module-core_base-service_errors-NotFound.html">NotFound</a></li><li><a href="module-core_base-service_errors-ShieldsRuntimeError.html">ShieldsRuntimeError</a></li><li><a href="module-core_server_server-Server.html">Server</a></li><li><a href="module-core_service-test-runner_runner-Runner.html">Runner</a></li><li><a href="module-core_service-test-runner_service-tester-ServiceTester.html">ServiceTester</a></li><li><a href="module-core_token-pooling_token-pool-Token.html">Token</a></li><li><a href="module-core_token-pooling_token-pool-TokenPool.html">TokenPool</a></li><li><a href="module-services_route-builder.html">services/route-builder</a></li><li><a href="module-services_steam_steam-base-BaseSteamAPI.html">BaseSteamAPI</a></li></ul><h3>Tutorials</h3><ul><li><a href="tutorial-TUTORIAL.html">TUTORIAL</a></li><li><a href="tutorial-adding-new-config-values.html">adding-new-config-values</a></li><li><a href="tutorial-authentication.html">authentication</a></li><li><a href="tutorial-badge-urls.html">badge-urls</a></li><li><a href="tutorial-code-walkthrough.html">code-walkthrough</a></li><li><a href="tutorial-deprecating-badges.html">deprecating-badges</a></li><li><a href="tutorial-input-validation.html">input-validation</a></li><li><a href="tutorial-json-format.html">json-format</a></li><li><a href="tutorial-performance-testing.html">performance-testing</a></li><li><a href="tutorial-production-hosting.html">production-hosting</a></li><li><a href="tutorial-releases.html">releases</a></li><li><a href="tutorial-self-hosting.html">self-hosting</a></li><li><a href="tutorial-server-secrets.html">server-secrets</a></li><li><a href="tutorial-service-tests.html">service-tests</a></li><li><a href="tutorial-static-badges.html">static-badges</a></li></ul><h3>Global</h3><ul><li><a href="global.html#createNumRequestCounter">createNumRequestCounter</a></li><li><a href="global.html#fakeJwtToken">fakeJwtToken</a></li><li><a href="global.html#generateFakeConfig">generateFakeConfig</a></li><li><a href="global.html#getBadgeExampleCall">getBadgeExampleCall</a></li><li><a href="global.html#getServiceClassAuthOrigin">getServiceClassAuthOrigin</a></li><li><a href="global.html#isMetricWithPattern">isMetricWithPattern</a></li><li><a href="global.html#testAuth">testAuth</a></li></ul>
|
|
</nav>
|
|
|
|
<br class="clear">
|
|
|
|
<footer>
|
|
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Fri Feb 28 2025 19:23:31 GMT+0000 (Coordinated Universal Time)
|
|
</footer>
|
|
|
|
<script> prettyPrint(); </script>
|
|
<script src="scripts/linenumber.js"> </script>
|
|
</body>
|
|
</html>
|