Goodbye themes, hello views as a first-class citizen

This commit is contained in:
Chris Kankiewicz
2019-12-23 00:41:36 -07:00
parent d00e9a22f6
commit 9560aa403f
20 changed files with 50 additions and 103 deletions

3
.gitignore vendored
View File

@@ -1,4 +1,7 @@
/app/dist/
/node_modules/
/vendor/
.env
.php_cs.cache
.phpunit.result.cache
mix-manifest.json

View File

@@ -13,7 +13,7 @@ use Tightenco\Collect\Support\Collection;
class FinderComposer
{
/** @const Application paths to be hidden */
protected const APP_FILES = ['app', 'vendor', 'index.php'];
protected const APP_FILES = ['app', 'node_modules', 'vendor', 'index.php'];
/** @var Config Application config */
protected $config;

View File

@@ -5,6 +5,7 @@ namespace App\Bootstrap;
use DI\Container;
use PHLAK\Config\Config;
use Slim\Views\Twig;
use Symfony\Component\Finder\SplFileInfo;
use Twig\Extension\CoreExtension;
use Twig\TwigFunction;
@@ -35,34 +36,33 @@ class ViewComposer
*/
public function __invoke(): void
{
$twig = new Twig("app/themes/{$this->config->get('app.theme', 'default')}");
$twig = new Twig('app/resources/views');
$twig->getEnvironment()->setCache(
$this->config->get('cache.view_cache', 'app/cache/views')
$this->config->get('view.cache', 'app/cache/views')
);
$twig->getEnvironment()->getExtension(CoreExtension::class)->setDateFormat(
$this->config->get('app.date_format', 'Y-m-d H:i:s'), '%d days'
);
$this->registerGlobalFunctions($twig);
$this->registerThemeFunctions($twig);
$this->registerFunctions($twig);
$this->container->set(Twig::class, $twig);
}
/**
* Register global Twig functions.
* Register Twig functions.
*
* @param \Slim\Views\Twig $twig
*
* @return void
*/
protected function registerGlobalFunctions(Twig $twig): void
protected function registerFunctions(Twig $twig): void
{
$twig->getEnvironment()->addFunction(
new TwigFunction('asset', function (string $path) use ($twig) {
return "/{$twig->getLoader()->getPaths()[0]}/{$path}";
return "/app/dist/{$path}";
})
);
@@ -74,25 +74,24 @@ class ViewComposer
return sprintf('%.2f', $bytes / pow(1024, $factor)) . $sizes[$factor];
})
);
}
/**
* Register theme specific Twig functions.
*
* @param \Slim\Views\Twig $twig
*
* @return void
*/
protected function registerThemeFunctions(Twig $twig): void
{
$themeFunctionsFile = "{$twig->getLoader()->getPaths()[0]}/functions.php";
$twig->getEnvironment()->addFunction(
new TwigFunction('icon', function (SplFileInfo $file) {
$iconConfig = $this->config->split('icons');
if (file_exists($themeFunctionsFile)) {
$themeConfig = include $themeFunctionsFile;
}
$icon = $file->isDir() ? 'fas fa-folder'
: $iconConfig->get($file->getExtension(), 'fas fa-file');
foreach ($themeConfig['functions'] ?? [] as $function) {
$twig->getEnvironment()->addFunction($function);
}
return "<i class=\"{$icon} fa-fw fa-lg\"></i>";
})
);
$twig->getEnvironment()->addFunction(
new TwigFunction('config', function (string $key, $default = null) {
$viewConfig = $this->config->split('view');
return $viewConfig->get($key, $default);
})
);
}
}

View File

@@ -18,14 +18,7 @@ return [
'reverse_sort' => false,
/**
* Name of the theme to use for styling the application.
*
* Default value: default
*/
'theme' => 'default',
/**
* Description here...
* Default date format.
*
* Default value: 'Y-m-d H:i:s'
*/

View File

@@ -1,10 +0,0 @@
<?php
return [
/**
* Path to the view cache directory.
*
* Default value: 'app/cache/views'
*/
'view_cache' => env('VIEW_CACHE', 'app/cache/views'),
];

17
app/config/view.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
return [
/**
* Enable dark mode?
*
* Default value: true
*/
'dark_mode' => env('DARK_MODE', false),
/**
* Path to the view cache directory.
*
* Default value: 'app/cache/views'
*/
'cache' => env('VIEW_CACHE', 'app/cache/views'),
];

View File

@@ -2,8 +2,8 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="{{ asset('dist/app.js') }}" defer></script>
<link rel="stylesheet" href="{{ asset('dist/app.css') }}">
<script src="{{ asset('app.js') }}" defer></script>
<link rel="stylesheet" href="{{ asset('app.css') }}">
<title>Directory Lister</title>

View File

@@ -1,3 +0,0 @@
/dist/
/node_modules/
mix-manifest.json

View File

@@ -1,6 +0,0 @@
<?php
return [
/** Enable dark mode? */
'dark_mode' => false
];

View File

@@ -1,46 +0,0 @@
<?php
use Symfony\Component\Finder\SplFileInfo;
use Twig\TwigFunction;
return [
/** Theme specific Twig functions */
'functions' => [
/**
* Return the icon markup for a given file.
*
* @param SplFileInfo $file
*
* @return string
*/
new TwigFunction('icon', function (SplFileInfo $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>";
}),
/**
* Retrieve an item from the theme config.
*
* @param string $key
* @param mixed|null $default
*
* @return mixed
*/
new TwigFunction('config', function (string $key, $default = null) {
$config = require __DIR__ . '/config.php';
foreach (explode('.', $key) as $k) {
if (! isset($config[$k])) {
return $default;
}
$config = $config[$k];
}
return $config;
}),
]
];

View File

@@ -2,16 +2,16 @@ let mix = require('laravel-mix');
let tailwindcss = require('tailwindcss');
require('laravel-mix-purgecss');
mix.sass('src/sass/app.scss', 'dist').options({
mix.sass('app/resources/sass/app.scss', 'app/dist').options({
processCssUrls: false,
postCss: [tailwindcss('tailwind.config.js')]
});
mix.js('src/js/app.js', 'dist');
mix.js('app/resources/js/app.js', 'app/dist');
mix.copyDirectory(
'node_modules/@fortawesome/fontawesome-free/webfonts',
'dist/webfonts'
'app/dist/webfonts'
);
mix.purgeCss({