123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092 |
- <?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(type="json", nullable=true)
- */
- private $funding;
- /**
- * @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->getFunding()) {
- $data['funding'] = $this->getFunding();
- }
- 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 Funding
- *
- * @param array $funding
- */
- public function setFunding($funding)
- {
- // sort records when storing so to help the V2 metadata compression algo
- if ($funding) {
- usort($funding, function ($a, $b) {
- $keyA = ($a['type'] ?? '') . ($a['url'] ?? '');
- $keyB = ($b['type'] ?? '') . ($b['url'] ?? '');
-
- return $keyA <=> $keyB;
- });
- }
- $this->funding = $funding;
- }
- /**
- * Get funding
- *
- * @return array|null
- */
- public function getFunding()
- {
- return $this->funding;
- }
- /**
- * 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];
- }
- }
|