Skip to content

Commit 98a81d5

Browse files
committed
A bunch of small fixes and improvements
1 parent 51edb8e commit 98a81d5

File tree

9 files changed

+58
-32
lines changed

9 files changed

+58
-32
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ The last step is to call the widget.
8989
You've actually got several ways to do so.
9090

9191
```php
92-
{!! Widget::run('recentNews') !!}
92+
{{ Widget::run('recentNews') }}
9393
```
9494
or
9595
```php
96-
{!! Widget::recentNews() !!}
96+
{{ Widget::recentNews() }}
9797
```
9898
or even
9999
```php
@@ -165,6 +165,8 @@ By default the package tries to find your widget in the ```App\Widgets``` namesp
165165

166166
You can overwrite this by publishing package config and setting `default_namespace` property.
167167

168+
To publish config use: ```php artisan vendor:publish --provider="Arrilot\Widgets\ServiceProvider"```
169+
168170
Although using the default namespace is very convenient, in some situations you may wish to have more flexibility.
169171
For example, if you've got dozens of widgets it makes sense to group them in namespaced folders.
170172

@@ -173,21 +175,23 @@ You have two ways to call those widgets:
173175
1) You can pass the full widget name from the `default_namespace` (basically `App\Widgets`) to the `run` method.
174176
```php
175177
@widget('News\RecentNews', $config)
176-
{!! Widget::run('News\RecentNews', $config) !!}
178+
{{ Widget::run('News\RecentNews', $config) }}
177179
```
178180

179181
2) You can use dot notation instead.
180182
```php
181183
@widget('news.recentNews', $config)
182-
{!! Widget::run('news.recentNews', $config) !!}
184+
{{ Widget::run('news.recentNews', $config) }}
183185
```
184186

185-
Note: you can pass FQCN too.
187+
You can pass FQCN too.
186188
```php
187189
@widget('\App\Http\Some\Namespace\Widget', $config)
188-
{!! Widget::run('\App\Http\Some\Namespace\Widget', $config) !!}
190+
{{ Widget::run('\App\Http\Some\Namespace\Widget', $config) }}
189191
```
190192

193+
*Note: For Laravel 5.0.0 - 5.1.3 you must use `{!! !!}` tags instead of `{{ }}`*
194+
191195
## Asynchronous widgets
192196

193197
In some situations it can be very beneficial to load widget content with AJAX.
@@ -292,7 +296,7 @@ Widget::group('sidebar')->position(5)->addWidget(<the same arguments list as in
292296
Widget::group('sidebar')->position(4)->addAsyncWidget(<the same arguments list as in run() method>);
293297

294298
// display them in a view in the correct order
295-
{!! Widget::group('sidebar')->display() !!}
299+
{{ Widget::group('sidebar')->display() }}
296300
//or
297301
@widgetGroup('sidebar')
298302
```

src/Console/WidgetMakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected function replaceView($stub)
138138
*/
139139
protected function getDefaultNamespace($rootNamespace)
140140
{
141-
return $rootNamespace.'\Widgets';
141+
return config('laravel-widgets.default_namespace', $rootNamespace.'\Widgets');
142142
}
143143

144144
/**

src/Factories/AbstractWidgetFactory.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
use Arrilot\Widgets\AbstractWidget;
66
use Arrilot\Widgets\Contracts\ApplicationWrapperContract;
77
use Arrilot\Widgets\Misc\InvalidWidgetClassException;
8+
use Arrilot\Widgets\Misc\ViewExpressionTrait;
89
use Arrilot\Widgets\WidgetId;
9-
use Illuminate\View\Expression;
1010

1111
abstract class AbstractWidgetFactory
1212
{
13+
use ViewExpressionTrait;
14+
1315
/**
1416
* Widget object to work with.
1517
*
@@ -109,7 +111,7 @@ protected function instantiateWidget(array $params = [])
109111
$this->widgetConfig = (array) array_shift($params);
110112
$this->widgetParams = $params;
111113

112-
$rootNamespace = $this->app->config('arrilot-widget.defaultNamespace', $this->app->getNamespace().'Widgets');
114+
$rootNamespace = $this->app->config('laravel-widgets.default_namespace', $this->app->getNamespace().'Widgets');
113115

114116
$widgetClass = class_exists($this->widgetName)
115117
? $this->widgetName
@@ -155,20 +157,4 @@ protected function wrapContentInContainer($content)
155157

156158
return '<'.$container['element'].' id="'.$this->javascriptFactory->getContainerId().'" '.$container['attributes'].'>'.$content.'</'.$container['element'].'>';
157159
}
158-
159-
/**
160-
* Final preparation for display.
161-
*
162-
* @param string $content
163-
* @return \Illuminate\View\Expression|string
164-
*/
165-
protected function displayContent($content)
166-
{
167-
if (interface_exists('Illuminate\Contracts\Support\Htmlable') && class_exists('Illuminate\View\Expression'))
168-
{
169-
return new Expression($content);
170-
}
171-
172-
return $content;
173-
}
174160
}

src/Factories/AsyncWidgetFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public function run()
1717
$loader = $this->javascriptFactory->getLoader();
1818
$content = $this->wrapContentInContainer($placeholder.$loader);
1919

20-
return $this->displayContent($content);
20+
return $this->convertToViewExpression($content);
2121
}
2222
}

src/Factories/WidgetFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function run()
3737
$content = $this->wrapContentInContainer($content);
3838
}
3939

40-
return $this->displayContent($content);
40+
return $this->convertToViewExpression($content);
4141
}
4242

4343
/**

src/Misc/ViewExpressionTrait.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Arrilot\Widgets\Misc;
4+
5+
use Illuminate\View\Expression;
6+
7+
trait ViewExpressionTrait
8+
{
9+
/**
10+
* Convert a given html to View Expression object that was introduced in Laravel 5.1.
11+
*
12+
* @param string $html
13+
* @return \Illuminate\View\Expression|string
14+
*/
15+
protected function convertToViewExpression($html)
16+
{
17+
if (interface_exists('Illuminate\Contracts\Support\Htmlable') && class_exists('Illuminate\View\Expression'))
18+
{
19+
return new Expression($html);
20+
}
21+
22+
return $html;
23+
}
24+
}

src/WidgetGroup.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
namespace Arrilot\Widgets;
44

55
use Arrilot\Widgets\Contracts\ApplicationWrapperContract;
6-
use Arrilot\Widgets\Misc\LaravelApplicationWrapper;
6+
use Arrilot\Widgets\Misc\ViewExpressionTrait;
77

88
class WidgetGroup
99
{
10+
use ViewExpressionTrait;
11+
1012
/**
1113
* The widget group name.
1214
*
@@ -17,7 +19,7 @@ class WidgetGroup
1719
/**
1820
* The application wrapper.
1921
*
20-
* @var string
22+
* @var ApplicationWrapperContract
2123
*/
2224
protected $app;
2325

@@ -62,7 +64,7 @@ public function display()
6264
}
6365
}
6466

65-
return $output;
67+
return $this->convertToViewExpression($output);
6668
}
6769

6870
/**

tests/TestApplicationWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function call($method, $params = [])
4747
*/
4848
public function config($key, $default = null)
4949
{
50-
if ($key == 'arrilot-widget.defaultNamespace') {
50+
if ($key == 'laravel-widgets.default_namespace') {
5151
return 'Arrilot\Widgets\Test\Dummies';
5252
}
5353

tests/WidgetFactoryTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Arrilot\Widgets\Factories\WidgetFactory;
66
use Arrilot\Widgets\Test\Dummies\TestCachedWidget;
7+
use Arrilot\Widgets\WidgetGroup;
78
use Arrilot\Widgets\WidgetId;
89
use PHPUnit_Framework_TestCase;
910

@@ -131,4 +132,13 @@ public function testItCanCacheWidgets()
131132

132133
$this->assertEquals('Cached output. Key: '.$key.', minutes: '.$widget->cacheTime, $output);
133134
}
135+
136+
public function testItGrantsAccessToWidgetGroup()
137+
{
138+
$groupObject = $this->factory->group('sidebar');
139+
140+
$expectedObject = new WidgetGroup('sidebar', new TestApplicationWrapper());
141+
142+
$this->assertEquals($expectedObject, $groupObject);
143+
}
134144
}

0 commit comments

Comments
 (0)