无法应用自定义补丁
我在使用带Docker的 Magento。我在Docker中添加了S3扩展;它已经安装好了。
RUN composer require magecomp/magento-2-amazon-s3-remote-storage:1.0.8
我的PHP版本是8.4。镜像构建完成后,我执行Magento的升级命令,遇到了弃用(deprecated)错误
PHP致命错误:Magecomp\S3Amazon\Console\Command\S3Export::execute(Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output) 的声明必须与Symfony\Component\Console\Command\Command::execute(Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output): int的签名兼容,在 /var/www/html/vendor/magecomp/magento-2-amazon-s3-remote-storage/Console/Command/S3Export.php的第71行
然后我创建了一个补丁并在Docker中应用,但仍然遇到错误
Patch File
diff --git a/vendor/magecomp/magento-2-amazon-s3-remote-storage/Console/Command/S3Export.php b/vendor/magecomp/magento-2-amazon-s3-remote-storage/Console/Command/S3Export.php
--- a/vendor/magecomp/magento-2-amazon-s3-remote-storage/Console/Command/S3Export.php
+++ b/vendor/magecomp/magento-2-amazon-s3-remote-storage/Console/Command/S3Export.php
@@ -71,7 +61,7 @@
- protected function execute(InputInterface $input, OutputInterface $output)
+ protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->setDecorated(true);
$this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
Error
=> ERROR [79/81] RUN patch -p1 < patches/Magecomp-php84.patch 0.3s
------
> [79/81] RUN patch -p1 < patches/Magecomp-php84.patch:
#0 0.268 patch: **** unexpected end of file in patch
#0 0.268 patching file vendor/magecomp/magento-2-amazon-s3-remote-storage/Console/Command/S3Export.php
#0 0.268 patch unexpectedly ends in middle of line
------
Dockerfile:140
S3Export.php的完整文件
<?php
namespace Magecomp\S3Amazon\Console\Command;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\MediaStorage\Helper\File\Storage;
use Magento\MediaStorage\Helper\File\StorageFactory;
use Symfony\Component\Console\Command\Command;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magecomp\S3Amazon\Helper\Data as DataHelper;
use Magento\MediaStorage\Helper\File\Storage\Database;
use Magento\Framework\File\Mime;
use Magento\Framework\Filesystem;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\Driver\File;
use Aws\S3\S3Client;
class S3Export extends \Symfony\Component\Console\Command\Command
{
protected $appState;
protected $helper;
protected $coreFileStorage;
protected $destinationModel;
protected $mime;
protected $client;
protected $storageHelper;
protected $_filesystem;
protected $_mediaBaseDirectory;
protected $_objectCreate;
protected $_file;
protected $errors;
public function __construct(
State $appState,
StorageFactory $coreFileStorageFactory,
DataHelper $helper,
Mime $mime,
Database $storageHelper,
Filesystem $filesystem,
ObjectManagerInterface $_objectCreate,
File $_file
) {
$this->appState = $appState;
$this->coreFileStorage = $coreFileStorageFactory->create();
$this->helper = $helper;
$this->mime = $mime;
$this->storageHelper = $storageHelper;
$this->_filesystem = $filesystem;
$this->_objectCreate = $_objectCreate;
$this->_file = $_file;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('amazons3:export');
$this->setDescription('Sync all media to Amazon S3.');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->setDecorated(true);
$this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
try {
$output->writeln("");
if(!$this->helper->isS3Enabled())
{
$output->writeln('<error>Please Enable Amazon S3 Extension.</error>');
return 1;
}
$errors = $this->validate();
if ($errors) {
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');
return 1;
}
$options = [
'version' => 'latest',
'region' => $this->helper->getRegion(),
'credentials' => [
'key' => $this->helper->getAccessKey(),
'secret' => $this->helper->getSecretKey(),
],
];
if ($this->helper->getEndpointEnabled()) {
$options['endpoint'] = $this->helper->getEndpoint();
$options['use_path_style_endpoint'] = true;
}
try {
$client1 = new S3Client($options);
} catch (\Exception $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
return 1;
}
if (!$client1->doesBucketExist($this->helper->getBucket())) {
$output->writeln('<error>The AWS Authentication Failed</error>');
return 1;
}
if ($this->coreFileStorage->getCurrentStorageCode() === \Magecomp\S3Amazon\Model\Core\File\Storage::STORAGE_MEDIA_S3) {
$output->writeln($this->coreFileStorage->getCurrentStorageCode().'<error>You are already using S3 as your media file storage backend!</error>');
return 1;
}
$sourceModel = $this->coreFileStorage->getStorageModel();
$offset = 0;
$temp = false;
if($this->helper->getRemoveMediaDiretory()){
$question = $this->_objectCreate->create('Symfony\Component\Console\Question\ConfirmationQuestion', ['question'=>'Are you sure you want to remove media?(type : yes/no) :', 'default' => FALSE]);
$helper = $this->getHelper('question');
$ans = $helper->ask($input, $output, $question);
if ($ans == 'yes') {
$temp = true;
}else{
$temp = false;
}
}
while (($files = $sourceModel->exportFiles($offset, 1)) !== false) {
foreach ($files as $file) {
$object = ltrim($file['directory'] . '/' . $file['filename'], '/');
$output->writeln(sprintf('Uploading %s to use S3.', $object));
try {
$this->client = new S3Client($this->helper->getS3Options());
$uploadresponce= $this->client->putObject($this->getAllParams([
'Body' => $file['content'],
'Bucket' => $this->helper->getBucket(),
'ContentType' => $this->mime->getMimeType($this->getMediaBaseDirectory().'/'.$object),
'Key' => $file['directory'] . '/' . $file['filename'],
]));
$responce=$uploadresponce->ToArray()['@metadata']['statusCode'];
if($responce=="200" && $this->helper->getRemoveMediaDiretory()){
if($temp==true){
$mediapath = $this->helper->getAbsolutePath()."/".$object;
if ($this->_file->isExists($mediapath)) {
$this->_file->deleteFile($mediapath);
$output->writeln(sprintf('Remove Image %s to use S3.',$mediapath));
}
}
}
} catch (\Exception $e) {
$output->writeln(sprintf('Error %s S3 :',$e->getMessage()));
$this->errors[] = $e->getMessage();
}
}
$offset += count($files);
}
return 0;
/*return \Magento\Framework\Console\Cli::RETURN_SUCCESS;*/
} catch (\Exception $exception) {
$output->writeln("");
$output->writeln("<error>{$exception->getMessage()}</error>");
return 1;
/*return \Magento\Framework\Console\Cli::RETURN_FAILURE;*/
}
}
public function validate()
{
$errors = [];
if ($this->helper->getAccessKey() === null) {
$errors[] = 'You have not provided an AWS access key ID.';
}
if ($this->helper->getSecretKey() === null) {
$errors[] = 'You have not provided an AWS secret access key.';
}
if ($this->helper->getBucket() === null) {
$errors[] = 'You have not provided an S3 bucket.';
}
if ($this->helper->getRegion() === null) {
$errors[] = 'You have not provided an S3 region.';
}
return $errors;
}
public function getMediaBaseDirectory()
{
if (null === $this->_mediaBaseDirectory) {
$mediaDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
$this->_mediaBaseDirectory = rtrim($mediaDir, '/');
}
return $this->_mediaBaseDirectory;
}
public function getAllParams(array $headers = [])
{
$headers['ACL'] = 'public-read';
return $headers;
}
}
``` 我不知道问题出在哪里
解决方案
It is a known issue.. It is happening because your patch file formatting is corrupted. patch is very strict about counting lines, tracking line modifiers (@@), and matching trailing whitespace.
Furthermore, your original patch header stated -71,7 +61,7 @@, which tells the engine it is removing 7 lines and adding 7 lines, but your patch contents only showed a single line swap.
update your patch with this code and run
``` diff --git a/vendor/magecomp/magento-2-amazon-s3-remote-storage/Console/Command/S3Export.php b/vendor/magecomp/magento-2-amazon-s3-remote-storage/Console/Command/S3Export.php --- a/vendor/magecomp/magento-2-amazon-s3-remote-storage/Console/Command/S3Export.php +++ b/vendor/magecomp/magento-2-amazon-s3-remote-storage/Console/Command/S3Export.php @@ -70,3 +70,3 @@ /* * {@inheritdoc} / - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int {