first commit
This commit is contained in:
127
bundles/KupShop/AdminBundle/Util/ShopStoreUtils.php
Normal file
127
bundles/KupShop/AdminBundle/Util/ShopStoreUtils.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KupShop\AdminBundle\Util;
|
||||
|
||||
use KupShop\KupShopBundle\Config;
|
||||
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
||||
use KupShop\KupShopBundle\Util\System\BundleFinder;
|
||||
|
||||
class ShopStoreUtils
|
||||
{
|
||||
public const CACHE_KEY = 'shop_modules';
|
||||
|
||||
public function getModules(bool $skipCache = false): array
|
||||
{
|
||||
$modules = null;
|
||||
|
||||
if (!$skipCache) {
|
||||
$modules = getCache(self::CACHE_KEY);
|
||||
}
|
||||
|
||||
if (!$modules) {
|
||||
$modules = $this->fetchModules();
|
||||
|
||||
$this->updateModulesAttributes($modules['modules']);
|
||||
$this->updateCategoriesAttributes($modules['categories'], $modules['modules']);
|
||||
|
||||
if ($modules) {
|
||||
setCache(self::CACHE_KEY, $modules);
|
||||
}
|
||||
}
|
||||
|
||||
return $modules;
|
||||
}
|
||||
|
||||
public function fetchModules()
|
||||
{
|
||||
$modules = file_get_contents('https://klient.wpj.cz/__shop__/modules/');
|
||||
|
||||
return json_decode($modules, true);
|
||||
}
|
||||
|
||||
public function updateModulesAttributes(array &$modules)
|
||||
{
|
||||
foreach ($modules as $id => &$module) {
|
||||
try {
|
||||
$module['id'] = $id;
|
||||
$this->updateModuleFlags($module);
|
||||
} catch (\Throwable $e) {
|
||||
$module['active'] = false;
|
||||
if (isSuperuser() || isFunctionalTests()) {
|
||||
$module['title'] .= ' !!Chyba!! '.$e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function updateCategoriesAttributes(array &$sections, array $modules)
|
||||
{
|
||||
foreach ($sections as $id => &$section) {
|
||||
$section['id'] = $id;
|
||||
$section['count'] = count(array_filter($modules, fn ($module) => in_array($id, $module['categories']) && $module['visible'] === 1));
|
||||
}
|
||||
}
|
||||
|
||||
public function isModuleActive($module)
|
||||
{
|
||||
$modules = array_filter(array_map(fn ($x) => trim($x), explode("\n", $module['enabled_modules'])));
|
||||
|
||||
$modulesWrapper = \ModulesWrapper::getInstance();
|
||||
|
||||
$active = !empty($module['enabled_expression']);
|
||||
|
||||
foreach ($modules as $mod) {
|
||||
if ($modulesWrapper[$mod]) {
|
||||
$active = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($active && $module['enabled_expression']) {
|
||||
$dbcfg = \Settings::getDefault();
|
||||
$cfg = Config::get();
|
||||
$bundles = $this->getActiveBundles();
|
||||
|
||||
return (bool) eval($module['enabled_expression']);
|
||||
}
|
||||
|
||||
return $active;
|
||||
}
|
||||
|
||||
public function updateModuleFlags(&$module)
|
||||
{
|
||||
if ($module['date_update']) {
|
||||
$date = \DateTime::createFromFormat('Y-m-d\TH:i:s', $module['date_update']);
|
||||
if ($date > new \DateTime('14 days ago')) {
|
||||
$module['new'] = true;
|
||||
}
|
||||
}
|
||||
|
||||
$module['recommend'] = in_array(2, $module['categories']);
|
||||
|
||||
$module['active'] = $this->isModuleActive($module);
|
||||
}
|
||||
|
||||
private function getActiveBundles(): array
|
||||
{
|
||||
static $bundles = null;
|
||||
|
||||
if ($bundles === null) {
|
||||
$bundles = array_map(
|
||||
function (string $bundleFqn) {
|
||||
$splitFqn = explode('\\', $bundleFqn);
|
||||
|
||||
if (empty($splitFqn)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $splitFqn[count($splitFqn) - 1];
|
||||
},
|
||||
ServiceContainer::getService(BundleFinder::class)->getBundles(),
|
||||
);
|
||||
}
|
||||
|
||||
return $bundles;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user