Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
octoper committed Aug 26, 2020
1 parent 6924e47 commit ab0433d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 136 deletions.
13 changes: 2 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@
},
"require-dev": {
"nunomaduro/collision": "^4.2",
"friendsofphp/php-cs-fixer": "^2.16",
"orchestra/testbench": "^5.0",
"phpunit/phpunit": "^9.0"
"friendsofphp/php-cs-fixer": "^2.16"
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
},
"config": {
Expand All @@ -36,19 +32,14 @@
"Octoper\\BladeComponents\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Octoper\\BladeComponents\\Tests\\": "tests"
}
},
"extra": {
"statamic": {
"name": "Blade Components",
"description": "Integration for Blade components inside Statamic's Antler template engine"
},
"laravel": {
"providers": [
"Octoper\\BladeComponents\\BladeComponentsServiceProvider"
"Octoper\\BladeComponents\\ServiceProvider"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Octoper\BladeComponents\Tags\BladeComponent;
use Statamic\Providers\AddonServiceProvider;

class BladeComponentsServiceProvider extends AddonServiceProvider
class ServiceProvider extends AddonServiceProvider
{
protected $tags = [
BladeComponent::class,
Expand Down
116 changes: 59 additions & 57 deletions src/Tags/BladeComponent.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Octoper\BladeComponents\Tags;

use Illuminate\Container\Container;
Expand All @@ -13,77 +15,34 @@

class BladeComponent extends Tags
{
protected static $handle = 'component';
protected static $handle = 'x';

public function wildcard($expression): string
public function wildcard(string $expression): string
{
if (empty($this->componentClass($expression)))
{
if (empty($this->componentClass($expression))) {
return "";
}

$generated = Blade::compileString(
$compiledBladeView = Blade::compileString(
<<<EOT
<x-{$expression} {$this->createAttributes($this->params->toArray())}>{$this->content}</x-{$expression}>
EOT
);

$factory = Container::getInstance()->make('view');

return view($this->createViewFromString($factory, $generated))->render();
}

protected function createAttributes(array $attributes): string
{
return (new ComponentAttributeBag($attributes))->toHtml();
}

protected function createViewFromString($factory, $contents): string
{
$factory->addNamespace(
'__components',
$directory = Container::getInstance()['config']->get('view.compiled')
);

if (! file_exists($viewFile = $directory.'/'.sha1($contents).'.php')) {
if (! is_dir($directory)) {
mkdir($directory, 0755, true);
}

file_put_contents($viewFile, $contents);
}

return '__components::'.basename($viewFile, '.php');
}

/**
* Guess the view name for the given component.
*
* @param string $name
* @return string
*/
protected function guessViewName($name): string
{
$prefix = 'components.';

$delimiter = ViewFinderInterface::HINT_PATH_DELIMITER;

if (Str::contains($name, $delimiter)) {
return Str::replaceFirst($delimiter, $delimiter.$prefix, $name);
try {
return view($this->createViewFromString($factory, $compiledBladeView))->render();
} catch (\Throwable $e) {
return "";
}

return $prefix.$name;
}

/**
* Get the component class for a given component alias.
*
* @param string $component
* @return string
*
* @throws \InvalidArgumentException
*/
protected function componentClass(string $component)
protected function componentClass(string $component): string
{
$viewFactory = Container::getInstance()->make(Factory::class);

Expand All @@ -95,16 +54,14 @@ protected function componentClass(string $component)
return $view;
}

return;
return "";
}

/**
* Guess the class name for the given component.
*
* @param string $component
* @return string
*/
protected function guessClassName(string $component)
protected function guessClassName(string $component): string
{
$namespace = Container::getInstance()
->make(Application::class)
Expand All @@ -114,7 +71,52 @@ protected function guessClassName(string $component)
return ucfirst(Str::camel($componentPiece));
}, explode('.', $component));

return $namespace.'View\\Components\\'.implode('\\', $componentPieces);
return $namespace . 'View\\Components\\' . implode('\\', $componentPieces);
}

/**
* Guess the view name for the given component.
*/
protected function guessViewName(string $name): string
{
$prefix = 'components.';

$delimiter = ViewFinderInterface::HINT_PATH_DELIMITER;

if (Str::contains($name, $delimiter)) {
return Str::replaceFirst($delimiter, $delimiter . $prefix, $name);
}

return $prefix . $name;
}

/**
* Creates attributes tags.
*/
protected function createAttributes(array $attributes): string
{
return (new ComponentAttributeBag($attributes))->toHtml();
}

/**
* Created a view based on a string
*/
protected function createViewFromString(Factory $factory, string $contents): string
{
$factory->addNamespace(
'__components',
$directory = Container::getInstance()['config']->get('view.compiled')
);

if (!file_exists($viewFile = $directory . '/' . sha1($contents) . '.php')) {
if (!is_dir($directory)) {
mkdir($directory, 0755, true);
}

file_put_contents($viewFile, $contents);
}

return '__components::' . basename($viewFile, '.php');
}

}
12 changes: 0 additions & 12 deletions tests/ExampleTest.php

This file was deleted.

55 changes: 0 additions & 55 deletions tests/TestCase.php

This file was deleted.

0 comments on commit ab0433d

Please sign in to comment.