Browse Source

Add Author entity and relations

Jordi Boggiano 14 years ago
parent
commit
2daccb3750

+ 2 - 1
app/config/config.yml

@@ -58,4 +58,5 @@ jms_security_extra:
 fos_user:
     db_driver:     orm
     firewall_name: main
-    user_class:  Packagist\WebBundle\Entity\User
+    user_class:  Packagist\WebBundle\Entity\User
+    #from_email:     { webmaster@example.com: Admin }

+ 237 - 0
src/Packagist/WebBundle/Entity/Author.php

@@ -0,0 +1,237 @@
+<?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 Doctrine\ORM\Mapping as ORM;
+use Symfony\Component\Validator\Constraints as Assert;
+use Symfony\Component\Validator\ExecutionContext;
+use Doctrine\Common\Collections\ArrayCollection;
+
+/**
+ * @ORM\Entity
+ * @ORM\Table(name="author")
+ * @author Jordi Boggiano <j.boggiano@seld.be>
+ */
+class Author
+{
+    /**
+     * @ORM\Id
+     * @ORM\Column(type="integer")
+     * @ORM\GeneratedValue(strategy="AUTO")
+     */
+    private $id;
+
+    /**
+     * Unique package name
+     *
+     * @ORM\Column(type="text", nullable="true")
+     * @Assert\NotBlank()
+     */
+    private $name;
+
+    /**
+     * @ORM\Column(type="text", nullable="true")
+     */
+    private $email;
+
+    /**
+     * @ORM\Column(type="text", nullable="true")
+     */
+    private $homepage;
+
+    /**
+     * @ORM\ManyToMany(targetEntity="Packagist\WebBundle\Entity\Version", mappedBy="tags")
+     */
+    private $versions;
+
+    /**
+     * @ORM\ManyToOne(targetEntity="Packagist\WebBundle\Entity\User", inversedBy="authors")
+     * @Assert\Type(type="Packagist\WebBundle\Entity\User")
+     */
+    private $owner;
+
+    /**
+     * @ORM\Column(type="datetime")
+     */
+    private $createdAt;
+
+    /**
+     * @ORM\Column(type="datetime", nullable="true")
+     */
+    private $updatedAt;
+
+    public function __construct()
+    {
+        $this->versions = new ArrayCollection();
+        $this->createdAt = new \DateTime;
+    }
+
+    public function toArray()
+    {
+        return array(
+            'name' => $this->name,
+            'email' => $this->email,
+            'homepage' => $this->homepage,
+        );
+    }
+
+    /**
+     * 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;
+    }
+
+    /**
+     * Set createdAt
+     *
+     * @param datetime $createdAt
+     */
+    public function setCreatedAt($createdAt)
+    {
+        $this->createdAt = $createdAt;
+    }
+
+    /**
+     * Get createdAt
+     *
+     * @return datetime $createdAt
+     */
+    public function getCreatedAt()
+    {
+        return $this->createdAt;
+    }
+
+    /**
+     * Add versions
+     *
+     * @param Packagist\WebBundle\Entity\Version $versions
+     */
+    public function addVersions(Version $versions)
+    {
+        $this->versions[] = $versions;
+    }
+
+    /**
+     * Get versions
+     *
+     * @return string $versions
+     */
+    public function getVersions()
+    {
+        return $this->versions;
+    }
+
+    /**
+     * Set updatedAt
+     *
+     * @param datetime $updatedAt
+     */
+    public function setUpdatedAt($updatedAt)
+    {
+        $this->updatedAt = $updatedAt;
+    }
+
+    /**
+     * Get updatedAt
+     *
+     * @return datetime $updatedAt
+     */
+    public function getUpdatedAt()
+    {
+        return $this->updatedAt;
+    }
+
+    /**
+     * Set email
+     *
+     * @param text $email
+     */
+    public function setEmail($email)
+    {
+        $this->email = $email;
+    }
+
+    /**
+     * Get email
+     *
+     * @return text
+     */
+    public function getEmail()
+    {
+        return $this->email;
+    }
+
+    /**
+     * Set homepage
+     *
+     * @param text $homepage
+     */
+    public function setHomepage($homepage)
+    {
+        $this->homepage = $homepage;
+    }
+
+    /**
+     * Get homepage
+     *
+     * @return text
+     */
+    public function getHomepage()
+    {
+        return $this->homepage;
+    }
+
+    /**
+     * Set owner
+     *
+     * @param Packagist\WebBundle\Entity\User $owner
+     */
+    public function setOwner(User $owner)
+    {
+        $this->owner = $owner;
+    }
+
+    /**
+     * Get owner
+     *
+     * @return Packagist\WebBundle\Entity\User
+     */
+    public function getOwner()
+    {
+        return $this->owner;
+    }
+}

+ 3 - 3
src/Packagist/WebBundle/Entity/Package.php

@@ -21,9 +21,9 @@ use Doctrine\Common\Collections\ArrayCollection;
  * @ORM\Entity
  * @ORM\Table(
  *     name="package",
- *     uniqueConstraints={@ORM\UniqueConstraint(name="name_idx",columns={"name"})}
+ *     uniqueConstraints={@ORM\UniqueConstraint(name="name_idx", columns={"name"})}
  * )
- * @Assert\Callback(methods={"isRepositoryValid","isPackageUnique"})
+ * @Assert\Callback(methods={"isRepositoryValid", "isPackageUnique"})
  * @author Jordi Boggiano <j.boggiano@seld.be>
  */
 class Package
@@ -49,7 +49,7 @@ class Package
     private $description;
 
     /**
-     * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\Version",mappedBy="package")
+     * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\Version", mappedBy="package")
      */
     private $versions;
 

+ 26 - 0
src/Packagist/WebBundle/Entity/User.php

@@ -24,9 +24,15 @@ class User extends BaseUser
      */
     private $packages;
 
+    /**
+     * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\Author", mappedBy="owner")
+     */
+    private $authors;
+
     public function __construct()
     {
         $this->packages = new ArrayCollection();
+        $this->authors = new ArrayCollection();
         parent::__construct();
     }
 
@@ -49,4 +55,24 @@ class User extends BaseUser
     {
         return $this->packages;
     }
+
+    /**
+     * Add authors
+     *
+     * @param Packagist\WebBundle\Entity\Author $authors
+     */
+    public function addAuthors(\Packagist\WebBundle\Entity\Author $authors)
+    {
+        $this->authors[] = $authors;
+    }
+
+    /**
+     * Get authors
+     *
+     * @return Doctrine\Common\Collections\Collection
+     */
+    public function getAuthors()
+    {
+        return $this->authors;
+    }
 }

+ 35 - 7
src/Packagist/WebBundle/Entity/Version.php

@@ -75,10 +75,14 @@ class Version
      */
     private $license;
 
-//    /**
-//     * @ORM\ManyToMany(targetEntity="User")
-//     */
-//    private $authors;
+    /**
+     * @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;
 
     /**
      * JSON object of source spec
@@ -124,9 +128,13 @@ class Version
     public function toArray()
     {
         $tags = array();
-        foreach ($this->tags as $tag) {
+        foreach ($this->getTags() as $tag) {
             $tags[] = $tag->getName();
         }
+        $authors = array();
+        foreach ($this->getAuthors() as $author) {
+            $authors[] = $author->toArray();
+        }
         return array(
             'name' => $this->name,
             'description' => $this->description,
@@ -134,10 +142,10 @@ class Version
             'homepage' => $this->homepage,
             'version' => $this->version,
             'license' => $this->license,
-            'authors' => array(),
+            'authors' => $authors,
             'require' => $this->getRequire(),
             'source' => $this->getSource(),
-            'time' => $this->releasedAt->format('Y-m-d\TH:i:s'),
+            'time' => $this->releasedAt ? $this->releasedAt->format('Y-m-d\TH:i:sP') : null,
             'dist' => array(),
         );
     }
@@ -448,4 +456,24 @@ class Version
     {
         return $this->updatedAt;
     }
+
+    /**
+     * Add authors
+     *
+     * @param Packagist\WebBundle\Entity\Author $authors
+     */
+    public function addAuthors(\Packagist\WebBundle\Entity\Author $authors)
+    {
+        $this->authors[] = $authors;
+    }
+
+    /**
+     * Get authors
+     *
+     * @return Doctrine\Common\Collections\Collection
+     */
+    public function getAuthors()
+    {
+        return $this->authors;
+    }
 }