1
我有多個附有某種文件的實體。我遵循食譜條目如何處理文件上傳與教條(here),但我的問題是這樣的:如何編寫在Symfony2中共享函數的多個實體類
我怎樣才能做到這一點,而不重複大量的代碼?現在我的所有實體都共享與獲取路徑相關的功能,儘管在這裏和那裏進行了一些修改,所以它們不完全相同,但大部分代碼都是。這樣做的最乾淨的方式是什麼?
這裏是兩個實體的一個例子,通知重疊功能:
圖片:
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';
}
這聽起來很合理,謝謝!雖然我不太確定使用特性,但PHP 5.4在託管提供商中的普及程度如何? – moljac024
是的,這是一個問題。我不確定它有多受歡迎,我通常不會使用我無法管理的服務器。如果您使用的是第三方服務器,那麼您應該事先知道哪個版本的PHP,或者他們是否會提供您的應用程序所需的所有服務器和擴展。同樣,我仍然認爲服務比特質更好。我也不確定PHPUnit在測試特性方面有多成熟,這對我來說是一個問題。 – fd8s0