diff --git a/app/Bootstrap/ViewComposer.php b/app/Bootstrap/ViewComposer.php index 14809fc..0c23e51 100644 --- a/app/Bootstrap/ViewComposer.php +++ b/app/Bootstrap/ViewComposer.php @@ -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); + } } } diff --git a/app/config/icons.php b/app/config/icons.php deleted file mode 100644 index 016e79f..0000000 --- a/app/config/icons.php +++ /dev/null @@ -1,33 +0,0 @@ - [ - /** 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' - ] -]; diff --git a/app/themes/default/config.php b/app/themes/default/config.php new file mode 100644 index 0000000..041a58c --- /dev/null +++ b/app/themes/default/config.php @@ -0,0 +1,17 @@ + [ + new TwigFunction('icon', function ($file) { + $icons = require __DIR__ . '/icons.php'; + + $icon = $file->isDir() ? 'fas fa-folder' + : $icons[$file->getExtension()] ?? 'fas fa-file'; + + return ""; + }) + ] +]; diff --git a/app/themes/default/icons.php b/app/themes/default/icons.php new file mode 100644 index 0000000..1a6d371 --- /dev/null +++ b/app/themes/default/icons.php @@ -0,0 +1,132 @@ + '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', +]; diff --git a/app/themes/default/index.twig b/app/themes/default/index.twig index 6eafa9e..a95c51f 100644 --- a/app/themes/default/index.twig +++ b/app/themes/default/index.twig @@ -39,11 +39,7 @@ class="flex justify-between items-center rounded-lg p-4 hover:bg-gray-200 hover:p-4 hover:shadow" >