ある種のファイルが添付された複数のエンティティがあります。doctrine を使用してファイルのアップロードを処理する方法に関するクックブックのエントリ ( here )に従いましたが、私の質問は次のとおりです。
多くのコードを複製することなく、複数のエンティティに対してこれを行うにはどうすればよいですか? 現在、私のすべてのエンティティは、パスの取得に関連する関数を共有していますが、あちこちに変更が加えられているため、同一ではありませんが、ほとんどのコードは同一です。これを行う最もクリーンな方法は何ですか?
2 つのエンティティの例を次に示します。重複する機能に注意してください。
画像:
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';
}