Skip to content

Commit acb755c

Browse files
committed
Extract widget groups from WidgetFactory
1 parent e6adfad commit acb755c

File tree

6 files changed

+94
-37
lines changed

6 files changed

+94
-37
lines changed

src/Facade.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,16 @@ protected static function getFacadeAccessor()
88
{
99
return 'arrilot.widget';
1010
}
11+
12+
/**
13+
* Get the widget group object.
14+
*
15+
* @param $name
16+
*
17+
* @return WidgetGroup
18+
*/
19+
public static function group($name)
20+
{
21+
return app('arrilot.widget-group-collection')->group($name);
22+
}
1123
}

src/Factories/WidgetFactory.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,8 @@
22

33
namespace Arrilot\Widgets\Factories;
44

5-
use Arrilot\Widgets\WidgetGroup;
6-
75
class WidgetFactory extends AbstractWidgetFactory
86
{
9-
/**
10-
* The array of widget groups.
11-
*
12-
* @var array
13-
*/
14-
protected $groups;
15-
167
/**
178
* Run widget without magic method.
189
*
@@ -33,24 +24,6 @@ public function run()
3324
return $this->convertToViewExpression($content);
3425
}
3526

36-
/**
37-
* Get the widget group object.
38-
*
39-
* @param $name
40-
*
41-
* @return mixed
42-
*/
43-
public function group($name)
44-
{
45-
if (isset($this->groups[$name])) {
46-
return $this->groups[$name];
47-
}
48-
49-
$this->groups[$name] = new WidgetGroup($name, $this->app);
50-
51-
return $this->groups[$name];
52-
}
53-
5427
/**
5528
* Get widget reload timeout or false if it's not reloadable.
5629
*

src/ServiceProvider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public function register()
3131
$this->app->bind('arrilot.async-widget', function () {
3232
return new AsyncWidgetFactory(new LaravelApplicationWrapper());
3333
});
34+
35+
$this->app->singleton('arrilot.widget-group-collection', function () {
36+
return new WidgetGroupCollection(new LaravelApplicationWrapper());
37+
});
3438

3539
$this->app->singleton('command.widget.make', function ($app) {
3640
return new WidgetMakeCommand($app['files']);
@@ -40,6 +44,7 @@ public function register()
4044

4145
$this->app->alias('arrilot.widget', 'Arrilot\Widgets\Factories\WidgetFactory');
4246
$this->app->alias('arrilot.async-widget', 'Arrilot\Widgets\Factories\AsyncWidgetFactory');
47+
$this->app->alias('arrilot.widget-group-collection', 'Arrilot\Widgets\WidgetGroupCollection');
4348
}
4449

4550
/**
@@ -67,7 +72,7 @@ public function boot()
6772
$this->registerBladeDirective('widget', '$1<?php echo app("arrilot.widget")->run$2; ?>');
6873
$this->registerBladeDirective('async-widget', '$1<?php echo app("arrilot.async-widget")->run$2; ?>');
6974
$this->registerBladeDirective('asyncWidget', '$1<?php echo app("arrilot.async-widget")->run$2; ?>');
70-
$this->registerBladeDirective('widgetGroup', '$1<?php echo Widget::group$2->display(); ?>');
75+
$this->registerBladeDirective('widgetGroup', '$1<?php echo app("arrilot.widget-group-collection")->group$2->display(); ?>');
7176
}
7277

7378
/**

src/WidgetGroupCollection.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Arrilot\Widgets;
4+
5+
use Arrilot\Widgets\Contracts\ApplicationWrapperContract;
6+
7+
class WidgetGroupCollection
8+
{
9+
10+
/**
11+
* The array of widget groups.
12+
*
13+
* @var array
14+
*/
15+
protected $groups;
16+
17+
/**
18+
* Constructor.
19+
*
20+
* @param ApplicationWrapperContract $app
21+
*/
22+
public function __construct(ApplicationWrapperContract $app)
23+
{
24+
$this->app = $app;
25+
}
26+
27+
/**
28+
* Get the widget group object.
29+
*
30+
* @param $name
31+
*
32+
* @return WidgetGroup
33+
*/
34+
public function group($name)
35+
{
36+
if (isset($this->groups[$name])) {
37+
return $this->groups[$name];
38+
}
39+
40+
$this->groups[$name] = new WidgetGroup($name, $this->app);
41+
42+
return $this->groups[$name];
43+
}
44+
}

tests/WidgetFactoryTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,4 @@ public function testItCanCacheWidgets()
152152

153153
$this->assertEquals('Cached output. Key: '.$key.', minutes: '.$widget->cacheTime, $output);
154154
}
155-
156-
public function testItGrantsAccessToWidgetGroup()
157-
{
158-
$groupObject = $this->factory->group('sidebar');
159-
160-
$expectedObject = new WidgetGroup('sidebar', new TestApplicationWrapper());
161-
162-
$this->assertEquals($expectedObject, $groupObject);
163-
}
164155
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Arrilot\Widgets\Test;
4+
5+
use Arrilot\Widgets\Factories\WidgetFactory;
6+
use Arrilot\Widgets\Test\Dummies\TestCachedWidget;
7+
use Arrilot\Widgets\Test\Support\TestApplicationWrapper;
8+
use Arrilot\Widgets\Test\Support\TestCase;
9+
use Arrilot\Widgets\WidgetGroup;
10+
use Arrilot\Widgets\WidgetGroupCollection;
11+
12+
class WidgetGroupCollectionTest extends TestCase
13+
{
14+
/**
15+
* @var WidgetGroupCollection
16+
*/
17+
protected $collection;
18+
19+
public function setUp()
20+
{
21+
$this->collection = new WidgetGroupCollection(new TestApplicationWrapper());
22+
}
23+
24+
public function testItGrantsAccessToWidgetGroup()
25+
{
26+
$groupObject = $this->collection->group('sidebar');
27+
28+
$expectedObject = new WidgetGroup('sidebar', new TestApplicationWrapper());
29+
30+
$this->assertEquals($expectedObject, $groupObject);
31+
}
32+
}

0 commit comments

Comments
 (0)