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\Entity;
15
use Packagist\WebBundle\Repository\RepositoryProviderInterface;
17
use Doctrine\ORM\Mapping as ORM;
18
use Symfony\Component\Validator\Constraints as Assert;
19
use Symfony\Component\Validator\ExecutionContext;
20
use Doctrine\Common\Collections\ArrayCollection;
23
* @ORM\Entity(repositoryClass="Packagist\WebBundle\Entity\PackageRepository")
26
* uniqueConstraints={@ORM\UniqueConstraint(name="name_idx", columns={"name"})}
28
* @Assert\Callback(methods={"isPackageUnique"})
29
* @author Jordi Boggiano <j.boggiano@seld.be>
35
* @ORM\Column(type="integer")
36
* @ORM\GeneratedValue(strategy="AUTO")
49
* @ORM\Column(type="text", nullable="true")
54
* @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\Version", mappedBy="package")
59
* @ORM\ManyToMany(targetEntity="User", inversedBy="packages")
60
* @ORM\JoinTable(name="maintainers_packages")
70
// dist-tags / rel or runtime?
73
* @ORM\Column(type="datetime")
78
* @ORM\Column(type="datetime", nullable="true")
83
* @ORM\Column(type="datetime", nullable="true")
87
public function __construct()
89
$this->versions = new ArrayCollection();
90
$this->createdAt = new \DateTime;
93
public function toJson()
96
foreach ($this->getVersions() as $version) {
97
$versions[$version->getVersion()] = $version->toArray();
99
$maintainers = array();
100
foreach ($this->getMaintainers() as $maintainer) {
101
$maintainers[] = $maintainer->toArray();
104
'name' => $this->name,
105
'description' => $this->description,
106
'dist-tags' => array(),
107
'maintainers' => $maintainers,
108
'versions' => $versions,
110
return json_encode($data);
114
public function isRepositoryValid(ExecutionContext $context)
116
$propertyPath = $context->getPropertyPath() . '.repository';
117
$context->setPropertyPath($propertyPath);
119
if (!preg_match('#^git://.+|https?://github.com/[^/]+/[^/]+(?:\.git)?$#', $this->repository)) {
120
$context->addViolation('This is not a valid git repository url', array(), null);
124
if (!preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $this->repository, $match)) {
125
$context->addViolation('Only GitHub repositories are supported at the moment', array(), null);
126
// TODO handle other types of git repos, and later svn/hg too
130
// handle github repositories
132
$repository = $match[2];
134
$repoData = json_decode(file_get_contents('http://github.com/api/v2/json/repos/show/'.$owner.'/'.$repository), true);
136
$context->addViolation('Could not fetch information from this repository (if GitHub is down, please try again later)', array(), null);
140
$masterData = json_decode(file_get_contents('https://raw.github.com/'.$owner.'/'.$repository.'/master/composer.json'), true);
141
if ($masterData['name'] !== $this->name) {
142
$context->addViolation('The repository\'s composer.json information does not match the given package name, '.$masterData['name'].' found.', array(), null);
148
public function isPackageUnique(ExecutionContext $context)
150
// TODO check for uniqueness of package name
153
public function fromProvider(RepositoryProviderInterface $provider)
155
$repo = $provider->getRepository($this->repository);
156
$composerFile = $repo->getComposerInformation('master');
158
$this->setName($composerFile['name']);
166
public function getId()
174
* @param string $name
176
public function setName($name)
184
* @return string $name
186
public function getName()
194
* @param text $description
196
public function setDescription($description)
198
$this->description = $description;
204
* @return text $description
206
public function getDescription()
208
return $this->description;
214
* @param datetime $createdAt
216
public function setCreatedAt($createdAt)
218
$this->createdAt = $createdAt;
224
* @return datetime $createdAt
226
public function getCreatedAt()
228
return $this->createdAt;
234
* @param string $repository
236
public function setRepository($repository)
238
$this->repository = $repository;
244
* @return string $repository
246
public function getRepository()
248
return $this->repository;
254
* @param Packagist\WebBundle\Entity\Version $versions
256
public function addVersions(Version $versions)
258
$this->versions[] = $versions;
264
* @return string $versions
266
public function getVersions()
268
return $this->versions;
274
* @param datetime $updatedAt
276
public function setUpdatedAt($updatedAt)
278
$this->updatedAt = $updatedAt;
284
* @return datetime $updatedAt
286
public function getUpdatedAt()
288
return $this->updatedAt;
294
* @param datetime $crawledAt
296
public function setCrawledAt($crawledAt)
298
$this->crawledAt = $crawledAt;
304
* @return datetime $crawledAt
306
public function getCrawledAt()
308
return $this->crawledAt;
314
* @param Packagist\WebBundle\Entity\User $maintainers
316
public function addMaintainers(User $maintainers)
318
$this->maintainers[] = $maintainers;
324
* @return Doctrine\Common\Collections\Collection $maintainers
326
public function getMaintainers()
328
return $this->maintainers;
b'\\ No newline at end of file'