Skip to content

Commit cca5cf8

Browse files
committed
Fix widget class reslution
1 parent 5f59154 commit cca5cf8

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/Factories/AbstractWidgetFactory.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,14 @@ protected function instantiateWidget(array $params = [])
113113

114114
$rootNamespace = $this->app->config('laravel-widgets.default_namespace', $this->app->getNamespace().'Widgets');
115115

116-
$widgetClass = class_exists($this->widgetName)
117-
? $this->widgetName
118-
: $rootNamespace.'\\'.$this->widgetName;
116+
$fqcn = $rootNamespace.'\\'.$this->widgetName;
117+
$widgetClass = class_exists($fqcn) ? $fqcn : $this->widgetName;
119118

120-
$widget = new $widgetClass($this->widgetConfig);
121-
if ($widget instanceof AbstractWidget === false) {
122-
throw new InvalidWidgetClassException();
119+
if (!is_subclass_of($widgetClass, 'Arrilot\Widgets\AbstractWidget')) {
120+
throw new InvalidWidgetClassException('Class "'.$widgetClass.'" must extend "Arrilot\Widgets\AbstractWidget" class');
123121
}
124122

125-
$this->widget = $widget;
123+
$this->widget = new $widgetClass($this->widgetConfig);
126124
}
127125

128126
/**

tests/Dummies/Exception.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Arrilot\Widgets\Test\Dummies;
4+
5+
use Arrilot\Widgets\AbstractWidget;
6+
7+
class Exception extends AbstractWidget
8+
{
9+
public function run()
10+
{
11+
return "Exception widget was executed instead of predefined php class";
12+
}
13+
}

tests/WidgetFactoryTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ public function testItCanRunWidgetsUsingFQCN()
6969
$this->assertEquals('Default test slider was executed with $slides = 6', $output);
7070
}
7171

72+
public function testItLoadsWidgetsFromRootNamespaceFirst()
73+
{
74+
$output = $this->factory->run('Exception');
75+
76+
$this->assertEquals('Exception widget was executed instead of predefined php class', $output);
77+
}
78+
7279
public function testItCanRunNestedWidgets()
7380
{
7481
$output = $this->factory->run('Profile\TestNamespace\TestFeed');

0 commit comments

Comments
 (0)