Ejemplo n.º 1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if ($input->getOption('add-all')) {
         $input->setOption('add-categories', true);
         $input->setOption('add-products', true);
         $input->setOption('add-cmspages', true);
     }
     $this->detectMagento($output, true);
     if ($this->initMagento()) {
         $stores = explode(',', $input->getArgument('stores'));
         $urls = array();
         foreach ($stores as $storeId) {
             try {
                 $currentStore = $this->storeManager->getStore($storeId);
             } catch (\Exception $e) {
                 throw new \RuntimeException("Store with id {$storeId} doesn´t exist");
             }
             // base url
             $urls[] = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
             $linkBaseUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);
             if ($input->getOption('add-categories')) {
                 $collection = $this->sitemapCategoryCollection->getCollection($storeId);
                 if ($collection) {
                     foreach ($collection as $item) {
                         /* @var $item \Magento\Framework\Object */
                         $urls[] = $linkBaseUrl . $item->getUrl();
                     }
                     unset($collection);
                 }
             }
             if ($input->getOption('add-products')) {
                 $collection = $this->sitemapProductCollection->getCollection($storeId);
                 if ($collection) {
                     foreach ($collection as $item) {
                         /* @var $item \Magento\Framework\Object */
                         $urls[] = $linkBaseUrl . $item->getUrl();
                     }
                     unset($collection);
                 }
             }
             if ($input->getOption('add-cmspages')) {
                 $collection = $this->sitemapPageCollection->getCollection($storeId);
                 if ($collection) {
                     foreach ($collection as $item) {
                         /* @var $item \Magento\Framework\Object */
                         $urls[] = $linkBaseUrl . $item->getUrl();
                     }
                     unset($collection);
                 }
             }
         }
         // foreach ($stores as $storeId)
         if (count($urls) > 0) {
             foreach ($urls as $url) {
                 // pre-process
                 $line = $input->getArgument('linetemplate');
                 $line = str_replace('{url}', $url, $line);
                 $parts = parse_url($url);
                 foreach ($parts as $key => $value) {
                     $line = str_replace('{' . $key . '}', $value, $line);
                 }
                 // ... and output
                 $output->writeln($line);
             }
         }
     }
 }