Skip to content

Commit

Permalink
5.3.0
Browse files Browse the repository at this point in the history
- Added hooks
- Added Blade facade
- Cleaned classes
- Refactored classes
  • Loading branch information
beebmx committed Jul 31, 2024
1 parent cbd28be commit 8643bd6
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 289 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": [
"kirby", "kirby-3", "kirby-4", "blade", "view", "template"
],
"version": "5.2.1",
"version": "5.3.0",
"type": "kirby-plugin",
"license": "MIT",
"authors": [
Expand Down
19 changes: 19 additions & 0 deletions extensions/hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Beebmx\Blade\Container as BladeContainer;
use Beebmx\KirbyBlade\Blade;
use Beebmx\Template;
use Illuminate\Container\Container;

return [
'system.loadPlugins:after' => function () {
$container = new BladeContainer;
Container::setInstance($container);

new Blade(
Template::getPathTemplates(),
Template::getPathViews(),
$container
);
},
];
11 changes: 11 additions & 0 deletions extensions/options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Kirby\Cms\App;

return [
'views' => function (): string {
return App::instance()->roots()->cache().'/views';
},
'directives' => [],
'ifs' => [],
];
13 changes: 5 additions & 8 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
<?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;

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

Kirby::plugin('beebmx/kirby-blade', [
'options' => [
'views' => function () {
return kirby()->roots()->cache().'/views';
},
'directives' => [],
'ifs' => [],
],
'options' => require_once __DIR__.'/extensions/options.php',
'hooks' => require_once __DIR__.'/extensions/hooks.php',

'components' => [
'template' => function (Kirby $kirby, string $name, ?string $contentType = null) {
if (Str::endsWith($kirby->request()->url(), '.php')) {
Expand Down
14 changes: 14 additions & 0 deletions src/Facades/Blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Beebmx\Facades;

use Illuminate\Container\Container;
use Kirby\Toolkit\Facade;

class Blade extends Facade
{
public static function instance()
{
return Container::getInstance()->get('blade.compiler');
}
}
10 changes: 6 additions & 4 deletions src/KirbyBlade/Blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ class Blade extends BladeProvider

public function __construct($viewPaths, string $cachePath, ?ContainerInterface $container = null)
{
$this->container = $container ?: new Container;
$this->setupContainer((array) $viewPaths, $cachePath);

(new ViewServiceProvider($this->container))->register();
$this->container = $container ?: Container::getInstance();
Container::setInstance($this->container);

if (! $this->container->has('blade.compiler') || ! $this->container->has('view')) {
$this->setupContainer((array) $viewPaths, $cachePath);
(new ViewServiceProvider($this->container))->register();
}

$this->factory = $this->container->get('view');
$this->compiler = $this->container->get('blade.compiler');
}
Expand Down
12 changes: 9 additions & 3 deletions src/Snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

class Snippet extends Template
{
protected $snippet;
protected string $snippet;

public function __construct(Kirby $kirby, string $name, string $type = 'html', string $defaultType = 'html')
{
$this->template = $kirby->roots()->snippets();
$this->views = $this->getPathViews();
$this->template = static::getPathSnippets();
$this->views = static::getPathViews();
$this->snippet = $this->template.'/'.$name.'.php';

$blade = $this->template.'/'.$name.'.'.$this->bladeExtension();
Expand All @@ -38,6 +38,7 @@ public function render(array $data = []): string
$this->views,
new Container
);

$this->setDirectives();
$this->setIfStatements();

Expand All @@ -46,4 +47,9 @@ public function render(array $data = []): string
return Tpl::load($this->snippet, $data);
}
}

public static function getPathSnippets(): string
{
return Kirby::instance()->roots()->snippets();
}
}
47 changes: 29 additions & 18 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Beebmx;

use Beebmx\Blade\Container;
use Beebmx\Blade\Container as KirbyContainer;
use Beebmx\KirbyBlade\Blade;
use Exception;
use Illuminate\Container\Container;
use Illuminate\Contracts\Container\Container as ContainerInterface;
use Kirby\Cms\App as Kirby;
use Kirby\Filesystem\Dir;
use Kirby\Filesystem\F;
Expand All @@ -15,24 +17,22 @@ class Template extends KirbyTemplate
{
protected Blade $blade;

protected $views;
protected string $views;

protected string $defaultType;

protected string $name;

protected $template;
protected string $template;

protected string $type;

protected Container $app;

public static array $data = [];

public function __construct(Kirby $kirby, string $name, string $type = 'html', string $defaultType = 'html')
{
$this->template = $kirby->roots()->templates();
$this->views = $this->getPathViews();
$this->template = static::getPathTemplates();
$this->views = static::getPathViews();

$this->name = strtolower($name);
$this->type = $type;
Expand Down Expand Up @@ -76,18 +76,22 @@ public function file(): ?string
public function render(array $data = []): string
{
if ($this->isBlade()) {
$this->app = new Container;
$application = $this->getContainer(
Container::getInstance()
);

$this->blade = new Blade(
$this->template,
$this->views,
$this->app
$application
);

$this->setDirectives();
$this->setIfStatements();

if ($this->hasDefaultType() === true) {
return tap($this->blade->make($this->name, $data), function () {
$this->app->terminate();
return tap($this->blade->make($this->name, $data), function () use ($application) {
$application->terminate();
});
}
}
Expand Down Expand Up @@ -270,11 +274,6 @@ public function isBlade(): bool
return (bool) file_exists($this->template.'/'.$this->name().'.'.$this->bladeExtension());
}

public function app(): ?Container
{
return $this->app ?? null;
}

/**
* Returns the expected template file extension
*/
Expand All @@ -283,13 +282,25 @@ public function bladeExtension(): string
return 'blade.php';
}

protected function getPathViews()
public static function getPathTemplates(): string
{
$path = option('beebmx.kirby-blade.views');
return Kirby::instance()->roots()->templates();
}

public static function getPathViews(): string
{
$path = Kirby::instance()->option('beebmx.kirby-blade.views');
if (is_callable($path)) {
return $path();
}

return $path;
}

protected function getContainer(ContainerInterface $container): ContainerInterface
{
return method_exists($container, 'terminate')
? $container
: new KirbyContainer;
}
}
18 changes: 0 additions & 18 deletions src/View/AnonymousComponent.php

This file was deleted.

16 changes: 16 additions & 0 deletions src/View/Compiler/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Beebmx\View\Compiler;

use Illuminate\Container\Container;
use Illuminate\View\Compilers\BladeCompiler as Compiler;

class BladeCompiler extends Compiler
Expand Down Expand Up @@ -59,4 +60,19 @@ public static function sanitizeComponentAttribute($value): mixed
? _e($value)
: $value;
}

public function anonymousComponentPath(string $path, ?string $prefix = null): void
{
$prefixHash = md5($prefix ?: $path);

$this->anonymousComponentPaths[] = [
'path' => $path,
'prefix' => $prefix,
'prefixHash' => $prefixHash,
];

Container::getInstance()
->get('view')
->addNamespace($prefixHash, $path);
}
}
82 changes: 9 additions & 73 deletions src/View/Compiler/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,13 @@

namespace Beebmx\View\Compiler;

use Beebmx\View\AnonymousComponent;
use Beebmx\View\DynamicComponent;
use Illuminate\Container\Container;
use Illuminate\Support\Str;
use Illuminate\View\Compilers\ComponentTagCompiler as TagCompiler;
use InvalidArgumentException;

class ComponentTagCompiler extends TagCompiler
{
/**
* Compile the Blade component string for the given component and attributes.
*
*
* @throws \InvalidArgumentException
*/
protected function componentString(string $component, array $attributes): string
{
$class = $this->componentClass($component);

[$data, $attributes] = $this->partitionDataAndAttributes($class, $attributes);

$data = $data->mapWithKeys(function ($value, $key) {
return [Str::camel($key) => $value];
});

// 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)) {
$parameters = [
'view' => "'$class'",
'data' => '['.$this->attributesToString($data->all(), $escapeBound = false).']',
];

$class = AnonymousComponent::class;
} else {
$parameters = $data->all();
}

return "##BEGIN-COMPONENT-CLASS##@component('{$class}', '{$component}', [".$this->attributesToString($parameters, $escapeBound = false).'])
<?php $component->withAttributes(['.$this->attributesToString($attributes->all(), $escapeAttributes = $class !== DynamicComponent::class).']); ?>';
}

/**
* Get the component class for a given component alias.
*
Expand All @@ -69,49 +33,21 @@ public function componentClass(string $component): string
);
}

$guess = collect($this->blade->getAnonymousComponentNamespaces())
->filter(function ($directory, $prefix) use ($component) {
return Str::startsWith($component, $prefix.'::');
})
->prepend('components', $component)
->reduce(function ($carry, $directory, $prefix) use ($component, $viewFactory) {
if (! is_null($carry)) {
return $carry;
}

$componentName = Str::after($component, $prefix.'::');

if ($viewFactory->exists($view = $this->guessViewName($componentName, $directory))) {
return $view;
}

if ($viewFactory->exists($view = $this->guessViewName($componentName, $directory).'.index')) {
return $view;
}
});
if ($class = $this->findClassByComponent($component)) {
return $class;
}

if (! is_null($guess)) {
if (! is_null($guess = $this->guessAnonymousComponentUsingNamespaces($viewFactory, $component)) ||
! is_null($guess = $this->guessAnonymousComponentUsingPaths($viewFactory, $component))) {
return $guess;
}

if (Str::startsWith($component, 'mail::')) {
return $component;
}

throw new InvalidArgumentException(
"Unable to locate a class or view for component [{$component}]."
);
}

/**
* Convert an array of attributes to a string.
*
* @param bool $escapeBound
*/
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)
? "'{$attribute}' => \Beebmx\View\Compiler\BladeCompiler::sanitizeComponentAttribute({$value})"
: "'{$attribute}' => {$value}";
})
->implode(',');
}
}
Loading

0 comments on commit 8643bd6

Please sign in to comment.