|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\CloudComponents\Console\Command; |
| 7 | + |
| 8 | +use Magento\Framework\App\Area; |
| 9 | +use Magento\Framework\App\State; |
| 10 | +use Magento\Framework\Console\Cli; |
| 11 | +use Magento\Framework\Exception\LocalizedException; |
| 12 | +use Magento\Framework\UrlFactory; |
| 13 | +use Magento\Store\Api\Data\StoreInterface; |
| 14 | +use Magento\Store\Model\StoreManagerInterface; |
| 15 | +use Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite; |
| 16 | +use Magento\UrlRewrite\Model\UrlFinderInterface; |
| 17 | +use Symfony\Component\Console\Command\Command; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Input\InputOption; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | +use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; |
| 22 | + |
| 23 | +/** |
| 24 | + * Returns list of category or cms-page urls for given stores |
| 25 | + */ |
| 26 | +class ConfigShowEntityUrlsCommand extends Command |
| 27 | +{ |
| 28 | + /** |
| 29 | + * Names of input arguments or options. |
| 30 | + */ |
| 31 | + const INPUT_OPTION_STORE_ID = 'store-id'; |
| 32 | + const INPUT_OPTION_ENTITY_TYPE = 'entity-type'; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var StoreManagerInterface |
| 36 | + */ |
| 37 | + private $storeManager; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var UrlFinderInterface |
| 41 | + */ |
| 42 | + private $urlFinder; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var UrlFactory |
| 46 | + */ |
| 47 | + private $urlFactory; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var State |
| 51 | + */ |
| 52 | + private $state; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var array |
| 56 | + */ |
| 57 | + private $possibleEntities = [Rewrite::ENTITY_TYPE_CMS_PAGE, Rewrite::ENTITY_TYPE_CATEGORY]; |
| 58 | + |
| 59 | + /** |
| 60 | + * @param StoreManagerInterface $storeManager |
| 61 | + * @param UrlFinderInterface $urlFinder |
| 62 | + * @param UrlFactory $urlFactory |
| 63 | + * @param State $state |
| 64 | + */ |
| 65 | + public function __construct( |
| 66 | + StoreManagerInterface $storeManager, |
| 67 | + UrlFinderInterface $urlFinder, |
| 68 | + UrlFactory $urlFactory, |
| 69 | + State $state |
| 70 | + ) { |
| 71 | + $this->storeManager = $storeManager; |
| 72 | + $this->urlFinder = $urlFinder; |
| 73 | + $this->urlFactory = $urlFactory; |
| 74 | + $this->state = $state; |
| 75 | + |
| 76 | + parent::__construct(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @inheritdoc |
| 81 | + */ |
| 82 | + protected function configure() |
| 83 | + { |
| 84 | + $this->setName('config:show:urls') |
| 85 | + ->setDescription( |
| 86 | + 'Returns urls for entity type and given store id or for all stores if store id isn\'t provided.' |
| 87 | + ); |
| 88 | + |
| 89 | + $this->addOption( |
| 90 | + self::INPUT_OPTION_STORE_ID, |
| 91 | + null, |
| 92 | + InputOption::VALUE_OPTIONAL, |
| 93 | + 'Store ID' |
| 94 | + ); |
| 95 | + $this->addOption( |
| 96 | + self::INPUT_OPTION_ENTITY_TYPE, |
| 97 | + null, |
| 98 | + InputOption::VALUE_REQUIRED, |
| 99 | + 'Entity type: ' . implode(',', $this->possibleEntities) |
| 100 | + ); |
| 101 | + |
| 102 | + parent::configure(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @param InputInterface $input |
| 107 | + * @param OutputInterface $output |
| 108 | + * @return int|null |
| 109 | + */ |
| 110 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 111 | + { |
| 112 | + try { |
| 113 | + $this->setArea(); |
| 114 | + $entityType = $input->getOption(self::INPUT_OPTION_ENTITY_TYPE); |
| 115 | + if (!in_array($entityType, $this->possibleEntities)) { |
| 116 | + $output->write(sprintf( |
| 117 | + 'Wrong entity type "%s", possible values: %s', |
| 118 | + $entityType, |
| 119 | + implode(',', $this->possibleEntities) |
| 120 | + )); |
| 121 | + return Cli::RETURN_FAILURE; |
| 122 | + } |
| 123 | + |
| 124 | + $storeId = $input->getOption(self::INPUT_OPTION_STORE_ID); |
| 125 | + |
| 126 | + if ($storeId === null) { |
| 127 | + $stores = $this->storeManager->getStores(); |
| 128 | + } else { |
| 129 | + $stores = [$this->storeManager->getStore($storeId)]; |
| 130 | + } |
| 131 | + |
| 132 | + $urls = $this->getPageUrls($stores, $entityType); |
| 133 | + |
| 134 | + $output->write(json_encode(array_unique($urls))); |
| 135 | + return Cli::RETURN_SUCCESS; |
| 136 | + } catch (\Exception $e) { |
| 137 | + $output->writeln($e->getMessage()); |
| 138 | + return Cli::RETURN_FAILURE; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @param StoreInterface[] $stores |
| 144 | + * @param string $entityType |
| 145 | + * @return array |
| 146 | + */ |
| 147 | + private function getPageUrls(array $stores, string $entityType): array |
| 148 | + { |
| 149 | + $urls = []; |
| 150 | + |
| 151 | + foreach ($stores as $store) { |
| 152 | + $url = $this->urlFactory->create()->setScope($store->getId()); |
| 153 | + |
| 154 | + $entities = $this->urlFinder->findAllByData([ |
| 155 | + UrlRewrite::STORE_ID => $store->getId(), |
| 156 | + UrlRewrite::ENTITY_TYPE => $entityType |
| 157 | + ]); |
| 158 | + |
| 159 | + foreach ($entities as $urlRewrite) { |
| 160 | + $urls[] = $url->getUrl($urlRewrite->getRequestPath()); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return $urls; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Sets area code. |
| 169 | + */ |
| 170 | + private function setArea() |
| 171 | + { |
| 172 | + try { |
| 173 | + $this->state->setAreaCode(Area::AREA_GLOBAL); |
| 174 | + } catch (LocalizedException $e) { |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments