Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Commit

Permalink
prefix single components and collections - allow to remove namespace (#…
Browse files Browse the repository at this point in the history
…84)

* unify/improve component directories

* allow to register multiple components and prefix them separately
allow to prefix single components

* fix and add unittests

* improve bladex facade

* replace registerComponents by components method
flag registerComponents as internal

* allow to register components by list of paths

* fix php cs
  • Loading branch information
Gummibeer authored and AlexVanderbist committed Sep 4, 2019
1 parent 783cc39 commit 112ad46
Show file tree
Hide file tree
Showing 10 changed files with 431 additions and 80 deletions.
87 changes: 68 additions & 19 deletions src/BladeX.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,21 @@ class BladeX
protected $prefix = '';

/**
* @param string|array $view
* @param string|string[] $view
* @param string $tag
*
* @return null|\Spatie\BladeX\Component
*/
public function component($view, string $tag = ''): ?Component
{
if (is_array($view)) {
foreach ($view as $singleView) {
$this->component($singleView);
}
if (is_iterable($view)) {
$this->registerViews($view);

return null;
}

if ($view instanceof Component) {
$this->registeredComponents[$view->tag] = $view;
$this->registeredComponents[] = $view;

return $view;
}
Expand All @@ -55,14 +53,43 @@ public function component($view, string $tag = ''): ?Component

$component = new Component($view, $tag);

$this->registeredComponents[$component->tag] = $component;
$this->registeredComponents[] = $component;

return $component;
}

/**
* @param string|string[] $viewDirectory
*
* @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[]
*/
public function components($viewDirectory): ComponentCollection
{
if (is_iterable($viewDirectory)) {
$components = new ComponentCollection();

foreach ($viewDirectory as $singleViewDirectory) {
if (Str::endsWith($singleViewDirectory, '*')) {
$components = $components->merge($this->registerComponents($singleViewDirectory));
} else {
$components->push($this->component($singleViewDirectory));
}
}

return $components;
}

return $this->registerComponents($viewDirectory);
}

/**
* @return \Spatie\BladeX\Component[]
*/
public function registeredComponents(): array
{
return array_values($this->registeredComponents);
return collect($this->registeredComponents)->reverse()->unique(function (Component $component) {
return $component->getTag();
})->reverse()->values()->all();
}

public function prefix(string $prefix = ''): self
Expand All @@ -77,21 +104,43 @@ public function getPrefix(): string
return empty($this->prefix) ? '' : Str::finish($this->prefix, '-');
}

public function registerComponents(string $viewDirectory)
/**
* @internal
*
* @param string $viewDirectory
*
* @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[]
*/
public function registerComponents(string $viewDirectory): ComponentCollection
{
if (! Str::endsWith($viewDirectory, '*')) {
throw CouldNotRegisterComponent::viewDirectoryWithoutWildcard($viewDirectory);
}

$componentDirectory = Str::contains($viewDirectory, '::')
? new NamespacedDirectory($viewDirectory)
: new RegularDirectory($viewDirectory);

collect(File::files($componentDirectory->getAbsoluteDirectory()))
->filter(function (SplFileInfo $file) {
return Str::endsWith($file->getFilename(), '.blade.php');
})
->map(function (SplFileInfo $file) use ($componentDirectory) {
return $componentDirectory->getViewName($file);
})
->each(function (string $viewName) {
$this->component($viewName);
});
return $this->registerViews(
ComponentCollection::make(File::files($componentDirectory->getAbsoluteDirectory()))
->filter(function (SplFileInfo $file) {
return Str::endsWith($file->getFilename(), '.blade.php');
})
->map(function (SplFileInfo $file) use ($componentDirectory) {
return $componentDirectory->getViewName($file);
})
);
}

/**
* @param iterable|string[] $views
*
* @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[]
*/
protected function registerViews(iterable $views): ComponentCollection
{
return ComponentCollection::make($views)->map(function (string $viewName) {
return $this->component($viewName);
});
}
}
17 changes: 3 additions & 14 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ protected function parseComponentHtml(string $viewContents, Component $component

protected function parseSelfClosingTags(string $viewContents, Component $component): string
{
$prefix = $this->bladeX->getPrefix();

$pattern = "/<\s*{$prefix}{$component->tag}\s*(?<attributes>(?:\s+[\w\-:]+(=(?:\\\"[^\\\"]+\\\"|\'[^\']+\'|[^\'\\\"=<>]+))?)*\s*)\/>/";
$pattern = "/<\s*{$component->getTag()}\s*(?<attributes>(?:\s+[\w\-:]+(=(?:\\\"[^\\\"]+\\\"|\'[^\']+\'|[^\'\\\"=<>]+))?)*\s*)\/>/";

return preg_replace_callback($pattern, function (array $matches) use ($component) {
$attributes = $this->getAttributesFromAttributeString($matches['attributes']);
Expand All @@ -51,9 +49,7 @@ protected function parseSelfClosingTags(string $viewContents, Component $compone

protected function parseOpeningTags(string $viewContents, Component $component): string
{
$prefix = $this->bladeX->getPrefix();

$pattern = "/<\s*{$prefix}{$component->tag}(?<attributes>(?:\s+[\w\-:]+(=(?:\\\"[^\\\"]*\\\"|\'[^\']*\'|[^\'\\\"=<>]+))?)*\s*)(?<![\/=\-])>/";
$pattern = "/<\s*{$component->getTag()}(?<attributes>(?:\s+[\w\-:]+(=(?:\\\"[^\\\"]*\\\"|\'[^\']*\'|[^\'\\\"=<>]+))?)*\s*)(?<![\/=\-])>/";

return preg_replace_callback($pattern, function (array $matches) use ($component) {
$attributes = $this->getAttributesFromAttributeString($matches['attributes']);
Expand All @@ -64,9 +60,7 @@ protected function parseOpeningTags(string $viewContents, Component $component):

protected function parseClosingTags(string $viewContents, Component $component): string
{
$prefix = $this->bladeX->getPrefix();

$pattern = "/<\/\s*{$prefix}{$component->tag}\s*>/";
$pattern = "/<\/\s*{$component->getTag()}\s*>/";

return preg_replace($pattern, $this->componentEndString($component), $viewContents);
}
Expand Down Expand Up @@ -164,11 +158,6 @@ protected function parseSlots(string $viewContents): string
return $viewContents;
}

protected function isOpeningHtmlTag(string $tagName, string $html): bool
{
return ! Str::endsWith($html, ["</{$tagName}>", '/>']);
}

protected function parseBindAttributes(string $attributeString): string
{
return preg_replace("/\s*:([\w-]+)=/m", ' bind:$1=', $attributeString);
Expand Down
67 changes: 54 additions & 13 deletions src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,50 @@

namespace Spatie\BladeX;

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Contracts\Support\Arrayable;
use Spatie\BladeX\Exceptions\CouldNotRegisterComponent;

class Component
{
/** @var BladeX */
protected $bladeX;

/** @var string */
public $view;

/** @var string */
/**
* @var string
* @internal
* @see Component::getTag()
*/
public $tag;

/** @var string */
public $viewModel;

public static function make(string $view, string $tag = '')
/** @var string */
protected $prefix;

/** @var bool */
protected $withNamespace;

public static function make(string $view, string $tag = '', string $prefix = '', bool $withNamespace = true)
{
return new static($view, $tag);
return new static($view, $tag, $prefix, $withNamespace);
}

public function __construct(string $view, string $tag = '')
public function __construct(string $view, string $tag = '', string $prefix = '', bool $withNamespace = true)
{
if ($tag === '') {
$tag = $this->determineDefaultTag($view);
}

$this->view = $view;

$this->tag = $tag;

$this->prefix = $prefix;

$this->withNamespace = $withNamespace;

$this->bladeX = app(BladeX::class);
}

public function tag(string $tag)
Expand All @@ -41,6 +55,20 @@ public function tag(string $tag)
return $this;
}

public function prefix(string $prefix)
{
$this->prefix = $prefix;

return $this;
}

public function withoutNamespace()
{
$this->withNamespace = false;

return $this;
}

public function viewModel(string $viewModel)
{
if (! class_exists($viewModel)) {
Expand All @@ -56,17 +84,30 @@ public function viewModel(string $viewModel)
return $this;
}

protected function determineDefaultTag(string $view): string
public function getTag(): string
{
$tag = empty($this->prefix) ? $this->bladeX->getPrefix() : Str::finish($this->prefix, '-');

$tag .= empty($this->tag) ? $this->determineDefaultTag() : $this->tag;

return $tag;
}

protected function determineDefaultTag(): string
{
$baseComponentName = explode('.', $view);
$baseComponentName = explode('.', $this->view);

$tag = Str::kebab(end($baseComponentName));

if (Str::contains($view, '::') && ! Str::contains($tag, '::')) {
$namespace = Arr::first(explode('::', $view));
if (Str::contains($this->view, '::') && ! Str::contains($tag, '::')) {
$namespace = Str::before($this->view, '::');
$tag = "{$namespace}::{$tag}";
}

if (! $this->withNamespace && Str::contains($tag, '::')) {
$tag = Str::after($tag, '::');
}

return $tag;
}
}
25 changes: 25 additions & 0 deletions src/ComponentCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Spatie\BladeX;

use Illuminate\Support\Collection;

/**
* @property-read Component $each
*/
class ComponentCollection extends Collection
{
public function prefix(string $prefix)
{
$this->each->prefix($prefix);

return $this;
}

public function withoutNamespace()
{
$this->each->withoutNamespace();

return $this;
}
}
5 changes: 4 additions & 1 deletion src/ComponentDirectory/ComponentDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

abstract class ComponentDirectory
{
/** @var string */
protected $viewDirectory;

abstract public function getAbsoluteDirectory(): string;

public function getViewName(SplFileInfo $viewFile): string
{
$view = Str::replaceLast('.blade.php', '', $viewFile->getFilename());

return "{$this->viewDirectory}.{$view}";
return empty($this->viewDirectory) ? $view : "{$this->viewDirectory}.{$view}";
}
}
15 changes: 2 additions & 13 deletions src/ComponentDirectory/NamespacedDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ class NamespacedDirectory extends ComponentDirectory
/** @var string */
protected $namespace;

/** @var string */
protected $viewDirectory;

public function __construct(string $viewDirectory)
{
[$this->namespace, $viewDirectory] = explode('::', $viewDirectory);
$this->viewDirectory = Str::before($viewDirectory, '*');
$this->viewDirectory = trim(Str::before($viewDirectory, '*'), '.');
}

public function getAbsoluteDirectory(): string
Expand All @@ -36,14 +33,6 @@ public function getAbsoluteDirectory(): string

public function getViewName(SplFileInfo $viewFile): string
{
$view = Str::replaceLast('.blade.php', '', $viewFile->getFilename());

$viewDirectory = '';

if ($this->viewDirectory !== '') {
$viewDirectory = $this->viewDirectory;
}

return "{$this->namespace}::{$viewDirectory}{$view}";
return "{$this->namespace}::".parent::getViewName($viewFile);
}
}
11 changes: 0 additions & 11 deletions src/ComponentDirectory/RegularDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@

use Illuminate\Support\Str;
use Illuminate\Support\Facades\View;
use Symfony\Component\Finder\SplFileInfo;
use Spatie\BladeX\Exceptions\CouldNotRegisterComponent;

class RegularDirectory extends ComponentDirectory
{
/** @var string */
protected $viewDirectory;

public function __construct(string $viewDirectory)
{
$this->viewDirectory = Str::before($viewDirectory, '.*');
Expand All @@ -34,11 +30,4 @@ public function getAbsoluteDirectory(): string

return $absoluteDirectory;
}

public function getViewName(SplFileInfo $viewFile): string
{
$view = Str::replaceLast('.blade.php', '', $viewFile->getFilename());

return "{$this->viewDirectory}.{$view}";
}
}
Loading

0 comments on commit 112ad46

Please sign in to comment.