From 9560aa403fd5f1c7411c0d0d642b0dc2d20a65b8 Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Mon, 23 Dec 2019 00:41:36 -0700 Subject: [PATCH] Goodbye themes, hello views as a first-class citizen --- .gitignore | 3 ++ app/Bootstrap/FinderComposer.php | 2 +- app/Bootstrap/ViewComposer.php | 47 +++++++++---------- app/config/app.php | 9 +--- app/config/cache.php | 10 ---- app/{themes/default => config}/icons.php | 0 app/config/view.php | 17 +++++++ .../default/src => resources}/js/app.js | 0 .../js/components/file-info-modal.vue | 0 .../default/src => resources}/sass/app.scss | 0 .../src => resources}/sass/dark-mode.scss | 0 .../views}/components/file.twig | 0 .../default => resources/views}/index.twig | 4 +- app/themes/default/.gitignore | 3 -- app/themes/default/config.php | 6 --- app/themes/default/functions.php | 46 ------------------ .../package-lock.json => package-lock.json | 0 .../default/package.json => package.json | 0 .../tailwind.config.js => tailwind.config.js | 0 .../default/webpack.mix.js => webpack.mix.js | 6 +-- 20 files changed, 50 insertions(+), 103 deletions(-) delete mode 100644 app/config/cache.php rename app/{themes/default => config}/icons.php (100%) create mode 100644 app/config/view.php rename app/{themes/default/src => resources}/js/app.js (100%) rename app/{themes/default/src => resources}/js/components/file-info-modal.vue (100%) rename app/{themes/default/src => resources}/sass/app.scss (100%) rename app/{themes/default/src => resources}/sass/dark-mode.scss (100%) rename app/{themes/default => resources/views}/components/file.twig (100%) rename app/{themes/default => resources/views}/index.twig (94%) delete mode 100644 app/themes/default/.gitignore delete mode 100644 app/themes/default/config.php delete mode 100644 app/themes/default/functions.php rename app/themes/default/package-lock.json => package-lock.json (100%) rename app/themes/default/package.json => package.json (100%) rename app/themes/default/tailwind.config.js => tailwind.config.js (100%) rename app/themes/default/webpack.mix.js => webpack.mix.js (80%) diff --git a/.gitignore b/.gitignore index 6f26b5e..0b4d763 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ +/app/dist/ +/node_modules/ /vendor/ .env .php_cs.cache .phpunit.result.cache +mix-manifest.json diff --git a/app/Bootstrap/FinderComposer.php b/app/Bootstrap/FinderComposer.php index dc1205f..0500b21 100644 --- a/app/Bootstrap/FinderComposer.php +++ b/app/Bootstrap/FinderComposer.php @@ -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; diff --git a/app/Bootstrap/ViewComposer.php b/app/Bootstrap/ViewComposer.php index d85846c..28419c4 100644 --- a/app/Bootstrap/ViewComposer.php +++ b/app/Bootstrap/ViewComposer.php @@ -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 ""; + }) + ); + + $twig->getEnvironment()->addFunction( + new TwigFunction('config', function (string $key, $default = null) { + $viewConfig = $this->config->split('view'); + + return $viewConfig->get($key, $default); + }) + ); } } diff --git a/app/config/app.php b/app/config/app.php index 7cb20fe..57c9111 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -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' */ diff --git a/app/config/cache.php b/app/config/cache.php deleted file mode 100644 index eede807..0000000 --- a/app/config/cache.php +++ /dev/null @@ -1,10 +0,0 @@ - env('VIEW_CACHE', 'app/cache/views'), -]; diff --git a/app/themes/default/icons.php b/app/config/icons.php similarity index 100% rename from app/themes/default/icons.php rename to app/config/icons.php diff --git a/app/config/view.php b/app/config/view.php new file mode 100644 index 0000000..96c20e4 --- /dev/null +++ b/app/config/view.php @@ -0,0 +1,17 @@ + env('DARK_MODE', false), + + /** + * Path to the view cache directory. + * + * Default value: 'app/cache/views' + */ + 'cache' => env('VIEW_CACHE', 'app/cache/views'), +]; diff --git a/app/themes/default/src/js/app.js b/app/resources/js/app.js similarity index 100% rename from app/themes/default/src/js/app.js rename to app/resources/js/app.js diff --git a/app/themes/default/src/js/components/file-info-modal.vue b/app/resources/js/components/file-info-modal.vue similarity index 100% rename from app/themes/default/src/js/components/file-info-modal.vue rename to app/resources/js/components/file-info-modal.vue diff --git a/app/themes/default/src/sass/app.scss b/app/resources/sass/app.scss similarity index 100% rename from app/themes/default/src/sass/app.scss rename to app/resources/sass/app.scss diff --git a/app/themes/default/src/sass/dark-mode.scss b/app/resources/sass/dark-mode.scss similarity index 100% rename from app/themes/default/src/sass/dark-mode.scss rename to app/resources/sass/dark-mode.scss diff --git a/app/themes/default/components/file.twig b/app/resources/views/components/file.twig similarity index 100% rename from app/themes/default/components/file.twig rename to app/resources/views/components/file.twig diff --git a/app/themes/default/index.twig b/app/resources/views/index.twig similarity index 94% rename from app/themes/default/index.twig rename to app/resources/views/index.twig index f6f9f19..5e936f4 100644 --- a/app/themes/default/index.twig +++ b/app/resources/views/index.twig @@ -2,8 +2,8 @@ - - + + Directory Lister diff --git a/app/themes/default/.gitignore b/app/themes/default/.gitignore deleted file mode 100644 index 678f283..0000000 --- a/app/themes/default/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -/dist/ -/node_modules/ -mix-manifest.json diff --git a/app/themes/default/config.php b/app/themes/default/config.php deleted file mode 100644 index d960444..0000000 --- a/app/themes/default/config.php +++ /dev/null @@ -1,6 +0,0 @@ - false -]; diff --git a/app/themes/default/functions.php b/app/themes/default/functions.php deleted file mode 100644 index e77969e..0000000 --- a/app/themes/default/functions.php +++ /dev/null @@ -1,46 +0,0 @@ - [ - /** - * 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 ""; - }), - - /** - * 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; - }), - ] -]; diff --git a/app/themes/default/package-lock.json b/package-lock.json similarity index 100% rename from app/themes/default/package-lock.json rename to package-lock.json diff --git a/app/themes/default/package.json b/package.json similarity index 100% rename from app/themes/default/package.json rename to package.json diff --git a/app/themes/default/tailwind.config.js b/tailwind.config.js similarity index 100% rename from app/themes/default/tailwind.config.js rename to tailwind.config.js diff --git a/app/themes/default/webpack.mix.js b/webpack.mix.js similarity index 80% rename from app/themes/default/webpack.mix.js rename to webpack.mix.js index 97d40f8..fb8aa18 100644 --- a/app/themes/default/webpack.mix.js +++ b/webpack.mix.js @@ -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({