Extracted twig configuration to a dedicated ViewComposer class and reorganized a bit

This commit is contained in:
Chris Kankiewicz
2019-11-09 00:22:23 -07:00
parent 4e826cdb37
commit 070e9f18fe
5 changed files with 59 additions and 43 deletions

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Bootstrap;
use PHLAK\Config\Config;
use Slim\Views\Twig;
use Twig\Extension\CoreExtension;
use Twig\TwigFunction;
class ViewComposer
{
/**
* Set up the Twig component.
*
* @return void
*/
public function __invoke(Config $config, Twig $twig): void
{
$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('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];
})
);
}
}

View File

@@ -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;
}, []);
}
}

View File

@@ -30,7 +30,7 @@
"psr-4": {
"App\\": "app/"
},
"files": ["app/support/helpers.php"]
"files": ["app/Support/helpers.php"]
},
"autoload-dev": {
"psr-4": {

View File

@@ -1,14 +1,12 @@
<?php
use App\Bootstrap\ViewComposer;
use App\Controllers;
use DI\Container;
use DI\Bridge\Slim\Bridge;
use Dotenv\Dotenv;
use PHLAK\Config\Config;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
use Twig\Extension\CoreExtension;
use Twig\TwigFunction;
require __DIR__ . '/vendor/autoload.php';
@@ -18,37 +16,22 @@ $dotenv->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! */