Files
stemcell/app/code/Stemcell/Catalog/Console/Command/Test.php
T
2026-09-15 10:26:34 +08:00

119 lines
4.3 KiB
PHP

<?php
namespace Stemcell\Catalog\Console\Command;
use LanguageDetector\LanguageDetector;
use Magento\Catalog\Model\ProductRepository;
use Magento\Cms\Model\Template\FilterProvider;
use Magento\Framework\App\Config\Storage\WriterInterface as ConfigWriter;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;
use phpseclib3\Crypt\Common\SymmetricKey;
use Stemcell\Datacollection\Cron\FormRecordsNotification;
use Stemcell\ProductImport\Console\Command\ExtractDescriptionFromProductDataCSV;
use Stemcell\ProductImport\Cron\ImportPrice;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use phpseclib3\Crypt\AES;
use phpseclib3\Crypt\Common\BlockCipher;
use Magento\Framework\App\Cache\TypeListInterface;
class Test extends Command
{
const NO_PRICE_SKU_PATH = 'catalog/product_settings/no_price_sku';
public function __construct(
private FormRecordsNotification $formRecordsNotification,
private DirectoryList $directoryList,
protected ImportPrice $importStock,
protected ConfigWriter $configWriter,
protected TypeListInterface $cacheTypeList,
protected TimezoneInterface $timezone,
protected FilterProvider $filterProvider,
protected StoreManagerInterface $storeManager,
protected ProductRepository $productRepository,
protected State $state
)
{
$name = 'test';
parent::__construct($name);
}
public function getMediaUrl()
{
return $this->storeManager
->getStore()
->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
}
protected function isDirEmpty($dir) {
if (!is_dir($dir)) {
return false; // 或者抛出异常
}
$files = scandir($dir);
// 减去 . 和 .. 后,如果数量为0,则文件夹为空
return count($files) == 2; // 2 表示只有 . 和 ..
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// $this->formRecordsNotification->execute();
// $output->writeln($this->directoryList->getPath('media'));
// $content = "Hello world !";
// $ivStr128 = "1234567812345678"; // 16字节
// $AESSecretGCM256 = "IhHEhIvTS8zbVTY391GP8oHJ7RmSJ8pQ"; // 32字节
// $output->writeln(self::encryptGCMWithPhpseclib($content, $AESSecretGCM256, $ivStr128));
// $output->writeln($this->timezone->date()->format('Y-m-d H:i:s'));
$this->state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
$product = $this->productRepository->get('MRDX20359', true);
$existingEntries = $product->getMediaGalleryEntries();
if (!empty($existingEntries)) {
$output->writeln(" 清理现有图片,共 " . count($existingEntries) . " 张");
// 清空媒体库条目
$product->setMediaGalleryEntries([]);
// 清除图片角色
$product->setImage(null);
$product->setSmallImage(null);
$product->setThumbnail(null);
$this->productRepository->save($product);
// $output->writeln(" 现有图片清理完成");
}
// $this->configWriter->save(
// self::NO_PRICE_SKU_PATH,
// '111111111111111111111',
// ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
// 0
// );
// $this->cacheTypeList->cleanType('config');
// $this->importStock->execute();
return Command::SUCCESS;
}
public static function encryptGCMWithPhpseclib($content, $key, $iv)
{
$cipher = new AES('gcm');
$cipher->setPreferredEngine(SymmetricKey::ENGINE_OPENSSL_GCM);
$cipher->setKey($key);
$cipher->setNonce($iv);
$cipher->setKeyLength(256); // 尝试设置256位标签
$cipherText = $cipher->encrypt($content);
$tag = $cipher->getTag();
// 打包数据
$ivLength = pack('N', strlen($iv));
$byteBuffer = $ivLength . $iv . $cipherText . $tag;
return base64_encode($byteBuffer);
}
}