forked from magento/ece-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagentoVersion.php
149 lines (128 loc) · 3.61 KB
/
MagentoVersion.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\MagentoCloud\Package;
use Composer\Composer;
use Composer\Semver\Comparator;
use Composer\Semver\Semver;
use Magento\MagentoCloud\Config\ConfigException;
use Magento\MagentoCloud\Config\GlobalSection;
/**
* Defines methods for comparing version constraints with base Magento package.
*/
class MagentoVersion
{
public const MIN_VERSION = '2.1.14';
/**
* @var Manager
*/
private $manager;
/**
* @var Comparator
*/
private $comparator;
/**
* @var Semver
*/
private $semver;
/**
* @var GlobalSection
*/
private $globalSection;
/**
* @var string
*/
private $version;
/**
* @var Composer
*/
private $composer;
/**
* @param Manager $manager
* @param Comparator $comparator
* @param Semver $semver
* @param GlobalSection $globalSection
* @param Composer $composer
*/
public function __construct(
Manager $manager,
Comparator $comparator,
Semver $semver,
GlobalSection $globalSection,
Composer $composer
) {
$this->manager = $manager;
$this->comparator = $comparator;
$this->semver = $semver;
$this->globalSection = $globalSection;
$this->composer = $composer;
}
/**
* Extracts application version.
*
* @return string
* @throws UndefinedPackageException
*/
public function getVersion(): string
{
if (null !== $this->version) {
return $this->version;
}
try {
if ($this->globalSection->get(GlobalSection::VAR_DEPLOYED_MAGENTO_VERSION_FROM_GIT)) {
return $this->version = $this->globalSection->get(GlobalSection::VAR_DEPLOYED_MAGENTO_VERSION_FROM_GIT);
}
} catch (ConfigException $exception) {
throw new UndefinedPackageException($exception->getMessage(), $exception->getCode(), $exception);
}
if ($this->manager->has('magento/magento2-base')) {
return $this->version = $this->manager->get('magento/magento2-base')->getVersion();
}
if ($version = $this->composer->getPackage()->getPrettyVersion()) {
return $this->version = $version;
}
throw new UndefinedPackageException('Magento version cannot be resolved');
}
/**
* Check if Magento is installed from Git.
*
* @return bool
* @throws ConfigException
*/
public function isGitInstallation(): bool
{
if ($this->globalSection->get(GlobalSection::VAR_DEPLOYED_MAGENTO_VERSION_FROM_GIT)) {
return true;
}
if ($this->manager->has('magento/magento2-base')) {
return false;
}
if ($this->composer->getPackage()->getPrettyVersion()) {
return true;
}
throw new ConfigException('Version cannot be determined');
}
/**
* @param string $version
* @return bool
* @throws UndefinedPackageException
*/
public function isGreaterOrEqual(string $version): bool
{
return $this->comparator::compare($this->getVersion(), '>=', $version);
}
/**
* Check the current Magento version against Composer-style constraints.
*
* @param string $constraints
* @return bool
* @throws UndefinedPackageException
*/
public function satisfies(string $constraints): bool
{
return $this->semver::satisfies($this->getVersion(), $constraints);
}
}