Skip to content

Commit 680c3db

Browse files
committed
Merge branch 'autoload-class-components' into 8.x
2 parents 02522b0 + fbc9b5e commit 680c3db

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

src/Illuminate/Support/Facades/Blade.php

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* @method static void compile(string|null $path = null)
1616
* @method static void component(string $class, string|null $alias = null, string $prefix = '')
1717
* @method static void components(array $components, string $prefix = '')
18+
* @method static void componentNamespace(string $namespace, string $prefix)
1819
* @method static void directive(string $name, callable $handler)
1920
* @method static void extend(callable $compiler)
2021
* @method static void if(string $name, callable $callback)

src/Illuminate/View/Compilers/BladeCompiler.php

+31-2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ class BladeCompiler extends Compiler implements CompilerInterface
120120
*/
121121
protected $classComponentAliases = [];
122122

123+
/**
124+
* The array of class component namespaces to autoload from.
125+
*
126+
* @var array
127+
*/
128+
protected $classComponentNamespaces = [];
129+
123130
/**
124131
* Indicates if component tags should be compiled.
125132
*
@@ -318,7 +325,7 @@ protected function compileComponentTags($value)
318325
}
319326

320327
return (new ComponentTagCompiler(
321-
$this->classComponentAliases, $this
328+
$this->classComponentAliases, $this->classComponentNamespaces, $this
322329
))->compile($value);
323330
}
324331

@@ -585,6 +592,28 @@ public function getClassComponentAliases()
585592
return $this->classComponentAliases;
586593
}
587594

595+
/**
596+
* Register a class-based component namespace.
597+
*
598+
* @param string $namespace
599+
* @param string $prefix
600+
* @return void
601+
*/
602+
public function componentNamespace($namespace, $prefix)
603+
{
604+
$this->classComponentNamespaces[$prefix] = $namespace;
605+
}
606+
607+
/**
608+
* Get the registered class component namespaces.
609+
*
610+
* @return array
611+
*/
612+
public function getClassComponentNamespaces()
613+
{
614+
return $this->classComponentNamespaces;
615+
}
616+
588617
/**
589618
* Register a component alias directive.
590619
*
@@ -616,7 +645,7 @@ public function aliasComponent($path, $alias = null)
616645
*/
617646
public function include($path, $alias = null)
618647
{
619-
return $this->aliasInclude($path, $alias);
648+
$this->aliasInclude($path, $alias);
620649
}
621650

622651
/**

src/Illuminate/View/Compilers/ComponentTagCompiler.php

+48-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class ComponentTagCompiler
3232
*/
3333
protected $aliases = [];
3434

35+
/**
36+
* The component class namespaces.
37+
*
38+
* @var array
39+
*/
40+
protected $namespaces = [];
41+
3542
/**
3643
* The "bind:" attributes that have been compiled for the current component.
3744
*
@@ -46,9 +53,10 @@ class ComponentTagCompiler
4653
* @param \Illuminate\View\Compilers\BladeCompiler|null
4754
* @return void
4855
*/
49-
public function __construct(array $aliases = [], ?BladeCompiler $blade = null)
56+
public function __construct(array $aliases = [], array $namespaces = [], ?BladeCompiler $blade = null)
5057
{
5158
$this->aliases = $aliases;
59+
$this->namespaces = $namespaces;
5260

5361
$this->blade = $blade ?: new BladeCompiler(new Filesystem, sys_get_temp_dir());
5462
}
@@ -250,6 +258,10 @@ public function componentClass(string $component)
250258
);
251259
}
252260

261+
if ($class = $this->findClassByComponent($component)) {
262+
return $class;
263+
}
264+
253265
if (class_exists($class = $this->guessClassName($component))) {
254266
return $class;
255267
}
@@ -263,6 +275,27 @@ public function componentClass(string $component)
263275
);
264276
}
265277

278+
/**
279+
* Find the class for the given component using the registered namespaces.
280+
*
281+
* @param string $component
282+
* @return string|null
283+
*/
284+
public function findClassByComponent(string $component)
285+
{
286+
$segments = explode('::', $component);
287+
288+
$prefix = $segments[0];
289+
290+
if (! isset($this->namespaces[$prefix]) || ! isset($segments[1])) {
291+
return;
292+
}
293+
294+
if (class_exists($class = $this->namespaces[$prefix].'\\'.$this->formatClassName($segments[1]))) {
295+
return $class;
296+
}
297+
}
298+
266299
/**
267300
* Guess the class name for the given component.
268301
*
@@ -275,11 +308,24 @@ public function guessClassName(string $component)
275308
->make(Application::class)
276309
->getNamespace();
277310

311+
$class = $this->formatClassName($component);
312+
313+
return $namespace.'View\\Components\\'.$class;
314+
}
315+
316+
/**
317+
* Format the class name for the given component.
318+
*
319+
* @param string $component
320+
* @return string
321+
*/
322+
public function formatClassName(string $component)
323+
{
278324
$componentPieces = array_map(function ($componentPiece) {
279325
return ucfirst(Str::camel($componentPiece));
280326
}, explode('.', $component));
281327

282-
return $namespace.'View\\Components\\'.implode('\\', $componentPieces);
328+
return implode('\\', $componentPieces);
283329
}
284330

285331
/**

src/Illuminate/View/DynamicComponent.php

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ protected function compiler()
172172
if (! static::$compiler) {
173173
static::$compiler = new ComponentTagCompiler(
174174
Container::getInstance()->make('blade.compiler')->getClassComponentAliases(),
175+
Container::getInstance()->make('blade.compiler')->getClassComponentNamespaces(),
175176
Container::getInstance()->make('blade.compiler')
176177
);
177178
}

0 commit comments

Comments
 (0)