Skip to content

Commit 498ea11

Browse files
committed
basically 3.0.0
1 parent 81119d6 commit 498ea11

21 files changed

+144
-118
lines changed

README.md

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
?>
2626
```
2727

28-
3) Add some facades here too.
28+
3) Add some facades here too. If you prefer custom blade directives instead of facades (see later) you can skip it.
2929

3030
```php
3131
<?php
@@ -91,11 +91,11 @@ or even
9191
@widget('recentNews')
9292
```
9393

94-
## Configuration
94+
There is no real difference between them. The choice is up to you.
9595

96-
### Widget configuration
96+
## Passing variables to widget
9797

98-
#### Using config array
98+
### Via config array
9999

100100
Let's carry on with the "recent news" example.
101101

@@ -105,7 +105,10 @@ This can be easily achieved like that:
105105
```php
106106
class RecentNews extends AbstractWidget {
107107
...
108-
protected $count = 5;
108+
protected $config = [
109+
'count' => 5
110+
];
111+
109112
...
110113
}
111114

@@ -115,11 +118,28 @@ class RecentNews extends AbstractWidget {
115118
@widget('recentNews', ['count' => 10])
116119
```
117120
`['count' => 10]` is a config array.
118-
Notice that you don't need to manually map the config array and class properties in constructor. It's done automatically behind the scenes.
119121

120-
#### Using additional parameters
122+
Note: no config fields that are not specified when you call the widget are overwritten.
121123

122-
You can also choose to pass additional parameters to the `run()` method directly if you like it.
124+
```php
125+
class RecentNews extends AbstractWidget {
126+
...
127+
protected $config = [
128+
'count' => 5,
129+
'foo' => 'bar'
130+
];
131+
132+
...
133+
}
134+
135+
@widget('recentNews', ['count' => 10]) // $this->config('foo') is still 'bar'
136+
```
137+
138+
Config array is available in all widget methods so you can use it to configure placeholder and container too.
139+
140+
### Directly
141+
142+
You can also choose to pass additional parameters to `run()` method directly if you like it.
123143

124144
```php
125145
@widget('recentNews', ['count' => 10], 'date', 'asc')
@@ -130,37 +150,33 @@ public function run($sort_by, $sort_order) { }
130150

131151
`run()` method is resolved via Laravel service container so method injection is available here too.
132152

133-
### Namespaces configuration
153+
## Namespaces
134154

135-
By default package tries to find your widget in the ```App\Widgets``` namespace.
155+
By default the package tries to find your widget in the ```App\Widgets``` namespace.
136156

137157
You can overwrite this by publishing package config and setting `default_namespace` property.
138158

139-
Although using the default namespace is very convenient and keeps you from doing unnecessary actions, in some situations you may wish to have more flexibility.
159+
Although using the default namespace is very convenient, in some situations you may wish to have more flexibility.
140160
For example, if you've got dozens of widgets it makes sense to group them in namespaced folders.
141161

142-
You actually have several ways to call those widgets:
162+
You have two ways to call those widgets:
143163

144-
1) You can pass the full name to the `run` method.
164+
1) You can pass the full widget name to the `run` method.
145165
```php
146166
@widget('News\RecentNews', $config)
167+
{!! Widget::run('News\RecentNews', $config) !!}
147168
```
148169

149170
2) You can use dot notation instead.
150171
```php
151172
@widget('news.recentNews', $config)
173+
{!! Widget::run('news.recentNews', $config) !!}
152174
```
153175

154-
3) Finally, you can register a widget in package config like that.
176+
Note: you can pass FQCN too.
155177
```php
156-
'custom_namespaces_for_specific_widgets' => [
157-
'recentNews' => 'App\Widgets\News'
158-
....
159-
]
160-
```
161-
and then call it without namespaces
162-
```php
163-
@widget('recentNews', $config)
178+
@widget('\App\Http\Some\Namespace\Widget', $config)
179+
{!! Widget::run('\App\Http\Some\Namespace\Widget', $config) !!}
164180
```
165181

166182
## Asynchronous widgets
@@ -212,11 +228,31 @@ Both sync and async widgets can become reloadable.
212228
You should use this feature with care, because it can easily spam your app with ajax calls if timeouts are too low.
213229
Consider using web sockets too but they are waaaay harder to set up on the other hand.
214230

231+
## Container
232+
233+
Async and Reloadable widgets both require some DOM interaction so they wrap all widget output in a Container.
234+
This container is defined by AbstractWidget::container() method and can be customized
235+
236+
```php
237+
/**
238+
* Async and reloadable widgets are wrapped in container.
239+
* You can customize it by overwriting this method.
240+
*
241+
* @return array
242+
*/
243+
public function container()
244+
{
245+
return [
246+
'element' => 'div',
247+
'attributes' => 'style="display:inline" class="arrilot-widget-container"',
248+
];
249+
}
250+
```
215251

216252
## Caching
217253

218254
There is also a simple built-in way to cache entire widget output.
219-
Just set $cacheTime property and you are done.
255+
Just set $cacheTime property in your widget class and you are done.
220256

221257
```php
222258
class RecentNews extends AbstractWidget

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
},
2020
"autoload": {
2121
"psr-4": {
22-
"Arrilot\\Widgets\\": "src/"
22+
"Arrilot\\Widgets\\": "src/",
23+
"Arrilot\\Widgets\\Test\\Dummies\\": "spec/Dummies"
2324
}
2425
},
2526
"autoload-dev": {

spec/Arrilot/Widgets/Factories/AsyncWidgetFactorySpec.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
class AsyncWidgetFactorySpec extends ObjectBehavior
1010
{
1111
protected $config = [
12-
'defaultNamespace' => 'App\Widgets',
13-
'customNamespaces' => [
14-
'slider' => 'spec\Arrilot\Widgets\Dummies',
15-
'testWidgetName' => '',
16-
],
12+
'defaultNamespace' => 'Arrilot\Widgets\Test\Dummies',
1713
];
1814

1915
/**

spec/Arrilot/Widgets/Factories/WidgetFactorySpec.php

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

33
namespace spec\Arrilot\Widgets\Factories;
44

5-
use App\Widgets\Profile\TestNamespace\TestFeed;
6-
use App\Widgets\TestCachedWidget;
7-
use App\Widgets\TestDefaultSlider;
8-
use App\Widgets\TestMyClass;
9-
use App\Widgets\TestRepeatableFeed;
10-
use App\Widgets\TestWidgetWithCustomContainer;
11-
use App\Widgets\TestWidgetWithDIInRun;
12-
use App\Widgets\TestWidgetWithParamsInRun;
5+
use Arrilot\Widgets\Test\Dummies\Profile\TestNamespace\TestFeed;
6+
use Arrilot\Widgets\Test\Dummies\TestCachedWidget;
7+
use Arrilot\Widgets\Test\Dummies\TestDefaultSlider;
8+
use Arrilot\Widgets\Test\Dummies\TestMyClass;
9+
use Arrilot\Widgets\Test\Dummies\TestRepeatableFeed;
10+
use Arrilot\Widgets\Test\Dummies\TestWidgetWithCustomContainer;
11+
use Arrilot\Widgets\Test\Dummies\TestWidgetWithDIInRun;
12+
use Arrilot\Widgets\Test\Dummies\TestWidgetWithParamsInRun;
1313
use Arrilot\Widgets\Misc\LaravelApplicationWrapper;
1414
use Arrilot\Widgets\WidgetId;
1515
use PhpSpec\ObjectBehavior;
1616
use Prophecy\Argument;
17-
use spec\Arrilot\Widgets\Dummies\Slider;
17+
use Arrilot\Widgets\Test\Dummies\Slider;
1818

1919
class WidgetFactorySpec extends ObjectBehavior
2020
{
21+
protected $config = [
22+
'defaultNamespace' => 'Arrilot\Widgets\Test\Dummies',
23+
];
24+
2125
/**
2226
* A mock for producing JS object for ajax.
2327
*
@@ -36,15 +40,6 @@ private function mockProduceJavascriptData($widgetName, $widgetParams = [])
3640
]);
3741
}
3842

39-
protected $config = [
40-
'defaultNamespace' => 'App\Widgets',
41-
'customNamespaces' => [
42-
'slider' => 'spec\Arrilot\Widgets\Dummies',
43-
'testRepeatableFeed' => 'spec\Arrilot\Widgets\Dummies',
44-
'testWidgetName' => '',
45-
],
46-
];
47-
4843
public function let(LaravelApplicationWrapper $wrapper)
4944
{
5045
$this->beConstructedWith($this->config, $wrapper);
@@ -67,25 +62,25 @@ public function it_can_run_widget_from_default_namespace(LaravelApplicationWrapp
6762
);
6863
}
6964

70-
public function it_can_run_widget_from_custom_namespace(LaravelApplicationWrapper $wrapper)
65+
public function it_allows_its_config_to_be_partly_overwritten(LaravelApplicationWrapper $wrapper)
7166
{
7267
$wrapper->call(Argument::any(), Argument::any())->willReturn(
73-
call_user_func_array([new Slider([]), 'run'], [])
68+
call_user_func_array([new Slider(['slides' => 5]), 'run'], ['slides' => 5])
7469
);
75-
$this->slider()
70+
$this->slider(['slides' => 5])
7671
->shouldReturn(
77-
'Slider was executed with $slides = 6'
72+
'Slider was executed with $slides = 5 foo: bar'
7873
);
7974
}
8075

81-
public function it_provides_config_override(LaravelApplicationWrapper $wrapper)
76+
public function it_allows_its_config_to_be_overwritten(LaravelApplicationWrapper $wrapper)
8277
{
8378
$wrapper->call(Argument::any(), Argument::any())->willReturn(
84-
call_user_func_array([new Slider(['slides' => 5]), 'run'], ['slides' => 5])
79+
call_user_func_array([new Slider(['slides' => 5, 'foo' => 'baz']), 'run'], ['slides' => 5, 'foo' => 'baz'])
8580
);
86-
$this->slider(['slides' => 5])
81+
$this->slider(['slides' => 5, 'foo' => 'baz'])
8782
->shouldReturn(
88-
'Slider was executed with $slides = 5'
83+
'Slider was executed with $slides = 5 foo: baz'
8984
);
9085
}
9186

@@ -116,25 +111,25 @@ public function it_can_run_widgets_with_method_injection(LaravelApplicationWrapp
116111
);
117112
}
118113

119-
public function it_can_run_widgets_with_run_method(LaravelApplicationWrapper $wrapper)
114+
public function it_can_run_widgets_with_run_method_and_config_override(LaravelApplicationWrapper $wrapper)
120115
{
121116
$wrapper->call(Argument::any(), Argument::any())->willReturn(
122-
call_user_func_array([new TestDefaultSlider([]), 'run'], [])
117+
call_user_func_array([new Slider(['slides' => 5]), 'run'], ['slides' => 5])
123118
);
124-
$this->run('testDefaultSlider')
119+
$this->run('slider', ['slides' => 5])
125120
->shouldReturn(
126-
'Default test slider was executed with $slides = 6'
121+
'Slider was executed with $slides = 5 foo: bar'
127122
);
128123
}
129124

130-
public function it_can_run_widgets_with_run_method_and_config_override(LaravelApplicationWrapper $wrapper)
125+
public function it_can_run_widgets_using_global_namespace(LaravelApplicationWrapper $wrapper)
131126
{
132127
$wrapper->call(Argument::any(), Argument::any())->willReturn(
133-
call_user_func_array([new Slider(['slides' => 5]), 'run'], ['slides' => 5])
128+
call_user_func_array([new TestDefaultSlider([]), 'run'], [])
134129
);
135-
$this->run('slider', ['slides' => 5])
130+
$this->run('\Arrilot\Widgets\Test\Dummies\TestDefaultSlider')
136131
->shouldReturn(
137-
'Slider was executed with $slides = 5'
132+
'Default test slider was executed with $slides = 6'
138133
);
139134
}
140135

@@ -167,15 +162,15 @@ public function it_can_run_multiple_widgets(LaravelApplicationWrapper $wrapper)
167162
);
168163
$this->slider()
169164
->shouldReturn(
170-
'Slider was executed with $slides = 6'
165+
'Slider was executed with $slides = 6 foo: bar'
171166
);
172167

173168
$wrapper->call(Argument::any(), Argument::any())->willReturn(
174169
call_user_func_array([new Slider(['slides' => 5]), 'run'], ['slides' => 5])
175170
);
176171
$this->slider(['slides' => 5])
177172
->shouldReturn(
178-
'Slider was executed with $slides = 5'
173+
'Slider was executed with $slides = 5 foo: bar'
179174
);
180175
}
181176

spec/Dummies/TestFeed.php renamed to spec/Dummies/Profile/TestNamespace/TestFeed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Widgets\Profile\TestNamespace;
3+
namespace Arrilot\Widgets\Test\Dummies\Profile\TestNamespace;
44

55
use Arrilot\Widgets\AbstractWidget;
66

spec/Dummies/Slider.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<?php
22

3-
namespace spec\Arrilot\Widgets\Dummies;
3+
namespace Arrilot\Widgets\Test\Dummies;
44

55
use Arrilot\Widgets\AbstractWidget;
66

77
class Slider extends AbstractWidget
88
{
9-
protected $slides = 6;
9+
protected $config = [
10+
'slides' => 6,
11+
'foo' => 'bar'
12+
];
1013

1114
public function run()
1215
{
13-
return "Slider was executed with \$slides = ".$this->slides;
16+
return "Slider was executed with \$slides = ".$this->config['slides']." foo: ".$this->config['foo'];
1417
}
1518

1619
public function placeholder()

spec/Dummies/TestBadSlider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Widgets;
3+
namespace Arrilot\Widgets\Test\Dummies;
44

55
class TestBadSlider
66
{

spec/Dummies/TestCachedWidget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Widgets;
3+
namespace Arrilot\Widgets\Test\Dummies;
44

55
use Arrilot\Widgets\AbstractWidget;
66

spec/Dummies/TestDefaultSlider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Widgets;
3+
namespace Arrilot\Widgets\Test\Dummies;
44

55
use Arrilot\Widgets\AbstractWidget;
66

spec/Dummies/TestRepeatableFeed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Widgets;
3+
namespace Arrilot\Widgets\Test\Dummies;
44

55
use Arrilot\Widgets\AbstractWidget;
66

0 commit comments

Comments
 (0)