Enabled per-theme functions via a theme config.php file and moved 'icons' Twig function definition into the default theme config

This commit is contained in:
Chris Kankiewicz
2019-11-16 23:11:07 -07:00
parent 9bc99a8506
commit f491786b8e
6 changed files with 195 additions and 53 deletions

View File

@@ -9,32 +9,41 @@ use Twig\TwigFunction;
class ViewComposer
{
/** @var Config Application config */
protected $config;
/** @var Twig Twig instance */
protected $twig;
/**
* Create a new ViewComposer object.
*
* @param \PHLAK\Config\Config $config
*/
public function __construct(Config $config, Twig $twig)
{
$this->config = $config;
$this->twig = $twig;
}
/**
* Set up the Twig component.
*
* @return void
*/
public function __invoke(Config $config, Twig $twig): void
public function __invoke(): void
{
$twig->getEnvironment()->getExtension(CoreExtension::class)->setDateFormat(
$config->get('date_format', 'Y-m-d H:i:s'), '%d days'
$this->twig->getEnvironment()->getExtension(CoreExtension::class)->setDateFormat(
$this->config->get('date_format', 'Y-m-d H:i:s'), '%d days'
);
$twig->getEnvironment()->addFunction(
new TwigFunction('asset', function ($path) use ($config) {
return "/app/themes/{$config->get('theme', 'defualt')}/{$path}";
$this->twig->getEnvironment()->addFunction(
new TwigFunction('asset', function ($path) {
return "/app/themes/{$this->config->get('theme', 'defualt')}/{$path}";
})
);
$twig->getEnvironment()->addFunction(
new TwigFunction('icon', function ($file) use ($config) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
return $config->get("icons.{$extension}", 'fa-file');
})
);
$twig->getEnvironment()->addFunction(
$this->twig->getEnvironment()->addFunction(
new TwigFunction('sizeForHumans', function ($bytes) {
$sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
$factor = floor((strlen($bytes) - 1) / 3);
@@ -42,5 +51,25 @@ class ViewComposer
return sprintf('%.2f', $bytes / pow(1024, $factor)) . $sizes[$factor];
})
);
$this->registerThemeFunctions();
}
/**
* Register theme Twig functions.
*
* @return void
*/
public function registerThemeFunctions(): void
{
$themeConfigPath = "app/themes/{$this->config->get('theme')}/config.php";
if (file_exists($themeConfigPath)) {
$themeConfig = include $themeConfigPath;
}
foreach ($themeConfig['functions'] ?? [] as $function) {
$this->twig->getEnvironment()->addFunction($function);
}
}
}

View File

@@ -1,33 +0,0 @@
<?php
return [
'icons' => [
/** Images */
'gif' => 'fa-file-image',
'jpeg' => 'fa-file-image',
'jpg' => 'fa-file-image',
'png' => 'fa-file-image',
/** Data */
'csv' => 'fa-file-csv',
'json' => 'fa-file-code',
'yaml' => 'fa-file-alt',
/** Code */
'js' => 'fa-file-code',
'php' => 'fa-file-code',
/** Text and Markup */
'md' => 'fa-file-download',
'txt' => 'fa-file-alt',
/** Documents */
'doc' => 'fa-file-word',
'docx' => 'fa-file-word',
'pdf' => 'fa-file-pdf',
'ppt' => 'fa-file-powerpoint',
'pptx' => 'fa-file-powerpoint',
'xls' => 'fa-file-excel',
'xlsx' => 'fa-file-excel'
]
];

View File

@@ -0,0 +1,17 @@
<?php
use Twig\TwigFunction;
return [
/** Theme specific Twig functions */
'functions' => [
new TwigFunction('icon', function ($file) {
$icons = require __DIR__ . '/icons.php';
$icon = $file->isDir() ? 'fas fa-folder'
: $icons[$file->getExtension()] ?? 'fas fa-file';
return "<i class=\"{$icon} fa-fw fa-lg\"></i>";
})
]
];

View File

@@ -0,0 +1,132 @@
<?php
return [
// Images
'ai' => 'fab fa-image',
'bmp' => 'fas fa-image',
'eps' => 'fab fa-image',
'gif' => 'fas fa-image',
'jpg' => 'fas fa-image',
'jpeg' => 'fas fa-image',
'png' => 'fas fa-image',
'ps' => 'fab fa-image',
'psd' => 'fas fa-image',
'svg' => 'fab fa-image',
'tga' => 'fas fa-image',
'tif' => 'fas fa-image',
'drw' => 'fab fa-image',
// Data
'csv' => 'fas fa-file-csv',
'json' => 'fas fa-file-alt',
'yaml' => 'fas fa-file-alt',
// Code
'c' => 'fas fa-code',
'class' => 'fas fa-code',
'cpp' => 'fas fa-code',
'css' => 'fab fab fa-css3',
'erb' => 'fas fa-code',
'htm' => 'fab fa-html5',
'html' => 'fab fa-html5',
'java' => 'fab fa-java',
'js' => 'fab fa-js',
'php' => 'fab fa-php',
'pl' => 'fas fa-code',
'py' => 'fab fa-python',
'rb' => 'fas fa-code',
'xhtml' => 'fas fa-code',
'xml' => 'fas fa-code',
// Text and Markup
'cfg' => 'fas fa-file-alt',
'ini' => 'fas fa-file-alt',
'log' => 'fas fa-file-alt',
'md' => 'fab fa-markdown',
'rtf' => 'fas fa-file-alt',
'txt' => 'fas fa-file-alt',
// Documents
'doc' => 'fas fa-file-word',
'docx' => 'fas fa-file-word',
'odt' => 'fas fa-file-alt',
'pdf' => 'fas fa-file-pdf',
'ppt' => 'fas fa-file-powerpoint',
'pptx' => 'fas fa-file-powerpoint',
'xls' => 'fas fa-file-excel',
'xlsx' => 'fas fa-file-excel',
// Archives
'7z' => 'fas fa-file-archive',
'bz' => 'fas fa-file-archive',
'gz' => 'fas fa-file-archive',
'rar' => 'fas fa-file-archive',
'tar' => 'fas fa-file-archive',
'zip' => 'fas fa-file-archive',
// Audio
'aac' => 'fas fa-music',
'flac' => 'fas fa-music',
'mid' => 'fas fa-music',
'midi' => 'fas fa-music',
'mp3' => 'fas fa-music',
'ogg' => 'fas fa-music',
'wma' => 'fas fa-music',
'wav' => 'fas fa-music',
// Databases
'accdb' => 'fas fa-database',
'db' => 'fas fa-database',
'dbf' => 'fas fa-database',
'mdb' => 'fas fa-database',
'pdb' => 'fas fa-database',
'sql' => 'fas fa-database',
// Executables
'app' => 'fas fa-window',
'bat' => 'fas fa-window',
'com' => 'fas fa-window',
'exe' => 'fas fa-window',
'jar' => 'fas fa-window',
'msi' => 'fas fa-window',
'vb' => 'fas fa-window',
// Fonts
'eot' => 'fas fa-font-case',
'otf' => 'fas fa-font-case',
'ttf' => 'fas fa-font-case',
'woff' => 'fas fa-font-case',
// Game Files
'gam' => 'fas fa-gamepad',
'nes' => 'fas fa-gamepad',
'rom' => 'fas fa-gamepad',
'sav' => 'fas fa-save',
// Package Files
'box' => 'fas fa-archive',
'deb' => 'fas fa-archive',
'rpm' => 'fas fa-archive',
// Scripts
'bat' => 'fas fa-terminal',
'cmd' => 'fas fa-terminal',
'sh' => 'fas fa-terminal',
// Video
'avi' => 'fas fa-video',
'flv' => 'fas fa-video',
'mkv' => 'fas fa-video',
'mov' => 'fas fa-video',
'mp4' => 'fas fa-video',
'mpg' => 'fas fa-video',
'ogv' => 'fas fa-video',
'webm' => 'fas fa-video',
'wmv' => 'fas fa-video',
'swf' => 'fas fa-video',
// Miscellaneous
'bak' => 'fas fa-save',
'lock' => 'fas fa-lock',
'msg' => 'fas fa-envelope',
];

View File

@@ -39,11 +39,7 @@
class="flex justify-between items-center rounded-lg p-4 hover:bg-gray-200 hover:p-4 hover:shadow"
>
<div class="flex-shrink text-gray-700 pr-2">
{% if file.isDir %}
<i class="fas fa-fw fa-lg fa-folder"></i>
{% else %}
<i class="fas fa-fw fa-lg {{ icon(file.getBasename) }}"></i>
{% endif %}
{{ icon(file) | raw }}
</div>
<div class="flex-grow truncate mr-2">

View File

@@ -6,6 +6,7 @@
// Icons
$fa-font-path: "./webfonts";
@import "~@fortawesome/fontawesome-free/scss/fontawesome.scss";
@import "~@fortawesome/fontawesome-free/scss/brands.scss";
@import "~@fortawesome/fontawesome-free/scss/solid.scss";
// Fonts