file storage with factory

This commit is contained in:
Yuri Kuznetsov
2020-06-19 21:13:22 +03:00
parent b9fce4527a
commit 970b648145
5 changed files with 89 additions and 36 deletions

View File

@@ -146,7 +146,7 @@ class Container
{
return new \Espo\Core\FileStorage\Manager(
$this->get('metadata')->get(['app', 'fileStorage', 'implementationClassNameMap']),
$this
$this->get('injectableFactory')
);
}

View File

@@ -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);

View File

@@ -0,0 +1,49 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\FileStorage;
use Espo\Entities\Attachment;
interface StorageInterface
{
public function hasDownloadUrl(Attachment $attachment) : bool;
public function getDownloadUrl(Attachment $attachment) : string;
public function unlink(Attachment $attachment);
public function getContents(Attachment $attachment);
public function isFile(Attachment $attachment) : bool;
public function putContents(Attachment $attachment, $contents);
public function getLocalFilePath(Attachment $attachment) : string;
}

View File

@@ -31,6 +31,7 @@ namespace Espo\Core\FileStorage\Storages;
use \Espo\Core\Interfaces\Injectable;
/** Deprecated */
abstract class Base implements Injectable
{
protected $dependencyList = [];

View File

@@ -29,17 +29,25 @@
namespace Espo\Core\FileStorage\Storages;
use \Espo\Entities\Attachment;
use Espo\Core\Utils\File\Manager as FileManager;
use Espo\Entities\Attachment;
use \Espo\Core\Exceptions\Error;
use Espo\Core\FileStorage\StorageInterface;
class EspoUploadDir extends Base
use Espo\Core\Exceptions\Error;
class EspoUploadDir implements StorageInterface
{
protected $dependencyList = ['fileManager'];
protected $fileManager;
public function __construct(FileManager $fileManager)
{
$this->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;
}