Skip to content

MAGETWO-2745: added appEmulation Plugin for the $sitemap->generateXml() for generating the sitemap via cron and backend #27476

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

Closed
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
22 changes: 20 additions & 2 deletions app/code/Magento/Sitemap/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sitemap\Model;

use Magento\Sitemap\Model\EmailNotification as SitemapEmail;
use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Sitemap\Model\EmailNotification as SitemapEmail;
use Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory;
use Magento\Store\Model\App\Emulation;
use Magento\Store\Model\ScopeInterface;

/**
Expand Down Expand Up @@ -61,20 +65,28 @@ class Observer
*/
private $emailNotification;

/**
* @var Emulation
*/
private $appEmulation;

/**
* Observer constructor.
* @param ScopeConfigInterface $scopeConfig
* @param CollectionFactory $collectionFactory
* @param EmailNotification $emailNotification
* @param Emulation $appEmulation
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
CollectionFactory $collectionFactory,
SitemapEmail $emailNotification
SitemapEmail $emailNotification,
Emulation $appEmulation
) {
$this->scopeConfig = $scopeConfig;
$this->collectionFactory = $collectionFactory;
$this->emailNotification = $emailNotification;
$this->appEmulation = $appEmulation;
}

/**
Expand Down Expand Up @@ -105,7 +117,13 @@ public function scheduledGenerateSitemaps()
foreach ($collection as $sitemap) {
/* @var $sitemap \Magento\Sitemap\Model\Sitemap */
try {
$this->appEmulation->startEnvironmentEmulation(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest moving area emulation outside of the foreach statement

$sitemap->getStoreId(),
Area::AREA_FRONTEND,
true
);
$sitemap->generateXml();
$this->appEmulation->stopEnvironmentEmulation();
} catch (\Exception $e) {
$errors[] = $e->getMessage();
}
Comment on lines +126 to 129
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->appEmulation->stopEnvironmentEmulation();
} catch (\Exception $e) {
$errors[] = $e->getMessage();
}
} catch (\Exception $e) {
$errors[] = $e->getMessage();
} finally {
$this->appEmulation->stopEnvironmentEmulation();
}

Expand Down