| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?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\Package;
- use Composer\Package\AliasPackage;
- use Composer\Package\PackageInterface;
- use Composer\Repository\RepositoryInterface;
- use Composer\Util\ErrorHandler;
- use Packagist\WebBundle\Entity\Author;
- use Packagist\WebBundle\Entity\Package;
- use Packagist\WebBundle\Entity\Tag;
- use Packagist\WebBundle\Entity\Version;
- use Symfony\Bridge\Doctrine\RegistryInterface;
- /**
- * @author Jordi Boggiano <j.boggiano@seld.be>
- */
- class Updater
- {
- /**
- * Doctrine
- * @var RegistryInterface
- */
- protected $doctrine;
- /**
- * Supported link types
- * @var array
- */
- protected $supportedLinkTypes = array(
- 'require' => 'RequireLink',
- 'conflict' => 'ConflictLink',
- 'provide' => 'ProvideLink',
- 'replace' => 'ReplaceLink',
- 'recommend' => 'RecommendLink',
- 'suggest' => 'SuggestLink',
- );
- /**
- * Constructor
- *
- * @param RegistryInterface $doctrine
- */
- public function __construct(RegistryInterface $doctrine)
- {
- $this->doctrine = $doctrine;
- ErrorHandler::register();
- }
- /**
- * Update a project
- *
- * @param PackageInterface $package
- * @param boolean $clearExistingVersions
- * @param DateTime $start
- */
- public function update(Package $package, RepositoryInterface $repository, $clearExistingVersions = false, \DateTime $start = null)
- {
- if (null === $start) {
- $start = new \DateTime();
- }
- $pruneDate = clone $start;
- $pruneDate->modify('-8days');
- $versions = $repository->getPackages();
- $em = $this->doctrine->getEntityManager();
- usort($versions, function ($a, $b) {
- return version_compare($a->getVersion(), $b->getVersion());
- });
- $versionRepository = $this->doctrine->getRepository('PackagistWebBundle:Version');
- if ($clearExistingVersions) {
- foreach ($package->getVersions() as $version) {
- $versionRepository->remove($version);
- }
- $em->flush();
- $em->refresh($package);
- }
- foreach ($versions as $version) {
- if ($version instanceof AliasPackage) {
- continue;
- }
- $this->updateInformation($package, $version);
- $em->flush();
- }
- // remove outdated -dev versions
- foreach ($package->getVersions() as $version) {
- if ($version->getDevelopment() && $version->getUpdatedAt() < $pruneDate) {
- $versionRepository->remove($version);
- }
- }
- $package->setUpdatedAt(new \DateTime);
- $package->setCrawledAt(new \DateTime);
- $em->flush();
- }
- private function updateInformation(Package $package, PackageInterface $data)
- {
- $em = $this->doctrine->getEntityManager();
- $version = new Version();
- $version->setName($package->getName());
- $version->setNormalizedVersion($data->getVersion());
- // check if we have that version yet
- foreach ($package->getVersions() as $existingVersion) {
- if ($existingVersion->equals($version)) {
- // avoid updating newer versions, in case two branches have the same version in their composer.json
- if ($existingVersion->getReleasedAt() > $data->getReleaseDate()) {
- return;
- }
- if ($existingVersion->getDevelopment()) {
- $version = $existingVersion;
- break;
- }
- return;
- }
- }
- $version->setVersion($data->getPrettyVersion());
- $version->setDevelopment($data->isDev());
- $em->persist($version);
- $version->setDescription($data->getDescription());
- $package->setDescription($data->getDescription());
- $version->setHomepage($data->getHomepage());
- $version->setLicense($data->getLicense() ?: array());
- $version->setPackage($package);
- $version->setUpdatedAt(new \DateTime);
- $version->setReleasedAt($data->getReleaseDate());
- if ($data->getSourceType()) {
- $source['type'] = $data->getSourceType();
- $source['url'] = $data->getSourceUrl();
- $source['reference'] = $data->getSourceReference();
- $version->setSource($source);
- }
- if ($data->getDistType()) {
- $dist['type'] = $data->getDistType();
- $dist['url'] = $data->getDistUrl();
- $dist['reference'] = $data->getDistReference();
- $dist['shasum'] = $data->getDistSha1Checksum();
- $version->setDist($dist);
- }
- if ($data->getType()) {
- $version->setType($data->getType());
- if ($data->getType() && $data->getType() !== $package->getType()) {
- $package->setType($data->getType());
- }
- }
- $version->setTargetDir($data->getTargetDir());
- $version->setAutoload($data->getAutoload());
- $version->setExtra($data->getExtra());
- $version->setBinaries($data->getBinaries());
- $version->getTags()->clear();
- if ($data->getKeywords()) {
- foreach ($data->getKeywords() as $keyword) {
- $version->addTag(Tag::getByName($em, $keyword, true));
- }
- }
- $authorRepository = $this->doctrine->getRepository('PackagistWebBundle:Author');
- $version->getAuthors()->clear();
- if ($data->getAuthors()) {
- foreach ($data->getAuthors() as $authorData) {
- $author = null;
- // skip authors with no information
- if (empty($authorData['email']) && empty($authorData['name'])) {
- continue;
- }
- if (!empty($authorData['email'])) {
- $author = $authorRepository->findOneByEmail($authorData['email']);
- }
- if (!$author && !empty($authorData['homepage'])) {
- $author = $authorRepository->findOneBy(array(
- 'name' => $authorData['name'],
- 'homepage' => $authorData['homepage']
- ));
- }
- if (!$author && !empty($authorData['name'])) {
- $author = $authorRepository->findOneByNameAndPackage($authorData['name'], $package);
- }
- if (!$author) {
- $author = new Author();
- $em->persist($author);
- }
- foreach (array('email', 'name', 'homepage') as $field) {
- if (isset($authorData[$field])) {
- $author->{'set'.$field}($authorData[$field]);
- }
- }
- $author->setUpdatedAt(new \DateTime);
- if (!$version->getAuthors()->contains($author)) {
- $version->addAuthor($author);
- }
- if (!$author->getVersions()->contains($version)) {
- $author->addVersion($version);
- }
- }
- }
- foreach ($this->supportedLinkTypes as $linkType => $linkEntity) {
- $links = array();
- foreach ($data->{'get'.$linkType.'s'}() as $link) {
- $links[$link->getTarget()] = $link->getPrettyConstraint();
- }
- foreach ($version->{'get'.$linkType}() as $link) {
- // clear links that have changed/disappeared (for updates)
- if (!isset($links[$link->getPackageName()]) || $links[$link->getPackageName()] !== $link->getPackageVersion()) {
- $version->{'get'.$linkType}()->removeElement($link);
- $em->remove($link);
- } else {
- // clear those that are already set
- unset($links[$link->getPackageName()]);
- }
- }
- foreach ($links as $linkPackageName => $linkPackageVersion) {
- $class = 'Packagist\WebBundle\Entity\\'.$linkEntity;
- $link = new $class;
- $link->setPackageName($linkPackageName);
- $link->setPackageVersion($linkPackageVersion);
- $version->{'add'.$linkType.'Link'}($link);
- $link->setVersion($version);
- $em->persist($link);
- }
- }
- if (!$package->getVersions()->contains($version)) {
- $package->addVersions($version);
- }
- }
- }
|