4
* This file is part of Packagist.
6
* (c) Jordi Boggiano <j.boggiano@seld.be>
7
* Nils Adermann <naderman@naderman.de>
9
* For the full copyright and license information, please view the LICENSE
10
* file that was distributed with this source code.
13
namespace Packagist\WebBundle\Command;
15
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Bridge\Doctrine\RegistryInterface;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\HttpKernel\KernelInterface;
21
use Symfony\Component\Finder\Finder;
22
use Packagist\WebBundle\Entity\Version;
23
use Packagist\WebBundle\Entity\Tag;
24
use Packagist\WebBundle\Entity\Author;
25
use Packagist\WebBundle\Entity\Requirement;
28
* @author Jordi Boggiano <j.boggiano@seld.be>
30
class UpdatePackagesCommand extends ContainerAwareCommand
35
protected function configure()
38
->setName('pkg:update')
39
->setDefinition(array(
41
->setDescription('Updates packages')
52
protected function execute(InputInterface $input, OutputInterface $output)
54
$doctrine = $this->getContainer()->get('doctrine');
56
$logger = $this->getContainer()->get('logger');
57
$provider = $this->getContainer()->get('packagist.repository_provider');
59
$packages = $doctrine->getRepository('PackagistWebBundle:Package')->getStalePackages();
61
foreach ($packages as $package) {
62
$repository = $provider->getRepository($package->getRepository());
65
$output->writeln('<error>Unsupported repository: '.$package->getRepository().'</error>');
69
$output->writeln('Importing '.$repository->getUrl());
72
foreach ($repository->getTags() as $tag => $identifier) {
73
// TODO parse tag name (or fetch composer file?) w/ composer version parser, if no match, ignore the tag
74
$this->fetchInformation($output, $doctrine, $package, $repository, $identifier);
77
foreach ($repository->getBranches() as $branch => $identifier) {
78
// TODO parse branch name, matching a "$num.x.x" version scheme, + the master one
79
// use for all "x.y.z-dev" versions, usable through "latest-dev"
80
$this->fetchInformation($output, $doctrine, $package, $repository, $identifier);
83
$package->setUpdatedAt(new \DateTime);
84
$package->setCrawledAt(new \DateTime);
85
$doctrine->getEntityManager()->flush();
86
} catch (\Exception $e) {
87
$output->writeln('<error>Exception: '.$e->getMessage().', skipping package.</error>');
93
protected function fetchInformation(OutputInterface $output, RegistryInterface $doctrine, $package, $repository, $identifier)
95
$data = $repository->getComposerInformation($identifier);
96
$em = $doctrine->getEntityManager();
98
// check if we have that version yet
99
foreach ($package->getVersions() as $version) {
100
if ($version->getVersion() === $data['version']) {
105
if ($data['name'] !== $package->getName()) {
106
$output->writeln('<error>Package name seems to have changed for '.$repository->getUrl().'@'.$identifier.', skipping.</error>');
110
$version = new Version();
111
$em->persist($version);
113
foreach (array('name', 'description', 'homepage', 'license', 'version') as $field) {
114
if (isset($data[$field])) {
115
$version->{'set'.$field}($data[$field]);
119
$version->setPackage($package);
120
$version->setUpdatedAt(new \DateTime);
121
$version->setReleasedAt(new \DateTime($data['time']));
122
$version->setSource(array('type' => $repository->getType(), 'url' => $repository->getUrl()));
123
$version->setDist($repository->getDist($identifier));
125
if (isset($data['keywords'])) {
126
foreach ($data['keywords'] as $keyword) {
127
$version->addTags(Tag::getByName($em, $keyword, true));
131
if (isset($data['authors'])) {
132
foreach ($data['authors'] as $authorData) {
134
// skip authors with no information
135
if (!isset($authorData['email']) && !isset($authorData['name'])) {
139
if (isset($authorData['email'])) {
140
$author = $doctrine->getRepository('PackagistWebBundle:Author')->findOneByEmail($authorData['email']);
144
$author = new Author();
145
$em->persist($author);
147
foreach (array('email', 'name', 'homepage') as $field) {
148
if (isset($authorData[$field])) {
149
$author->{'set'.$field}($authorData[$field]);
152
$author->setUpdatedAt(new \DateTime);
153
$version->addAuthors($author);
154
$author->addVersions($version);
158
if (isset($data['require'])) {
159
foreach ($data['require'] as $requireName => $requireVersion) {
160
$requirement = new Requirement();
161
$em->persist($requirement);
162
$requirement->setPackageName($requireName);
163
$requirement->setPackageVersion($requireVersion);
164
$version->addRequirements($requirement);
165
$requirement->setVersion($version);