-
-
Notifications
You must be signed in to change notification settings - Fork 456
/
Copy pathApi.php
162 lines (145 loc) · 5.41 KB
/
Api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
*
* This file is part of Phpfastcache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
*
* @author Georges.L (Geolim4) <[email protected]>
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
*/
declare(strict_types=1);
namespace Phpfastcache;
use Phpfastcache\Exceptions\PhpfastcacheIOException;
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
use Phpfastcache\Helper\UninstanciableObjectTrait;
/**
* Class Api
* @package Phpfastcache
*/
class Api
{
use UninstanciableObjectTrait;
protected static string $version = '4.3.0';
/**
* This method will return the current
* API version, the API version will be
* updated by following the semantic versioning
* based on changes of:
* - ExtendedCacheItemPoolInterface
* - ExtendedCacheItemInterface
* - AggregatablePoolInterface
* - AggregatorInterface
* - ClusterPoolInterface
* - EventManagerInterface
*
* @see https://semver.org/
* @return string
*/
public static function getVersion(): string
{
return self::$version;
}
/**
* @param bool $fallbackOnChangelog
* @param bool $cacheable
* @return string
* @throws PhpfastcacheLogicException
* @throws PhpfastcacheIOException
*/
public static function getPhpfastcacheVersion(bool $fallbackOnChangelog = true, bool $cacheable = true): string
{
/**
* Cache the version statically to improve
* performances on multiple calls
*/
static $version;
if ($version && $cacheable) {
return $version;
}
if (\function_exists('shell_exec')) {
$command = 'git -C "' . __DIR__ . '" describe --abbrev=0 --tags ' . (DIRECTORY_SEPARATOR === '\\' ? '2>nul' : '2>/dev/null');
$stdout = \shell_exec($command);
if (\is_string($stdout)) {
return trim($stdout);
}
if (!$fallbackOnChangelog) {
throw new PhpfastcacheLogicException('The git command used to retrieve the Phpfastcache version has failed.');
}
}
if (!$fallbackOnChangelog) {
throw new PhpfastcacheLogicException('shell_exec is disabled therefore the Phpfastcache version cannot be retrieved.');
}
$changelogFilename = __DIR__ . '/../../CHANGELOG.md';
if (\file_exists($changelogFilename)) {
$semverRegexp = '/^([\d]+)\.([\d]+)\.([\d]+)(?:-([\dA-Za-z-]+(?:\.[\dA-Za-z-]+)*))?(?:\+[\dA-Za-z-]+)?$/';
$changelog = \explode("\n", self::getPhpfastcacheChangelog());
foreach ($changelog as $line) {
$trimmedLine = \trim($line, " \t\n\r\0\x0B#");
if (\str_starts_with($line, '#') && \preg_match($semverRegexp, $trimmedLine)) {
return $trimmedLine;
}
}
throw new PhpfastcacheLogicException('Unable to retrieve the Phpfastcache version through the CHANGELOG.md as no valid string were found in it.');
}
throw new PhpfastcacheLogicException(
'shell_exec being disabled we attempted to retrieve the Phpfastcache version through the CHANGELOG.md file but it is not readable or has been removed.'
);
}
/**
* Return the Phpfastcache changelog, as a string.
* @return string
* @throws PhpfastcacheLogicException
* @throws PhpfastcacheIOException
*/
public static function getPhpfastcacheChangelog(): string
{
$changelogFilename = __DIR__ . '/../../CHANGELOG.md';
if (\file_exists($changelogFilename)) {
$string = \str_replace(["\r\n", "\r"], "\n", \trim(\file_get_contents($changelogFilename)));
if ($string) {
return $string;
}
throw new PhpfastcacheLogicException('Unable to retrieve the Phpfastcache changelog as it seems to be empty.');
}
throw new PhpfastcacheIOException('The CHANGELOG.md file is not readable or has been removed.');
}
/**
* @param bool $cacheable
* @return string
*/
public static function getPhpfastcacheGitHeadHash(bool $cacheable = true): string
{
static $hash;
if ($hash && $cacheable) {
return $hash;
}
if (\function_exists('shell_exec')) {
$stdout = \shell_exec('git rev-parse --short HEAD ' . (DIRECTORY_SEPARATOR === '\\' ? '2>nul' : '2>/dev/null'));
if (\is_string($stdout)) {
return '#' . \trim($stdout);
}
}
return '';
}
/**
* Return the API changelog, as a string.
* @return string
* @throws PhpfastcacheLogicException
* @throws PhpfastcacheIOException
*/
public static function getChangelog(): string
{
$changelogFilename = __DIR__ . '/../../CHANGELOG_API.md';
if (\file_exists($changelogFilename)) {
$string = \str_replace(["\r\n", "\r"], "\n", \trim(\file_get_contents($changelogFilename)));
if ($string) {
return $string;
}
throw new PhpfastcacheLogicException('Unable to retrieve the Phpfastcache API changelog as it seems to be empty.');
}
throw new PhpfastcacheIOException('The CHANGELOG_API.md file is not readable or has been removed.');
}
}