-
-
Notifications
You must be signed in to change notification settings - Fork 476
/
Package.php
891 lines (758 loc) · 28.8 KB
/
Package.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
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
<?php
/*
* This file is part of Packagist.
*
* (c) Jordi Boggiano <[email protected]>
* Nils Adermann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Entity;
use App\Service\UpdaterWorker;
use App\Validator\TypoSquatters;
use App\Validator\Copyright;
use Composer\Downloader\TransportException;
use Composer\Factory;
use Composer\IO\NullIO;
use Composer\Pcre\Preg;
use Composer\Repository\VcsRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Selectable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Composer\Repository\Vcs\GitHubDriver;
use Composer\Util\HttpDownloader;
use DateTimeInterface;
/**
* @ORM\Entity(repositoryClass="App\Entity\PackageRepository")
* @ORM\Table(
* name="package",
* uniqueConstraints={@ORM\UniqueConstraint(name="package_name_idx", columns={"name"})},
* indexes={
* @ORM\Index(name="indexed_idx",columns={"indexedAt"}),
* @ORM\Index(name="crawled_idx",columns={"crawledAt"}),
* @ORM\Index(name="dumped_idx",columns={"dumpedAt"}),
* @ORM\Index(name="dumped2_idx",columns={"dumpedAtV2"}),
* @ORM\Index(name="repository_idx",columns={"repository"}),
* @ORM\Index(name="remoteid_idx",columns={"remoteId"})
* }
* )
* @Assert\Callback(callback="isPackageUnique", groups={"Create"})
* @Assert\Callback(callback="isVendorWritable", groups={"Create"})
* @Assert\Callback(callback="isRepositoryValid", groups={"Update", "Default"})
* @TypoSquatters(groups={"Create"})
* @Copyright(groups={"Create"})
* @author Jordi Boggiano <[email protected]>
*/
class Package
{
const AUTO_NONE = 0;
const AUTO_MANUAL_HOOK = 1;
const AUTO_GITHUB_HOOK = 2;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private int $id;
/**
* Unique package name
*
* @ORM\Column(length=191)
*/
private string $name = '';
/**
* @ORM\Column(nullable=true)
*/
private string|null $type = null;
/**
* @ORM\Column(type="text", nullable=true)
*/
private string|null $description = null;
/**
* @ORM\Column(type="string", nullable=true)
*/
private string|null $language = null;
/**
* @ORM\Column(type="text", nullable=true)
*/
private string|null $readme = null;
/**
* @ORM\Column(type="integer", nullable=true, name="github_stars")
*/
private int|null $gitHubStars = null;
/**
* @ORM\Column(type="integer", nullable=true, name="github_watches")
*/
private int|null $gitHubWatches = null;
/**
* @ORM\Column(type="integer", nullable=true, name="github_forks")
*/
private int|null $gitHubForks = null;
/**
* @ORM\Column(type="integer", nullable=true, name="github_open_issues")
*/
private int|null $gitHubOpenIssues = null;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Version", mappedBy="package")
* @var Collection<int, Version>&Selectable<int, Version>
*/
private Collection $versions;
/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="packages")
* @ORM\JoinTable(name="maintainers_packages")
* @var Collection<int, User>&Selectable<int, User>
*/
private Collection $maintainers;
/**
* @ORM\Column()
* @Assert\NotBlank(groups={"Update", "Default"})
*/
private string $repository;
/**
* @ORM\Column(type="datetime")
*/
private DateTimeInterface $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private DateTimeInterface|null $updatedAt = null;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private DateTimeInterface|null $crawledAt = null;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private DateTimeInterface|null $indexedAt = null;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private DateTimeInterface|null $dumpedAt = null;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private DateTimeInterface|null $dumpedAtV2 = null;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Download", mappedBy="package")
* @var Collection<int, Download>&Selectable<int, Download>
*/
private Collection $downloads;
/**
* @ORM\Column(type="string", nullable=true)
*/
private string|null $remoteId = null;
/**
* @ORM\Column(type="smallint")
* @var int one of self::AUTO_*
*/
private int $autoUpdated = 0;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
private bool $abandoned = false;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private string|null $replacementPackage = null;
/**
* @ORM\Column(type="boolean", options={"default"=false})
*/
private bool $updateFailureNotified = false;
/**
* If set, the content is the reason for being marked suspicious
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private string|null $suspect = null;
private $entityRepository;
private $router;
/**
* @var true|null|\Composer\Repository\Vcs\VcsDriverInterface
*/
private $vcsDriver = true;
private $vcsDriverError;
/**
* @var array lookup table for versions
*/
private $cachedVersions;
public function __construct()
{
$this->versions = new ArrayCollection();
$this->maintainers = new ArrayCollection();
$this->downloads = new ArrayCollection();
$this->createdAt = new \DateTime;
}
public function toArray(VersionRepository $versionRepo, bool $serializeForApi = false): array
{
$maintainers = [];
foreach ($this->getMaintainers() as $maintainer) {
$maintainers[] = $maintainer->toArray();
}
$versions = [];
$partialVersions = $this->getVersions()->toArray();
while ($partialVersions) {
$versionRepo->getEntityManager()->clear();
$slice = array_splice($partialVersions, 0, 100);
$fullVersions = $versionRepo->refreshVersions($slice);
$versionData = $versionRepo->getVersionData(array_map(function ($v) { return $v->getId(); }, $fullVersions));
$versions = array_merge($versions, $versionRepo->detachToArray($fullVersions, $versionData, $serializeForApi));
}
$data = [
'name' => $this->getName(),
'description' => $this->getDescription(),
'time' => $this->getCreatedAt()->format('c'),
'maintainers' => $maintainers,
'versions' => $versions,
'type' => $this->getType(),
'repository' => $this->getRepository(),
'github_stars' => $this->getGitHubStars(),
'github_watchers' => $this->getGitHubWatches(),
'github_forks' => $this->getGitHubForks(),
'github_open_issues' => $this->getGitHubOpenIssues(),
'language' => $this->getLanguage(),
];
if ($this->isAbandoned()) {
$data['abandoned'] = $this->getReplacementPackage() ?: true;
}
return $data;
}
public function isRepositoryValid(ExecutionContextInterface $context): void
{
// vcs driver was not nulled which means the repository was not set/modified and is still valid
if (true === $this->vcsDriver && '' !== $this->name) {
return;
}
$property = 'repository';
$driver = $this->vcsDriver;
if (!is_object($driver)) {
if (Preg::isMatch('{^http://}', $this->repository)) {
$context->buildViolation('Non-secure HTTP URLs are not supported, make sure you use an HTTPS or SSH URL')
->atPath($property)
->addViolation()
;
} elseif (Preg::isMatch('{https?://.+@}', $this->repository)) {
$context->buildViolation('URLs with user@host are not supported, use a read-only public URL')
->atPath($property)
->addViolation()
;
} elseif (is_string($this->vcsDriverError)) {
$context->buildViolation('Uncaught Exception: '.htmlentities($this->vcsDriverError, ENT_COMPAT, 'utf-8'))
->atPath($property)
->addViolation()
;
} else {
$context->buildViolation('No valid/supported repository was found at the given URL')
->atPath($property)
->addViolation()
;
}
return;
}
try {
$information = $driver->getComposerInformation($driver->getRootIdentifier());
if (empty($information['name']) || !is_string($information['name'])) {
$context->buildViolation('The package name was not found in the composer.json, make sure there is a name present.')
->atPath($property)
->addViolation()
;
return;
}
if (!Preg::isMatch('{^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9]([_.-]?[a-z0-9]+)*$}iD', $information['name'])) {
$context->buildViolation('The package name '.htmlentities($information['name'], ENT_COMPAT, 'utf-8').' is invalid, it should have a vendor name, a forward slash, and a package name. The vendor and package name can be words separated by -, . or _. The complete name should match "[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9]([_.-]?[a-z0-9]+)*".')
->atPath($property)
->addViolation()
;
return;
}
if (
Preg::isMatch('{(free.*watch|watch.*free|(stream|online).*anschauver.*pelicula|ver.*completa|pelicula.*complet|season.*episode.*online|film.*(complet|entier)|(voir|regarder|guarda|assistir).*(film|complet)|full.*movie|online.*(free|tv|full.*hd)|(free|full|gratuit).*stream|movie.*free|free.*(movie|hack)|watch.*movie|watch.*full|generate.*resource|generate.*unlimited|hack.*coin|coin.*(hack|generat)|vbucks|hack.*cheat|hack.*generat|generat.*hack|hack.*unlimited|cheat.*(unlimited|generat)|(mod|cheat|apk).*(hack|cheat|mod)|hack.*(apk|mod|free|gold|gems|diamonds|coin)|putlocker|generat.*free|coins.*generat|(download|telecharg).*album|album.*(download|telecharg)|album.*(free|gratuit)|generat.*coins|unlimited.*coins|(fortnite|pubg|apex.*legend|t[1i]k.*t[o0]k).*(free|gratuit|generat|unlimited|coins|mobile|hack|follow))}i', str_replace(['.', '-'], '', $information['name']))
&& !Preg::isMatch('{^(hexmode|calgamo|liberty_code(_module)?|dvi|thelia|clayfreeman|watchfulli|assaneonline|awema-pl|magemodules?|simplepleb|modullo|modernmt|modina|havefnubb|lucid-modules|cointavia|magento-hackathon|pragmatic-modules)/}', $information['name'])
) {
$context->buildViolation('The package name '.htmlentities($information['name'], ENT_COMPAT, 'utf-8').' is blocked, if you think this is a mistake please get in touch with us.')
->atPath($property)
->addViolation()
;
return;
}
$reservedVendors = ['php'];
$bits = explode('/', strtolower($information['name']));
if (in_array($bits[0], $reservedVendors, true)) {
$context->buildViolation('The vendor name '.htmlentities($bits[0], ENT_COMPAT, 'utf-8').' is reserved, please use another name or reach out to us if you have a legitimate use for it.')
->atPath($property)
->addViolation()
;
return;
}
$reservedNames = ['nul', 'con', 'prn', 'aux', 'com1', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9', 'lpt1', 'lpt2', 'lpt3', 'lpt4', 'lpt5', 'lpt6', 'lpt7', 'lpt8', 'lpt9'];
$bits = explode('/', strtolower($information['name']));
if (in_array($bits[0], $reservedNames, true) || in_array($bits[1], $reservedNames, true)) {
$context->buildViolation('The package name '.htmlentities($information['name'], ENT_COMPAT, 'utf-8').' is reserved, package and vendor names can not match any of: '.implode(', ', $reservedNames).'.')
->atPath($property)
->addViolation()
;
return;
}
if (Preg::isMatch('{\.json$}', $information['name'])) {
$context->buildViolation('The package name '.htmlentities($information['name'], ENT_COMPAT, 'utf-8').' is invalid, package names can not end in .json, consider renaming it or perhaps using a -json suffix instead.')
->atPath($property)
->addViolation()
;
return;
}
if (Preg::isMatch('{[A-Z]}', $information['name'])) {
$suggestName = Preg::replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $information['name']);
$suggestName = strtolower($suggestName);
$context->buildViolation('The package name '.htmlentities($information['name'], ENT_COMPAT, 'utf-8').' is invalid, it should not contain uppercase characters. We suggest using '.$suggestName.' instead.')
->atPath($property)
->addViolation()
;
return;
}
} catch (\Exception $e) {
if ($e instanceof TransportException && $e->getCode() === 404) {
$context->buildViolation('No composer.json was found in the '.$driver->getRootIdentifier().' branch.')
->atPath($property)
->addViolation()
;
return;
}
$context->buildViolation('We had problems parsing your composer.json file, the parser reports: '.htmlentities($e->getMessage(), ENT_COMPAT, 'utf-8'))
->atPath($property)
->addViolation()
;
return;
}
if ('' === $this->name) {
$context->buildViolation('An unexpected error has made our parser fail to find a package name in your repository, if you think this is incorrect please try again')
->atPath($property)
->addViolation()
;
}
}
public function setEntityRepository(PackageRepository $repository): void
{
$this->entityRepository = $repository;
}
public function setRouter(UrlGeneratorInterface $router): void
{
$this->router = $router;
}
public function isPackageUnique(ExecutionContextInterface $context): void
{
try {
if ($this->entityRepository->findOneByName($this->name)) {
$context->buildViolation('A package with the name <a href="'.$this->router->generate('view_package', ['name' => $this->name]).'">'.$this->name.'</a> already exists.')
->atPath('repository')
->addViolation()
;
}
} catch (\Doctrine\ORM\NoResultException $e) {}
}
public function isVendorWritable(ExecutionContextInterface $context): void
{
try {
$vendor = $this->getVendor();
if ($vendor && $this->entityRepository->isVendorTaken($vendor, $this->maintainers->first())) {
$context->buildViolation('The vendor name "'.$vendor.'" was already claimed by someone else on Packagist.org. '
. 'You may ask them to add your package and give you maintainership access. '
. 'If they add you as a maintainer on any package in that vendor namespace, '
. 'you will then be able to add new packages in that namespace. '
. 'The packages already in that vendor namespace can be found at '
. '<a href="'.$this->router->generate('view_vendor', ['vendor' => $vendor]).'">'.$vendor.'</a>.'
. 'If those packages belong to you but were submitted by someone else, you can <a href="mailto:[email protected]">contact us</a> to resolve the issue.')
->atPath('repository')
->addViolation()
;
}
} catch (\Doctrine\ORM\NoResultException $e) {}
}
public function getId(): int
{
return $this->id;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getName(): string
{
if ('' === $this->name) {
throw new \LogicException('This should not be called on an invalid package object which was not initialized with a name yet');
}
return $this->name;
}
/**
* Get vendor prefix
*/
public function getVendor(): string
{
return Preg::replace('{/.*$}', '', $this->name);
}
/**
* Get package name without vendor
*/
public function getPackageName(): string
{
return Preg::replace('{^[^/]*/}', '', $this->name);
}
public function setDescription(string|null $description): void
{
$this->description = $description;
}
public function getDescription(): string|null
{
return $this->description;
}
public function setLanguage(string $language): void
{
$this->language = $language;
}
public function getLanguage(): string|null
{
return $this->language;
}
public function setReadme(string $readme): void
{
$this->readme = $readme;
}
public function getReadme(): string
{
return (string) $this->readme;
}
/**
* Get readme with transformations that should not be done in the stored readme as they might not be valid in the long run
*/
public function getOptimizedReadme(): string
{
if ($this->readme === null) {
return '';
}
return str_replace(['<img src="https://raw.github.com/', '<img src="https://raw.githubusercontent.com/'], '<img src="https://rawcdn.githack.com/', $this->readme);
}
public function setGitHubStars(int|null $val): void
{
$this->gitHubStars = $val;
}
public function getGitHubStars(): int|null
{
return $this->gitHubStars;
}
public function setGitHubWatches(int|null $val): void
{
$this->gitHubWatches = $val;
}
public function getGitHubWatches(): int|null
{
return $this->gitHubWatches;
}
public function setGitHubForks(int|null $val): void
{
$this->gitHubForks = $val;
}
public function getGitHubForks(): int|null
{
return $this->gitHubForks;
}
public function setGitHubOpenIssues(int|null $val): void
{
$this->gitHubOpenIssues = $val;
}
public function getGitHubOpenIssues(): int|null
{
return $this->gitHubOpenIssues;
}
public function setCreatedAt(DateTimeInterface $createdAt): void
{
$this->createdAt = $createdAt;
}
public function getCreatedAt(): DateTimeInterface
{
return $this->createdAt;
}
public function setRepository(string $repoUrl): void
{
$this->vcsDriver = null;
// prevent local filesystem URLs
if (Preg::isMatch('{^(\.|[a-z]:|/)}i', $repoUrl)) {
return;
}
$repoUrl = Preg::replace('{^[email protected]:}i', 'https://github.com/', $repoUrl);
$repoUrl = Preg::replace('{^git://github.com/}i', 'https://github.com/', $repoUrl);
$repoUrl = Preg::replace('{^(https://github.com/.*?)\.git$}i', '$1', $repoUrl);
$repoUrl = Preg::replace('{^[email protected]:}i', 'https://gitlab.com/', $repoUrl);
$repoUrl = Preg::replace('{^(https://gitlab.com/.*?)\.git$}i', '$1', $repoUrl);
$repoUrl = Preg::replace('{^git@+bitbucket.org:}i', 'https://bitbucket.org/', $repoUrl);
$repoUrl = Preg::replace('{^bitbucket.org:}i', 'https://bitbucket.org/', $repoUrl);
$repoUrl = Preg::replace('{^https://[a-z0-9_-]*@bitbucket.org/}i', 'https://bitbucket.org/', $repoUrl);
$repoUrl = Preg::replace('{^(https://bitbucket.org/[^/]+/[^/]+)/src/[^.]+}i', '$1.git', $repoUrl);
// normalize protocol case
$repoUrl = Preg::replaceCallback('{^(https?|git|svn)://}i', fn ($match) => strtolower($match[1]) . '://', $repoUrl);
$this->repository = $repoUrl;
$this->remoteId = null;
// avoid user@host URLs
if (Preg::isMatch('{https?://.+@}', $repoUrl)) {
return;
}
// validate that this is a somewhat valid URL
if (!Preg::isMatch('{^([a-z0-9][^@\s]+@[a-z0-9-_.]+:\S+ | [a-z0-9]+://\S+)$}Dx', $repoUrl)) {
return;
}
try {
$io = new NullIO();
$config = Factory::createConfig();
$io->loadConfiguration($config);
$httpDownloader = new HttpDownloader($io, $config);
$repository = new VcsRepository(['url' => $this->repository], $io, $config, $httpDownloader, null, null, UpdaterWorker::VCS_REPO_DRIVERS);
$driver = $this->vcsDriver = $repository->getDriver();
if (!$driver) {
return;
}
$information = $driver->getComposerInformation($driver->getRootIdentifier());
if (!isset($information['name']) || !is_string($information['name'])) {
return;
}
if ('' === $this->name) {
$this->setName(trim($information['name']));
}
if ($driver instanceof GitHubDriver) {
$this->repository = $driver->getRepositoryUrl();
if ($repoData = $driver->getRepoData()) {
$this->remoteId = parse_url($this->repository, PHP_URL_HOST).'/'.$repoData['id'];
}
}
} catch (\Exception $e) {
$this->vcsDriverError = '['.get_class($e).'] '.$e->getMessage();
}
}
/**
* Get repository
*
* @return string $repository
*/
public function getRepository(): string
{
return $this->repository;
}
/**
* Get a user-browsable version of the repository URL
*
* @return string $repository
*/
public function getBrowsableRepository(): string
{
if (Preg::isMatch('{(://|@)bitbucket.org[:/]}i', $this->repository)) {
return Preg::replace('{^(?:git@|https://|git://)bitbucket.org[:/](.+?)(?:\.git)?$}i', 'https://bitbucket.org/$1', $this->repository);
}
return Preg::replace('{^(git://github.com/|[email protected]:)}', 'https://github.com/', $this->repository);
}
public function addVersion(Version $version): void
{
$this->versions[] = $version;
}
/**
* @return Collection<int, Version>&Selectable<int, Version>
*/
public function getVersions(): Collection
{
return $this->versions;
}
public function getVersion(string $normalizedVersion): Version|null
{
if (null === $this->cachedVersions) {
$this->cachedVersions = [];
foreach ($this->getVersions() as $version) {
$this->cachedVersions[strtolower($version->getNormalizedVersion())] = $version;
}
}
if (isset($this->cachedVersions[strtolower($normalizedVersion)])) {
return $this->cachedVersions[strtolower($normalizedVersion)];
}
return null;
}
public function setUpdatedAt(DateTimeInterface $updatedAt): void
{
$this->updatedAt = $updatedAt;
$this->setUpdateFailureNotified(false);
}
public function getUpdatedAt(): ?DateTimeInterface
{
return $this->updatedAt;
}
public function wasUpdatedInTheLast24Hours(): bool
{
return $this->updatedAt && $this->updatedAt > new \DateTime('-24 hours');
}
public function setCrawledAt(?DateTimeInterface $crawledAt): void
{
$this->crawledAt = $crawledAt;
}
public function getCrawledAt(): ?DateTimeInterface
{
return $this->crawledAt;
}
public function setIndexedAt(?DateTimeInterface $indexedAt): void
{
$this->indexedAt = $indexedAt;
}
public function getIndexedAt(): ?DateTimeInterface
{
return $this->indexedAt;
}
public function setDumpedAt(?DateTimeInterface $dumpedAt): void
{
$this->dumpedAt = $dumpedAt;
}
public function getDumpedAt(): ?DateTimeInterface
{
return $this->dumpedAt;
}
public function setDumpedAtV2(?DateTimeInterface $dumpedAt): void
{
$this->dumpedAtV2 = $dumpedAt;
}
public function getDumpedAtV2(): ?DateTimeInterface
{
return $this->dumpedAtV2;
}
public function addMaintainer(User $maintainer): void
{
$this->maintainers[] = $maintainer;
}
/**
* @return Collection<int, User>&Selectable<int, User>
*/
public function getMaintainers(): Collection
{
return $this->maintainers;
}
public function setType(string $type): void
{
$this->type = $type;
}
public function getType(): string|null
{
return $this->type;
}
public function setRemoteId(string|null $remoteId): void
{
$this->remoteId = $remoteId;
}
public function getRemoteId(): string|null
{
return $this->remoteId;
}
/**
* @param self::AUTO_* $autoUpdated
*/
public function setAutoUpdated(int $autoUpdated): void
{
$this->autoUpdated = $autoUpdated;
}
/**
* @return self::AUTO_*
*/
public function getAutoUpdated(): int
{
assert(in_array($this->autoUpdated, [self::AUTO_NONE, self::AUTO_MANUAL_HOOK, self::AUTO_GITHUB_HOOK], true));
return $this->autoUpdated;
}
/**
* Get autoUpdated
*
* @return Boolean
*/
public function isAutoUpdated(): bool
{
return $this->autoUpdated > 0;
}
/**
* Set updateFailureNotified
*
* @param Boolean $updateFailureNotified
*/
public function setUpdateFailureNotified($updateFailureNotified): void
{
$this->updateFailureNotified = $updateFailureNotified;
}
/**
* Get updateFailureNotified
*
* @return Boolean
*/
public function isUpdateFailureNotified(): bool
{
return $this->updateFailureNotified;
}
public function setSuspect(?string $reason): void
{
$this->suspect = $reason;
}
public function isSuspect(): bool
{
return !is_null($this->suspect);
}
public function getSuspect(): ?string
{
return $this->suspect;
}
/**
* @return boolean
*/
public function isAbandoned(): bool
{
return $this->abandoned;
}
/**
* @param boolean $abandoned
*/
public function setAbandoned($abandoned): void
{
$this->abandoned = $abandoned;
}
public function getReplacementPackage(): ?string
{
return $this->replacementPackage;
}
public function setReplacementPackage(?string $replacementPackage): void
{
$this->replacementPackage = $replacementPackage;
}
public static function sortVersions(Version $a, Version $b): int
{
$aVersion = $a->getNormalizedVersion();
$bVersion = $b->getNormalizedVersion();
// use branch alias for sorting if one is provided
if (isset($a->getExtra()['branch-alias'][$aVersion])) {
$aVersion = Preg::replace('{(.x)?-dev$}', '.9999999-dev', $a->getExtra()['branch-alias'][$aVersion]);
}
if (isset($b->getExtra()['branch-alias'][$bVersion])) {
$bVersion = Preg::replace('{(.x)?-dev$}', '.9999999-dev', $b->getExtra()['branch-alias'][$bVersion]);
}
$aVersion = Preg::replace('{^dev-.*}', '0.0.0-alpha', $aVersion);
$bVersion = Preg::replace('{^dev-.*}', '0.0.0-alpha', $bVersion);
// sort default branch first if it is non numeric
if ($aVersion === '0.0.0-alpha' && $a->isDefaultBranch()) {
return -1;
}
if ($bVersion === '0.0.0-alpha' && $b->isDefaultBranch()) {
return 1;
}
// equal versions are sorted by date
if ($aVersion === $bVersion) {
// make sure sort is stable
if ($a->getReleasedAt() == $b->getReleasedAt()) {
return $a->getNormalizedVersion() <=> $b->getNormalizedVersion();
}
return $b->getReleasedAt() > $a->getReleasedAt() ? 1 : -1;
}
// the rest is sorted by version
return version_compare($bVersion, $aVersion);
}
}