From 08bb17daa4b2a9c55c16d66be006a6fcf135c619 Mon Sep 17 00:00:00 2001 From: Fernando Gutierrez Date: Wed, 29 Nov 2023 15:39:28 -0600 Subject: [PATCH] 5.0.0 - 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 --- composer.json | 19 +++-- helpers.php | 21 +++--- index.php | 10 +-- src/Foundation/Vite.php | 86 ++++++++-------------- src/KirbyBlade/Blade.php | 4 +- src/Snippet.php | 8 +- src/Template.php | 31 +++----- src/View/AnonymousComponent.php | 4 +- src/View/Compiler/BladeCompiler.php | 24 ++---- src/View/Compiler/ComponentTagCompiler.php | 35 ++++----- src/View/ComponentAttributeBag.php | 7 +- src/View/Concerns/ManagesLayouts.php | 7 +- src/View/DynamicComponent.php | 16 ++-- src/View/Factory.php | 12 +-- src/View/ViewServiceProvider.php | 8 +- 15 files changed, 118 insertions(+), 174 deletions(-) diff --git a/composer.json b/composer.json index cf8b963..05416fa 100644 --- a/composer.json +++ b/composer.json @@ -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, diff --git a/helpers.php b/helpers.php index 6681b05..0a73cb4 100644 --- a/helpers.php +++ b/helpers.php @@ -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(); @@ -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); } -} \ No newline at end of file +} diff --git a/index.php b/index.php index ae81724..2196020 100644 --- a/index.php +++ b/index.php @@ -1,17 +1,17 @@ [ 'views' => function () { - return kirby()->roots()->cache() . '/views'; + return kirby()->roots()->cache().'/views'; }, 'directives' => [], 'ifs' => [], @@ -26,6 +26,6 @@ }, 'snippet' => function (Kirby $kirby, string $name, array $data = []) { return (new Snippet($kirby, $name))->render($data); - } - ] + }, + ], ]); diff --git a/src/Foundation/Vite.php b/src/Foundation/Vite.php index 75d9427..22bfc58 100644 --- a/src/Foundation/Vite.php +++ b/src/Foundation/Vite.php @@ -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; } @@ -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); } @@ -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; @@ -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; } @@ -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; } @@ -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 = []; @@ -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}"); } @@ -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}."); } @@ -184,7 +172,7 @@ public function __invoke($entrypoints, $buildDirectory = 'build') [$stylesheets, $scripts] = $tags->partition(fn ($tag) => str_starts_with($tag, 'join('') . $scripts->join('')); + return new HtmlString($stylesheets->join('').$scripts->join('')); } /** @@ -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); @@ -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] @@ -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] @@ -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); @@ -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, []); } @@ -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, []); } @@ -312,9 +294,8 @@ 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', @@ -322,7 +303,7 @@ protected function makeScriptTagWithAttributes($url, $attributes) 'nonce' => $this->nonce ?? false, ], $attributes)); - return ''; + return ''; } /** @@ -330,9 +311,8 @@ protected function makeScriptTagWithAttributes($url, $attributes) * * @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', @@ -340,16 +320,15 @@ protected function makeStylesheetTagWithAttributes($url, $attributes) 'nonce' => $this->nonce ?? false, ], $attributes)); - return ''; + return ''; } /** * 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; } @@ -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(); } @@ -377,7 +355,7 @@ protected function parseAttributes($attributes) */ public function reactRefresh() { - if (!is_file(public_path('/hot'))) { + if (! is_file(public_path('/hot'))) { return; } diff --git a/src/KirbyBlade/Blade.php b/src/KirbyBlade/Blade.php index 31af1b0..98091a0 100644 --- a/src/KirbyBlade/Blade.php +++ b/src/KirbyBlade/Blade.php @@ -2,12 +2,12 @@ namespace Beebmx\KirbyBlade; -use Illuminate\Contracts\View\View; -use Illuminate\View\Compilers\BladeCompiler; use Beebmx\Blade\Blade as BladeProvider; use Beebmx\View\ViewServiceProvider; use Illuminate\Container\Container; use Illuminate\Contracts\Container\Container as ContainerInterface; +use Illuminate\Contracts\View\View; +use Illuminate\View\Compilers\BladeCompiler; class Blade extends BladeProvider { diff --git a/src/Snippet.php b/src/Snippet.php index c3f67af..d97bcba 100644 --- a/src/Snippet.php +++ b/src/Snippet.php @@ -15,9 +15,9 @@ public function __construct(Kirby $kirby, string $name, string $type = 'html', s { $this->template = $kirby->roots()->snippets(); $this->views = $this->getPathViews(); - $this->snippet = $this->template . '/' . $name . '.php'; + $this->snippet = $this->template.'/'.$name.'.php'; - $blade = $this->template . '/' . $name . '.' . $this->bladeExtension(); + $blade = $this->template.'/'.$name.'.'.$this->bladeExtension(); if (file_exists($this->snippet) === false && file_exists($blade) === false) { $this->snippet = $kirby->extensions('snippets')[$name]; @@ -30,10 +30,6 @@ public function __construct(Kirby $kirby, string $name, string $type = 'html', s $this->setViewDirectory(); } - /** - * @param array $data - * @return string - */ public function render(array $data = []): string { if ($this->isBlade()) { diff --git a/src/Template.php b/src/Template.php index fd859b1..87d01d6 100644 --- a/src/Template.php +++ b/src/Template.php @@ -3,12 +3,12 @@ namespace Beebmx; use Beebmx\Blade\Application; -use Kirby\Cms\App as Kirby; -use Kirby\Template\Template as KirbyTemplate; use Beebmx\KirbyBlade\Blade; use Exception; +use Kirby\Cms\App as Kirby; use Kirby\Filesystem\Dir; use Kirby\Filesystem\F; +use Kirby\Template\Template as KirbyTemplate; use Kirby\Toolkit\Tpl; class Template extends KirbyTemplate @@ -42,8 +42,6 @@ public function __construct(Kirby $kirby, string $name, string $type = 'html', s /** * Detects the location of the template file * if it exists. - * - * @return string|null */ public function file(): ?string { @@ -61,7 +59,7 @@ public function file(): ?string } } - $name = $this->name() . '.' . $this->type(); + $name = $this->name().'.'.$this->type(); try { // Try the template with type extension in the default template directory. @@ -73,10 +71,6 @@ public function file(): ?string } } - /** - * @param array $data - * @return string - */ public function render(array $data = []): string { if ($this->isBlade()) { @@ -90,7 +84,7 @@ public function render(array $data = []): string $this->setIfStatements(); if ($this->hasDefaultType() === true) { - return tap($this->blade->make($this->name, $data), function() use ($application) { + return tap($this->blade->make($this->name, $data), function () use ($application) { $application->terminate(); }); } @@ -103,7 +97,7 @@ public function render(array $data = []): string public function setViewDirectory() { - if (!file_exists($this->views)) { + if (! file_exists($this->views)) { Dir::make($this->views); } } @@ -250,28 +244,26 @@ protected function setIfStatements() } } - public function getFilename(string $name = null) + public function getFilename(string $name = null): string { if ($name) { - return $this->root() . '/' . $name . '.' . $this->extension(); + return $this->root().'/'.$name.'.'.$this->extension(); } if ($this->isBlade()) { - return $this->root() . '/' . $this->name() . '.' . $this->bladeExtension(); + return $this->root().'/'.$this->name().'.'.$this->bladeExtension(); } else { - return $this->root() . '/' . $this->name() . '.' . $this->extension(); + return $this->root().'/'.$this->name().'.'.$this->extension(); } } - public function isBlade() + public function isBlade(): bool { - return !!file_exists($this->template . '/' . $this->name() . '.' . $this->bladeExtension()); + return (bool) file_exists($this->template.'/'.$this->name().'.'.$this->bladeExtension()); } /** * Returns the expected template file extension - * - * @return string */ public function bladeExtension(): string { @@ -284,6 +276,7 @@ protected function getPathViews() if (is_callable($path)) { return $path(); } + return $path; } } diff --git a/src/View/AnonymousComponent.php b/src/View/AnonymousComponent.php index 04c4c02..073d79c 100644 --- a/src/View/AnonymousComponent.php +++ b/src/View/AnonymousComponent.php @@ -8,10 +8,8 @@ class AnonymousComponent extends Anonymous { /** * Get the data that should be supplied to the view. - * - * @return array */ - public function data() + public function data(): array { $this->attributes = $this->attributes ?: new ComponentAttributeBag; diff --git a/src/View/Compiler/BladeCompiler.php b/src/View/Compiler/BladeCompiler.php index c5668b7..c8130e1 100644 --- a/src/View/Compiler/BladeCompiler.php +++ b/src/View/Compiler/BladeCompiler.php @@ -17,10 +17,8 @@ class BladeCompiler extends Compiler * Register an "if" statement directive. * * @param string $name - * @param callable $callback - * @return void */ - public function if($name, callable $callback) + public function if($name, callable $callback): void { $this->conditions[$name] = $callback; $this->directive($name, function ($expression) use ($name) { @@ -28,32 +26,28 @@ public function if($name, callable $callback) ? "" : ""; }); - $this->directive('else' . $name, function ($expression) use ($name) { + $this->directive('else'.$name, function ($expression) use ($name) { return $expression !== '' ? "" : ""; }); - $this->directive('end' . $name, function () { + $this->directive('end'.$name, function () { return ''; }); } /** * Set the "echo" format to double encode entities. - * - * @return void */ - public function withDoubleEncoding() + public function withDoubleEncoding(): void { $this->setEchoFormat('_e(%s, true)'); } /** * Set the "echo" format to not double encode entities. - * - * @return void */ - public function withoutDoubleEncoding() + public function withoutDoubleEncoding(): void { $this->setEchoFormat('_e(%s, false)'); } @@ -62,11 +56,10 @@ public function withoutDoubleEncoding() * Compile the component tags. * * @param string $value - * @return string */ - protected function compileComponentTags($value) + protected function compileComponentTags($value): string { - if (!$this->compilesComponentTags) { + if (! $this->compilesComponentTags) { return $value; } @@ -81,9 +74,8 @@ protected function compileComponentTags($value) * Sanitize the given component attribute value. * * @param mixed $value - * @return mixed */ - public static function sanitizeComponentAttribute($value) + public static function sanitizeComponentAttribute($value): mixed { return is_string($value) || (is_object($value) && method_exists($value, '__toString')) diff --git a/src/View/Compiler/ComponentTagCompiler.php b/src/View/Compiler/ComponentTagCompiler.php index 761ae0e..30969c4 100644 --- a/src/View/Compiler/ComponentTagCompiler.php +++ b/src/View/Compiler/ComponentTagCompiler.php @@ -6,21 +6,18 @@ use Beebmx\View\DynamicComponent; use Illuminate\Container\Container; use Illuminate\Support\Str; +use Illuminate\View\Compilers\ComponentTagCompiler as TagCompiler; use InvalidArgumentException; -use \Illuminate\View\Compilers\ComponentTagCompiler as TagCompiler; class ComponentTagCompiler extends TagCompiler { /** * Compile the Blade component string for the given component and attributes. * - * @param string $component - * @param array $attributes - * @return string * * @throws \InvalidArgumentException */ - protected function componentString(string $component, array $attributes) + protected function componentString(string $component, array $attributes): string { $class = $this->componentClass($component); @@ -33,10 +30,10 @@ protected function componentString(string $component, array $attributes) // If the component doesn't exists as a class we'll assume it's a class-less // component and pass the component as a view parameter to the data so it // can be accessed within the component and we can render out the view. - if (!class_exists($class)) { + if (! class_exists($class)) { $parameters = [ 'view' => "'$class'", - 'data' => '[' . $this->attributesToString($data->all(), $escapeBound = false) . ']', + 'data' => '['.$this->attributesToString($data->all(), $escapeBound = false).']', ]; $class = AnonymousComponent::class; @@ -44,19 +41,17 @@ protected function componentString(string $component, array $attributes) $parameters = $data->all(); } - return "##BEGIN-COMPONENT-CLASS##@component('{$class}', '{$component}', [" . $this->attributesToString($parameters, $escapeBound = false) . ']) -withAttributes([' . $this->attributesToString($attributes->all(), $escapeAttributes = $class !== DynamicComponent::class) . ']); ?>'; + return "##BEGIN-COMPONENT-CLASS##@component('{$class}', '{$component}', [".$this->attributesToString($parameters, $escapeBound = false).']) +withAttributes(['.$this->attributesToString($attributes->all(), $escapeAttributes = $class !== DynamicComponent::class).']); ?>'; } /** * Get the component class for a given component alias. * - * @param string $component - * @return string * * @throws \InvalidArgumentException */ - public function componentClass(string $component) + public function componentClass(string $component): string { $viewFactory = Container::getInstance()->get('view'); @@ -76,26 +71,26 @@ public function componentClass(string $component) $guess = collect($this->blade->getAnonymousComponentNamespaces()) ->filter(function ($directory, $prefix) use ($component) { - return Str::startsWith($component, $prefix . '::'); + return Str::startsWith($component, $prefix.'::'); }) ->prepend('components', $component) ->reduce(function ($carry, $directory, $prefix) use ($component, $viewFactory) { - if (!is_null($carry)) { + if (! is_null($carry)) { return $carry; } - $componentName = Str::after($component, $prefix . '::'); + $componentName = Str::after($component, $prefix.'::'); if ($viewFactory->exists($view = $this->guessViewName($componentName, $directory))) { return $view; } - if ($viewFactory->exists($view = $this->guessViewName($componentName, $directory) . '.index')) { + if ($viewFactory->exists($view = $this->guessViewName($componentName, $directory).'.index')) { return $view; } }); - if (!is_null($guess)) { + if (! is_null($guess)) { return $guess; } @@ -107,15 +102,13 @@ public function componentClass(string $component) /** * Convert an array of attributes to a string. * - * @param array $attributes * @param bool $escapeBound - * @return string */ - protected function attributesToString(array $attributes, $escapeBound = true) + protected function attributesToString(array $attributes, $escapeBound = true): string { return collect($attributes) ->map(function (string $value, string $attribute) use ($escapeBound) { - return $escapeBound && isset($this->boundAttributes[$attribute]) && $value !== 'true' && !is_numeric($value) + return $escapeBound && isset($this->boundAttributes[$attribute]) && $value !== 'true' && ! is_numeric($value) ? "'{$attribute}' => \Beebmx\View\Compiler\BladeCompiler::sanitizeComponentAttribute({$value})" : "'{$attribute}' => {$value}"; }) diff --git a/src/View/ComponentAttributeBag.php b/src/View/ComponentAttributeBag.php index b0ed0b6..da9965e 100644 --- a/src/View/ComponentAttributeBag.php +++ b/src/View/ComponentAttributeBag.php @@ -10,11 +10,9 @@ class ComponentAttributeBag extends AttributeBag /** * Merge additional attributes / values into the attribute bag. * - * @param array $attributeDefaults * @param bool $escape - * @return static */ - public function merge(array $attributeDefaults = [], $escape = true) + public function merge(array $attributeDefaults = [], $escape = true): static { $attributeDefaults = array_map(function ($value) use ($escape) { return $this->shouldEscapeAttributeValue($escape, $value) @@ -46,9 +44,8 @@ public function merge(array $attributeDefaults = [], $escape = true) * @param array $attributeDefaults * @param string $key * @param bool $escape - * @return mixed */ - protected function resolveAppendableAttributeDefault($attributeDefaults, $key, $escape) + protected function resolveAppendableAttributeDefault($attributeDefaults, $key, $escape): mixed { if ($this->shouldEscapeAttributeValue($escape, $value = $attributeDefaults[$key]->value)) { $value = _e($value); diff --git a/src/View/Concerns/ManagesLayouts.php b/src/View/Concerns/ManagesLayouts.php index 3075357..f889d39 100644 --- a/src/View/Concerns/ManagesLayouts.php +++ b/src/View/Concerns/ManagesLayouts.php @@ -16,9 +16,8 @@ trait ManagesLayouts * * @param string $section * @param string|null $content - * @return void */ - public function startSection($section, $content = null) + public function startSection($section, $content = null): void { if ($content === null) { if (ob_start()) { @@ -34,15 +33,15 @@ public function startSection($section, $content = null) * * @param string $section * @param string $default - * @return string */ - public function yieldContent($section, $default = '') + public function yieldContent($section, $default = ''): string { $sectionContent = $default instanceof View ? $default : b($default); if (isset($this->sections[$section])) { $sectionContent = $this->sections[$section]; } $sectionContent = str_replace('@@parent', '--parent--holder--', $sectionContent); + return str_replace( '--parent--holder--', '@parent', diff --git a/src/View/DynamicComponent.php b/src/View/DynamicComponent.php index ffdeef5..d0b332c 100644 --- a/src/View/DynamicComponent.php +++ b/src/View/DynamicComponent.php @@ -3,12 +3,14 @@ namespace Beebmx\View; use Beebmx\View\Compiler\ComponentTagCompiler; +use Closure; use Illuminate\Container\Container; +use Illuminate\View\Compilers\BladeTagCompiler; use Illuminate\View\DynamicComponent as Dynamic; class DynamicComponent extends Dynamic { - public function render() + public function render(): Closure { $template = <<<'EOF' getAttributes())->mapWithKeys(function ($value, $key) { return [Illuminate\Support\Str::camel(str_replace([':', '.'], ' ', $key)) => $value]; })->all(), EXTR_SKIP); ?> @@ -43,9 +45,9 @@ class_exists($class) ? '{{ $attributes }}' : '', }; } - protected function compiler() + protected function compiler(): ComponentTagCompiler|BladeTagCompiler { - if (!static::$compiler) { + if (! static::$compiler) { static::$compiler = new ComponentTagCompiler( Container::getInstance()->get('blade.compiler')->getClassComponentAliases(), Container::getInstance()->get('blade.compiler')->getClassComponentNamespaces(), @@ -56,21 +58,21 @@ protected function compiler() return static::$compiler; } - protected function createBladeViewFromString($factory, $contents) + protected function createBladeViewFromString($factory, $contents): string { $factory->addNamespace( '__components', $directory = Container::getInstance()->get('config')['view.compiled'] ); - if (!is_file($viewFile = $directory . '/' . sha1($contents) . '.blade.php')) { - if (!is_dir($directory)) { + if (! is_file($viewFile = $directory.'/'.sha1($contents).'.blade.php')) { + if (! is_dir($directory)) { mkdir($directory, 0755, true); } file_put_contents($viewFile, $contents); } - return '__components::' . basename($viewFile, '.blade.php'); + return '__components::'.basename($viewFile, '.blade.php'); } } diff --git a/src/View/Factory.php b/src/View/Factory.php index 23c929b..2e44a7e 100644 --- a/src/View/Factory.php +++ b/src/View/Factory.php @@ -2,12 +2,12 @@ namespace Beebmx\View; -use Illuminate\Support\HtmlString; -use Illuminate\View\FileViewFinder; +use Beebmx\View\Concerns\ManagesLayouts; use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Support\HtmlString; use Illuminate\View\Engines\EngineResolver; use Illuminate\View\Factory as FactoryView; -use Beebmx\View\Concerns\ManagesLayouts; +use Illuminate\View\FileViewFinder; class Factory extends FactoryView { @@ -16,9 +16,7 @@ class Factory extends FactoryView /** * Create a new view factory instance. * - * @param \Illuminate\View\Engines\EngineResolver $engines * @param \Illuminate\View\ViewFinderInterface $finder - * @param \Illuminate\Contracts\Events\Dispatcher $events * @return void */ public function __construct(EngineResolver $engines, FileViewFinder $finder, Dispatcher $events = null) @@ -32,10 +30,8 @@ public function __construct(EngineResolver $engines, FileViewFinder $finder, Dis /** * Get the data for the given component. - * - * @return array */ - protected function componentData() + protected function componentData(): array { $defaultSlot = new HtmlString(trim(ob_get_clean())); diff --git a/src/View/ViewServiceProvider.php b/src/View/ViewServiceProvider.php index e18273d..fb7e43e 100644 --- a/src/View/ViewServiceProvider.php +++ b/src/View/ViewServiceProvider.php @@ -2,22 +2,20 @@ namespace Beebmx\View; -use Illuminate\View\ViewServiceProvider as ViewProvider; use Beebmx\View\Compiler\BladeCompiler; +use Illuminate\View\ViewServiceProvider as ViewProvider; class ViewServiceProvider extends ViewProvider { - protected function createFactory($resolver, $finder, $events) + protected function createFactory($resolver, $finder, $events): Factory { return new Factory($resolver, $finder, $events); } /** * Register the Blade compiler implementation. - * - * @return void */ - public function registerBladeCompiler() + public function registerBladeCompiler(): void { $this->app->singleton('blade.compiler', function ($app) { return tap(new BladeCompiler($app['files'], $app['config']['view.compiled']), function ($blade) {