我有多个实体附有某种文件。我遵循了关于如何使用学说(此处)处理文件上传的食谱条目,但我的问题是:
如何在不复制大量代码的情况下为多个实体执行此操作?现在我的所有实体都共享与获取路径相关的功能,尽管在这里和那里进行了一些修改,所以它们并不相同,但大部分代码都是。这样做最干净的方法是什么?
这是两个实体的示例,请注意重叠功能:
图片:
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';
}