1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054 |
- <?php
- /*
- * This file is part of Packagist.
- *
- * (c) Jordi Boggiano <j.boggiano@seld.be>
- * Nils Adermann <naderman@naderman.de>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Packagist\WebBundle\Entity;
- use Composer\Package\Version\VersionParser;
- use Doctrine\Common\Collections\ArrayCollection;
- use Doctrine\ORM\Mapping as ORM;
- use Symfony\Component\Validator\Constraints as Assert;
- /**
- * @ORM\Entity(repositoryClass="Packagist\WebBundle\Entity\VersionRepository")
- * @ORM\Table(
- * name="package_version",
- * uniqueConstraints={@ORM\UniqueConstraint(name="pkg_ver_idx",columns={"package_id","normalizedVersion"})},
- * indexes={
- * @ORM\Index(name="release_idx",columns={"releasedAt"}),
- * @ORM\Index(name="is_devel_idx",columns={"development"})
- * }
- * )
- * @author Jordi Boggiano <j.boggiano@seld.be>
- */
- class Version
- {
- /**
- * @ORM\Id
- * @ORM\Column(type="integer")
- * @ORM\GeneratedValue(strategy="AUTO")
- */
- private $id;
- /**
- * @ORM\Column
- * @Assert\NotBlank()
- */
- private $name;
- /**
- * @ORM\Column(type="text", nullable=true)
- */
- private $description;
- /**
- * @ORM\Column(nullable=true)
- */
- private $type;
- /**
- * @ORM\Column(nullable=true)
- */
- private $targetDir;
- /**
- * @ORM\Column(type="array", nullable=true)
- */
- private $extra = array();
- /**
- * @ORM\ManyToMany(targetEntity="Packagist\WebBundle\Entity\Tag", inversedBy="versions")
- * @ORM\JoinTable(name="version_tag",
- * joinColumns={@ORM\JoinColumn(name="version_id", referencedColumnName="id")},
- * inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
- * )
- */
- private $tags;
- /**
- * @ORM\ManyToOne(targetEntity="Packagist\WebBundle\Entity\Package", fetch="EAGER", inversedBy="versions")
- * @Assert\Type(type="Packagist\WebBundle\Entity\Package")
- */
- private $package;
- /**
- * @ORM\Column(nullable=true)
- * @Assert\Url()
- */
- private $homepage;
- /**
- * @ORM\Column
- * @Assert\NotBlank()
- */
- private $version;
- /**
- * @ORM\Column(length=191)
- * @Assert\NotBlank()
- */
- private $normalizedVersion;
- /**
- * @ORM\Column(type="boolean")
- * @Assert\NotBlank()
- */
- private $development;
- /**
- * @ORM\Column(type="text", nullable=true)
- */
- private $license;
- /**
- * Deprecated relation table, use the authorJson property instead
- *
- * @ORM\ManyToMany(targetEntity="Packagist\WebBundle\Entity\Author", inversedBy="versions")
- * @ORM\JoinTable(name="version_author",
- * joinColumns={@ORM\JoinColumn(name="version_id", referencedColumnName="id")},
- * inverseJoinColumns={@ORM\JoinColumn(name="author_id", referencedColumnName="id")}
- * )
- */
- private $authors;
- /**
- * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\RequireLink", mappedBy="version")
- */
- private $require;
- /**
- * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\ReplaceLink", mappedBy="version")
- */
- private $replace;
- /**
- * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\ConflictLink", mappedBy="version")
- */
- private $conflict;
- /**
- * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\ProvideLink", mappedBy="version")
- */
- private $provide;
- /**
- * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\DevRequireLink", mappedBy="version")
- */
- private $devRequire;
- /**
- * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\SuggestLink", mappedBy="version")
- */
- private $suggest;
- /**
- * @ORM\Column(type="text", nullable=true)
- */
- private $source;
- /**
- * @ORM\Column(type="text", nullable=true)
- */
- private $dist;
- /**
- * @ORM\Column(type="text", nullable=true)
- */
- private $autoload;
- /**
- * @ORM\Column(type="text", nullable=true)
- */
- private $binaries;
- /**
- * @ORM\Column(type="text", nullable=true)
- */
- private $includePaths;
- /**
- * @ORM\Column(type="text", nullable=true)
- */
- private $support;
- /**
- * @ORM\Column(name="authors", type="json", nullable=true)
- */
- private $authorJson;
- /**
- * @ORM\Column(type="datetime")
- */
- private $createdAt;
- /**
- * @ORM\Column(type="datetime", nullable=true)
- */
- private $softDeletedAt;
- /**
- * @ORM\Column(type="datetime")
- */
- private $updatedAt;
- /**
- * @ORM\Column(type="datetime", nullable=true)
- */
- private $releasedAt;
- public function __construct()
- {
- $this->tags = new ArrayCollection();
- $this->require = new ArrayCollection();
- $this->replace = new ArrayCollection();
- $this->conflict = new ArrayCollection();
- $this->provide = new ArrayCollection();
- $this->devRequire = new ArrayCollection();
- $this->suggest = new ArrayCollection();
- $this->authors = new ArrayCollection();
- $this->createdAt = new \DateTime;
- $this->updatedAt = new \DateTime;
- }
- public function toArray(array $versionData, bool $serializeForApi = false)
- {
- if (isset($versionData[$this->id]['tags'])) {
- $tags = $versionData[$this->id]['tags'];
- } else {
- $tags = array();
- foreach ($this->getTags() as $tag) {
- /** @var $tag Tag */
- $tags[] = $tag->getName();
- }
- }
- if (!is_null($this->getAuthorJson())) {
- $authors = $this->getAuthorJson();
- } else {
- if (isset($versionData[$this->id]['authors'])) {
- $authors = $versionData[$this->id]['authors'];
- } else {
- $authors = array();
- foreach ($this->getAuthors() as $author) {
- /** @var $author Author */
- $authors[] = $author->toArray();
- }
- }
- }
- foreach ($authors as &$author) {
- uksort($author, [$this, 'sortAuthorKeys']);
- }
- unset($author);
- $data = array(
- 'name' => $this->getName(),
- 'description' => (string) $this->getDescription(),
- 'keywords' => $tags,
- 'homepage' => (string) $this->getHomepage(),
- 'version' => $this->getVersion(),
- 'version_normalized' => $this->getNormalizedVersion(),
- 'license' => $this->getLicense(),
- 'authors' => $authors,
- 'source' => $this->getSource(),
- 'dist' => $this->getDist(),
- 'type' => $this->getType(),
- );
- if ($serializeForApi && $this->getSupport()) {
- $data['support'] = $this->getSupport();
- }
- if ($this->getReleasedAt()) {
- $data['time'] = $this->getReleasedAt()->format('Y-m-d\TH:i:sP');
- }
- if ($this->getAutoload()) {
- $data['autoload'] = $this->getAutoload();
- }
- if ($this->getExtra()) {
- $data['extra'] = $this->getExtra();
- }
- if ($this->getTargetDir()) {
- $data['target-dir'] = $this->getTargetDir();
- }
- if ($this->getIncludePaths()) {
- $data['include-path'] = $this->getIncludePaths();
- }
- if ($this->getBinaries()) {
- $data['bin'] = $this->getBinaries();
- }
- $supportedLinkTypes = array(
- 'require' => 'require',
- 'devRequire' => 'require-dev',
- 'suggest' => 'suggest',
- 'conflict' => 'conflict',
- 'provide' => 'provide',
- 'replace' => 'replace',
- );
- foreach ($supportedLinkTypes as $method => $linkType) {
- if (isset($versionData[$this->id][$method])) {
- foreach ($versionData[$this->id][$method] as $link) {
- $data[$linkType][$link['name']] = $link['version'];
- }
- continue;
- }
- foreach ($this->{'get'.$method}() as $link) {
- $link = $link->toArray();
- $data[$linkType][key($link)] = current($link);
- }
- }
- if ($this->getPackage()->isAbandoned()) {
- $data['abandoned'] = $this->getPackage()->getReplacementPackage() ?: true;
- }
- return $data;
- }
- public function toV2Array(array $versionData)
- {
- $array = $this->toArray($versionData);
- if ($this->getSupport()) {
- $array['support'] = $this->getSupport();
- ksort($array['support']);
- }
- return $array;
- }
- public function equals(Version $version)
- {
- return strtolower($version->getName()) === strtolower($this->getName())
- && strtolower($version->getNormalizedVersion()) === strtolower($this->getNormalizedVersion());
- }
- /**
- * Get id
- *
- * @return string $id
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * Set name
- *
- * @param string $name
- */
- public function setName($name)
- {
- $this->name = $name;
- }
- /**
- * Get name
- *
- * @return string $name
- */
- public function getName()
- {
- return $this->name;
- }
- public function getNames(array $versionData = null)
- {
- $names = array(
- strtolower($this->name) => true
- );
- if (isset($versionData[$this->id])) {
- foreach (($versionData[$this->id]['replace'] ?? []) as $link) {
- $names[strtolower($link['name'])] = true;
- }
- foreach (($versionData[$this->id]['provide'] ?? []) as $link) {
- $names[strtolower($link['name'])] = true;
- }
- } else {
- foreach ($this->getReplace() as $link) {
- $names[strtolower($link->getPackageName())] = true;
- }
- foreach ($this->getProvide() as $link) {
- $names[strtolower($link->getPackageName())] = true;
- }
- }
- return array_keys($names);
- }
- /**
- * Set description
- *
- * @param string $description
- */
- public function setDescription($description)
- {
- $this->description = $description;
- }
- /**
- * Get description
- *
- * @return string $description
- */
- public function getDescription()
- {
- return $this->description;
- }
- /**
- * Set homepage
- *
- * @param string $homepage
- */
- public function setHomepage($homepage)
- {
- $this->homepage = $homepage;
- }
- /**
- * Get homepage
- *
- * @return string $homepage
- */
- public function getHomepage()
- {
- return $this->homepage;
- }
- /**
- * Set version
- *
- * @param string $version
- */
- public function setVersion($version)
- {
- $this->version = $version;
- }
- /**
- * Get version
- *
- * @return string $version
- */
- public function getVersion()
- {
- return $this->version;
- }
- /**
- * @return string
- */
- public function getRequireVersion()
- {
- return preg_replace('{^v(\d)}', '$1', str_replace('.x-dev', '.*@dev', $this->getVersion()));
- }
- /**
- * Set normalizedVersion
- *
- * @param string $normalizedVersion
- */
- public function setNormalizedVersion($normalizedVersion)
- {
- $this->normalizedVersion = $normalizedVersion;
- }
- /**
- * Get normalizedVersion
- *
- * @return string $normalizedVersion
- */
- public function getNormalizedVersion()
- {
- return $this->normalizedVersion;
- }
- /**
- * Set license
- *
- * @param array $license
- */
- public function setLicense(array $license)
- {
- $this->license = json_encode($license);
- }
- /**
- * Get license
- *
- * @return array $license
- */
- public function getLicense()
- {
- return json_decode($this->license, true);
- }
- /**
- * Set source
- *
- * @param array $source
- */
- public function setSource($source)
- {
- $this->source = null === $source ? $source : json_encode($source);
- }
- /**
- * Get source
- *
- * @return array|null
- */
- public function getSource()
- {
- return json_decode($this->source, true);
- }
- /**
- * Set dist
- *
- * @param array $dist
- */
- public function setDist($dist)
- {
- $this->dist = null === $dist ? $dist : json_encode($dist);
- }
- /**
- * Get dist
- *
- * @return array|null
- */
- public function getDist()
- {
- return json_decode($this->dist, true);
- }
- /**
- * Set autoload
- *
- * @param array $autoload
- */
- public function setAutoload($autoload)
- {
- $this->autoload = json_encode($autoload);
- }
- /**
- * Get autoload
- *
- * @return array|null
- */
- public function getAutoload()
- {
- return json_decode($this->autoload, true);
- }
- /**
- * Set binaries
- *
- * @param array $binaries
- */
- public function setBinaries($binaries)
- {
- $this->binaries = null === $binaries ? $binaries : json_encode($binaries);
- }
- /**
- * Get binaries
- *
- * @return array|null
- */
- public function getBinaries()
- {
- return json_decode($this->binaries, true);
- }
- /**
- * Set include paths.
- *
- * @param array $paths
- */
- public function setIncludePaths($paths)
- {
- $this->includePaths = $paths ? json_encode($paths) : null;
- }
- /**
- * Get include paths.
- *
- * @return array|null
- */
- public function getIncludePaths()
- {
- return json_decode($this->includePaths, true);
- }
- /**
- * Set support
- *
- * @param array $support
- */
- public function setSupport($support)
- {
- $this->support = $support ? json_encode($support) : null;
- }
- /**
- * Get support
- *
- * @return array|null
- */
- public function getSupport()
- {
- return json_decode($this->support, true);
- }
- /**
- * Set createdAt
- *
- * @param \DateTime $createdAt
- */
- public function setCreatedAt($createdAt)
- {
- $this->createdAt = $createdAt;
- }
- /**
- * Get createdAt
- *
- * @return \DateTime
- */
- public function getCreatedAt()
- {
- return $this->createdAt;
- }
- /**
- * Set releasedAt
- *
- * @param \DateTime $releasedAt
- */
- public function setReleasedAt($releasedAt)
- {
- $this->releasedAt = $releasedAt;
- }
- /**
- * Get releasedAt
- *
- * @return \DateTime
- */
- public function getReleasedAt()
- {
- return $this->releasedAt;
- }
- /**
- * Set package
- *
- * @param Package $package
- */
- public function setPackage(Package $package)
- {
- $this->package = $package;
- }
- /**
- * Get package
- *
- * @return Package
- */
- public function getPackage()
- {
- return $this->package;
- }
- /**
- * Get tags
- *
- * @return Tag[]
- */
- public function getTags()
- {
- return $this->tags;
- }
- /**
- * Set updatedAt
- *
- * @param \DateTime $updatedAt
- */
- public function setUpdatedAt($updatedAt)
- {
- $this->updatedAt = $updatedAt;
- }
- /**
- * Get updatedAt
- *
- * @return \DateTime $updatedAt
- */
- public function getUpdatedAt()
- {
- return $this->updatedAt;
- }
- /**
- * Set softDeletedAt
- *
- * @param \DateTime|null $softDeletedAt
- */
- public function setSoftDeletedAt($softDeletedAt)
- {
- $this->softDeletedAt = $softDeletedAt;
- }
- /**
- * Get softDeletedAt
- *
- * @return \DateTime|null $softDeletedAt
- */
- public function getSoftDeletedAt()
- {
- return $this->softDeletedAt;
- }
- /**
- * Get authors
- *
- * @return Author[]
- */
- public function getAuthors()
- {
- return $this->authors;
- }
- public function getAuthorJson(): ?array
- {
- return $this->authorJson;
- }
- public function setAuthorJson(?array $authors): void
- {
- $this->authorJson = $authors ?: [];
- }
- /**
- * Get authors
- *
- * @return array[]
- */
- public function getAuthorData(): array
- {
- if (!is_null($this->getAuthorJson())) {
- return $this->getAuthorJson();
- }
- $authors = [];
- foreach ($this->getAuthors() as $author) {
- $authors[] = array_filter([
- 'name' => $author->getName(),
- 'homepage' => $author->getHomepage(),
- 'email' => $author->getEmail(),
- 'role' => $author->getRole(),
- ]);
- }
- return $authors;
- }
- /**
- * Set type
- *
- * @param string $type
- */
- public function setType($type)
- {
- $this->type = $type;
- }
- /**
- * Get type
- *
- * @return string
- */
- public function getType()
- {
- return $this->type;
- }
- /**
- * Set targetDir
- *
- * @param string $targetDir
- */
- public function setTargetDir($targetDir)
- {
- $this->targetDir = $targetDir;
- }
- /**
- * Get targetDir
- *
- * @return string
- */
- public function getTargetDir()
- {
- return $this->targetDir;
- }
- /**
- * Set extra
- *
- * @param array $extra
- */
- public function setExtra($extra)
- {
- $this->extra = $extra;
- }
- /**
- * Get extra
- *
- * @return array
- */
- public function getExtra()
- {
- return $this->extra;
- }
- /**
- * Set development
- *
- * @param Boolean $development
- */
- public function setDevelopment($development)
- {
- $this->development = $development;
- }
- /**
- * Get development
- *
- * @return Boolean
- */
- public function getDevelopment()
- {
- return $this->development;
- }
- /**
- * @return Boolean
- */
- public function isDevelopment()
- {
- return $this->getDevelopment();
- }
- /**
- * Add tag
- *
- * @param Tag $tag
- */
- public function addTag(Tag $tag)
- {
- $this->tags[] = $tag;
- }
- /**
- * Add authors
- *
- * @param Author $author
- */
- public function addAuthor(Author $author)
- {
- $this->authors[] = $author;
- }
- /**
- * Add require
- *
- * @param RequireLink $require
- */
- public function addRequireLink(RequireLink $require)
- {
- $this->require[] = $require;
- }
- /**
- * Get require
- *
- * @return RequireLink[]
- */
- public function getRequire()
- {
- return $this->require;
- }
- /**
- * Add replace
- *
- * @param ReplaceLink $replace
- */
- public function addReplaceLink(ReplaceLink $replace)
- {
- $this->replace[] = $replace;
- }
- /**
- * Get replace
- *
- * @return ReplaceLink[]
- */
- public function getReplace()
- {
- return $this->replace;
- }
- /**
- * Add conflict
- *
- * @param ConflictLink $conflict
- */
- public function addConflictLink(ConflictLink $conflict)
- {
- $this->conflict[] = $conflict;
- }
- /**
- * Get conflict
- *
- * @return ConflictLink[]
- */
- public function getConflict()
- {
- return $this->conflict;
- }
- /**
- * Add provide
- *
- * @param ProvideLink $provide
- */
- public function addProvideLink(ProvideLink $provide)
- {
- $this->provide[] = $provide;
- }
- /**
- * Get provide
- *
- * @return ProvideLink[]
- */
- public function getProvide()
- {
- return $this->provide;
- }
- /**
- * Add devRequire
- *
- * @param DevRequireLink $devRequire
- */
- public function addDevRequireLink(DevRequireLink $devRequire)
- {
- $this->devRequire[] = $devRequire;
- }
- /**
- * Get devRequire
- *
- * @return DevRequireLink[]
- */
- public function getDevRequire()
- {
- return $this->devRequire;
- }
- /**
- * Add suggest
- *
- * @param SuggestLink $suggest
- */
- public function addSuggestLink(SuggestLink $suggest)
- {
- $this->suggest[] = $suggest;
- }
- /**
- * Get suggest
- *
- * @return SuggestLink[]
- */
- public function getSuggest()
- {
- return $this->suggest;
- }
- /**
- * @return Boolean
- */
- public function hasVersionAlias()
- {
- return $this->getDevelopment() && $this->getVersionAlias();
- }
- /**
- * @return string
- */
- public function getVersionAlias()
- {
- $extra = $this->getExtra();
- if (isset($extra['branch-alias'][$this->getVersion()])) {
- $parser = new VersionParser;
- $version = $parser->normalizeBranch(str_replace('-dev', '', $extra['branch-alias'][$this->getVersion()]));
- return preg_replace('{(\.9{7})+}', '.x', $version);
- }
- return '';
- }
- /**
- * @return string
- */
- public function getRequireVersionAlias()
- {
- return str_replace('.x-dev', '.*@dev', $this->getVersionAlias());
- }
- public function __toString()
- {
- return $this->name.' '.$this->version.' ('.$this->normalizedVersion.')';
- }
- private function sortAuthorKeys($a, $b)
- {
- static $order = ['name' => 1, 'email' => 2, 'homepage' => 3, 'role' => 4];
- $aIndex = $order[$a] ?? 5;
- $bIndex = $order[$b] ?? 5;
- if ($aIndex === $bIndex) {
- return $a <=> $b;
- }
- return $aIndex <=> $bIndex;
- }
- public function getMajorVersion(): int
- {
- return (int) explode('.', $this->normalizedVersion, 2)[0];
- }
- }
|