Skip to content

Commit eb55b12

Browse files
committed
AC-13573: Investigate replacement of package - laminas/laminas-file
1 parent 4865fbf commit eb55b12

File tree

9 files changed

+230
-82
lines changed

9 files changed

+230
-82
lines changed

app/code/Magento/Catalog/Model/Product/Option/Type/File/ValidatorFile.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace Magento\Catalog\Model\Product\Option\Type\File;
99

10-
use Laminas\Filter\File\Rename;
11-
use Laminas\File\Transfer\Exception\PhpEnvironmentException;
1210
use Magento\Catalog\Model\Product;
1311
use Magento\Catalog\Model\Product\Exception as ProductException;
1412
use Magento\Framework\App\Filesystem\DirectoryList;
@@ -126,7 +124,6 @@ public function setProduct(Product $product)
126124
* @throws \Exception
127125
* @throws \Magento\Framework\Exception\InputException
128126
* @throws \Magento\Framework\Validator\Exception
129-
* @throws PhpEnvironmentException
130127
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
131128
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
132129
*/
@@ -184,8 +181,6 @@ public function validate($processingParams, $option)
184181
$filePath = $dispersion . '/' . $fileName;
185182
$fileFullPath = $this->mediaDirectory->getAbsolutePath($this->quotePath . $filePath);
186183

187-
$upload->addFilter(new Rename(['target' => $fileFullPath, 'overwrite' => true]));
188-
189184
if ($this->product !== null) {
190185
$this->product->getTypeInstance()->addFileQueue(
191186
[

app/code/Magento/ImportExport/Model/Source/Upload.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\ImportExport\Model\Source;
99

10-
use Laminas\File\Transfer\Adapter\Http;
10+
use Magento\Framework\File\HttpInterface as Http;
1111
use Laminas\Validator\File\Upload as FileUploadValidator;
1212
use Magento\Framework\App\Filesystem\DirectoryList;
1313
use Magento\Framework\Exception\LocalizedException;

app/code/Magento/ImportExport/Test/Unit/Model/Source/UploadTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\ImportExport\Test\Unit\Model\Source;
99

10-
use Laminas\File\Transfer\Adapter\Http;
10+
use Magento\Framework\File\Http;
1111
use Magento\Framework\Filesystem;
1212
use Magento\Framework\Filesystem\Directory\WriteInterface;
1313
use Magento\Framework\HTTP\Adapter\FileTransferFactory;

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
"magento/elasticsearch": "^1.0.0",
7676
"magento/laminas-db": "^1.0.0",
7777
"magento/laminas-di": "^1.0.0",
78-
"magento/laminas-file": "^1.0.0",
7978
"magento/magento-composer-installer": ">=0.4.0",
8079
"magento/ringphp": "^1.0.0",
8180
"magento/zend-cache": "^1.16",

composer.lock

Lines changed: 3 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Type/AbstractTypeTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\Catalog\Model\Product\Type;
99

10-
use Laminas\File\Transfer\Adapter\Http;
10+
use Magento\Framework\File\Http;
1111
use Magento\Catalog\Api\ProductRepositoryInterface;
1212
use Magento\Catalog\Model\Product;
1313
use Magento\Catalog\Model\Product\Option;
Lines changed: 157 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,176 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\Framework\File;
99

10-
use Laminas\File\Transfer\Adapter\Http as LaminasHttp;
10+
use Laminas\Validator\File\Upload;
11+
use Laminas\Validator\ValidatorInterface;
12+
use Magento\Framework\Exception\InputException;
1113

12-
class Http extends LaminasHttp
14+
class Http implements HttpInterface
1315
{
16+
17+
/**
18+
* Internal list of validators
19+
* @var array
20+
*/
21+
protected $validators = [];
22+
23+
/**
24+
* Internal list of files
25+
* @var array
26+
*/
27+
protected $files = [];
28+
29+
/**
30+
* TMP directory
31+
* @var string
32+
*/
33+
protected $tmpDir;
34+
35+
/**
36+
* Available options for file transfers
37+
* @var array
38+
*/
39+
protected $options = [
40+
'ignoreNoFile' => false,
41+
'useByteString' => true,
42+
'magicFile' => null,
43+
'detectInfos' => true,
44+
];
45+
46+
/**
47+
*
48+
* @var array
49+
*/
50+
protected $messages = [];
51+
52+
/**
53+
* Constructor for Http File Transfers
54+
*
55+
* @param array $options
56+
* @throws InputException
57+
*/
58+
59+
public function __construct(
60+
array $options = [],
61+
) {
62+
$this->options = array_merge($this->options, $options);
63+
$this->prepareFiles();
64+
$this->addValidator(new Upload());
65+
}
66+
/**
67+
* Checks if the files are valid
68+
*
69+
* @param string|array $files (Optional) Files to check
70+
* @return bool True if all checks are valid
71+
*/
72+
public function isValid($files = null): bool
73+
{
74+
$fileContent = $this->getFileInfo($files);
75+
76+
$valid = true;
77+
foreach ($fileContent as $file) {
78+
foreach ($this->validators as $validator) {
79+
if (!$validator->isValid($file['tmp_name'], $file)) {
80+
$valid = false;
81+
$this->messages += $validator->getMessages();
82+
}
83+
}
84+
}
85+
return $valid;
86+
}
87+
1488
/**
15-
* Method to change file name.
89+
* Prepare the $_FILES array to match the internal syntax of one file per entry
1690
*
17-
* @return Http
91+
* @return HttpInterface
1892
*/
19-
protected function prepareFiles()
93+
protected function prepareFiles() : HttpInterface
2094
{
21-
$http = parent::prepareFiles();
95+
96+
$this->files = [];
97+
$options = $this->options;
98+
foreach ($_FILES as $form => $content) {
99+
$content['options'] = $options;
100+
$content['validated'] = false;
101+
$content['received'] = false;
102+
$content['filtered'] = false;
103+
$this->files[$form] = $content;
104+
}
105+
return $this;
106+
}
22107

23-
foreach (array_keys($http->files) as $key) {
24-
$http->files[$key]['name'] = str_replace(
25-
basename($this->files[$key]['tmp_name']) . '_',
26-
'',
27-
$this->files[$key]['name']
108+
/**
109+
* Retrieve error codes
110+
*
111+
* @return array
112+
*/
113+
public function getErrors(): array
114+
{
115+
return array_keys($this->messages);
116+
}
117+
118+
/**
119+
* Adds a new validator for this class
120+
*
121+
* @param string|ValidatorInterface $validator
122+
* @return HttpInterface
123+
* @throws InputException
124+
*/
125+
public function addValidator(
126+
string|ValidatorInterface $validator
127+
):HttpInterface {
128+
if (! $validator instanceof ValidatorInterface) {
129+
throw new InputException(
130+
'Invalid validator provided to addValidator; ' .
131+
'must be string or Laminas\Validator\ValidatorInterface'
28132
);
29133
}
30134

31-
return $http;
135+
$this->validators[] = $validator;
136+
137+
return $this;
138+
}
139+
140+
/**
141+
* Has a file been uploaded ?
142+
*
143+
* @param array|string|null $files
144+
* @return bool
145+
*/
146+
public function isUploaded($files = null): bool
147+
{
148+
if (empty($this->files)) {
149+
return false;
150+
}
151+
$fileContent = $this->getFileInfo($files);
152+
foreach ($fileContent as $file) {
153+
if (empty($file['name'])) {
154+
return false;
155+
}
156+
}
157+
158+
return true;
159+
}
160+
161+
/**
162+
* Retrieve additional internal file information for files
163+
*
164+
* @param string $file (Optional) File to get information for
165+
* @return mixed
166+
*/
167+
public function getFileInfo($file = null): mixed
168+
{
169+
$check = [];
170+
if ($file !== null && isset($this->files[$file])) {
171+
$check[$file] = $this->files[$file];
172+
return $check;
173+
}
174+
return $this->files;
32175
}
33176
}

0 commit comments

Comments
 (0)