openvk/Web/Themes/Themepack.php

125 lines
4.1 KiB
PHP
Raw Normal View History

2020-06-11 23:21:49 +03:00
<?php declare(strict_types=1);
namespace openvk\Web\Themes;
2021-09-16 21:29:50 +03:00
use ScssPhp\ScssPhp\Compiler as CSSCompiler;
use ScssPhp\ScssPhp\OutputStyle;
2020-06-11 23:21:49 +03:00
class Themepack
{
private $id;
private $ver;
private $inh;
private $tpl;
2020-06-11 23:21:49 +03:00
private $meta;
private $home;
2020-06-14 14:08:50 +03:00
private $enabled;
2020-06-11 23:21:49 +03:00
2021-09-16 21:29:50 +03:00
private $cssExtensions = [
"css",
"scss",
];
function __construct(string $id, string $ver, bool $inh, bool $tpl, bool $enabled, object $meta)
2020-06-11 23:21:49 +03:00
{
2020-06-14 14:08:50 +03:00
$this->id = $id;
$this->ver = $ver;
$this->inh = $inh;
$this->tpl = $tpl;
2020-06-14 14:08:50 +03:00
$this->meta = $meta;
$this->home = OPENVK_ROOT . "/themepacks/$id";
$this->enabled = $enabled;
2020-06-11 23:21:49 +03:00
}
function getId(): string
{
return $this->id;
}
2020-06-14 14:08:50 +03:00
function isEnabled(): bool
{
return $this->enabled;
}
2020-06-11 23:21:49 +03:00
function getName(?string $lang = NULL): string
{
if(!$this->meta->name)
return $this->getId() . " theme";
else if(is_array($this->meta->name))
return $this->meta->name[$lang ?? "_"] ?? $this->getId() . " theme";
else
return $this->meta->name;
}
2020-08-01 17:30:29 +03:00
function getBaseDir(): string
{
return $this->home;
}
2020-06-11 23:21:49 +03:00
function getVersion(): string
{
return $this->ver;
}
function getDescription(): string
{
return $this->meta->description ?? "A theme with name \"" . $this->getName() . "\"";
}
function getAuthor(): string
{
return $this->meta->author ?? $this->getName() . " authors";
}
function inheritDefault(): bool
{
return $this->inh;
}
function overridesTemplates(): bool
{
return $this->tpl;
}
2021-09-16 21:29:50 +03:00
function fetchResource(string $resource, bool $processCSS = false): ?string
{
$file = "$this->home/$resource";
if(!file_exists($file))
return NULL;
$result = file_get_contents($file);
if(in_array(@end(explode(".", $resource)), $this->cssExtensions) && $processCSS) {
$compiler = new CSSCompiler([ "cacheDir" => OPENVK_ROOT . "/tmp" ]);
$compiler->setOutputStyle(OutputStyle::COMPRESSED);
$result = $compiler->compileString($result, $file)->getCSS();
}
return $result;
}
2020-06-11 23:21:49 +03:00
function fetchStyleSheet(): ?string
{
2021-09-16 21:29:50 +03:00
return $this->fetchResource("stylesheet.scss", true) ?? $this->fetchResource("stylesheet.css", true);
2020-06-11 23:21:49 +03:00
}
function fetchStaticResource(string $name): ?string
{
2021-09-16 21:29:50 +03:00
return $this->fetchResource("res/$name");
2020-06-11 23:21:49 +03:00
}
static function themepackFromDir(string $dirname): Themepack
{
$manifestFile = "$dirname/theme.yml";
if(!file_exists($manifestFile))
throw new Exceptions\NotThemeDirectoryException("Could not locate manifest at $dirname");
$manifest = (object) chandler_parse_yaml($manifestFile);
if(!isset($manifest->id) || !isset($manifest->version) || !isset($manifest->openvk_version) || !isset($manifest->metadata))
throw new Exceptions\MalformedManifestException("Manifest is missing required information");
if($manifest->openvk_version > Themepacks::THEMPACK_ENGINE_VERSION)
throw new Exceptions\IncompatibleThemeException("Theme is built for newer OVK (themeEngine" . $manifest->openvk_version . ")");
return new static($manifest->id, $manifest->version, (bool) ($manifest->inherit_master ?? true), (bool) ($manifest->override_templates ?? false), (bool) ($manifest->enabled ?? true), (object) $manifest->metadata);
2020-06-11 23:21:49 +03:00
}
}