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