diff --git a/application/Espo/Core/Container.php b/application/Espo/Core/Container.php index 5f710ed85e..6841c86514 100644 --- a/application/Espo/Core/Container.php +++ b/application/Espo/Core/Container.php @@ -146,7 +146,7 @@ class Container { return new \Espo\Core\FileStorage\Manager( $this->get('metadata')->get(['app', 'fileStorage', 'implementationClassNameMap']), - $this + $this->get('injectableFactory') ); } diff --git a/application/Espo/Core/FileStorage/Manager.php b/application/Espo/Core/FileStorage/Manager.php index 2889a80389..57e15148f7 100644 --- a/application/Espo/Core/FileStorage/Manager.php +++ b/application/Espo/Core/FileStorage/Manager.php @@ -29,49 +29,44 @@ namespace Espo\Core\FileStorage; -use \Espo\Entities\Attachment; +use Espo\Core\InjectableFactory; -use \Espo\Core\Exceptions\Error; +use Espo\Entities\Attachment; + +use Espo\Core\Exceptions\Error; class Manager { - private $implementations = array(); + private $implementations = []; - private $implementationClassNameMap = array(); + private $implementationClassNameMap = []; - private $container; + private $injectableFactory; - public function __construct(array $implementationClassNameMap, $container) + public function __construct(array $implementationClassNameMap, InjectableFactory $injectableFactory) { $this->implementationClassNameMap = $implementationClassNameMap; - $this->container = $container; + $this->injectableFactory = $injectableFactory; } - private function getImplementation($storage = null) + private function getImplementation(?string $storage = null) { if (!$storage) { $storage = 'EspoUploadDir'; } - if (array_key_exists($storage, $this->implementations)) { - return $this->implementations[$storage]; + if (!array_key_exists($storage, $this->implementations)) { + if (!array_key_exists($storage, $this->implementationClassNameMap)) { + throw new Error("FileStorageManager: Unknown storage '{$storage}'"); + } + $className = $this->implementationClassNameMap[$storage]; + $this->implementations[$storage] = $this->injectableFactory->create($className); } - if (!array_key_exists($storage, $this->implementationClassNameMap)) { - throw new Error("FileStorageManager: Unknown storage '{$storage}'"); - } - $className = $this->implementationClassNameMap[$storage]; - - $implementation = new $className(); - foreach ($implementation->getDependencyList() as $dependencyName) { - $implementation->inject($dependencyName, $this->container->get($dependencyName)); - } - $this->implementations[$storage] = $implementation; - - return $implementation; + return $this->implementations[$storage]; } - public function isFile(Attachment $attachment) + public function isFile(Attachment $attachment) : bool { $implementation = $this->getImplementation($attachment->get('storage')); return $implementation->isFile($attachment); @@ -95,19 +90,19 @@ class Manager return $implementation->unlink($attachment); } - public function getLocalFilePath(Attachment $attachment) + public function getLocalFilePath(Attachment $attachment) : string { $implementation = $this->getImplementation($attachment->get('storage')); return $implementation->getLocalFilePath($attachment); } - public function hasDownloadUrl(Attachment $attachment) + public function hasDownloadUrl(Attachment $attachment) : bool { $implementation = $this->getImplementation($attachment->get('storage')); return $implementation->hasDownloadUrl($attachment); } - public function getDownloadUrl(Attachment $attachment) + public function getDownloadUrl(Attachment $attachment) : string { $implementation = $this->getImplementation($attachment->get('storage')); return $implementation->getDownloadUrl($attachment); diff --git a/application/Espo/Core/FileStorage/StorageInterface.php b/application/Espo/Core/FileStorage/StorageInterface.php new file mode 100644 index 0000000000..5c4942ffa8 --- /dev/null +++ b/application/Espo/Core/FileStorage/StorageInterface.php @@ -0,0 +1,49 @@ +fileManager = $fileManager; + } protected function getFileManager() { - return $this->getInjection('fileManager'); + return $this->fileManager; } public function unlink(Attachment $attachment) @@ -47,7 +55,7 @@ class EspoUploadDir extends Base return $this->getFileManager()->unlink($this->getFilePath($attachment)); } - public function isFile(Attachment $attachment) + public function isFile(Attachment $attachment) : bool { return $this->getFileManager()->isFile($this->getFilePath($attachment)); } @@ -62,7 +70,7 @@ class EspoUploadDir extends Base return $this->getFileManager()->putContents($this->getFilePath($attachment), $contents); } - public function getLocalFilePath(Attachment $attachment) + public function getLocalFilePath(Attachment $attachment) : string { return $this->getFilePath($attachment); } @@ -73,12 +81,12 @@ class EspoUploadDir extends Base return 'data/upload/' . $sourceId; } - public function getDownloadUrl(Attachment $attachment) + public function getDownloadUrl(Attachment $attachment) : string { throw new Error(); } - public function hasDownloadUrl(Attachment $attachment) + public function hasDownloadUrl(Attachment $attachment) : bool { return false; }