@@ -22,56 +22,56 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | class File implements FileInterface |
| 24 | 24 | {
|
| 25 | - /** |
|
| 25 | +/** |
|
| 26 | 26 | * The uploaded file type |
| 27 | 27 | * |
| 28 | 28 | * @var string |
| 29 | 29 | */ |
| 30 | - protected $sType; |
|
| 30 | +protected $sType; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 32 | +/** |
|
| 33 | 33 | * The uploaded file name, without the extension and sanitized |
| 34 | 34 | * |
| 35 | 35 | * @var string |
| 36 | 36 | */ |
| 37 | - protected $sName; |
|
| 37 | +protected $sName; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 39 | +/** |
|
| 40 | 40 | * The uploaded file name, with the extension |
| 41 | 41 | * |
| 42 | 42 | * @var string |
| 43 | 43 | */ |
| 44 | - protected $sFilename; |
|
| 44 | +protected $sFilename; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 46 | +/** |
|
| 47 | 47 | * The uploaded file path |
| 48 | 48 | * |
| 49 | 49 | * @var string |
| 50 | 50 | */ |
| 51 | - protected $sPath; |
|
| 51 | +protected $sPath; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 53 | +/** |
|
| 54 | 54 | * The uploaded file size |
| 55 | 55 | * |
| 56 | 56 | * @var int |
| 57 | 57 | */ |
| 58 | - protected $nSize; |
|
| 58 | +protected $nSize; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 60 | +/** |
|
| 61 | 61 | * The uploaded file extension |
| 62 | 62 | * |
| 63 | 63 | * @var string |
| 64 | 64 | */ |
| 65 | - protected $sExtension; |
|
| 65 | +protected $sExtension; |
|
| 66 | 66 | |
| 67 | - /** |
|
| 67 | +/** |
|
| 68 | 68 | * The filesystem where the file is stored |
| 69 | 69 | * |
| 70 | 70 | * @var Filesystem |
| 71 | 71 | */ |
| 72 | - protected $xFilesystem; |
|
| 72 | +protected $xFilesystem; |
|
| 73 | 73 | |
| 74 | - /** |
|
| 74 | +/** |
|
| 75 | 75 | * Create an instance of this class using data from an uploaded file. |
| 76 | 76 | * |
| 77 | 77 | * @param Filesystem $xFilesystem |
@@ -81,87 +81,87 @@ discard block |
||
| 81 | 81 | * |
| 82 | 82 | * @return File |
| 83 | 83 | */ |
| 84 | - public static function fromHttpFile(Filesystem $xFilesystem, |
|
| 85 | - UploadedFile $xHttpFile, string $sUploadDir, string $sName): File |
|
| 86 | - {
|
|
| 87 | - $xFile = new File(); |
|
| 88 | - $xFile->xFilesystem = $xFilesystem; |
|
| 89 | - $xFile->sType = $xHttpFile->getClientMediaType(); |
|
| 90 | - $xFile->sName = $sName; |
|
| 91 | - $xFile->sFilename = $xHttpFile->getClientFilename(); |
|
| 92 | - $xFile->sExtension = pathinfo($xFile->sFilename, PATHINFO_EXTENSION); |
|
| 93 | - $xFile->nSize = $xHttpFile->getSize(); |
|
| 94 | - $xFile->sPath = $sUploadDir . $xFile->sName . '.' . $xFile->sExtension; |
|
| 95 | - return $xFile; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 84 | +public static function fromHttpFile(Filesystem $xFilesystem, |
|
| 85 | +UploadedFile $xHttpFile, string $sUploadDir, string $sName): File |
|
| 86 | +{
|
|
| 87 | +$xFile = new File(); |
|
| 88 | +$xFile->xFilesystem = $xFilesystem; |
|
| 89 | +$xFile->sType = $xHttpFile->getClientMediaType(); |
|
| 90 | +$xFile->sName = $sName; |
|
| 91 | +$xFile->sFilename = $xHttpFile->getClientFilename(); |
|
| 92 | +$xFile->sExtension = pathinfo($xFile->sFilename, PATHINFO_EXTENSION); |
|
| 93 | +$xFile->nSize = $xHttpFile->getSize(); |
|
| 94 | +$xFile->sPath = $sUploadDir . $xFile->sName . '.' . $xFile->sExtension; |
|
| 95 | +return $xFile; |
|
| 96 | +} |
|
| 97 | + |
|
| 98 | +/** |
|
| 99 | 99 | * Get the filesystem where the file is stored |
| 100 | 100 | * |
| 101 | 101 | * @return Filesystem |
| 102 | 102 | */ |
| 103 | - public function filesystem(): Filesystem |
|
| 104 | - {
|
|
| 105 | - return $this->xFilesystem; |
|
| 106 | - } |
|
| 103 | +public function filesystem(): Filesystem |
|
| 104 | +{
|
|
| 105 | +return $this->xFilesystem; |
|
| 106 | +} |
|
| 107 | 107 | |
| 108 | - /** |
|
| 108 | +/** |
|
| 109 | 109 | * Get the uploaded file type |
| 110 | 110 | * |
| 111 | 111 | * @return string |
| 112 | 112 | */ |
| 113 | - public function type(): string |
|
| 114 | - {
|
|
| 115 | - return $this->sType; |
|
| 116 | - } |
|
| 113 | +public function type(): string |
|
| 114 | +{
|
|
| 115 | +return $this->sType; |
|
| 116 | +} |
|
| 117 | 117 | |
| 118 | - /** |
|
| 118 | +/** |
|
| 119 | 119 | * Get the uploaded file name, without the extension and slugified |
| 120 | 120 | * |
| 121 | 121 | * @return string |
| 122 | 122 | */ |
| 123 | - public function name(): string |
|
| 124 | - {
|
|
| 125 | - return $this->sName; |
|
| 126 | - } |
|
| 123 | +public function name(): string |
|
| 124 | +{
|
|
| 125 | +return $this->sName; |
|
| 126 | +} |
|
| 127 | 127 | |
| 128 | - /** |
|
| 128 | +/** |
|
| 129 | 129 | * Get the uploaded file name, with the extension |
| 130 | 130 | * |
| 131 | 131 | * @return string |
| 132 | 132 | */ |
| 133 | - public function filename(): string |
|
| 134 | - {
|
|
| 135 | - return $this->sFilename; |
|
| 136 | - } |
|
| 133 | +public function filename(): string |
|
| 134 | +{
|
|
| 135 | +return $this->sFilename; |
|
| 136 | +} |
|
| 137 | 137 | |
| 138 | - /** |
|
| 138 | +/** |
|
| 139 | 139 | * Get the uploaded file path |
| 140 | 140 | * |
| 141 | 141 | * @return string |
| 142 | 142 | */ |
| 143 | - public function path(): string |
|
| 144 | - {
|
|
| 145 | - return $this->sPath; |
|
| 146 | - } |
|
| 143 | +public function path(): string |
|
| 144 | +{
|
|
| 145 | +return $this->sPath; |
|
| 146 | +} |
|
| 147 | 147 | |
| 148 | - /** |
|
| 148 | +/** |
|
| 149 | 149 | * Get the uploaded file size |
| 150 | 150 | * |
| 151 | 151 | * @return int |
| 152 | 152 | */ |
| 153 | - public function size(): int |
|
| 154 | - {
|
|
| 155 | - return $this->nSize; |
|
| 156 | - } |
|
| 153 | +public function size(): int |
|
| 154 | +{
|
|
| 155 | +return $this->nSize; |
|
| 156 | +} |
|
| 157 | 157 | |
| 158 | - /** |
|
| 158 | +/** |
|
| 159 | 159 | * Get the uploaded file extension |
| 160 | 160 | * |
| 161 | 161 | * @return string |
| 162 | 162 | */ |
| 163 | - public function extension(): string |
|
| 164 | - {
|
|
| 165 | - return $this->sExtension; |
|
| 166 | - } |
|
| 163 | +public function extension(): string |
|
| 164 | +{
|
|
| 165 | +return $this->sExtension; |
|
| 166 | +} |
|
| 167 | 167 | } |
@@ -4,12 +4,12 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface FileNameInterface |
| 6 | 6 | { |
| 7 | - /** |
|
| 7 | +/** |
|
| 8 | 8 | * Generate a random name for a file or dir |
| 9 | 9 | * |
| 10 | 10 | * @param int $nLength |
| 11 | 11 | * |
| 12 | 12 | * @return string |
| 13 | 13 | */ |
| 14 | - public function random(int $nLength): string; |
|
| 14 | +public function random(int $nLength): string; |
|
| 15 | 15 | } |
@@ -25,58 +25,58 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | class UploadHandler implements UploadHandlerInterface |
| 27 | 27 | { |
| 28 | - /** |
|
| 28 | +/** |
|
| 29 | 29 | * The uploaded files copied in the user dir |
| 30 | 30 | * |
| 31 | 31 | * @var array |
| 32 | 32 | */ |
| 33 | - private $aUserFiles = []; |
|
| 33 | +private $aUserFiles = []; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 35 | +/** |
|
| 36 | 36 | * The constructor |
| 37 | 37 | * |
| 38 | 38 | * @param FileStorage $xFileStorage |
| 39 | 39 | * @param UploadManager $xUploadManager |
| 40 | 40 | */ |
| 41 | - public function __construct(private FileStorage $xFileStorage, |
|
| 42 | - private UploadManager $xUploadManager) |
|
| 43 | - {} |
|
| 41 | +public function __construct(private FileStorage $xFileStorage, |
|
| 42 | +private UploadManager $xUploadManager) |
|
| 43 | +{} |
|
| 44 | 44 | |
| 45 | - /** |
|
| 45 | +/** |
|
| 46 | 46 | * Set the uploaded file name sanitizer |
| 47 | 47 | * |
| 48 | 48 | * @param Closure $cSanitizer The closure |
| 49 | 49 | * |
| 50 | 50 | * @return void |
| 51 | 51 | */ |
| 52 | - public function sanitizer(Closure $cSanitizer): void |
|
| 53 | - { |
|
| 54 | - $this->xUploadManager->setNameSanitizer($cSanitizer); |
|
| 55 | - } |
|
| 52 | +public function sanitizer(Closure $cSanitizer): void |
|
| 53 | +{ |
|
| 54 | +$this->xUploadManager->setNameSanitizer($cSanitizer); |
|
| 55 | +} |
|
| 56 | 56 | |
| 57 | - /** |
|
| 57 | +/** |
|
| 58 | 58 | * Get the uploaded files |
| 59 | 59 | * |
| 60 | 60 | * @return array |
| 61 | 61 | */ |
| 62 | - public function files(): array |
|
| 63 | - { |
|
| 64 | - return $this->aUserFiles; |
|
| 65 | - } |
|
| 62 | +public function files(): array |
|
| 63 | +{ |
|
| 64 | +return $this->aUserFiles; |
|
| 65 | +} |
|
| 66 | 66 | |
| 67 | - /** |
|
| 67 | +/** |
|
| 68 | 68 | * Check if the current request contains uploaded files |
| 69 | 69 | * |
| 70 | 70 | * @param ServerRequestInterface $xRequest |
| 71 | 71 | * |
| 72 | 72 | * @return bool |
| 73 | 73 | */ |
| 74 | - public function canProcessRequest(ServerRequestInterface $xRequest): bool |
|
| 75 | - { |
|
| 76 | - return count($xRequest->getUploadedFiles()) > 0; |
|
| 77 | - } |
|
| 74 | +public function canProcessRequest(ServerRequestInterface $xRequest): bool |
|
| 75 | +{ |
|
| 76 | +return count($xRequest->getUploadedFiles()) > 0; |
|
| 77 | +} |
|
| 78 | 78 | |
| 79 | - /** |
|
| 79 | +/** |
|
| 80 | 80 | * Process the uploaded files in the HTTP request |
| 81 | 81 | * |
| 82 | 82 | * @param ServerRequestInterface $xRequest |
@@ -84,21 +84,21 @@ discard block |
||
| 84 | 84 | * @return bool |
| 85 | 85 | * @throws RequestException |
| 86 | 86 | */ |
| 87 | - public function processRequest(ServerRequestInterface $xRequest): bool |
|
| 88 | - { |
|
| 89 | - // Copy the uploaded files from the HTTP request. |
|
| 90 | - $this->aUserFiles = $this->xUploadManager->readFromHttpData($xRequest); |
|
| 91 | - return true; |
|
| 92 | - } |
|
| 87 | +public function processRequest(ServerRequestInterface $xRequest): bool |
|
| 88 | +{ |
|
| 89 | +// Copy the uploaded files from the HTTP request. |
|
| 90 | +$this->aUserFiles = $this->xUploadManager->readFromHttpData($xRequest); |
|
| 91 | +return true; |
|
| 92 | +} |
|
| 93 | 93 | |
| 94 | - /** |
|
| 94 | +/** |
|
| 95 | 95 | * @param string $sStorage |
| 96 | 96 | * @param Closure $cFactory |
| 97 | 97 | * |
| 98 | 98 | * @return void |
| 99 | 99 | */ |
| 100 | - public function registerStorageAdapter(string $sStorage, Closure $cFactory): void |
|
| 101 | - { |
|
| 102 | - $this->xFileStorage->registerAdapter($sStorage, $cFactory); |
|
| 103 | - } |
|
| 100 | +public function registerStorageAdapter(string $sStorage, Closure $cFactory): void |
|
| 101 | +{ |
|
| 102 | +$this->xFileStorage->registerAdapter($sStorage, $cFactory); |
|
| 103 | +} |
|
| 104 | 104 | } |
@@ -20,45 +20,45 @@ |
||
| 20 | 20 | |
| 21 | 21 | interface UploadHandlerInterface |
| 22 | 22 | { |
| 23 | - /** |
|
| 23 | +/** |
|
| 24 | 24 | * Set the uploaded file name sanitizer |
| 25 | 25 | * |
| 26 | 26 | * @param Closure $cSanitizer The closure |
| 27 | 27 | * |
| 28 | 28 | * @return void |
| 29 | 29 | */ |
| 30 | - public function sanitizer(Closure $cSanitizer); |
|
| 30 | +public function sanitizer(Closure $cSanitizer); |
|
| 31 | 31 | |
| 32 | - /** |
|
| 32 | +/** |
|
| 33 | 33 | * Get the uploaded files |
| 34 | 34 | * |
| 35 | 35 | * @return FileInterface[] |
| 36 | 36 | */ |
| 37 | - public function files(): array; |
|
| 37 | +public function files(): array; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 39 | +/** |
|
| 40 | 40 | * Check if the current request contains uploaded files |
| 41 | 41 | * |
| 42 | 42 | * @param ServerRequestInterface $xRequest |
| 43 | 43 | * |
| 44 | 44 | * @return bool |
| 45 | 45 | */ |
| 46 | - public function canProcessRequest(ServerRequestInterface $xRequest): bool; |
|
| 46 | +public function canProcessRequest(ServerRequestInterface $xRequest): bool; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 48 | +/** |
|
| 49 | 49 | * Process the uploaded files in the HTTP request |
| 50 | 50 | * |
| 51 | 51 | * @param ServerRequestInterface $xRequest |
| 52 | 52 | * |
| 53 | 53 | * @return bool |
| 54 | 54 | */ |
| 55 | - public function processRequest(ServerRequestInterface $xRequest): bool; |
|
| 55 | +public function processRequest(ServerRequestInterface $xRequest): bool; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 57 | +/** |
|
| 58 | 58 | * @param string $sStorage |
| 59 | 59 | * @param Closure $cFactory |
| 60 | 60 | * |
| 61 | 61 | * @return void |
| 62 | 62 | */ |
| 63 | - public function registerStorageAdapter(string $sStorage, Closure $cFactory); |
|
| 63 | +public function registerStorageAdapter(string $sStorage, Closure $cFactory); |
|
| 64 | 64 | } |
@@ -28,18 +28,18 @@ discard block |
||
| 28 | 28 | |
| 29 | 29 | class CallFactory |
| 30 | 30 | { |
| 31 | - /** |
|
| 31 | +/** |
|
| 32 | 32 | * The constructor. |
| 33 | 33 | * |
| 34 | 34 | * @param ComponentContainer $cdi |
| 35 | 35 | * @param DialogCommand $xDialogCommand |
| 36 | 36 | */ |
| 37 | - public function __construct(private ComponentContainer $cdi, DialogCommand $xDialogCommand) |
|
| 38 | - { |
|
| 39 | - JsExpr::setDialogCommand($xDialogCommand); |
|
| 40 | - } |
|
| 37 | +public function __construct(private ComponentContainer $cdi, DialogCommand $xDialogCommand) |
|
| 38 | +{ |
|
| 39 | +JsExpr::setDialogCommand($xDialogCommand); |
|
| 40 | +} |
|
| 41 | 41 | |
| 42 | - /** |
|
| 42 | +/** |
|
| 43 | 43 | * Get a factory for a registered class. |
| 44 | 44 | * |
| 45 | 45 | * @param string $sClassName |
@@ -47,14 +47,14 @@ discard block |
||
| 47 | 47 | * @return JxnCall|null |
| 48 | 48 | * @throws SetupException |
| 49 | 49 | */ |
| 50 | - public function rq(string $sClassName = ''): ?JxnCall |
|
| 51 | - { |
|
| 52 | - $sClassName = trim($sClassName); |
|
| 53 | - return $sClassName === '' ? $this->cdi->getFunctionRequestFactory() : |
|
| 54 | - $this->cdi->getComponentRequestFactory($sClassName); |
|
| 55 | - } |
|
| 50 | +public function rq(string $sClassName = ''): ?JxnCall |
|
| 51 | +{ |
|
| 52 | +$sClassName = trim($sClassName); |
|
| 53 | +return $sClassName === '' ? $this->cdi->getFunctionRequestFactory() : |
|
| 54 | +$this->cdi->getComponentRequestFactory($sClassName); |
|
| 55 | +} |
|
| 56 | 56 | |
| 57 | - /** |
|
| 57 | +/** |
|
| 58 | 58 | * Get a factory for a Javascript object. |
| 59 | 59 | * |
| 60 | 60 | * @param string $sObject |
@@ -62,17 +62,17 @@ discard block |
||
| 62 | 62 | * |
| 63 | 63 | * @return JsObjectCall|null |
| 64 | 64 | */ |
| 65 | - public function jo(string $sObject = '', ?Closure $xExprCb = null): ?JsObjectCall |
|
| 66 | - { |
|
| 67 | - /* |
|
| 65 | +public function jo(string $sObject = '', ?Closure $xExprCb = null): ?JsObjectCall |
|
| 66 | +{ |
|
| 67 | +/* |
|
| 68 | 68 | * The provided closure will be called each time a js expression is created with this factory, |
| 69 | 69 | * with the expression as the only parameter. |
| 70 | 70 | * It is currently used to attach the expression to a Jaxon response. |
| 71 | 71 | */ |
| 72 | - return new JsObjectCall($xExprCb, trim($sObject, " \t")); |
|
| 73 | - } |
|
| 72 | +return new JsObjectCall($xExprCb, trim($sObject, " \t")); |
|
| 73 | +} |
|
| 74 | 74 | |
| 75 | - /** |
|
| 75 | +/** |
|
| 76 | 76 | * Get a factory for a JQuery selector. |
| 77 | 77 | * |
| 78 | 78 | * @param string $sPath The jQuery selector path |
@@ -81,17 +81,17 @@ discard block |
||
| 81 | 81 | * |
| 82 | 82 | * @return JqSelectorCall |
| 83 | 83 | */ |
| 84 | - public function jq(string $sPath = '', $xContext = null, ?Closure $xExprCb = null): JqSelectorCall |
|
| 85 | - { |
|
| 86 | - /* |
|
| 84 | +public function jq(string $sPath = '', $xContext = null, ?Closure $xExprCb = null): JqSelectorCall |
|
| 85 | +{ |
|
| 86 | +/* |
|
| 87 | 87 | * The provided closure will be called each time a js expression is created with this factory, |
| 88 | 88 | * with the expression as the only parameter. |
| 89 | 89 | * It is currently used to attach the expression to a Jaxon response. |
| 90 | 90 | */ |
| 91 | - return new JqSelectorCall($xExprCb, trim($sPath, " \t"), $xContext); |
|
| 92 | - } |
|
| 91 | +return new JqSelectorCall($xExprCb, trim($sPath, " \t"), $xContext); |
|
| 92 | +} |
|
| 93 | 93 | |
| 94 | - /** |
|
| 94 | +/** |
|
| 95 | 95 | * Get a factory for a Javascript element selector. |
| 96 | 96 | * |
| 97 | 97 | * @param string $sElementId The DOM element id |
@@ -99,13 +99,13 @@ discard block |
||
| 99 | 99 | * |
| 100 | 100 | * @return JsSelectorCall |
| 101 | 101 | */ |
| 102 | - public function je(string $sElementId = '', ?Closure $xExprCb = null): JsSelectorCall |
|
| 103 | - { |
|
| 104 | - /* |
|
| 102 | +public function je(string $sElementId = '', ?Closure $xExprCb = null): JsSelectorCall |
|
| 103 | +{ |
|
| 104 | +/* |
|
| 105 | 105 | * The provided closure will be called each time a js expression is created with this factory, |
| 106 | 106 | * with the expression as the only parameter. |
| 107 | 107 | * It is currently used to attach the expression to a Jaxon response. |
| 108 | 108 | */ |
| 109 | - return new JsSelectorCall($xExprCb, trim($sElementId, " \t")); |
|
| 110 | - } |
|
| 109 | +return new JsSelectorCall($xExprCb, trim($sElementId, " \t")); |
|
| 110 | +} |
|
| 111 | 111 | } |
@@ -19,163 +19,163 @@ |
||
| 19 | 19 | |
| 20 | 20 | class UploadFsLocalTest extends TestCase |
| 21 | 21 | { |
| 22 | - /** |
|
| 22 | +/** |
|
| 23 | 23 | * @var string |
| 24 | 24 | */ |
| 25 | - protected $sNameWhite; |
|
| 25 | +protected $sNameWhite; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 27 | +/** |
|
| 28 | 28 | * @var string |
| 29 | 29 | */ |
| 30 | - protected $sPathWhite; |
|
| 30 | +protected $sPathWhite; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 32 | +/** |
|
| 33 | 33 | * @var int |
| 34 | 34 | */ |
| 35 | - protected $sSizeWhite; |
|
| 35 | +protected $sSizeWhite; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 37 | +/** |
|
| 38 | 38 | * @var string |
| 39 | 39 | */ |
| 40 | - protected $sNameBlue; |
|
| 40 | +protected $sNameBlue; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 42 | +/** |
|
| 43 | 43 | * @var string |
| 44 | 44 | */ |
| 45 | - protected $sPathBlue; |
|
| 45 | +protected $sPathBlue; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 47 | +/** |
|
| 48 | 48 | * @var int |
| 49 | 49 | */ |
| 50 | - protected $sSizeBlue; |
|
| 50 | +protected $sSizeBlue; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 52 | +/** |
|
| 53 | 53 | * @throws SetupException |
| 54 | 54 | */ |
| 55 | - public function setUp(): void |
|
| 56 | - { |
|
| 57 | - jaxon()->di()->getPluginManager()->registerPlugins(); |
|
| 58 | - jaxon()->setOption('core.response.send', false); |
|
| 59 | - jaxon()->setOption('upload.default.dir', __DIR__ . '/../upload/dst'); |
|
| 60 | - _register(); |
|
| 61 | - |
|
| 62 | - $tmpDir = __DIR__ . '/../upload/tmp'; |
|
| 63 | - @mkdir($tmpDir); |
|
| 64 | - |
|
| 65 | - $sSrcWhite = __DIR__ . '/../upload/src/white.png'; |
|
| 66 | - $this->sNameWhite = 'white.png'; |
|
| 67 | - $this->sPathWhite = "$tmpDir/{$this->sNameWhite}"; |
|
| 68 | - $this->sSizeWhite = filesize($sSrcWhite); |
|
| 69 | - // Copy the file to the temp dir. |
|
| 70 | - @copy($sSrcWhite, $this->sPathWhite); |
|
| 71 | - |
|
| 72 | - $sSrcBlue = __DIR__ . '/../upload/src/blue.png'; |
|
| 73 | - $this->sNameBlue = 'blue.png'; |
|
| 74 | - $this->sPathBlue = "$tmpDir/{$this->sNameBlue}"; |
|
| 75 | - $this->sSizeBlue = filesize($sSrcBlue); |
|
| 76 | - // Copy the file to the temp dir. |
|
| 77 | - @copy($sSrcBlue, $this->sPathBlue); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 55 | +public function setUp(): void |
|
| 56 | +{ |
|
| 57 | +jaxon()->di()->getPluginManager()->registerPlugins(); |
|
| 58 | +jaxon()->setOption('core.response.send', false); |
|
| 59 | +jaxon()->setOption('upload.default.dir', __DIR__ . '/../upload/dst'); |
|
| 60 | +_register(); |
|
| 61 | + |
|
| 62 | +$tmpDir = __DIR__ . '/../upload/tmp'; |
|
| 63 | +@mkdir($tmpDir); |
|
| 64 | + |
|
| 65 | +$sSrcWhite = __DIR__ . '/../upload/src/white.png'; |
|
| 66 | +$this->sNameWhite = 'white.png'; |
|
| 67 | +$this->sPathWhite = "$tmpDir/{$this->sNameWhite}"; |
|
| 68 | +$this->sSizeWhite = filesize($sSrcWhite); |
|
| 69 | +// Copy the file to the temp dir. |
|
| 70 | +@copy($sSrcWhite, $this->sPathWhite); |
|
| 71 | + |
|
| 72 | +$sSrcBlue = __DIR__ . '/../upload/src/blue.png'; |
|
| 73 | +$this->sNameBlue = 'blue.png'; |
|
| 74 | +$this->sPathBlue = "$tmpDir/{$this->sNameBlue}"; |
|
| 75 | +$this->sSizeBlue = filesize($sSrcBlue); |
|
| 76 | +// Copy the file to the temp dir. |
|
| 77 | +@copy($sSrcBlue, $this->sPathBlue); |
|
| 78 | +} |
|
| 79 | + |
|
| 80 | +/** |
|
| 81 | 81 | * @throws SetupException |
| 82 | 82 | */ |
| 83 | - public function tearDown(): void |
|
| 84 | - { |
|
| 85 | - jaxon()->reset(); |
|
| 86 | - parent::tearDown(); |
|
| 87 | - } |
|
| 83 | +public function tearDown(): void |
|
| 84 | +{ |
|
| 85 | +jaxon()->reset(); |
|
| 86 | +parent::tearDown(); |
|
| 87 | +} |
|
| 88 | 88 | |
| 89 | - /** |
|
| 89 | +/** |
|
| 90 | 90 | * @throws RequestException |
| 91 | 91 | */ |
| 92 | - public function testHttpUploadDisabled() |
|
| 93 | - { |
|
| 94 | - jaxon()->setOption('core.upload.enabled', false); |
|
| 95 | - jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleUpload', __DIR__ . '/../src/sample.php'); |
|
| 96 | - |
|
| 97 | - // Send a request to the registered class |
|
| 98 | - jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
| 99 | - return $c->g(ServerRequestCreator::class) |
|
| 100 | - ->fromGlobals() |
|
| 101 | - ->withUploadedFiles([ |
|
| 102 | - 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
|
| 103 | - UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
|
| 104 | - ]) |
|
| 105 | - ->withMethod('POST'); |
|
| 106 | - }); |
|
| 107 | - |
|
| 108 | - $this->assertFalse(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 92 | +public function testHttpUploadDisabled() |
|
| 93 | +{ |
|
| 94 | +jaxon()->setOption('core.upload.enabled', false); |
|
| 95 | +jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleUpload', __DIR__ . '/../src/sample.php'); |
|
| 96 | + |
|
| 97 | +// Send a request to the registered class |
|
| 98 | +jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
| 99 | +return $c->g(ServerRequestCreator::class) |
|
| 100 | + ->fromGlobals() |
|
| 101 | + ->withUploadedFiles([ |
|
| 102 | + 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
|
| 103 | + UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
|
| 104 | + ]) |
|
| 105 | + ->withMethod('POST'); |
|
| 106 | +}); |
|
| 107 | + |
|
| 108 | +$this->assertFalse(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
| 109 | +} |
|
| 110 | + |
|
| 111 | +/** |
|
| 112 | 112 | * @throws RequestException |
| 113 | 113 | */ |
| 114 | - public function testRequestWithNoPluginNoUpload() |
|
| 115 | - { |
|
| 116 | - jaxon()->setOption('core.upload.enabled', false); |
|
| 117 | - jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleUpload', __DIR__ . '/../src/sample.php'); |
|
| 118 | - |
|
| 119 | - // Send a request to the registered class |
|
| 120 | - jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
| 121 | - return $c->g(ServerRequestCreator::class) |
|
| 122 | - ->fromGlobals() |
|
| 123 | - ->withParsedBody([ |
|
| 124 | - 'jxncall' => json_encode([ |
|
| 125 | - 'who' => 'Nobody', |
|
| 126 | - 'args' => [], |
|
| 127 | - ]), |
|
| 128 | - ]) |
|
| 129 | - ->withMethod('POST'); |
|
| 130 | - }); |
|
| 131 | - |
|
| 132 | - $this->assertFalse(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 114 | +public function testRequestWithNoPluginNoUpload() |
|
| 115 | +{ |
|
| 116 | +jaxon()->setOption('core.upload.enabled', false); |
|
| 117 | +jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleUpload', __DIR__ . '/../src/sample.php'); |
|
| 118 | + |
|
| 119 | +// Send a request to the registered class |
|
| 120 | +jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
| 121 | +return $c->g(ServerRequestCreator::class) |
|
| 122 | + ->fromGlobals() |
|
| 123 | + ->withParsedBody([ |
|
| 124 | + 'jxncall' => json_encode([ |
|
| 125 | + 'who' => 'Nobody', |
|
| 126 | + 'args' => [], |
|
| 127 | + ]), |
|
| 128 | + ]) |
|
| 129 | + ->withMethod('POST'); |
|
| 130 | +}); |
|
| 131 | + |
|
| 132 | +$this->assertFalse(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
| 133 | +} |
|
| 134 | + |
|
| 135 | +/** |
|
| 136 | 136 | * @throws RequestException |
| 137 | 137 | */ |
| 138 | - public function testUploadInMemoryStorage() |
|
| 139 | - { |
|
| 140 | - jaxon()->setOption('core.upload.enabled', true); |
|
| 141 | - jaxon()->setOption('upload.default.storage', 'memory'); |
|
| 142 | - jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleUpload', __DIR__ . '/../src/sample.php'); |
|
| 143 | - |
|
| 144 | - // In memory file system adapter |
|
| 145 | - jaxon()->upload()->registerStorageAdapter('memory', |
|
| 146 | - fn() => new InMemoryFilesystemAdapter()); |
|
| 147 | - |
|
| 148 | - // Send a request to the registered class |
|
| 149 | - jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
| 150 | - return $c->g(ServerRequestCreator::class) |
|
| 151 | - ->fromGlobals() |
|
| 152 | - ->withParsedBody([ |
|
| 153 | - 'jxncall' => json_encode([ |
|
| 154 | - 'type' => 'class', |
|
| 155 | - 'name' => 'SampleUpload', |
|
| 156 | - 'method' => 'myMethod', |
|
| 157 | - 'args' => [], |
|
| 158 | - ]), |
|
| 159 | - ]) |
|
| 160 | - ->withUploadedFiles([ |
|
| 161 | - 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
|
| 162 | - UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
|
| 163 | - ]) |
|
| 164 | - ->withMethod('POST'); |
|
| 165 | - }); |
|
| 166 | - |
|
| 167 | - $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
| 168 | - $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
|
| 169 | - jaxon()->processRequest(); |
|
| 170 | - |
|
| 171 | - // Uploaded files |
|
| 172 | - $aFiles = jaxon()->upload()->files(); |
|
| 173 | - $this->assertCount(1, $aFiles); |
|
| 174 | - $this->assertCount(1, $aFiles['image']); |
|
| 175 | - $xFile = $aFiles['image'][0]; |
|
| 176 | - $this->assertEquals('white', $xFile->name()); |
|
| 177 | - $this->assertEquals($this->sNameWhite, $xFile->filename()); |
|
| 178 | - $this->assertEquals('png', $xFile->type()); |
|
| 179 | - $this->assertEquals('png', $xFile->extension()); |
|
| 180 | - } |
|
| 138 | +public function testUploadInMemoryStorage() |
|
| 139 | +{ |
|
| 140 | +jaxon()->setOption('core.upload.enabled', true); |
|
| 141 | +jaxon()->setOption('upload.default.storage', 'memory'); |
|
| 142 | +jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleUpload', __DIR__ . '/../src/sample.php'); |
|
| 143 | + |
|
| 144 | +// In memory file system adapter |
|
| 145 | +jaxon()->upload()->registerStorageAdapter('memory', |
|
| 146 | +fn() => new InMemoryFilesystemAdapter()); |
|
| 147 | + |
|
| 148 | +// Send a request to the registered class |
|
| 149 | +jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
|
| 150 | +return $c->g(ServerRequestCreator::class) |
|
| 151 | + ->fromGlobals() |
|
| 152 | + ->withParsedBody([ |
|
| 153 | + 'jxncall' => json_encode([ |
|
| 154 | + 'type' => 'class', |
|
| 155 | + 'name' => 'SampleUpload', |
|
| 156 | + 'method' => 'myMethod', |
|
| 157 | + 'args' => [], |
|
| 158 | + ]), |
|
| 159 | + ]) |
|
| 160 | + ->withUploadedFiles([ |
|
| 161 | + 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
|
| 162 | + UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
|
| 163 | + ]) |
|
| 164 | + ->withMethod('POST'); |
|
| 165 | +}); |
|
| 166 | + |
|
| 167 | +$this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
|
| 168 | +$this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
|
| 169 | +jaxon()->processRequest(); |
|
| 170 | + |
|
| 171 | +// Uploaded files |
|
| 172 | +$aFiles = jaxon()->upload()->files(); |
|
| 173 | +$this->assertCount(1, $aFiles); |
|
| 174 | +$this->assertCount(1, $aFiles['image']); |
|
| 175 | +$xFile = $aFiles['image'][0]; |
|
| 176 | +$this->assertEquals('white', $xFile->name()); |
|
| 177 | +$this->assertEquals($this->sNameWhite, $xFile->filename()); |
|
| 178 | +$this->assertEquals('png', $xFile->type()); |
|
| 179 | +$this->assertEquals('png', $xFile->extension()); |
|
| 180 | +} |
|
| 181 | 181 | } |
@@ -22,63 +22,63 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | function registerUpload(): void |
| 24 | 24 | { |
| 25 | - $jaxon = jaxon(); |
|
| 26 | - $di = $jaxon->di(); |
|
| 27 | - if($di->h(UploadHandler::class)) |
|
| 28 | - { |
|
| 29 | - return; |
|
| 30 | - } |
|
| 25 | +$jaxon = jaxon(); |
|
| 26 | +$di = $jaxon->di(); |
|
| 27 | +if($di->h(UploadHandler::class)) |
|
| 28 | +{ |
|
| 29 | +return; |
|
| 30 | +} |
|
| 31 | 31 | |
| 32 | - // Upload file and dir name generator |
|
| 33 | - $di->set(FileNameInterface::class, function() { |
|
| 34 | - return new class implements FileNameInterface |
|
| 35 | - { |
|
| 36 | - public function random(int $nLength): string |
|
| 37 | - { |
|
| 38 | - return bin2hex(random_bytes((int)($nLength / 2))); |
|
| 39 | - } |
|
| 40 | - }; |
|
| 41 | - }); |
|
| 42 | - // Upload validator |
|
| 43 | - $di->set(Validator::class, function($c) { |
|
| 44 | - return new Validator($c->g(ConfigManager::class), $c->g(Translator::class)); |
|
| 45 | - }); |
|
| 46 | - // File storage |
|
| 47 | - $di->set(FileStorage::class, function($c) { |
|
| 48 | - return new FileStorage($c->g(ConfigManager::class), $c->g(Translator::class)); |
|
| 49 | - }); |
|
| 50 | - // File upload manager |
|
| 51 | - $di->set(UploadManager::class, function($c) { |
|
| 52 | - // Translation directory |
|
| 53 | - $sTranslationDir = dirname(__DIR__) . '/translations'; |
|
| 54 | - // Load the upload translations |
|
| 55 | - $xTranslator = $c->g(Translator::class); |
|
| 56 | - $xTranslator->loadTranslations("$sTranslationDir/en/upload.php", 'en'); |
|
| 57 | - $xTranslator->loadTranslations("$sTranslationDir/fr/upload.php", 'fr'); |
|
| 58 | - $xTranslator->loadTranslations("$sTranslationDir/es/upload.php", 'es'); |
|
| 32 | +// Upload file and dir name generator |
|
| 33 | +$di->set(FileNameInterface::class, function() { |
|
| 34 | +return new class implements FileNameInterface |
|
| 35 | +{ |
|
| 36 | +public function random(int $nLength): string |
|
| 37 | +{ |
|
| 38 | + return bin2hex(random_bytes((int)($nLength / 2))); |
|
| 39 | +} |
|
| 40 | +}; |
|
| 41 | +}); |
|
| 42 | +// Upload validator |
|
| 43 | +$di->set(Validator::class, function($c) { |
|
| 44 | +return new Validator($c->g(ConfigManager::class), $c->g(Translator::class)); |
|
| 45 | +}); |
|
| 46 | +// File storage |
|
| 47 | +$di->set(FileStorage::class, function($c) { |
|
| 48 | +return new FileStorage($c->g(ConfigManager::class), $c->g(Translator::class)); |
|
| 49 | +}); |
|
| 50 | +// File upload manager |
|
| 51 | +$di->set(UploadManager::class, function($c) { |
|
| 52 | +// Translation directory |
|
| 53 | +$sTranslationDir = dirname(__DIR__) . '/translations'; |
|
| 54 | +// Load the upload translations |
|
| 55 | +$xTranslator = $c->g(Translator::class); |
|
| 56 | +$xTranslator->loadTranslations("$sTranslationDir/en/upload.php", 'en'); |
|
| 57 | +$xTranslator->loadTranslations("$sTranslationDir/fr/upload.php", 'fr'); |
|
| 58 | +$xTranslator->loadTranslations("$sTranslationDir/es/upload.php", 'es'); |
|
| 59 | 59 | |
| 60 | - return new UploadManager($c->g(LoggerInterface::class), $c->g(Validator::class), |
|
| 61 | - $xTranslator, $c->g(FileStorage::class), |
|
| 62 | - $c->g(FileNameInterface::class), $c->g(ConfigManager::class)); |
|
| 63 | - }); |
|
| 64 | - // File upload plugin |
|
| 65 | - $di->set(UploadHandler::class, function($c) { |
|
| 66 | - return new UploadHandler($c->g(FileStorage::class), $c->g(UploadManager::class)); |
|
| 67 | - }); |
|
| 68 | - // Set alias on the interface |
|
| 69 | - $di->alias(UploadHandlerInterface::class, UploadHandler::class); |
|
| 60 | +return new UploadManager($c->g(LoggerInterface::class), $c->g(Validator::class), |
|
| 61 | +$xTranslator, $c->g(FileStorage::class), |
|
| 62 | +$c->g(FileNameInterface::class), $c->g(ConfigManager::class)); |
|
| 63 | +}); |
|
| 64 | +// File upload plugin |
|
| 65 | +$di->set(UploadHandler::class, function($c) { |
|
| 66 | +return new UploadHandler($c->g(FileStorage::class), $c->g(UploadManager::class)); |
|
| 67 | +}); |
|
| 68 | +// Set alias on the interface |
|
| 69 | +$di->alias(UploadHandlerInterface::class, UploadHandler::class); |
|
| 70 | 70 | |
| 71 | - // Set a callback to process uploaded files in the incoming requests. |
|
| 72 | - $jaxon->callback()->before(function() use($di) { |
|
| 73 | - /** @var UploadHandler */ |
|
| 74 | - $xUploadHandler = $di->g(UploadHandler::class); |
|
| 75 | - // The HTTP request |
|
| 76 | - $xRequest = $di->getRequest(); |
|
| 77 | - if($xUploadHandler->canProcessRequest($xRequest)) |
|
| 78 | - { |
|
| 79 | - $xUploadHandler->processRequest($xRequest); |
|
| 80 | - } |
|
| 81 | - }); |
|
| 71 | +// Set a callback to process uploaded files in the incoming requests. |
|
| 72 | +$jaxon->callback()->before(function() use($di) { |
|
| 73 | +/** @var UploadHandler */ |
|
| 74 | +$xUploadHandler = $di->g(UploadHandler::class); |
|
| 75 | +// The HTTP request |
|
| 76 | +$xRequest = $di->getRequest(); |
|
| 77 | +if($xUploadHandler->canProcessRequest($xRequest)) |
|
| 78 | +{ |
|
| 79 | +$xUploadHandler->processRequest($xRequest); |
|
| 80 | +} |
|
| 81 | +}); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | /** |
@@ -88,22 +88,22 @@ discard block |
||
| 88 | 88 | */ |
| 89 | 89 | function _register(): void |
| 90 | 90 | { |
| 91 | - $jaxon = jaxon(); |
|
| 92 | - $jaxon->callback()->boot(function() use($jaxon) { |
|
| 93 | - if($jaxon->getOption('core.upload.enabled')) |
|
| 94 | - { |
|
| 95 | - registerUpload(); |
|
| 96 | - } |
|
| 97 | - }); |
|
| 91 | +$jaxon = jaxon(); |
|
| 92 | +$jaxon->callback()->boot(function() use($jaxon) { |
|
| 93 | +if($jaxon->getOption('core.upload.enabled')) |
|
| 94 | +{ |
|
| 95 | +registerUpload(); |
|
| 96 | +} |
|
| 97 | +}); |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | function register() |
| 101 | 101 | { |
| 102 | - // Do nothing if running in cli. |
|
| 103 | - if(php_sapi_name() !== 'cli') |
|
| 104 | - { |
|
| 105 | - _register(); |
|
| 106 | - }; |
|
| 102 | +// Do nothing if running in cli. |
|
| 103 | +if(php_sapi_name() !== 'cli') |
|
| 104 | +{ |
|
| 105 | +_register(); |
|
| 106 | +}; |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | register(); |
@@ -28,146 +28,146 @@ |
||
| 28 | 28 | |
| 29 | 29 | class FileStorage |
| 30 | 30 | { |
| 31 | - /** |
|
| 31 | +/** |
|
| 32 | 32 | * @var ConfigManager |
| 33 | 33 | */ |
| 34 | - protected $xConfigManager; |
|
| 34 | +protected $xConfigManager; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 36 | +/** |
|
| 37 | 37 | * @var Translator |
| 38 | 38 | */ |
| 39 | - protected $xTranslator; |
|
| 39 | +protected $xTranslator; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 41 | +/** |
|
| 42 | 42 | * @var array |
| 43 | 43 | */ |
| 44 | - protected $aAdapters = []; |
|
| 44 | +protected $aAdapters = []; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 46 | +/** |
|
| 47 | 47 | * @var array |
| 48 | 48 | */ |
| 49 | - protected $aFilesystems = []; |
|
| 49 | +protected $aFilesystems = []; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 51 | +/** |
|
| 52 | 52 | * The constructor |
| 53 | 53 | * |
| 54 | 54 | * @param ConfigManager $xConfigManager |
| 55 | 55 | * @param Translator $xTranslator |
| 56 | 56 | */ |
| 57 | - public function __construct(ConfigManager $xConfigManager, Translator $xTranslator) |
|
| 58 | - { |
|
| 59 | - $this->xConfigManager = $xConfigManager; |
|
| 60 | - $this->xTranslator = $xTranslator; |
|
| 61 | - $this->registerAdapters(); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 57 | +public function __construct(ConfigManager $xConfigManager, Translator $xTranslator) |
|
| 58 | +{ |
|
| 59 | +$this->xConfigManager = $xConfigManager; |
|
| 60 | +$this->xTranslator = $xTranslator; |
|
| 61 | +$this->registerAdapters(); |
|
| 62 | +} |
|
| 63 | + |
|
| 64 | +/** |
|
| 65 | 65 | * @param string $sStorage |
| 66 | 66 | * @param Closure $cFactory |
| 67 | 67 | * |
| 68 | 68 | * @return void |
| 69 | 69 | */ |
| 70 | - public function registerAdapter(string $sStorage, Closure $cFactory) |
|
| 71 | - { |
|
| 72 | - $this->aAdapters[$sStorage] = $cFactory; |
|
| 73 | - } |
|
| 70 | +public function registerAdapter(string $sStorage, Closure $cFactory) |
|
| 71 | +{ |
|
| 72 | +$this->aAdapters[$sStorage] = $cFactory; |
|
| 73 | +} |
|
| 74 | 74 | |
| 75 | - /** |
|
| 75 | +/** |
|
| 76 | 76 | * Register the file storage adapters |
| 77 | 77 | * |
| 78 | 78 | * @return void |
| 79 | 79 | */ |
| 80 | - private function registerAdapters() |
|
| 81 | - { |
|
| 82 | - // Local file system adapter |
|
| 83 | - $this->registerAdapter('local', function(string $sRootDir, $xOptions) { |
|
| 84 | - return empty($xOptions) ? new LocalFilesystemAdapter($sRootDir) : |
|
| 85 | - new LocalFilesystemAdapter($sRootDir, $xOptions); |
|
| 86 | - }); |
|
| 87 | - |
|
| 88 | - // AWS S3 file system adapter |
|
| 89 | - $this->registerAdapter('aws-s3', function(string $sRootDir, array $aOptions) { |
|
| 90 | - /** @var \Aws\S3\S3ClientInterface $client */ |
|
| 91 | - $client = new \Aws\S3\S3Client($aOptions['client'] ?? []); |
|
| 92 | - return new \League\Flysystem\AwsS3V3\AwsS3V3Adapter($client, $aOptions['bucket'] ?? '', $sRootDir); |
|
| 93 | - }); |
|
| 94 | - |
|
| 95 | - // Async AWS S3 file system adapter |
|
| 96 | - $this->registerAdapter('async-aws-s3', function(string $sRootDir, array $aOptions) { |
|
| 97 | - $client = isset($aOptions['client']) ? new \AsyncAws\S3\S3Client($aOptions['client']) : new \AsyncAws\S3\S3Client(); |
|
| 98 | - return new \League\Flysystem\AsyncAwsS3\AsyncAwsS3Adapter($client, $aOptions['bucket'] ?? '', $sRootDir); |
|
| 99 | - }); |
|
| 100 | - |
|
| 101 | - // Google Cloud file system adapter |
|
| 102 | - $this->registerAdapter('google-cloud', function(string $sRootDir, array $aOptions) { |
|
| 103 | - $storageClient = new \Google\Cloud\Storage\StorageClient($aOptions['client'] ?? []); |
|
| 104 | - $bucket = $storageClient->bucket($aOptions['bucket'] ?? ''); |
|
| 105 | - return new \League\Flysystem\AzureBlobStorage\GoogleCloudStorageAdapter($bucket, $sRootDir); |
|
| 106 | - }); |
|
| 107 | - |
|
| 108 | - // Microsoft Azure file system adapter |
|
| 109 | - $this->registerAdapter('azure-blob', function(string $sRootDir, array $aOptions) { |
|
| 110 | - $client = \MicrosoftAzure\Storage\Blob\BlobRestProxy::createBlobService($aOptions['dsn']); |
|
| 111 | - return new \League\Flysystem\AzureBlobStorage\AzureBlobStorageAdapter($client, $aOptions['container'], $sRootDir); |
|
| 112 | - }); |
|
| 113 | - |
|
| 114 | - // FTP file system adapter |
|
| 115 | - $this->registerAdapter('ftp', function(string $sRootDir, array $aOptions) { |
|
| 116 | - $aOptions['root'] = $sRootDir; |
|
| 117 | - $xOptions = \League\Flysystem\Ftp\FtpConnectionOptions::fromArray($aOptions); |
|
| 118 | - return new \League\Flysystem\Ftp\FtpAdapter($xOptions); |
|
| 119 | - }); |
|
| 120 | - |
|
| 121 | - // SFTP V2 file system adapter |
|
| 122 | - $this->registerAdapter('sftp-v2', function(string $sRootDir, array $aOptions) { |
|
| 123 | - $provider = new \League\Flysystem\PhpseclibV2\SftpConnectionProvider(...$aOptions); |
|
| 124 | - return new \League\Flysystem\PhpseclibV2\SftpAdapter($provider, $sRootDir); |
|
| 125 | - }); |
|
| 126 | - |
|
| 127 | - // SFTP V3 file system adapter |
|
| 128 | - $this->registerAdapter('sftp-v3', function(string $sRootDir, array $aOptions) { |
|
| 129 | - $provider = new \League\Flysystem\PhpseclibV3\SftpConnectionProvider(...$aOptions); |
|
| 130 | - return new \League\Flysystem\PhpseclibV3\SftpAdapter($provider, $sRootDir); |
|
| 131 | - }); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 80 | +private function registerAdapters() |
|
| 81 | +{ |
|
| 82 | +// Local file system adapter |
|
| 83 | +$this->registerAdapter('local', function(string $sRootDir, $xOptions) { |
|
| 84 | +return empty($xOptions) ? new LocalFilesystemAdapter($sRootDir) : |
|
| 85 | + new LocalFilesystemAdapter($sRootDir, $xOptions); |
|
| 86 | +}); |
|
| 87 | + |
|
| 88 | +// AWS S3 file system adapter |
|
| 89 | +$this->registerAdapter('aws-s3', function(string $sRootDir, array $aOptions) { |
|
| 90 | +/** @var \Aws\S3\S3ClientInterface $client */ |
|
| 91 | +$client = new \Aws\S3\S3Client($aOptions['client'] ?? []); |
|
| 92 | +return new \League\Flysystem\AwsS3V3\AwsS3V3Adapter($client, $aOptions['bucket'] ?? '', $sRootDir); |
|
| 93 | +}); |
|
| 94 | + |
|
| 95 | +// Async AWS S3 file system adapter |
|
| 96 | +$this->registerAdapter('async-aws-s3', function(string $sRootDir, array $aOptions) { |
|
| 97 | +$client = isset($aOptions['client']) ? new \AsyncAws\S3\S3Client($aOptions['client']) : new \AsyncAws\S3\S3Client(); |
|
| 98 | +return new \League\Flysystem\AsyncAwsS3\AsyncAwsS3Adapter($client, $aOptions['bucket'] ?? '', $sRootDir); |
|
| 99 | +}); |
|
| 100 | + |
|
| 101 | +// Google Cloud file system adapter |
|
| 102 | +$this->registerAdapter('google-cloud', function(string $sRootDir, array $aOptions) { |
|
| 103 | +$storageClient = new \Google\Cloud\Storage\StorageClient($aOptions['client'] ?? []); |
|
| 104 | +$bucket = $storageClient->bucket($aOptions['bucket'] ?? ''); |
|
| 105 | +return new \League\Flysystem\AzureBlobStorage\GoogleCloudStorageAdapter($bucket, $sRootDir); |
|
| 106 | +}); |
|
| 107 | + |
|
| 108 | +// Microsoft Azure file system adapter |
|
| 109 | +$this->registerAdapter('azure-blob', function(string $sRootDir, array $aOptions) { |
|
| 110 | +$client = \MicrosoftAzure\Storage\Blob\BlobRestProxy::createBlobService($aOptions['dsn']); |
|
| 111 | +return new \League\Flysystem\AzureBlobStorage\AzureBlobStorageAdapter($client, $aOptions['container'], $sRootDir); |
|
| 112 | +}); |
|
| 113 | + |
|
| 114 | +// FTP file system adapter |
|
| 115 | +$this->registerAdapter('ftp', function(string $sRootDir, array $aOptions) { |
|
| 116 | +$aOptions['root'] = $sRootDir; |
|
| 117 | +$xOptions = \League\Flysystem\Ftp\FtpConnectionOptions::fromArray($aOptions); |
|
| 118 | +return new \League\Flysystem\Ftp\FtpAdapter($xOptions); |
|
| 119 | +}); |
|
| 120 | + |
|
| 121 | +// SFTP V2 file system adapter |
|
| 122 | +$this->registerAdapter('sftp-v2', function(string $sRootDir, array $aOptions) { |
|
| 123 | +$provider = new \League\Flysystem\PhpseclibV2\SftpConnectionProvider(...$aOptions); |
|
| 124 | +return new \League\Flysystem\PhpseclibV2\SftpAdapter($provider, $sRootDir); |
|
| 125 | +}); |
|
| 126 | + |
|
| 127 | +// SFTP V3 file system adapter |
|
| 128 | +$this->registerAdapter('sftp-v3', function(string $sRootDir, array $aOptions) { |
|
| 129 | +$provider = new \League\Flysystem\PhpseclibV3\SftpConnectionProvider(...$aOptions); |
|
| 130 | +return new \League\Flysystem\PhpseclibV3\SftpAdapter($provider, $sRootDir); |
|
| 131 | +}); |
|
| 132 | +} |
|
| 133 | + |
|
| 134 | +/** |
|
| 135 | 135 | * @param string $sField |
| 136 | 136 | * |
| 137 | 137 | * @return Filesystem |
| 138 | 138 | * @throws RequestException |
| 139 | 139 | */ |
| 140 | - public function filesystem(string $sField = ''): Filesystem |
|
| 141 | - { |
|
| 142 | - $sField = trim($sField); |
|
| 143 | - if(isset($this->aFilesystems[$sField])) |
|
| 144 | - { |
|
| 145 | - return $this->aFilesystems[$sField]; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - // Default upload dir |
|
| 149 | - $sStorage = $this->xConfigManager->getOption('upload.default.storage', 'local'); |
|
| 150 | - $sRootDir = $this->xConfigManager->getOption('upload.default.dir', ''); |
|
| 151 | - $aOptions = $this->xConfigManager->getOption('upload.default.options'); |
|
| 152 | - $sConfigKey = "upload.files.$sField"; |
|
| 153 | - if($sField !== '' && $this->xConfigManager->hasOption($sConfigKey)) |
|
| 154 | - { |
|
| 155 | - $sStorage = $this->xConfigManager->getOption("$sConfigKey.storage", $sStorage); |
|
| 156 | - $sRootDir = $this->xConfigManager->getOption("$sConfigKey.dir", $sRootDir); |
|
| 157 | - $aOptions = $this->xConfigManager->getOption("$sConfigKey.options", $aOptions); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - if(!is_string($sRootDir)) |
|
| 161 | - { |
|
| 162 | - throw new RequestException($this->xTranslator->trans('errors.upload.dir')); |
|
| 163 | - } |
|
| 164 | - if(!isset($this->aAdapters[$sStorage])) |
|
| 165 | - { |
|
| 166 | - throw new RequestException($this->xTranslator->trans('errors.upload.adapter')); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - $xAdapter = call_user_func($this->aAdapters[$sStorage], $sRootDir, $aOptions); |
|
| 170 | - $this->aFilesystems[$sField] = new Filesystem($xAdapter); |
|
| 171 | - return $this->aFilesystems[$sField]; |
|
| 172 | - } |
|
| 140 | +public function filesystem(string $sField = ''): Filesystem |
|
| 141 | +{ |
|
| 142 | +$sField = trim($sField); |
|
| 143 | +if(isset($this->aFilesystems[$sField])) |
|
| 144 | +{ |
|
| 145 | +return $this->aFilesystems[$sField]; |
|
| 146 | +} |
|
| 147 | + |
|
| 148 | +// Default upload dir |
|
| 149 | +$sStorage = $this->xConfigManager->getOption('upload.default.storage', 'local'); |
|
| 150 | +$sRootDir = $this->xConfigManager->getOption('upload.default.dir', ''); |
|
| 151 | +$aOptions = $this->xConfigManager->getOption('upload.default.options'); |
|
| 152 | +$sConfigKey = "upload.files.$sField"; |
|
| 153 | +if($sField !== '' && $this->xConfigManager->hasOption($sConfigKey)) |
|
| 154 | +{ |
|
| 155 | +$sStorage = $this->xConfigManager->getOption("$sConfigKey.storage", $sStorage); |
|
| 156 | +$sRootDir = $this->xConfigManager->getOption("$sConfigKey.dir", $sRootDir); |
|
| 157 | +$aOptions = $this->xConfigManager->getOption("$sConfigKey.options", $aOptions); |
|
| 158 | +} |
|
| 159 | + |
|
| 160 | +if(!is_string($sRootDir)) |
|
| 161 | +{ |
|
| 162 | +throw new RequestException($this->xTranslator->trans('errors.upload.dir')); |
|
| 163 | +} |
|
| 164 | +if(!isset($this->aAdapters[$sStorage])) |
|
| 165 | +{ |
|
| 166 | +throw new RequestException($this->xTranslator->trans('errors.upload.adapter')); |
|
| 167 | +} |
|
| 168 | + |
|
| 169 | +$xAdapter = call_user_func($this->aAdapters[$sStorage], $sRootDir, $aOptions); |
|
| 170 | +$this->aFilesystems[$sField] = new Filesystem($xAdapter); |
|
| 171 | +return $this->aFilesystems[$sField]; |
|
| 172 | +} |
|
| 173 | 173 | } |
@@ -30,46 +30,46 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | class Validator |
| 32 | 32 | {
|
| 33 | - /** |
|
| 33 | +/** |
|
| 34 | 34 | * The config manager |
| 35 | 35 | * |
| 36 | 36 | * @var ConfigManager |
| 37 | 37 | */ |
| 38 | - protected $xConfigManager; |
|
| 38 | +protected $xConfigManager; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 40 | +/** |
|
| 41 | 41 | * The translator |
| 42 | 42 | * |
| 43 | 43 | * @var Translator |
| 44 | 44 | */ |
| 45 | - protected $xTranslator; |
|
| 45 | +protected $xTranslator; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 47 | +/** |
|
| 48 | 48 | * The last error message |
| 49 | 49 | * |
| 50 | 50 | * @var string |
| 51 | 51 | */ |
| 52 | - protected $sErrorMessage; |
|
| 52 | +protected $sErrorMessage; |
|
| 53 | 53 | |
| 54 | - public function __construct(ConfigManager $xConfigManager, Translator $xTranslator) |
|
| 55 | - {
|
|
| 56 | - // Set the config manager |
|
| 57 | - $this->xConfigManager = $xConfigManager; |
|
| 58 | - // Set the translator |
|
| 59 | - $this->xTranslator = $xTranslator; |
|
| 60 | - } |
|
| 54 | +public function __construct(ConfigManager $xConfigManager, Translator $xTranslator) |
|
| 55 | +{
|
|
| 56 | +// Set the config manager |
|
| 57 | +$this->xConfigManager = $xConfigManager; |
|
| 58 | +// Set the translator |
|
| 59 | +$this->xTranslator = $xTranslator; |
|
| 60 | +} |
|
| 61 | 61 | |
| 62 | - /** |
|
| 62 | +/** |
|
| 63 | 63 | * Get the last error message |
| 64 | 64 | * |
| 65 | 65 | * @return string |
| 66 | 66 | */ |
| 67 | - public function getErrorMessage(): string |
|
| 68 | - {
|
|
| 69 | - return $this->sErrorMessage; |
|
| 70 | - } |
|
| 67 | +public function getErrorMessage(): string |
|
| 68 | +{
|
|
| 69 | +return $this->sErrorMessage; |
|
| 70 | +} |
|
| 71 | 71 | |
| 72 | - /** |
|
| 72 | +/** |
|
| 73 | 73 | * Validate a property of an uploaded file |
| 74 | 74 | * |
| 75 | 75 | * @param string $sVarName The uploaded file variable name |
@@ -79,19 +79,19 @@ discard block |
||
| 79 | 79 | * |
| 80 | 80 | * @return bool |
| 81 | 81 | */ |
| 82 | - private function validateFileProperty(string $sVarName, string $sValue, string $sProperty, string $sField): bool |
|
| 83 | - {
|
|
| 84 | - $xDefault = $this->xConfigManager->getOption('upload.default.' . $sProperty);
|
|
| 85 | - $aAllowed = $this->xConfigManager->getOption('upload.files.' . $sVarName . '.' . $sProperty, $xDefault);
|
|
| 86 | - if(is_array($aAllowed) && !in_array($sValue, $aAllowed)) |
|
| 87 | - {
|
|
| 88 | - $this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sField, [$sField => $sValue]);
|
|
| 89 | - return false; |
|
| 90 | - } |
|
| 91 | - return true; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 82 | +private function validateFileProperty(string $sVarName, string $sValue, string $sProperty, string $sField): bool |
|
| 83 | +{
|
|
| 84 | +$xDefault = $this->xConfigManager->getOption('upload.default.' . $sProperty);
|
|
| 85 | +$aAllowed = $this->xConfigManager->getOption('upload.files.' . $sVarName . '.' . $sProperty, $xDefault);
|
|
| 86 | +if(is_array($aAllowed) && !in_array($sValue, $aAllowed)) |
|
| 87 | +{
|
|
| 88 | +$this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sField, [$sField => $sValue]);
|
|
| 89 | +return false; |
|
| 90 | +} |
|
| 91 | +return true; |
|
| 92 | +} |
|
| 93 | + |
|
| 94 | +/** |
|
| 95 | 95 | * Validate the size of an uploaded file |
| 96 | 96 | * |
| 97 | 97 | * @param string $sVarName The uploaded file variable name |
@@ -100,21 +100,21 @@ discard block |
||
| 100 | 100 | * |
| 101 | 101 | * @return bool |
| 102 | 102 | */ |
| 103 | - private function validateFileSize(string $sVarName, int $nFileSize, string $sProperty): bool |
|
| 104 | - {
|
|
| 105 | - $xDefault = $this->xConfigManager->getOption('upload.default.' . $sProperty, 0);
|
|
| 106 | - $nSize = $this->xConfigManager->getOption('upload.files.' . $sVarName . '.' . $sProperty, $xDefault);
|
|
| 107 | - if($nSize > 0 && ( |
|
| 108 | - ($sProperty == 'max-size' && $nFileSize > $nSize) || |
|
| 109 | - ($sProperty == 'min-size' && $nFileSize < $nSize))) |
|
| 110 | - {
|
|
| 111 | - $this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sProperty, ['size' => $nFileSize]);
|
|
| 112 | - return false; |
|
| 113 | - } |
|
| 114 | - return true; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 103 | +private function validateFileSize(string $sVarName, int $nFileSize, string $sProperty): bool |
|
| 104 | +{
|
|
| 105 | +$xDefault = $this->xConfigManager->getOption('upload.default.' . $sProperty, 0);
|
|
| 106 | +$nSize = $this->xConfigManager->getOption('upload.files.' . $sVarName . '.' . $sProperty, $xDefault);
|
|
| 107 | +if($nSize > 0 && ( |
|
| 108 | +($sProperty == 'max-size' && $nFileSize > $nSize) || |
|
| 109 | +($sProperty == 'min-size' && $nFileSize < $nSize))) |
|
| 110 | +{
|
|
| 111 | +$this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sProperty, ['size' => $nFileSize]);
|
|
| 112 | +return false; |
|
| 113 | +} |
|
| 114 | +return true; |
|
| 115 | +} |
|
| 116 | + |
|
| 117 | +/** |
|
| 118 | 118 | * Validate an uploaded file |
| 119 | 119 | * |
| 120 | 120 | * @param string $sVarName The uploaded file variable name |
@@ -122,33 +122,33 @@ discard block |
||
| 122 | 122 | * |
| 123 | 123 | * @return bool |
| 124 | 124 | */ |
| 125 | - public function validateUploadedFile(string $sVarName, File $xFile): bool |
|
| 126 | - {
|
|
| 127 | - $this->sErrorMessage = ''; |
|
| 128 | - // Verify the file extension |
|
| 129 | - if(!$this->validateFileProperty($sVarName, $xFile->type(), 'types', 'type')) |
|
| 130 | - {
|
|
| 131 | - return false; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // Verify the file extension |
|
| 135 | - if(!$this->validateFileProperty($sVarName, $xFile->extension(), 'extensions', 'extension')) |
|
| 136 | - {
|
|
| 137 | - return false; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - // Verify the max size |
|
| 141 | - if(!$this->validateFileSize($sVarName, $xFile->size(), 'max-size')) |
|
| 142 | - {
|
|
| 143 | - return false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // Verify the min size |
|
| 147 | - if(!$this->validateFileSize($sVarName, $xFile->size(), 'min-size')) |
|
| 148 | - {
|
|
| 149 | - return false; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - return true; |
|
| 153 | - } |
|
| 125 | +public function validateUploadedFile(string $sVarName, File $xFile): bool |
|
| 126 | +{
|
|
| 127 | +$this->sErrorMessage = ''; |
|
| 128 | +// Verify the file extension |
|
| 129 | +if(!$this->validateFileProperty($sVarName, $xFile->type(), 'types', 'type')) |
|
| 130 | +{
|
|
| 131 | +return false; |
|
| 132 | +} |
|
| 133 | + |
|
| 134 | +// Verify the file extension |
|
| 135 | +if(!$this->validateFileProperty($sVarName, $xFile->extension(), 'extensions', 'extension')) |
|
| 136 | +{
|
|
| 137 | +return false; |
|
| 138 | +} |
|
| 139 | + |
|
| 140 | +// Verify the max size |
|
| 141 | +if(!$this->validateFileSize($sVarName, $xFile->size(), 'max-size')) |
|
| 142 | +{
|
|
| 143 | +return false; |
|
| 144 | +} |
|
| 145 | + |
|
| 146 | +// Verify the min size |
|
| 147 | +if(!$this->validateFileSize($sVarName, $xFile->size(), 'min-size')) |
|
| 148 | +{
|
|
| 149 | +return false; |
|
| 150 | +} |
|
| 151 | + |
|
| 152 | +return true; |
|
| 153 | +} |
|
| 154 | 154 | } |