diff --git a/app/Bootstrap/ViewComposer.php b/app/Bootstrap/ViewComposer.php new file mode 100644 index 0000000..8716ac4 --- /dev/null +++ b/app/Bootstrap/ViewComposer.php @@ -0,0 +1,38 @@ +getEnvironment()->getExtension(CoreExtension::class)->setDateFormat( + $config->get('date_format'), '%d days' + ); + + $twig->getEnvironment()->addFunction( + new TwigFunction('asset', function ($path) use ($config) { + return "/app/themes/{$config->get('theme')}/{$path}"; + }) + ); + + $twig->getEnvironment()->addFunction( + new TwigFunction('sizeForHumans', function ($bytes) { + $sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + $factor = floor((strlen($bytes) - 1) / 3); + + return sprintf('%.2f', $bytes / pow(1024, $factor)) . $sizes[$factor]; + }) + ); + } +} diff --git a/app/controllers/DirectoryController.php b/app/Controllers/DirectoryController.php similarity index 80% rename from app/controllers/DirectoryController.php rename to app/Controllers/DirectoryController.php index 5b4c598..d9e9591 100644 --- a/app/controllers/DirectoryController.php +++ b/app/Controllers/DirectoryController.php @@ -7,6 +7,7 @@ use Slim\Psr7\Request; use Slim\Psr7\Response; use Slim\Views\Twig; use Symfony\Component\Finder\Finder; +use Tightenco\Collect\Support\Collection; class DirectoryController { @@ -61,20 +62,14 @@ class DirectoryController */ protected function breadcrumbs(string $path): array { - $breadcrumbs = []; - $crumbsPath = ''; + $breadcrumbs = Collection::make(array_filter(explode('/', $path))); - foreach (array_filter(explode('/', $path)) as $crumb) { - $crumbsPath .= "/{$crumb}"; + return $breadcrumbs->filter(function ($crumb) { + return $crumb !== '.'; + })->reduce(function (array $carry, string $crumb) { + $carry[$crumb] = end($carry) . "/{$crumb}"; - $breadcrumbs[] = [ - 'name' => $crumb, - 'path' => $crumbsPath - ]; - } - - return array_filter($breadcrumbs, function ($crumb) { - return $crumb['name'] !== '.'; - }); + return $carry; + }, []); } } diff --git a/app/support/helpers.php b/app/Support/helpers.php similarity index 100% rename from app/support/helpers.php rename to app/Support/helpers.php diff --git a/composer.json b/composer.json index 9fade52..7c491a7 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "psr-4": { "App\\": "app/" }, - "files": ["app/support/helpers.php"] + "files": ["app/Support/helpers.php"] }, "autoload-dev": { "psr-4": { diff --git a/index.php b/index.php index 3ba7f0c..01268b8 100644 --- a/index.php +++ b/index.php @@ -1,14 +1,12 @@ load(); /** Create the container */ $container = new Container(); -$container->set(Config::class, $config = new Config('app/config')); -/** Set up our view handler */ -$twig = new Twig("app/themes/{$config->get('theme')}", [ - 'cache' => $config->get('view_cache') -]); +/** Register dependencies */ +$container->set(Config::class, new Config('app/config')); +$container->set(Twig::class, function (Config $config) { + return new Twig("app/themes/{$config->get('theme')}", [ + 'cache' => $config->get('view_cache') + ]); +}); -$twig->getEnvironment()->getExtension(CoreExtension::class)->setDateFormat( - $config->get('date_format'), '%d days' -); - -$twig->getEnvironment()->addFunction( - new TwigFunction('asset', function ($path) use ($config) { - return "/app/themes/{$config->get('theme')}/{$path}"; - }) -); - -$twig->getEnvironment()->addFunction( - new TwigFunction('convertSize', function ($bytes) { - $sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - $factor = floor((strlen($bytes) - 1) / 3); - - return sprintf('%.2f', $bytes / pow(1024, $factor)) . $sizes[$factor]; - }) -); - -$container->set(Twig::class, $twig); +/** Configure the view handler */ +$container->call(ViewComposer::class); /** Create the application */ $app = Bridge::create($container); -$app->add(TwigMiddleware::createFromContainer($app, Twig::class)); + +/** Register routes */ $app->get('/[{path:.*}]', Controllers\DirectoryController::class); /** Enagage! */