Skip to content

Commit

Permalink
Include additional changes from Illuminate\View
Browse files Browse the repository at this point in the history
  • Loading branch information
damiani committed Sep 1, 2021
1 parent 250c25b commit 1859464
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions src/View/ComponentTagCompiler.php
Expand Up @@ -2,11 +2,12 @@

namespace TightenCo\Jigsaw\View;

use Exception;
use Illuminate\Container\Container;
use Illuminate\Support\Str;
use Illuminate\View\Compilers\ComponentTagCompiler as BaseComponentTagCompiler;
use Illuminate\View\Factory;
use Illuminate\View\ViewFinderInterface;
use InvalidArgumentException;

class ComponentTagCompiler extends BaseComponentTagCompiler
{
Expand All @@ -20,29 +21,65 @@ class ComponentTagCompiler extends BaseComponentTagCompiler
*/
protected function componentClass(string $component)
{
$viewFactory = Container::getInstance()[Factory::class];

if (isset($this->aliases[$component])) {
return $this->aliases[$component];
if (class_exists($alias = $this->aliases[$component])) {
return $alias;
}

if ($viewFactory->exists($alias)) {
return $alias;
}

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

if (class_exists($class = $this->guessClassName($component))) {
return $class;
}

if (Container::getInstance()[Factory::class]
->exists($view = "_components.{$component}")) {
if ($viewFactory->exists($view = $this->guessViewName($component))) {
return $view;
}

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

/**
* Guess the class name for the given component.
*
* @param string $component
* @return string
*/
public function guessClassName(string $component)
{
$componentPieces = array_map(function ($componentPiece) {
return ucfirst(Str::camel($componentPiece));
}, explode('.', $component));
return 'Components\\'.implode('\\', $componentPieces);
}

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

$delimiter = ViewFinderInterface::HINT_PATH_DELIMITER;

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

return $prefix.$name;
}
}

0 comments on commit 1859464

Please sign in to comment.