Skip to content

Commit

Permalink
5.0.0
Browse files Browse the repository at this point in the history
- Added more native PHP type hints
- Added support for Kirby 5.x
- Upgrade from illuminate/view 9.x to 10.x
- Upgrade support from beebmx/blade 1.5 to 1.7
- Dropped php@8.0
  • Loading branch information
beebmx committed Nov 29, 2023
1 parent 5830b7d commit 08bb17d
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 174 deletions.
19 changes: 11 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
{
"name": "beebmx/kirby-blade",
"description": "Blade template for Kirby 3",
"description": "Blade template for Kirby 3 & 4",
"keywords": [
"kirby", "kirby-3", "blade", "view", "template"
"kirby", "kirby-3", "kirby-4", "blade", "view", "template"
],
"version": "4.5.1",
"version": "5.0.0",
"type": "kirby-plugin",
"license": "MIT",
"authors": [
{
"name": "Fernando Gutierrez",
"email": "fernando@beeb.mx"
"homepage": "https://beeb.mx"
}
],
"require": {
"php": "^8.0.2",
"beebmx/blade": "^1.5",
"php": "^8.1",
"beebmx/blade": "^1.7",
"getkirby/composer-installer": "^1.2",
"illuminate/view": "^9.19",
"ramsey/uuid": "^4.2"
"illuminate/view": "^10.0",
"ramsey/uuid": "^4.7"
},
"require-dev": {
"laravel/pint": "^1.13"
},
"config": {
"optimize-autoloader": true,
Expand Down
21 changes: 10 additions & 11 deletions helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
use Beebmx\Foundation\Vite;
use Illuminate\Contracts\Support\Htmlable;

if (!function_exists('b')) {
function b($value, $doubleEncode = true)
if (! function_exists('b')) {
function b($value, $doubleEncode = true): string
{
return _e($value, $doubleEncode);
}
}

if (!function_exists('_e')) {
if (! function_exists('_e')) {
/**
* Encode HTML special characters in a string.
*
* @param \Illuminate\Contracts\Support\Htmlable|string $value
* @param bool $doubleEncode
* @return string
* @param \Illuminate\Contracts\Support\Htmlable|string $value
* @param bool $doubleEncode
*/
function _e($value, $doubleEncode = true)
function _e($value, $doubleEncode = true): string
{
if ($value instanceof Htmlable) {
return $value->toHtml();
Expand All @@ -28,16 +27,16 @@ function _e($value, $doubleEncode = true)
}
}

if (!function_exists('public_path')) {
function public_path($path = '')
if (! function_exists('public_path')) {
function public_path($path = ''): string
{
return kirby()->roots()->index().($path ? DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR) : $path);
}
}

if (!function_exists('vite')) {
if (! function_exists('vite')) {
function vite($entrypoints, $buildDirectory = 'build')
{
return (new Vite)($entrypoints, $buildDirectory);
}
}
}
10 changes: 5 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php

@include_once __DIR__ . '/vendor/autoload.php';
@include_once __DIR__.'/vendor/autoload.php';

use Beebmx\Snippet;
use Beebmx\Template;
use Illuminate\Support\Str;
use Kirby\Cms\App as Kirby;
use Kirby\Http\Header;
use Beebmx\Template;

Kirby::plugin('beebmx/kirby-blade', [
'options' => [
'views' => function () {
return kirby()->roots()->cache() . '/views';
return kirby()->roots()->cache().'/views';
},
'directives' => [],
'ifs' => [],
Expand All @@ -26,6 +26,6 @@
},
'snippet' => function (Kirby $kirby, string $name, array $data = []) {
return (new Snippet($kirby, $name))->render($data);
}
]
},
],
]);
86 changes: 32 additions & 54 deletions src/Foundation/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,28 @@ class Vite
{
/**
* The Content Security Policy nonce to apply to all generated tags.
*
* @var string|null
*/
protected $nonce;
protected ?string $nonce = null;

/**
* The key to check for integrity hashes within the manifest.
*
* @var string|false
*/
protected $integrityKey = 'integrity';
protected string|false $integrityKey = 'integrity';

/**
* The script tag attributes resolvers.
*
* @var array
*/
protected $scriptTagAttributesResolvers = [];
protected array $scriptTagAttributesResolvers = [];

/**
* The style tag attributes resolvers.
*
* @var array
*/
protected $styleTagAttributesResolvers = [];
protected array $styleTagAttributesResolvers = [];

/**
* Get the Content Security Policy nonce applied to all generated tags.
*
* @return string|null
*/
public function cspNonce()
public function cspNonce(): ?string
{
return $this->nonce;
}
Expand All @@ -52,9 +42,8 @@ public function cspNonce()
* Generate or set a Content Security Policy nonce to apply to all generated tags.
*
* @param ?string $nonce
* @return string
*/
public function useCspNonce($nonce = null)
public function useCspNonce($nonce = null): string
{
return $this->nonce = $nonce ?? Str::random(40);
}
Expand All @@ -65,7 +54,7 @@ public function useCspNonce($nonce = null)
* @param string|false $key
* @return $this
*/
public function useIntegrityKey($key)
public function useIntegrityKey($key): static
{
$this->integrityKey = $key;

Expand All @@ -78,9 +67,9 @@ public function useIntegrityKey($key)
* @param (callable(string, string, ?array, ?array): array)|array $attributes
* @return $this
*/
public function useScriptTagAttributes($attributes)
public function useScriptTagAttributes($attributes): static
{
if (!is_callable($attributes)) {
if (! is_callable($attributes)) {
$attributes = fn () => $attributes;
}

Expand All @@ -95,9 +84,9 @@ public function useScriptTagAttributes($attributes)
* @param (callable(string, string, ?array, ?array): array)|array $attributes
* @return $this
*/
public function useStyleTagAttributes($attributes)
public function useStyleTagAttributes($attributes): static
{
if (!is_callable($attributes)) {
if (! is_callable($attributes)) {
$attributes = fn () => $attributes;
}

Expand All @@ -111,11 +100,10 @@ public function useStyleTagAttributes($attributes)
*
* @param string|string[] $entrypoints
* @param string $buildDirectory
* @return \Illuminate\Support\HtmlString
*
* @throws \Exception
*/
public function __invoke($entrypoints, $buildDirectory = 'build')
public function __invoke($entrypoints, $buildDirectory = 'build'): HtmlString
{
static $manifests = [];

Expand All @@ -133,10 +121,10 @@ public function __invoke($entrypoints, $buildDirectory = 'build')
);
}

$manifestPath = public_path($buildDirectory . '/manifest.json');
$manifestPath = public_path($buildDirectory.'/manifest.json');

if (!isset($manifests[$manifestPath])) {
if (!is_file($manifestPath)) {
if (! isset($manifests[$manifestPath])) {
if (! is_file($manifestPath)) {
throw new Exception("Vite manifest not found at: {$manifestPath}");
}

Expand All @@ -148,7 +136,7 @@ public function __invoke($entrypoints, $buildDirectory = 'build')
$tags = collect();

foreach ($entrypoints as $entrypoint) {
if (!isset($manifest[$entrypoint])) {
if (! isset($manifest[$entrypoint])) {
throw new Exception("Unable to locate file in Vite manifest: {$entrypoint}.");
}

Expand Down Expand Up @@ -184,7 +172,7 @@ public function __invoke($entrypoints, $buildDirectory = 'build')

[$stylesheets, $scripts] = $tags->partition(fn ($tag) => str_starts_with($tag, '<link'));

return new HtmlString($stylesheets->join('') . $scripts->join(''));
return new HtmlString($stylesheets->join('').$scripts->join(''));
}

/**
Expand All @@ -194,14 +182,13 @@ public function __invoke($entrypoints, $buildDirectory = 'build')
* @param string $url
* @param ?array $chunk
* @param ?array $manifest
* @return string
*/
protected function makeTagForChunk($src, $url, $chunk, $manifest)
protected function makeTagForChunk($src, $url, $chunk, $manifest): string
{
if (
$this->nonce === null
&& $this->integrityKey !== false
&& !array_key_exists($this->integrityKey, $chunk ?? [])
&& ! array_key_exists($this->integrityKey, $chunk ?? [])
&& $this->scriptTagAttributesResolvers === []
&& $this->styleTagAttributesResolvers === []) {
return $this->makeTag($url);
Expand All @@ -227,9 +214,8 @@ protected function makeTagForChunk($src, $url, $chunk, $manifest)
* @param string $url
* @param ?array $chunk
* @param ?array $manifest
* @return array
*/
protected function resolveScriptTagAttributes($src, $url, $chunk, $manifest)
protected function resolveScriptTagAttributes($src, $url, $chunk, $manifest): array
{
$attributes = $this->integrityKey !== false
? ['integrity' => $chunk[$this->integrityKey] ?? false]
Expand All @@ -249,9 +235,8 @@ protected function resolveScriptTagAttributes($src, $url, $chunk, $manifest)
* @param string $url
* @param ?array $chunk
* @param ?array $manifest
* @return array
*/
protected function resolveStylesheetTagAttributes($src, $url, $chunk, $manifest)
protected function resolveStylesheetTagAttributes($src, $url, $chunk, $manifest): array
{
$attributes = $this->integrityKey !== false
? ['integrity' => $chunk[$this->integrityKey] ?? false]
Expand All @@ -270,9 +255,8 @@ protected function resolveStylesheetTagAttributes($src, $url, $chunk, $manifest)
* @deprecated Will be removed in a future Laravel version.
*
* @param string $url
* @return string
*/
protected function makeTag($url)
protected function makeTag($url): string
{
if ($this->isCssPath($url)) {
return $this->makeStylesheetTag($url);
Expand All @@ -287,9 +271,8 @@ protected function makeTag($url)
* @deprecated Will be removed in a future Laravel version.
*
* @param string $url
* @return string
*/
protected function makeScriptTag($url)
protected function makeScriptTag($url): string
{
return $this->makeScriptTagWithAttributes($url, []);
}
Expand All @@ -300,9 +283,8 @@ protected function makeScriptTag($url)
* @deprecated Will be removed in a future Laravel version.
*
* @param string $url
* @return string
*/
protected function makeStylesheetTag($url)
protected function makeStylesheetTag($url): string
{
return $this->makeStylesheetTagWithAttributes($url, []);
}
Expand All @@ -312,44 +294,41 @@ protected function makeStylesheetTag($url)
*
* @param string $url
* @param array $attributes
* @return string
*/
protected function makeScriptTagWithAttributes($url, $attributes)
protected function makeScriptTagWithAttributes($url, $attributes): string
{
$attributes = $this->parseAttributes(array_merge([
'type' => 'module',
'src' => $url,
'nonce' => $this->nonce ?? false,
], $attributes));

return '<script ' . implode(' ', $attributes) . '></script>';
return '<script '.implode(' ', $attributes).'></script>';
}

/**
* Generate a link tag with attributes for the given URL.
*
* @param string $url
* @param array $attributes
* @return string
*/
protected function makeStylesheetTagWithAttributes($url, $attributes)
protected function makeStylesheetTagWithAttributes($url, $attributes): string
{
$attributes = $this->parseAttributes(array_merge([
'rel' => 'stylesheet',
'href' => $url,
'nonce' => $this->nonce ?? false,
], $attributes));

return '<link ' . implode(' ', $attributes) . ' />';
return '<link '.implode(' ', $attributes).' />';
}

/**
* Determine whether the given path is a CSS file.
*
* @param string $path
* @return bool
*/
protected function isCssPath($path)
protected function isCssPath($path): bool
{
return preg_match('/\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/', $path) === 1;
}
Expand All @@ -358,14 +337,13 @@ protected function isCssPath($path)
* Parse the attributes into key="value" strings.
*
* @param array $attributes
* @return array
*/
protected function parseAttributes($attributes)
protected function parseAttributes($attributes): array
{
return Collection::make($attributes)
->reject(fn ($value, $key) => in_array($value, [false, null], true))
->flatMap(fn ($value, $key) => $value === true ? [$key] : [$key => $value])
->map(fn ($value, $key) => is_int($key) ? $value : $key . '="' . $value . '"')
->map(fn ($value, $key) => is_int($key) ? $value : $key.'="'.$value.'"')
->values()
->all();
}
Expand All @@ -377,7 +355,7 @@ protected function parseAttributes($attributes)
*/
public function reactRefresh()
{
if (!is_file(public_path('/hot'))) {
if (! is_file(public_path('/hot'))) {
return;
}

Expand Down
Loading

0 comments on commit 08bb17d

Please sign in to comment.