Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make current store configurable #2 #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Console/RunGenerateSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ class RunGenerateSitemap extends Command
*/
protected $generateSitemapCron;

protected $state;

public function __construct(
\Magento\Framework\App\State $state,
GenerateSitemap $generateSitemapCron
) {
$this->generateSitemapCron = $generateSitemapCron;
$this->state = $state;
parent::__construct();
}
protected function configure()
Expand All @@ -28,7 +32,7 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$this->generateSitemapCron->execute();
$this->state->emulateAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND, [$this->generateSitemapCron, 'execute']);
$output->writeln("VSF Sitemap Generated");
} catch (\Exception $e) {
$output->writeln($e->getMessage());
Expand Down
49 changes: 36 additions & 13 deletions Cron/GenerateSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Store\Model\App\Emulation;
use Magento\Store\Model\StoreManagerInterface;
use SitemapPHP\Sitemap;
use Vendic\VueStorefrontSitemap\Model\CategoryCollection;
use Vendic\VueStorefrontSitemap\Model\Configuration;
Expand Down Expand Up @@ -47,11 +49,23 @@ class GenerateSitemap
*/
protected $categoryCollection;
/**
* @var File
* @var StoreManagerInterface
*/
protected $fileDriver;

/**
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @var Emulation
*/
protected $storeEmulation;

public function __construct(
Emulation $storeEmulation,
StoreManagerInterface $storeManager,
CategoryCollection $categoryCollection,
Configuration $configuration,
ProductCollection $productCollection,
Expand All @@ -64,30 +78,39 @@ public function __construct(
$this->productCollection = $productCollection;
$this->configuration = $configuration;
$this->categoryCollection = $categoryCollection;
$this->fileDriver = $fileDriver;
$this->fileDriver = $fileDriver;
$this->storeManager = $storeManager;
$this->storeEmulation = $storeEmulation;
}

public function execute() : void
{
// Collect settings
$domain = $this->configuration->getVueStorefrontUrl();
$path = $this->getPubPath();
foreach ($this->storeManager->getStores() as $store) {
$this->storeEmulation->startEnvironmentEmulation($store->getId(),\Magento\Framework\App\Area::AREA_FRONTEND, true);
// Collect settings
$domain = $this->configuration->getVueStorefrontUrl();
$path = $this->getPubPath();

// Create directory at Path if doesn't exists
if (!$this->fileDriver->isDirectory($path)) $this->fileDriver->createDirectory($path, 0775);

if (!$this->fileDriver->isDirectory($path)) {
$this->fileDriver->createDirectory($path, 0775);
}

// Sitemap configuration
$this->sitemap = $this->sitemapFactory->create($domain);
$this->sitemap->setPath($path);
$this->sitemap->setFilename('sitemap');

// Add data
$this->addHomepageToSitemap();
$this->addCategoriesToSitemap();
$this->addProductsToSitemap();
// Add data
$this->addHomepageToSitemap();
$this->addCategoriesToSitemap();
$this->addProductsToSitemap();

// Generate
$this->sitemap->createSitemapIndex($domain, 'Today');
// Generate
$this->sitemap->createSitemapIndex($domain, 'Today');

$this->storeEmulation->stopEnvironmentEmulation();
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions Model/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Configuration

const VUE_STOREFRONT_EXCLUDE_PRODUCT_SKUS_ENABLED = 'vuestorefront/sitemap/exclude_product_skus';
const VUE_STOREFRONT_SITEMAP_FOLDER = 'vuestorefront/sitemap/sitemap_folder';
const VUE_STOREFRONT_SITEMAP_FILE = 'vuestorefront/sitemap/sitemap_file';

const VUE_STOREFRONT_CATEOGRY_URL_PATH_ENABLED = 'vuestorefront/sitemap/category_url_path';

Expand Down Expand Up @@ -86,6 +87,15 @@ public function getVueStorefrontSitemapFolder(): string
return $folder;
}

public function getVueStorefrontSitemapFilename(): string
{
$file = $this->scopeConfig->getValue(self::VUE_STOREFRONT_SITEMAP_FILE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
if (!is_string($file)) {
$folder = 'sitemap';
}
return $file;
}

public function getVueStorefrontCategoryUrlPath(): bool
{
$setting = $this->scopeConfig->getValue(
Expand Down
4 changes: 4 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<label>Location to generate sitemap</label>
<comment>Path in /pub/</comment>
</field>
<field id="sitemap_file" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Filename for Sitemap</label>
<comment>without .xml file extension</comment>
</field>
<field id="use_catalog_short_urls" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Use catalog_short_urls</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
Expand Down