![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/Catalog/Console/ |
<?php namespace Cnc\Catalog\Console; use Magento\Catalog\Api\CategoryRepositoryInterface; use Magento\Catalog\Model\Category; use Magento\Catalog\Model\ResourceModel\Category\Collection; use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; use Magento\Catalog\Model\CategoryFactory; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class RebuildCategoryUrlKey extends Command { /** @var Category */ private $categoryModel; /** * @var CategoryRepositoryInterface */ private $categoryRepository; /** * @var CollectionFactory */ private $collectionFactory; /** * @var int */ private $debugMode = 0; /** * @var OutputInterface */ private $output; /** * RebuildCategoryUrlKey constructor. * @param CategoryFactory $categoryFactory * @param CategoryRepositoryInterface $categoryRepository * @param string|null $name */ public function __construct( CategoryFactory $categoryFactory, CategoryRepositoryInterface $categoryRepository, CollectionFactory $collectionFactory, string $name = null ) { parent::__construct($name); $this->categoryModel = $categoryFactory->create(); $this->categoryRepository = $categoryRepository; $this->collectionFactory = $collectionFactory; } /** * @inheritDoc */ protected function configure() { $this->setName('catalog:category:url_key:rebuild'); $this->setDescription('Rebuild url keys'); $this->addOption( 'level', 'l', InputOption::VALUE_OPTIONAL, 'Category lvl' ); $this->addOption( 'category_id', 'i', InputOption::VALUE_OPTIONAL, 'Category id' ); $this->addOption( 'store_id', 's', InputOption::VALUE_OPTIONAL, 'Store id' ); $this->addOption( 'debug', 'd', InputOption::VALUE_OPTIONAL, 'Debug only' ); parent::configure(); } /** * CLI command description * * @param InputInterface $input * @param OutputInterface $output * * @return void */ protected function execute(InputInterface $input, OutputInterface $output): void { $this->output = $output; $this->debugMode = $input->getOption('debug'); $storeId = $input->getOption('store_id'); $stores = []; if ($storeId) { $stores []= $storeId; } else { $stores []= 0; } $categoryId = $input->getOption('category_id'); $level = $input->getOption('level'); if ($categoryId) { foreach ($stores as $store) { $category = $this->categoryRepository->get($categoryId, $store); $this->rebuildUrlKey($category, $store == 1); } } elseif ($level) { foreach ($stores as $store) { $collection = $this->collectionFactory->create(); $collection->addAttributeToFilter('level', $level); /** @var Category $category */ foreach ($collection->getAllIds() as $categoryId) { $category = $this->categoryRepository->get($categoryId, $store); $this->rebuildUrlKey($category, $store == 1); } } } } protected function rebuildUrlKey(Category $category, $isFr = false) { $urlKey = $this->categoryModel->formatUrlKey($this->prepareKey($category->getName(), $isFr)); $this->output->writeln($category->getName() .' : ' . $category->getUrlKey() . ' => ' . $urlKey); if ($category->getUrlKey() != $urlKey && !$this->debugMode) { $category->setUrlKey($urlKey)->save(); } } private function prepareKey(string $name, $isFr = false) { return str_replace('&', $isFr ? 'et' : 'and', $name); } }