如何编写在Symfony2中共享函数的多个实体类

时间:2012-12-31 12:07:49

标签: php symfony dry

我有多个实体附加了某种文件。我已经按照有关如何使用doctrine(here)处理文件上传的菜谱条目,但我的问题是:

如何在不重复大量代码的情况下为多个实体执行此操作?现在我的所有实体共享与获取路径相关的功能,虽然在这里和那里进行了一些修改,所以它们不相同,但大多数代码都是。这样做最干净的方法是什么?

以下是两个实体的示例,注意重叠功能:

图像:

public function preUpload()
{
    if (null === $this->file) {
        return;
    }

    $dir = $this->getUploadRootDir().'/'.$this->getParentDir();

    if (!is_dir($dir)) {
        mkdir($dir);
    }

    $slugger = new \Sam\TourBundle\Service\SlugService();

    $pathinfo = pathinfo($this->file->getClientOriginalName());
    $path = $slugger->slugize($pathinfo['filename']).'.'.$pathinfo['extension'];
    $this->path = $path;

    // If file already exists rename it
    if (file_exists($this->getAbsolutePath())) {
        $i = 1;
        while (file_exists($this->getAbsolutePath())) {
            $this->path = $i.'-'.$this->path;
            $i++;
        }
    }

}

/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
    // the file property can be empty if the field is not required
    if (null === $this->file) {
        return;
    }

    $dir = $this->getUploadRootDir().'/'.$this->getParentDir();
    $this->file->move($dir, $this->path);

    $thumb = new \Sam\TourBundle\Service\ThumbnailService();
    $thumb->makeThumbnail($this->getAbsolutePath());

    unset($this->file);
}

/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
    $file = $this->getAbsolutePath();
    $thumb_file = $this->getThumbnailAbsolutePath();

    if (file_exists($file)) {
        unlink($file);
    }

    if (file_exists($thumb_file)) {
        unlink($thumb_file);
    }
}

public function getParentDir()
{
    return $galerija = $this->getGalerija()->getSlug().'/';
    //return null === $galerija ? null : $this->getUploadRootDir().'/'.$galerija.$this->path;
}

public function getAbsolutePath()
{
    return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->getParentDir().$this->path;
}

public function getThumbnailAbsolutePath()
{
    $pathinfo = pathinfo($this->getAbsolutePath());
    $thumbpath = $pathinfo['dirname'].'/'.$pathinfo['filename'].'_thumb.'.$pathinfo['extension'];
    return null === $this->path ? null : $thumbpath;
}

public function getRootDir()
{
    return __DIR__;
}

public function getWebPath()
{
    return null === $this->path ? null : $this->getUploadDir().'/'.$this->getParentDir().$this->path;
}

public function getThumbnailWebPath()
{
    $pathinfo = pathinfo($this->getAbsolutePath());
    $thumbpath = $pathinfo['filename'].'_thumb.'.$pathinfo['extension'];
    return null === $this->path ? null : $this->getUploadDir().'/'.$this->getParentDir().$thumbpath;
}

protected function getUploadRootDir()
{
    // the absolute directory path where uploaded documents should be saved
    return realpath(__DIR__.'/../../../../web/'.$this->getUploadDir());
}

protected function getUploadDir()
{
    // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view
    return 'images';
}

文件:

public function preUpload()
{
    if (null === $this->file) {
        return;
    }

    $dir = $this->getUploadRootDir().'/'.$this->getParentDir();

    if (!is_dir($dir)) {
        mkdir($dir);
    }

    $slugger = new \Sam\TourBundle\Service\SlugService();

    $pathinfo = pathinfo($this->file->getClientOriginalName());
    $path = $slugger->slugize($pathinfo['filename']).'.'.$pathinfo['extension'];
    $this->path = $path;

    // If file already exists rename it
    if (file_exists($this->getAbsolutePath())) {
        $i = 1;
        while (file_exists($this->getAbsolutePath())) {
            $this->path = $i.'-'.$this->path;
            $i++;
        }
    }

}

/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
    // the file property can be empty if the field is not required
    if (null === $this->file) {
        return;
    }

    $dir = $this->getUploadRootDir().'/'.$this->getParentDir();
    $this->file->move($dir, $this->path);

    unset($this->file);
}

/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
    $file = $this->getAbsolutePath();

    if (file_exists($file)) {
        unlink($file);
    }
}

public function getParentDir()
{
    return $ponuda = $this->getPonuda()->getSlug().'/';
}

public function getAbsolutePath()
{
    return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->getParentDir().$this->path;
}

public function getRootDir()
{
    return __DIR__;
}

public function getWebPath()
{
    return null === $this->path ? null : $this->getUploadDir().'/'.$this->getParentDir().$this->path;
}

protected function getUploadRootDir()
{
    // the absolute directory path where uploaded documents should be saved
    return realpath(__DIR__.'/../../../../web/'.$this->getUploadDir());
}

protected function getUploadDir()
{
    // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view
    return 'documents';
}

1 个答案:

答案 0 :(得分:0)

我个人会创建一个上传服务类,其中包含一个可以传递实体的方法,您可以在任何需要的位置注入此服务,并将其写入一个地方。 在我认为的实体中拥有这种逻辑通常不是一个好主意。

从我的观点来看,这种情况下的继承看起来完全不正确,因为它是一种添加行为而不是泛化的方式,你可以这样做,但我会反对它。继承本身是过度使用而不是最干净的工作方式,并且Doctrine对它的实现也非常烦人。特质可以做到,虽然我在这种环境中对它们没有多少经验。 我强烈建议以Symfony的使用方式使用服务。