indexer support

This commit is contained in:
Bostjan Marusic 2020-03-13 17:34:38 +01:00
parent ffa154d3bd
commit f951455f69
3 changed files with 212 additions and 22 deletions

View File

@ -6,8 +6,6 @@ namespace Foomo\Magento2;
use Magento\Framework\App\Bootstrap;
class Boostrap
{
private static $bootstrap;
@ -16,9 +14,10 @@ class Boostrap
/**
* @return Bootstrap
* @return \Magento\Framework\App\Bootstrap
*/
public static function bootstrap() {
public static function bootstrap()
{
if (!class_exists('\Magento\Framework\App\Bootstrap')) {
return self::init();
}
@ -37,16 +36,20 @@ class Boostrap
if (is_null(self::$bootstrap)) {
//handle dockerized setup
if ($_SERVER['HTTP_HOST'] == 'site') {
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
$_SERVER['HTTPS'] = 'on';
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}
// add bootstrap
//let us pretend its https - > we are in a private net
$_SERVER['HTTPS'] = 'on';
$_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https';
$https = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
$_SERVER['REQUEST_SCHEME'] = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : $https;
//create the bootstrap object
self::$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = self::$bootstrap->getObjectManager();
/* @var \Magento\Framework\App\State $state */
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
}
@ -56,7 +59,20 @@ class Boostrap
/**
* @return \Magento\Store\Api\Data\StoreInterface[]
*/
public static function getStores() {
public static function getObjectManager()
{
if (!class_exists('\Magento\Framework\App\Bootstrap')) {
self::init();
}
return \Magento\Framework\App\ObjectManager::getInstance();
}
/**
* @return \Magento\Store\Api\Data\StoreInterface[]
*/
public static function getStores()
{
if (!class_exists('\Magento\Framework\App\Bootstrap')) {
self::init();
}
@ -65,11 +81,47 @@ class Boostrap
return $storeManager->getStores();
}
/**
* @param string $storeCode
* @return \Magento\Store\Api\Data\StoreInterface | false
*/
public static function getStoreForCode($storeCode)
{
if (!class_exists('\Magento\Framework\App\Bootstrap')) {
self::init();
}
/* @var \Magento\Store\Model\StoreManagerInterface $storeManager */
$storeManager = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Store\Model\StoreManagerInterface');
foreach ($storeManager->getStores() as $store) {
if ($store->getCode() == $storeCode) {
return $store;
}
}
return false;
}
/**
* set current store
* @param string $storeCode
*/
public static function setCurrentStore($storeCode)
{
if (!class_exists('\Magento\Framework\App\Bootstrap')) {
self::init();
}
/* @var \Magento\Store\Model\StoreManagerInterface $storeManager */
$storeManager = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Store\Model\StoreManagerInterface');
$store = self::getStoreForCode($storeCode);
return $storeManager->setCurrentStore(!empty($store) ? $store->getId() : 0); // default to admin store
}
/**
* @return \Magento\Store\Api\Data\WebsiteExtensionInterface
*/
public static function getWebsites() {
public static function getWebsites()
{
if (!class_exists('\Magento\Framework\App\Bootstrap')) {
self::init();
}
@ -78,7 +130,8 @@ class Boostrap
return $storeManager->getWebsites();
}
public static function getStoreCodes() {
public static function getStoreCodes()
{
$ret = [];
foreach (self::getStores() as $store) {
$ret[] = $store->getCode();
@ -87,11 +140,25 @@ class Boostrap
}
public static function getWebsiteCodes() {
public static function getWebsiteCodes()
{
$ret = [];
foreach (self::getWebsites() as $website) {
$ret[] = $website->getCode();
}
return $ret;
}
/**
* get country code to country for current store
* @param string $countryCode
* @return string
*/
public static function getCountryCodeTranslation($countryCode)
{
/* @var \Magento\Framework\Locale\ListsInterface $listInterface */
$listInterface = self::bootstrap()->getObjectManager()->get('\Magento\Framework\Locale\ListsInterface');
return $listInterface->getCountryTranslation($countryCode);
}
}

View File

@ -13,19 +13,9 @@ class Indexer
$indexerFactory = $bootstrap->getObjectManager()->get('Magento\Indexer\Model\IndexerFactory');
if (empty($indexerIds)) {
$indexerIds = array(
'catalog_category_product',
'catalog_product_category',
'catalog_product_price',
'catalog_product_attribute',
'cataloginventory_stock',
'catalogrule_product',
'catalogsearch_fulltext',
);
$indexerIds = self::getIndexerIds();
}
foreach ($indexerIds as $indexerId) {
$indexer = $indexerFactory->create();
$indexer->load($indexerId);
$indexer->reindexAll();
@ -33,5 +23,58 @@ class Indexer
}
}
public static function isIndexerRunning()
{
$bootstrap = Boostrap::bootstrap();
$indexerFactory = $bootstrap->getObjectManager()->get('Magento\Indexer\Model\IndexerFactory');
$indexerIds = self::getIndexerIds();
foreach ($indexerIds as $indexerId) {
/** @var \Magento\Indexer\Model\Indexer $indexer */
$indexer = $indexerFactory->create();
$indexer->load($indexerId);
if ($indexer->getStatus() == \Magento\Framework\Indexer\StateInterface::STATUS_WORKING) {
return true;
}
}
return false;
}
/**
* get running indexer codes
*
* @return string[]
*/
public static function getRunningIndexers()
{
$indexerIds = [];
$bootstrap = Boostrap::bootstrap();
//CacheCleaner::cleanAll();
/** @var \Magento\Indexer\Model\Indexer\Collection $indexerCollection */
$indexerCollection = $bootstrap->getObjectManager()->get(\Magento\Indexer\Model\Indexer\Collection::class);
$indexerCollection->load();
/** @var \Magento\Indexer\Model\Indexer $indexer */
foreach ($indexerCollection->getItems() as $indexer) {
//$indexer->reindexAll();
if ($indexer->getStatus() == \Magento\Framework\Indexer\StateInterface::STATUS_WORKING) {
$indexerIds[] = $indexer->getId();
}
}
return $indexerIds;
}
private static function getIndexerIds()
{
$indexerIds = [];
$bootstrap = Boostrap::bootstrap();
//CacheCleaner::cleanAll();
/** @var \Magento\Indexer\Model\Indexer\Collection $indexerCollection */
$indexerCollection = $bootstrap->getObjectManager()->get(\Magento\Indexer\Model\Indexer\Collection::class);
$indexerCollection->load();
/** @var \Magento\Indexer\Model\Indexer $indexer */
foreach ($indexerCollection->getItems() as $indexer) {
//$indexer->reindexAll();
$indexerIds[] = $indexer->getId();
}
return $indexerIds;
}
}

View File

@ -0,0 +1,80 @@
<?php
/*
*/
namespace Foomo\Magento2\Services;
class Cart
{
/**
* quote
*
* @param \Magento\Quote\Model\Quote
*
*/
private static $quote = null;
/**
* @param string $storeCode
* @return \Magento\Quote\Model\Quote
* @throws \Magento\Framework\Exception\SessionException
*/
public static function getQuote($storeCode)
{
if (!self::$quote) {
// do not touch magento calls !
$bootstrap = \Foomo\Magento2\Boostrap::bootstrap();
\Foomo\Magento2\Boostrap::setCurrentStore($storeCode);
$objectManager = $bootstrap->getObjectManager();
$quote = $objectManager->get('Magento\Checkout\Model\Session')->getQuote();
$country = 'DE';
if ($storeCode != 'admin' && $storeCode != 'base') {
$country = strtoupper(substr($storeCode, 0, 2));
}
if (!$quote->hasItems()) {
$quote->setBillingAddress(self::getEmptyAddress($country, $storeCode))->setShippingAddress(self::getEmptyAddress($country, $storeCode));
}
self::$quote = $quote;
}
return self::$quote;
}
private static function getEmptyAddress($country, $storeCode)
{
$bootstrap = \Foomo\Magento2\Boostrap::bootstrap();
\Foomo\Magento2\Boostrap::setCurrentStore($storeCode);
$objectManager = $bootstrap->getObjectManager();
$address = $objectManager->create('Magento\Quote\Api\Data\AddressInterface');
$address->setRegionId(0);
$address->setRegion("");
$address->setRegionCode("");
$address->setCountryId($country);
$address->setStreet([""]);
$address->setCompany("");
$address->setTelephone("");
$address->setFax("");
$address->setPostcode("");
$address->setCity("");
$address->setFirstname("");
$address->setLastname("");
$address->setMiddlename("");
$address->setEmail("");
$address->setSameAsBilling(1);
$address->setSaveInAddressBook(0);
return $address;
}
/**
* @param string $storeCode
* @return \Magento\Checkout\Model\Session
*/
public static function getCheckoutSession($storeCode) {
$bootstrap = \Foomo\Magento2\Boostrap::bootstrap();
\Foomo\Magento2\Boostrap::setCurrentStore($storeCode);
$objectManager = \Foomo\Magento2\Boostrap::bootstrap()->getObjectManager();
return $objectManager->get('Magento\Checkout\Model\Session');
}
}