Completed
Push — master ( 3d22df...95226d )
by Georges
22s queued 12s
created

Api   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 48
dl 0
loc 140
rs 10
c 3
b 0
f 0
wmc 24

6 Methods

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