Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Commit

Permalink
Fixed toString issue when blade throw an error #9
Browse files Browse the repository at this point in the history
  • Loading branch information
afbora committed Nov 5, 2019
1 parent 3fe5e56 commit 3979766
Show file tree
Hide file tree
Showing 21 changed files with 724 additions and 650 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.idea
/kirby
composer.lock
.idea
.DS_Store
composer.lock
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"template",
"template-engine"
],
"version": "1.5.0",
"version": "1.5.1",
"type": "kirby-plugin",
"license": "MIT",
"authors": [
Expand Down
1 change: 0 additions & 1 deletion src/Blade/Blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Afbora\Blade;

use Afbora\View\ViewServiceProvider;
use Pine\BladeFilters\BladeFiltersServiceProvider;
use Illuminate\Container\Container;
use Illuminate\Contracts\Container\Container as ContainerInterface;
use Illuminate\Contracts\View\Factory as FactoryContract;
Expand Down
137 changes: 69 additions & 68 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Afbora;

use Afbora\Blade\Blade;
use Exception;
use Kirby\Cms\App as Kirby;
use Kirby\Cms\Template as KirbyTemplate;
use Afbora\Blade\Blade;
use Kirby\Toolkit\F;
use Kirby\Toolkit\Tpl;
use Kirby\Toolkit\Dir;
Expand All @@ -14,13 +14,13 @@

class Template extends KirbyTemplate
{
public static $data = [];
protected $blade;
protected $views;
protected $defaultType;
protected $name;
protected $templates;
protected $type;
public static $data = [];

public function __construct(Kirby $kirby, string $name, string $type = 'html', string $defaultType = 'html')
{
Expand All @@ -34,33 +34,42 @@ public function __construct(Kirby $kirby, string $name, string $type = 'html', s
$this->setViewDirectory();
}

public function file(): ?string
protected function getPathTemplates()
{
if ($this->hasDefaultType() === true) {
try {
// Try the default template in the default template directory.
return F::realpath($this->getFilename(), $this->getPathTemplates());
} catch (Exception $e) {
//
}
if (!is_null($this->templates)) {
return $this->templates;
}

// Look for the default template provided by an extension.
$path = Kirby::instance()->extension($this->store(), $this->name());
$optionPath = option('afbora.blade.templates');

if ($path !== null) {
return $path;
if ($optionPath !== null && is_dir($optionPath)) {
if (is_callable($optionPath)) {
return $optionPath();
}

$path = kirby()->roots()->index() . "/" . $optionPath;
} else {
$path = $this->root();
}

$name = $this->name() . "." . $this->type();
return $path;
}

try {
// Try the template with type extension in the default template directory.
return F::realpath($this->getFilename($name), $this->getPathTemplates());
} catch (Exception $e) {
// Look for the template with type extension provided by an extension.
// This might be null if the template does not exist.
return Kirby::instance()->extension($this->store(), $name);
protected function getPathViews()
{
$path = option('afbora.blade.views');

if (is_callable($path)) {
return $path();
}

return $path;
}

public function setViewDirectory()
{
if (!file_exists($this->views)) {
Dir::make($this->views);
}
}

Expand All @@ -71,11 +80,12 @@ public function render(array $data = []): string
$this->templates,
$this->views
);

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

$html = $this->blade->make($this->name, $data);
$html = $this->blade->make($this->name, $data)->render();
} else {
$html = Tpl::load($this->file(), $data);
}
Expand All @@ -95,18 +105,14 @@ public function render(array $data = []): string
return $html;
}

public function setViewDirectory()
public function isBlade()
{
if (!file_exists($this->views)) {
Dir::make($this->views);
}
return !file_exists($this->getPathTemplates() . "/" . $this->name() . "." . $this->bladeExtension());
}

protected function setFilters()
public function bladeExtension(): string
{
foreach ($filters = option('afbora.blade.filters', []) as $filter => $callback) {
BladeFilters::macro($filter, $callback);
}
return 'blade.php';
}

protected function setDirectives()
Expand Down Expand Up @@ -340,58 +346,53 @@ protected function setIfStatements()
}
}

public function getFilename(string $name = null): string
protected function setFilters()
{
if ($name) {
return $this->getPathTemplates() . "/" . $name . "." . $this->extension();
}

if ($this->isBlade()) {
return $this->getPathTemplates() . "/" . $this->name() . "." . $this->bladeExtension();
foreach ($filters = option('afbora.blade.filters', []) as $filter => $callback) {
BladeFilters::macro($filter, $callback);
}

return $this->getPathTemplates() . "/" . $this->name() . "." . $this->extension();
}

public function isBlade()
{
return !!file_exists($this->getPathTemplates() . "/" . $this->name() . "." . $this->bladeExtension());
}

public function bladeExtension(): string
public function file(): ?string
{
return 'blade.php';
}
if ($this->hasDefaultType() === true) {
try {
// Try the default template in the default template directory.
return F::realpath($this->getFilename(), $this->getPathTemplates());
} catch (Exception $e) {
//
}

protected function getPathViews()
{
$path = option('afbora.blade.views');
// Look for the default template provided by an extension.
$path = Kirby::instance()->extension($this->store(), $this->name());

if (is_callable($path)) {
return $path();
if ($path !== null) {
return $path;
}
}

return $path;
$name = $this->name() . "." . $this->type();

try {
// Try the template with type extension in the default template directory.
return F::realpath($this->getFilename($name), $this->getPathTemplates());
} catch (Exception $e) {
// Look for the template with type extension provided by an extension.
// This might be null if the template does not exist.
return Kirby::instance()->extension($this->store(), $name);
}
}

protected function getPathTemplates()
public function getFilename(string $name = null): string
{
if (!is_null($this->templates)) {
return $this->templates;
if ($name) {
return $this->getPathTemplates() . "/" . $name . "." . $this->extension();
}

$optionPath = option('afbora.blade.templates');

if ($optionPath !== null && is_dir($optionPath)) {
if (is_callable($optionPath)) {
return $optionPath();
}

$path = kirby()->roots()->index() . "/" . $optionPath;
} else {
$path = $this->root();
if ($this->isBlade()) {
return $this->getPathTemplates() . "/" . $this->name() . "." . $this->bladeExtension();
}

return $path;
return $this->getPathTemplates() . "/" . $this->name() . "." . $this->extension();
}
}
Loading

0 comments on commit 3979766

Please sign in to comment.