openvk/Web/Themes/Themepacks.php

121 lines
3.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
2020-06-11 23:21:49 +03:00
namespace openvk\Web\Themes;
2020-06-11 23:21:49 +03:00
use Nette\InvalidStateException as ISE;
use Chandler\Session\Session;
use Chandler\Patterns\TSimpleSingleton;
class Themepacks implements \ArrayAccess
{
use TSimpleSingleton;
public const THEMPACK_ENGINE_VERSION = 1;
public const DEFAULT_THEME_ID = "ovk"; # блин было бы смешно если было бы Fore, потому что Лунка, а Luna это название дефолт темы винхп
2020-06-11 23:21:49 +03:00
private $loadedThemepacks = [];
public function __construct()
2020-06-11 23:21:49 +03:00
{
foreach (glob(OPENVK_ROOT . "/themepacks/*", GLOB_ONLYDIR) as $themeDir) {
2020-06-11 23:21:49 +03:00
try {
$theme = Themepack::themepackFromDir($themeDir);
$tid = $theme->getId();
if (isset($this->loadedThemepacks[$tid])) {
2020-06-11 23:21:49 +03:00
trigger_error("Duplicate theme $tid found at $themeDir, skipping...", E_USER_WARNING);
} else {
2020-06-11 23:21:49 +03:00
$this->loadedThemepacks[$tid] = $theme;
}
} catch (\Exception $e) {
trigger_error("Could not load theme at $themeDir. Exception: $e", E_USER_WARNING);
2020-06-11 23:21:49 +03:00
}
}
}
2020-06-11 23:21:49 +03:00
private function installUnpacked(string $path): bool
{
try {
$theme = Themepack::themepackFromDir($path);
$tid = $theme->getId();
if (isset($this->loadedThemepacks[$tid])) {
2020-06-11 23:21:49 +03:00
return false;
}
2020-06-11 23:21:49 +03:00
rename($path, OPENVK_ROOT . "/themepacks/$tid");
$this->loadedThemepacks[$tid] = $theme;
return true;
} catch (\Exception $e) {
2020-06-11 23:21:49 +03:00
return false;
}
}
public function getThemeList(): \Traversable
2020-06-11 23:21:49 +03:00
{
foreach ($this->loadedThemepacks as $id => $theme) {
if ($theme->isEnabled()) {
2020-06-14 14:08:50 +03:00
yield $id => ($theme->getName(Session::i()->get("lang", "ru")));
}
}
2020-06-11 23:21:49 +03:00
}
public function getAllThemes(): array
{
return $this->loadedThemepacks;
}
2020-06-11 23:21:49 +03:00
/* ArrayAccess */
public function offsetExists($offset): bool
2020-06-11 23:21:49 +03:00
{
return $offset === Themepacks::DEFAULT_THEME_ID ? false : isset($this->loadedThemepacks[$offset]);
}
public function offsetGet($offset): mixed
2020-06-11 23:21:49 +03:00
{
return $this->loadedThemepacks[$offset];
}
public function offsetSet($offset, $value): void
2020-06-11 23:21:49 +03:00
{
throw new ISE("Theme substitution in runtime is prohbited");
}
public function offsetUnset($offset): void
2020-06-11 23:21:49 +03:00
{
$this->uninstall($offset);
}
2020-06-11 23:21:49 +03:00
/* /ArrayAccess */
public function install(string $archivePath): bool
2020-06-11 23:21:49 +03:00
{
if (!file_exists($archivePath)) {
2020-06-11 23:21:49 +03:00
return false;
}
2020-06-11 23:21:49 +03:00
$tmpDir = mkdir(tempnam(OPENVK_ROOT . "/tmp/themepack_artifacts/", "themex_"));
try {
$archive = new \CabArchive($archivePath);
$archive->extract($tmpDir);
2020-06-11 23:21:49 +03:00
return $this->installUnpacked($tmpDir);
} catch (\Exception $e) {
return false;
} finally {
rmdir($tmpDir);
}
}
public function uninstall(string $id): bool
2020-06-11 23:21:49 +03:00
{
if (!isset($loadedThemepacks[$id])) {
2020-06-11 23:21:49 +03:00
return false;
}
2020-06-11 23:21:49 +03:00
rmdir(OPENVK_ROOT . "/themepacks/$id");
unset($loadedThemepacks[$id]);
return true;
}
}