示例#1
0
 public function dump(PackageInterface $package)
 {
     $keys = array('binaries' => 'bin', 'type', 'extra', 'installationSource' => 'installation-source', 'autoload', 'notificationUrl' => 'notification-url', 'includePaths' => 'include-path');
     $data = array();
     $data['name'] = $package->getPrettyName();
     $data['version'] = $package->getPrettyVersion();
     $data['version_normalized'] = $package->getVersion();
     if ($package->getTargetDir()) {
         $data['target-dir'] = $package->getTargetDir();
     }
     if ($package->getSourceType()) {
         $data['source']['type'] = $package->getSourceType();
         $data['source']['url'] = $package->getSourceUrl();
         $data['source']['reference'] = $package->getSourceReference();
     }
     if ($package->getDistType()) {
         $data['dist']['type'] = $package->getDistType();
         $data['dist']['url'] = $package->getDistUrl();
         $data['dist']['reference'] = $package->getDistReference();
         $data['dist']['shasum'] = $package->getDistSha1Checksum();
     }
     if ($package->getArchiveExcludes()) {
         $data['archive']['exclude'] = $package->getArchiveExcludes();
     }
     foreach (BasePackage::$supportedLinkTypes as $type => $opts) {
         if ($links = $package->{'get' . ucfirst($opts['method'])}()) {
             foreach ($links as $link) {
                 $data[$type][$link->getTarget()] = $link->getPrettyConstraint();
             }
             ksort($data[$type]);
         }
     }
     if ($packages = $package->getSuggests()) {
         ksort($packages);
         $data['suggest'] = $packages;
     }
     if ($package->getReleaseDate()) {
         $data['time'] = $package->getReleaseDate()->format('Y-m-d H:i:s');
     }
     $data = $this->dumpValues($package, $keys, $data);
     if ($package instanceof CompletePackageInterface) {
         $keys = array('scripts', 'license', 'authors', 'description', 'homepage', 'keywords', 'repositories', 'support');
         $data = $this->dumpValues($package, $keys, $data);
         if (isset($data['keywords']) && is_array($data['keywords'])) {
             sort($data['keywords']);
         }
     }
     if ($package instanceof RootPackageInterface) {
         $minimumStability = $package->getMinimumStability();
         if ($minimumStability) {
             $data['minimum-stability'] = $minimumStability;
         }
     }
     return $data;
 }
示例#2
0
 /**
  * {@inheritDoc}
  */
 public function update(PackageInterface $initial, PackageInterface $target, $path)
 {
     if (!$target->getSourceReference()) {
         throw new \InvalidArgumentException('Package ' . $target->getPrettyName() . ' is missing reference information');
     }
     $name = $target->getName();
     if ($initial->getPrettyVersion() == $target->getPrettyVersion()) {
         if ($target->getSourceType() === 'svn') {
             $from = $initial->getSourceReference();
             $to = $target->getSourceReference();
         } else {
             $from = substr($initial->getSourceReference(), 0, 7);
             $to = substr($target->getSourceReference(), 0, 7);
         }
         $name .= ' ' . $initial->getPrettyVersion();
     } else {
         $from = VersionParser::formatVersion($initial);
         $to = VersionParser::formatVersion($target);
     }
     $this->io->write("  - Updating <info>" . $name . "</info> (<comment>" . $from . "</comment> => <comment>" . $to . "</comment>)");
     $this->cleanChanges($initial, $path, true);
     $urls = $target->getSourceUrls();
     while ($url = array_shift($urls)) {
         try {
             if (Filesystem::isLocalPath($url)) {
                 $url = realpath($url);
             }
             $this->doUpdate($initial, $target, $path, $url);
             break;
         } catch (\Exception $e) {
             if ($this->io->isDebug()) {
                 $this->io->write('Failed: [' . get_class($e) . '] ' . $e->getMessage());
             } elseif (count($urls)) {
                 $this->io->write('    Failed, trying the next URL');
             } else {
                 // in case of failed update, try to reapply the changes before aborting
                 $this->reapplyChanges($path);
                 throw $e;
             }
         }
     }
     $this->reapplyChanges($path);
     // print the commit logs if in verbose mode
     if ($this->io->isVerbose()) {
         $message = 'Pulling in changes:';
         $logs = $this->getCommitLogs($initial->getSourceReference(), $target->getSourceReference(), $path);
         if (!trim($logs)) {
             $message = 'Rolling back changes:';
             $logs = $this->getCommitLogs($target->getSourceReference(), $initial->getSourceReference(), $path);
         }
         if (trim($logs)) {
             $logs = implode("\n", array_map(function ($line) {
                 return '      ' . $line;
             }, explode("\n", $logs)));
             $this->io->write('    ' . $message);
             $this->io->write($logs);
         }
     }
     $this->io->write('');
 }
示例#3
0
 public static function formatVersion(PackageInterface $package, $truncate = true)
 {
     if (!$package->isDev() || !in_array($package->getSourceType(), array('hg', 'git'))) {
         return $package->getPrettyVersion();
     }
     return $package->getPrettyVersion() . ' ' . ($truncate ? substr($package->getSourceReference(), 0, 6) : $package->getSourceReference());
 }
示例#4
0
 public function dump(PackageInterface $package)
 {
     $keys = array('binaries' => 'bin', 'scripts', 'type', 'extra', 'installationSource' => 'installation-source', 'license', 'authors', 'description', 'homepage', 'keywords', 'autoload', 'repositories', 'includePaths' => 'include-path', 'support');
     $data = array();
     $data['name'] = $package->getPrettyName();
     $data['version'] = $package->getPrettyVersion();
     $data['version_normalized'] = $package->getVersion();
     if ($package->getTargetDir()) {
         $data['target-dir'] = $package->getTargetDir();
     }
     if ($package->getReleaseDate()) {
         $data['time'] = $package->getReleaseDate()->format('Y-m-d H:i:s');
     }
     if ($package->getSourceType()) {
         $data['source']['type'] = $package->getSourceType();
         $data['source']['url'] = $package->getSourceUrl();
         $data['source']['reference'] = $package->getSourceReference();
     }
     if ($package->getDistType()) {
         $data['dist']['type'] = $package->getDistType();
         $data['dist']['url'] = $package->getDistUrl();
         $data['dist']['reference'] = $package->getDistReference();
         $data['dist']['shasum'] = $package->getDistSha1Checksum();
     }
     foreach (BasePackage::$supportedLinkTypes as $type => $opts) {
         if ($links = $package->{'get' . ucfirst($opts['method'])}()) {
             foreach ($links as $link) {
                 $data[$type][$link->getTarget()] = $link->getPrettyConstraint();
             }
         }
     }
     if ($packages = $package->getSuggests()) {
         $data['suggest'] = $packages;
     }
     foreach ($keys as $method => $key) {
         if (is_numeric($method)) {
             $method = $key;
         }
         $getter = 'get' . ucfirst($method);
         $value = $package->{$getter}();
         if (null !== $value && !(is_array($value) && 0 === count($value))) {
             $data[$key] = $value;
         }
     }
     return $data;
 }
示例#5
0
 public static function formatVersion(PackageInterface $package, $truncate = true)
 {
     if (!$package->isDev() || !in_array($package->getSourceType(), array('hg', 'git'))) {
         return $package->getPrettyVersion();
     }
     // if source reference is a sha1 hash -- truncate
     if ($truncate && strlen($package->getSourceReference()) === 40) {
         return $package->getPrettyVersion() . ' ' . substr($package->getSourceReference(), 0, 7);
     }
     return $package->getPrettyVersion() . ' ' . $package->getSourceReference();
 }
 public function updateCode(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
 {
     if (!$repo->hasPackage($initial)) {
         throw new \InvalidArgumentException('Package is not installed: ' . $initial);
     }
     if ($initial->getInstallationSource() == 'source' && $initial->getSourceType() == 'git' && $target->getSourceType() == 'git') {
         $this->downloadManager->update($initial, $target, $this->getInstallPath($target));
     } else {
         $this->installCode($target);
     }
     $repo->removePackage($initial);
     if (!$repo->hasPackage($target)) {
         $repo->addPackage(clone $target);
     }
 }
示例#7
0
 public function archive(PackageInterface $package, $format, $targetDir)
 {
     if (empty($format)) {
         throw new \InvalidArgumentException('Format must be specified');
     }
     $usableArchiver = null;
     foreach ($this->archivers as $archiver) {
         if ($archiver->supports($format, $package->getSourceType())) {
             $usableArchiver = $archiver;
             break;
         }
     }
     if (null === $usableArchiver) {
         throw new \RuntimeException(sprintf('No archiver found to support %s format', $format));
     }
     $filesystem = new Filesystem();
     $packageName = $this->getPackageFilename($package);
     $filesystem->ensureDirectoryExists($targetDir);
     $target = realpath($targetDir) . '/' . $packageName . '.' . $format;
     $filesystem->ensureDirectoryExists(dirname($target));
     if (!$this->overwriteFiles && file_exists($target)) {
         return $target;
     }
     if ($package instanceof RootPackageInterface) {
         $sourcePath = realpath('.');
     } else {
         $sourcePath = sys_get_temp_dir() . '/composer_archive' . uniqid();
         $filesystem->ensureDirectoryExists($sourcePath);
         $this->downloadManager->download($package, $sourcePath);
         if (file_exists($composerJsonPath = $sourcePath . '/composer.json')) {
             $jsonFile = new JsonFile($composerJsonPath);
             $jsonData = $jsonFile->read();
             if (!empty($jsonData['archive']['exclude'])) {
                 $package->setArchiveExcludes($jsonData['archive']['exclude']);
             }
         }
     }
     $tempTarget = sys_get_temp_dir() . '/composer_archive' . uniqid() . '.' . $format;
     $filesystem->ensureDirectoryExists(dirname($tempTarget));
     $archivePath = $usableArchiver->archive($sourcePath, $tempTarget, $format, $package->getArchiveExcludes());
     rename($archivePath, $target);
     if (!$package instanceof RootPackageInterface) {
         $filesystem->removeDirectory($sourcePath);
     }
     $filesystem->remove($tempTarget);
     return $target;
 }
示例#8
0
 public function update(PackageInterface $initial, PackageInterface $target, $path)
 {
     if (!$target->getSourceReference()) {
         throw new \InvalidArgumentException('Package ' . $target->getPrettyName() . ' is missing reference information');
     }
     $name = $target->getName();
     if ($initial->getPrettyVersion() == $target->getPrettyVersion()) {
         if ($target->getSourceType() === 'svn') {
             $from = $initial->getSourceReference();
             $to = $target->getSourceReference();
         } else {
             $from = substr($initial->getSourceReference(), 0, 7);
             $to = substr($target->getSourceReference(), 0, 7);
         }
         $name .= ' ' . $initial->getPrettyVersion();
     } else {
         $from = VersionParser::formatVersion($initial);
         $to = VersionParser::formatVersion($target);
     }
     $this->io->write("  - Updating <info>" . $name . "</info> (<comment>" . $from . "</comment> => <comment>" . $to . "</comment>)");
     $this->cleanChanges($initial, $path, true);
     try {
         $this->doUpdate($initial, $target, $path);
     } catch (\Exception $e) {
         $this->reapplyChanges($path);
         throw $e;
     }
     $this->reapplyChanges($path);
     if ($this->io->isVerbose()) {
         $message = 'Pulling in changes:';
         $logs = $this->getCommitLogs($initial->getSourceReference(), $target->getSourceReference(), $path);
         if (!trim($logs)) {
             $message = 'Rolling back changes:';
             $logs = $this->getCommitLogs($target->getSourceReference(), $initial->getSourceReference(), $path);
         }
         if (trim($logs)) {
             $logs = implode("\n", array_map(function ($line) {
                 return '      ' . $line;
             }, explode("\n", $logs)));
             $this->io->write('    ' . $message);
             $this->io->write($logs);
         }
     }
     $this->io->write('');
 }
 /**
  * {@inheritDoc}
  */
 public function update(PackageInterface $initial, PackageInterface $target, $path)
 {
     if (!$target->getSourceReference()) {
         throw new \InvalidArgumentException('Package ' . $target->getPrettyName() . ' is missing reference information');
     }
     $name = $target->getName();
     if ($initial->getPrettyVersion() == $target->getPrettyVersion()) {
         if ($target->getSourceType() === 'svn') {
             $from = $initial->getSourceReference();
             $to = $target->getSourceReference();
         } else {
             $from = substr($initial->getSourceReference(), 0, 7);
             $to = substr($target->getSourceReference(), 0, 7);
         }
         $name .= ' ' . $initial->getPrettyVersion();
     } else {
         $from = $initial->getFullPrettyVersion();
         $to = $target->getFullPrettyVersion();
     }
     $this->io->writeError("  - Updating <info>" . $name . "</info> (<comment>" . $from . "</comment> => <comment>" . $to . "</comment>)");
     $this->cleanChanges($initial, $path, true);
     $urls = $target->getSourceUrls();
     $exception = null;
     while ($url = array_shift($urls)) {
         try {
             if (Filesystem::isLocalPath($url)) {
                 $url = realpath($url);
             }
             $this->doUpdate($initial, $target, $path, $url);
             $exception = null;
             break;
         } catch (\Exception $exception) {
             // rethrow phpunit exceptions to avoid hard to debug bug failures
             if ($exception instanceof \PHPUnit_Framework_Exception) {
                 throw $exception;
             }
             if ($this->io->isDebug()) {
                 $this->io->writeError('Failed: [' . get_class($exception) . '] ' . $exception->getMessage());
             } elseif (count($urls)) {
                 $this->io->writeError('    Failed, trying the next URL');
             }
         }
     }
     $this->reapplyChanges($path);
     // print the commit logs if in verbose mode and VCS metadata is present
     // because in case of missing metadata code would trigger another exception
     if (!$exception && $this->io->isVerbose() && $this->hasMetadataRepository($path)) {
         $message = 'Pulling in changes:';
         $logs = $this->getCommitLogs($initial->getSourceReference(), $target->getSourceReference(), $path);
         if (!trim($logs)) {
             $message = 'Rolling back changes:';
             $logs = $this->getCommitLogs($target->getSourceReference(), $initial->getSourceReference(), $path);
         }
         if (trim($logs)) {
             $logs = implode("\n", array_map(function ($line) {
                 return '      ' . $line;
             }, explode("\n", $logs)));
             // escape angle brackets for proper output in the console
             $logs = str_replace('<', '\\<', $logs);
             $this->io->writeError('    ' . $message);
             $this->io->writeError($logs);
         }
     }
     if (!$urls && $exception) {
         throw $exception;
     }
     $this->io->writeError('');
 }
 /**
  * brings locally cached repository up to date if it is missing the requested tag
  *
  * @param PackageInterface $package
  * @param string $targetFolder
  */
 protected function checkRepository($package, $targetFolder)
 {
     if ($package->getSourceType() == 'git') {
         $command = sprintf('cd %s && git rev-parse refs/tags/%s', escapeshellarg($this->normalizePath($targetFolder)), escapeshellarg($package->getSourceReference()));
         $existingTags = shell_exec($command);
         if (!$existingTags) {
             $command = sprintf('cd %s && git fetch', escapeshellarg($this->normalizePath($targetFolder)));
             shell_exec($command);
         }
     } elseif ($package->getSourceType() == 'hg') {
         $command = sprintf('cd %s && hg log --template "{tags}" -r %s', escapeshellarg($targetFolder), escapeshellarg($package->getSourceReference()));
         $existingTag = shell_exec($command);
         if ($existingTag === $package->getSourceReference()) {
             $command = sprintf('cd %s && hg pull', escapeshellarg($targetFolder));
             shell_exec($command);
         }
     }
 }
示例#11
0
 private function updateInformation(Package $package, PackageInterface $data, $flags)
 {
     $em = $this->doctrine->getManager();
     $version = new Version();
     $normVersion = $data->getVersion();
     $existingVersion = $package->getVersion($normVersion);
     if ($existingVersion) {
         $source = $existingVersion->getSource();
         // update if the right flag is set, or the source reference has changed (re-tag or new commit on branch)
         if ($source['reference'] !== $data->getSourceReference() || $flags & self::UPDATE_EQUAL_REFS) {
             $version = $existingVersion;
         } else {
             // mark it updated to avoid it being pruned
             $existingVersion->setUpdatedAt(new \DateTime());
             return false;
         }
     }
     $version->setName($package->getName());
     $version->setVersion($data->getPrettyVersion());
     $version->setNormalizedVersion($normVersion);
     $version->setDevelopment($data->isDev());
     $em->persist($version);
     $descr = $this->sanitize($data->getDescription());
     $version->setDescription($descr);
     $package->setDescription($descr);
     $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);
     } else {
         $version->setSource(null);
     }
     if ($data->getDistType()) {
         $dist['type'] = $data->getDistType();
         $dist['url'] = $data->getDistUrl();
         $dist['reference'] = $data->getDistReference();
         $dist['shasum'] = $data->getDistSha1Checksum();
         $version->setDist($dist);
     } else {
         $version->setDist(null);
     }
     if ($data->getType()) {
         $type = $this->sanitize($data->getType());
         $version->setType($type);
         if ($type !== $package->getType()) {
             $package->setType($type);
         }
     }
     $version->setTargetDir($data->getTargetDir());
     $version->setAutoload($data->getAutoload());
     $version->setExtra($data->getExtra());
     $version->setBinaries($data->getBinaries());
     $version->setIncludePaths($data->getIncludePaths());
     $version->setSupport($data->getSupport());
     if ($data->getKeywords()) {
         $keywords = array();
         foreach ($data->getKeywords() as $keyword) {
             $keywords[mb_strtolower($keyword, 'UTF-8')] = $keyword;
         }
         $existingTags = [];
         foreach ($version->getTags() as $tag) {
             $existingTags[mb_strtolower($tag->getName(), 'UTF-8')] = $tag;
         }
         foreach ($keywords as $tagKey => $keyword) {
             if (isset($existingTags[$tagKey])) {
                 unset($existingTags[$tagKey]);
                 continue;
             }
             $tag = Tag::getByName($em, $keyword, true);
             if (!$version->getTags()->contains($tag)) {
                 $version->addTag($tag);
             }
         }
         foreach ($existingTags as $tag) {
             $version->getTags()->removeElement($tag);
         }
     } elseif (count($version->getTags())) {
         $version->getTags()->clear();
     }
     $authorRepository = $this->doctrine->getRepository('PackagistWebBundle:Author');
     $version->getAuthors()->clear();
     if ($data->getAuthors()) {
         foreach ($data->getAuthors() as $authorData) {
             $author = null;
             foreach (array('email', 'name', 'homepage', 'role') as $field) {
                 if (isset($authorData[$field])) {
                     $authorData[$field] = trim($authorData[$field]);
                     if ('' === $authorData[$field]) {
                         $authorData[$field] = null;
                     }
                 } else {
                     $authorData[$field] = null;
                 }
             }
             // skip authors with no information
             if (!isset($authorData['email']) && !isset($authorData['name'])) {
                 continue;
             }
             $author = $authorRepository->findOneBy(array('email' => $authorData['email'], 'name' => $authorData['name'], 'homepage' => $authorData['homepage'], 'role' => $authorData['role']));
             if (!$author) {
                 $author = new Author();
                 $em->persist($author);
             }
             foreach (array('email', 'name', 'homepage', 'role') as $field) {
                 if (isset($authorData[$field])) {
                     $author->{'set' . $field}($authorData[$field]);
                 }
             }
             // only update the author timestamp once a month at most as the value is kinda unused
             if ($author->getUpdatedAt() === null || $author->getUpdatedAt()->getTimestamp() < time() - 86400 * 30) {
                 $author->setUpdatedAt(new \DateTime());
             }
             if (!$version->getAuthors()->contains($author)) {
                 $version->addAuthor($author);
             }
             if (!$author->getVersions()->contains($version)) {
                 $author->addVersion($version);
             }
         }
     }
     // handle links
     foreach ($this->supportedLinkTypes as $linkType => $opts) {
         $links = array();
         foreach ($data->{$opts['method']}() as $link) {
             $constraint = $link->getPrettyConstraint();
             if (false !== strpos($constraint, ',') && false !== strpos($constraint, '@')) {
                 $constraint = preg_replace_callback('{([><]=?\\s*[^@]+?)@([a-z]+)}i', function ($matches) {
                     if ($matches[2] === 'stable') {
                         return $matches[1];
                     }
                     return $matches[1] . '-' . $matches[2];
                 }, $constraint);
             }
             $links[$link->getTarget()] = $constraint;
         }
         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\\' . $opts['entity'];
             $link = new $class();
             $link->setPackageName($linkPackageName);
             $link->setPackageVersion($linkPackageVersion);
             $version->{'add' . $linkType . 'Link'}($link);
             $link->setVersion($version);
             $em->persist($link);
         }
     }
     // handle suggests
     if ($suggests = $data->getSuggests()) {
         foreach ($version->getSuggest() as $link) {
             // clear links that have changed/disappeared (for updates)
             if (!isset($suggests[$link->getPackageName()]) || $suggests[$link->getPackageName()] !== $link->getPackageVersion()) {
                 $version->getSuggest()->removeElement($link);
                 $em->remove($link);
             } else {
                 // clear those that are already set
                 unset($suggests[$link->getPackageName()]);
             }
         }
         foreach ($suggests as $linkPackageName => $linkPackageVersion) {
             $link = new SuggestLink();
             $link->setPackageName($linkPackageName);
             $link->setPackageVersion($linkPackageVersion);
             $version->addSuggestLink($link);
             $link->setVersion($version);
             $em->persist($link);
         }
     } elseif (count($version->getSuggest())) {
         // clear existing suggests if present
         foreach ($version->getSuggest() as $link) {
             $em->remove($link);
         }
         $version->getSuggest()->clear();
     }
     if (!$package->getVersions()->contains($version)) {
         $package->addVersions($version);
     }
     return true;
 }
示例#12
0
 /**
  * {@inheritdoc}
  */
 public function getSourceType()
 {
     return $this->package->getSourceType();
 }
示例#13
0
 /**
  * Updates package from initial to target version.
  *
  * @param PackageInterface $initial   initial package version
  * @param PackageInterface $target    target package version
  * @param string           $targetDir target dir
  *
  * @throws \InvalidArgumentException if initial package is not installed
  */
 public function update(PackageInterface $initial, PackageInterface $target, $targetDir)
 {
     $downloader = $this->getDownloaderForInstalledPackage($initial);
     if (!$downloader) {
         return;
     }
     $installationSource = $initial->getInstallationSource();
     if ('dist' === $installationSource) {
         $initialType = $initial->getDistType();
         $targetType = $target->getDistType();
     } else {
         $initialType = $initial->getSourceType();
         $targetType = $target->getSourceType();
     }
     // upgrading from a dist stable package to a dev package, force source reinstall
     if ($target->isDev() && 'dist' === $installationSource) {
         $downloader->remove($initial, $targetDir);
         $this->download($target, $targetDir);
         return;
     }
     if ($initialType === $targetType) {
         $target->setInstallationSource($installationSource);
         try {
             $downloader->update($initial, $target, $targetDir);
             return;
         } catch (\RuntimeException $e) {
             if (!$this->io->isInteractive()) {
                 throw $e;
             }
             $this->io->writeError('<error>    Update failed (' . $e->getMessage() . ')</error>');
             if (!$this->io->askConfirmation('    Would you like to try reinstalling the package instead [<comment>yes</comment>]? ', true)) {
                 throw $e;
             }
         }
     }
     $downloader->remove($initial, $targetDir);
     $this->download($target, $targetDir, 'source' === $installationSource);
 }
 private function updateInformation(OutputInterface $output, RegistryInterface $doctrine, $package, PackageInterface $data)
 {
     $em = $doctrine->getEntityManager();
     $version = new Version();
     $version->setName($package->getName());
     $version->setNormalizedVersion(preg_replace('{-dev$}i', '', $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(substr($data->getVersion(), -4) === '-dev');
     $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));
         }
     }
     $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 = $doctrine->getRepository('PackagistWebBundle:Author')->findOneByEmail($authorData['email']);
             }
             if (!$author && !empty($authorData['homepage'])) {
                 $author = $doctrine->getRepository('PackagistWebBundle:Author')->findOneBy(array('name' => $authorData['name'], 'homepage' => $authorData['homepage']));
             }
             if (!$author && !empty($authorData['name'])) {
                 $author = $doctrine->getRepository('PackagistWebBundle:Author')->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);
     }
 }
示例#15
0
 public function getSourceType()
 {
     return $this->aliasOf->getSourceType();
 }
示例#16
0
文件: Installer.php 项目: cocona/core
 /**
  * @param PackageInterface $package
  * @return bool
  */
 protected function save(PackageInterface $package)
 {
     $name = $package->getPrettyName();
     /** @var Cocona $cocona */
     $cocona = self::$app['cocona'];
     if ($cocona->isInstalled()) {
         $model = Package::firstOrNew(['name' => $name]);
         $model->version = $package->getPrettyVersion();
         $model->source_type = $package->getSourceType();
         $model->source_url = $package->getSourceUrl();
         return $model->save();
     }
     /** @var Repository $cache */
     $cache = self::$app['cache'];
     $key = 'cocona.packages.queued';
     /** @var Collection $packages */
     $packages = $cache->rememberForever($key, function () {
         return new Collection();
     });
     $packages->put($name, $package);
     $cache->forever($key, $packages);
     return true;
 }
示例#17
0
 /**
  * Updates package from initial to target version.
  *
  * @param PackageInterface $initial   initial package version
  * @param PackageInterface $target    target package version
  * @param string           $targetDir target dir
  *
  * @throws InvalidArgumentException if initial package is not installed
  */
 public function update(PackageInterface $initial, PackageInterface $target, $targetDir)
 {
     $downloader = $this->getDownloaderForInstalledPackage($initial);
     $installationSource = $initial->getInstallationSource();
     if ('dist' === $installationSource) {
         $initialType = $initial->getDistType();
         $targetType = $target->getDistType();
     } else {
         $initialType = $initial->getSourceType();
         $targetType = $target->getSourceType();
     }
     // upgrading from a dist stable package to a dev package, force source reinstall
     if ($target->isDev() && 'dist' === $installationSource) {
         $downloader->remove($initial, $targetDir);
         $this->download($target, $targetDir);
         return;
     }
     if ($initialType === $targetType) {
         $target->setInstallationSource($installationSource);
         $downloader->update($initial, $target, $targetDir);
     } else {
         $downloader->remove($initial, $targetDir);
         $this->download($target, $targetDir, 'source' === $installationSource);
     }
 }
示例#18
0
 private function updateInformation(Package $package, PackageInterface $data, $flags)
 {
     $em = $this->doctrine->getEntityManager();
     $version = new Version();
     $version->setNormalizedVersion($data->getVersion());
     // check if we have that version yet
     foreach ($package->getVersions() as $existingVersion) {
         if ($existingVersion->getNormalizedVersion() === $version->getNormalizedVersion()) {
             if ($existingVersion->getDevelopment() || $flags & self::UPDATE_TAGS) {
                 $version = $existingVersion;
                 break;
             }
             // mark it updated to avoid it being pruned
             $existingVersion->setUpdatedAt(new \DateTime());
             return;
         }
     }
     $version->setName($package->getName());
     $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->setIncludePaths($data->getIncludePaths());
     $version->setSupport($data->getSupport());
     $version->getTags()->clear();
     if ($data->getKeywords()) {
         foreach ($data->getKeywords() as $keyword) {
             $tag = Tag::getByName($em, $keyword, true);
             if (!$version->getTags()->contains($tag)) {
                 $version->addTag($tag);
             }
         }
     }
     $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', 'role') 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);
             }
         }
     }
     // handle links
     foreach ($this->supportedLinkTypes as $linkType => $opts) {
         $links = array();
         foreach ($data->{$opts['method']}() as $link) {
             $constraint = $link->getPrettyConstraint();
             if (false !== strpos($constraint, '~')) {
                 $constraint = str_replace(array('[', ']'), '', $link->getConstraint());
                 $constraint = preg_replace('{(\\d\\.\\d)(\\.0)+(?=$|,|-)}', '$1', $constraint);
                 $constraint = preg_replace('{([><=,]) }', '$1', $constraint);
                 $constraint = preg_replace('{(<[0-9.]+)-dev}', '$1', $constraint);
             }
             if (false !== strpos($constraint, ',') && false !== strpos($constraint, '@')) {
                 $constraint = preg_replace_callback('{([><]=?\\s*[^@]+?)@([a-z]+)}i', function ($matches) {
                     if ($matches[2] === 'stable') {
                         return $matches[1];
                     }
                     return $matches[1] . '-' . $matches[2];
                 }, $constraint);
             }
             $links[$link->getTarget()] = $constraint;
         }
         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\\' . $opts['entity'];
             $link = new $class();
             $link->setPackageName($linkPackageName);
             $link->setPackageVersion($linkPackageVersion);
             $version->{'add' . $linkType . 'Link'}($link);
             $link->setVersion($version);
             $em->persist($link);
         }
     }
     // handle suggests
     if ($suggests = $data->getSuggests()) {
         foreach ($version->getSuggest() as $link) {
             // clear links that have changed/disappeared (for updates)
             if (!isset($suggests[$link->getPackageName()]) || $suggests[$link->getPackageName()] !== $link->getPackageVersion()) {
                 $version->getSuggest()->removeElement($link);
                 $em->remove($link);
             } else {
                 // clear those that are already set
                 unset($suggests[$link->getPackageName()]);
             }
         }
         foreach ($suggests as $linkPackageName => $linkPackageVersion) {
             $link = new SuggestLink();
             $link->setPackageName($linkPackageName);
             $link->setPackageVersion($linkPackageVersion);
             $version->addSuggestLink($link);
             $link->setVersion($version);
             $em->persist($link);
         }
     }
     if (!$package->getVersions()->contains($version)) {
         $package->addVersions($version);
     }
 }
示例#19
0
 /**
  * Create an archive of the specified package.
  *
  * @param  PackageInterface          $package   The package to archive
  * @param  string                    $format    The format of the archive (zip, tar, ...)
  * @param  string                    $targetDir The directory where to build the archive
  * @param  string|null               $fileName  The relative file name to use for the archive, or null to generate
  *                                              the package name. Note that the format will be appended to this name
  * @throws \InvalidArgumentException
  * @throws \RuntimeException
  * @return string                    The path of the created archive
  */
 public function archive(PackageInterface $package, $format, $targetDir, $fileName = null)
 {
     if (empty($format)) {
         throw new \InvalidArgumentException('Format must be specified');
     }
     // Search for the most appropriate archiver
     $usableArchiver = null;
     foreach ($this->archivers as $archiver) {
         if ($archiver->supports($format, $package->getSourceType())) {
             $usableArchiver = $archiver;
             break;
         }
     }
     // Checks the format/source type are supported before downloading the package
     if (null === $usableArchiver) {
         throw new \RuntimeException(sprintf('No archiver found to support %s format', $format));
     }
     $filesystem = new Filesystem();
     if (null === $fileName) {
         $packageName = $this->getPackageFilename($package);
     } else {
         $packageName = $fileName;
     }
     // Archive filename
     $filesystem->ensureDirectoryExists($targetDir);
     $target = realpath($targetDir) . '/' . $packageName . '.' . $format;
     $filesystem->ensureDirectoryExists(dirname($target));
     if (!$this->overwriteFiles && file_exists($target)) {
         return $target;
     }
     if ($package instanceof RootPackageInterface) {
         $sourcePath = realpath('.');
     } else {
         // Directory used to download the sources
         $sourcePath = sys_get_temp_dir() . '/composer_archive' . uniqid();
         $filesystem->ensureDirectoryExists($sourcePath);
         // Download sources
         $this->downloadManager->download($package, $sourcePath);
         // Check exclude from downloaded composer.json
         if (file_exists($composerJsonPath = $sourcePath . '/composer.json')) {
             $jsonFile = new JsonFile($composerJsonPath);
             $jsonData = $jsonFile->read();
             if (!empty($jsonData['archive']['exclude'])) {
                 $package->setArchiveExcludes($jsonData['archive']['exclude']);
             }
         }
     }
     // Create the archive
     $tempTarget = sys_get_temp_dir() . '/composer_archive' . uniqid() . '.' . $format;
     $filesystem->ensureDirectoryExists(dirname($tempTarget));
     $archivePath = $usableArchiver->archive($sourcePath, $tempTarget, $format, $package->getArchiveExcludes());
     rename($archivePath, $target);
     // cleanup temporary download
     if (!$package instanceof RootPackageInterface) {
         $filesystem->removeDirectory($sourcePath);
     }
     $filesystem->remove($tempTarget);
     return $target;
 }
示例#20
0
 /**
  * Updates package from initial to target version.
  *
  * @param   PackageInterface    $initial    initial package version
  * @param   PackageInterface    $target     target package version
  * @param   string              $targetDir  target dir
  *
  * @throws  InvalidArgumentException        if initial package is not installed
  */
 public function update(PackageInterface $initial, PackageInterface $target, $targetDir)
 {
     $downloader = $this->getDownloaderForInstalledPackage($initial);
     $installationSource = $initial->getInstallationSource();
     if ('dist' === $installationSource) {
         $initialType = $initial->getDistType();
         $targetType = $target->getDistType();
     } else {
         $initialType = $initial->getSourceType();
         $targetType = $target->getSourceType();
     }
     if ($initialType === $targetType) {
         $target->setInstallationSource($installationSource);
         $downloader->update($initial, $target, $targetDir);
     } else {
         $downloader->remove($initial, $targetDir);
         $this->download($target, $targetDir, 'source' === $installationSource);
     }
 }