Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Widgets and Snippets #382

Merged
merged 89 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
86351e6
Initial work on Snippets / Widgets
bobdenotter Apr 1, 2019
beaf556
Working on Widgets / snippets
bobdenotter Apr 3, 2019
69d035f
Don't add my name, PHPStorm.
bobdenotter Apr 3, 2019
d1535f6
Making "News" work
bobdenotter Apr 4, 2019
0946009
Getting more work done on Widgets
bobdenotter Apr 5, 2019
61bf743
CS
bobdenotter Apr 5, 2019
102a57f
Fixing merge conflict
bobdenotter Apr 6, 2019
38e8561
Working on Snippets
bobdenotter Apr 6, 2019
dd56f44
Working on widgets and snippets
bobdenotter Apr 6, 2019
fe22691
CS fixes
bobdenotter Apr 6, 2019
1c139c7
Working on Snippet processing
bobdenotter Apr 6, 2019
b1322a8
Working on snippets
bobdenotter Apr 6, 2019
5ae5491
And making a snippet modify the headers!
bobdenotter Apr 8, 2019
1707cd5
Add unit tests for injector
bobdenotter Apr 8, 2019
12ecdfc
CS fixes
bobdenotter Apr 8, 2019
9294e35
Adding Kakunin test
bobdenotter Apr 8, 2019
22460b6
Initial work on Snippets / Widgets
bobdenotter Apr 1, 2019
4ba72fd
Working on Widgets / snippets
bobdenotter Apr 3, 2019
210a83c
Don't add my name, PHPStorm.
bobdenotter Apr 3, 2019
480a0e7
Making "News" work
bobdenotter Apr 4, 2019
84d065f
Getting more work done on Widgets
bobdenotter Apr 5, 2019
889a9ff
CS
bobdenotter Apr 5, 2019
17c95fb
Fixing merge conflict
bobdenotter Apr 6, 2019
a3e6f87
Working on Snippets
bobdenotter Apr 6, 2019
1e1e2f6
Working on widgets and snippets
bobdenotter Apr 6, 2019
5fa38fc
Working on Snippet processing
bobdenotter Apr 6, 2019
4ec5d6b
Working on snippets
bobdenotter Apr 6, 2019
5717344
And making a snippet modify the headers!
bobdenotter Apr 8, 2019
d9f8231
Adding Kakunin test
bobdenotter Apr 8, 2019
ba0c56e
CS fixes to widgets / snippets
bobdenotter Apr 9, 2019
b81d074
Don't add my name, PHPStorm
bobdenotter Apr 10, 2019
03fe08a
Don't use "Manager", use "Snippets", cause that's what they are.
bobdenotter Apr 10, 2019
6d24420
Minor cleanup
bobdenotter Apr 10, 2019
6dc2c62
More tests
bobdenotter Apr 10, 2019
d92550a
Make ZoneSubscriber look at Interfaces
bobdenotter Apr 13, 2019
af5a6b5
Fix in typehint
bobdenotter Apr 13, 2019
28e7331
Make widgets use __invoke
bobdenotter Apr 13, 2019
99a7ffa
Don't pass around $twig
bobdenotter Apr 13, 2019
eecd517
CS
bobdenotter Apr 13, 2019
f779e31
Renaming FooControllerInterface to FooZone
bobdenotter Apr 14, 2019
36635db
Requested changes
bobdenotter Apr 14, 2019
8ca032e
Minor tweaks
bobdenotter Apr 14, 2019
e03e844
Make snippets into a type of widget!
bobdenotter Apr 14, 2019
f8203d8
CS fix
bobdenotter Apr 14, 2019
7e1acdd
Added TwigAware Interface
bobdenotter Apr 14, 2019
b58de93
More cleanup on Widgets
bobdenotter Apr 14, 2019
66fdaec
Working on widgets
bobdenotter Apr 14, 2019
7a4151f
Requested changes
bobdenotter Apr 14, 2019
f3cdbd8
Make WidgetInterface
bobdenotter Apr 17, 2019
2af8948
Making Traits (in progress)
bobdenotter Apr 17, 2019
1794ef6
Updating Traits
bobdenotter Apr 17, 2019
1ed954b
no traits, back to Interface
bobdenotter Apr 17, 2019
987fb6d
Use PHP 7.1.18, because Travis defaults to 7.1.11 and it breaks
bobdenotter Apr 18, 2019
81bd588
Clarify pinning of PHP 7.1.18 in Travis.yml
bobdenotter Apr 19, 2019
a92f288
Don't add arrays to queue, but just the $widgets
bobdenotter Apr 19, 2019
9bc7146
Requested changes
bobdenotter Apr 19, 2019
a700756
Refactor `Zone` to `RequestZone`.
bobdenotter Apr 19, 2019
129ad0c
Updating tests
bobdenotter Apr 19, 2019
7a582e8
remove registerBoltWidgets from Widgets class
bobdenotter Apr 19, 2019
89037e0
Refactor `isFrontend` to `isForFrontend`, etc.
bobdenotter Apr 19, 2019
62398cd
Fixing tests
bobdenotter Apr 19, 2019
0530698
Move Response to its own interface
bobdenotter Apr 19, 2019
bb51dfc
Have widgets 'get' the Request just before being invoked.
bobdenotter Apr 19, 2019
4bc744e
Move `registerBoltWidgets` to a subscriber.
bobdenotter Apr 19, 2019
c552dbc
Minor changes
bobdenotter Apr 19, 2019
db9c9aa
Add `ProcessWidgetsQueueSubscriber` for Response
bobdenotter Apr 20, 2019
5ba5299
Make widgets pass along extra parameters
bobdenotter Apr 21, 2019
2a36058
Correctly pass $params
bobdenotter Apr 21, 2019
5793bf4
Don't modify StreamedResponse
bobdenotter Apr 21, 2019
60fed0e
Requested changes
bobdenotter Apr 21, 2019
5ae6479
Revert "Requested changes"
bobdenotter Apr 21, 2019
77a1cce
Move `instanceof StreamedResponse` to start of method
bobdenotter Apr 21, 2019
9c317ab
Set config as true global
bobdenotter Apr 21, 2019
2587a11
Requested changes
bobdenotter Apr 21, 2019
aa9d9d4
CS fix
bobdenotter Apr 21, 2019
60122b1
Remove methods from Twigaware interface
bobdenotter Apr 21, 2019
9bdd3bc
Init $template as string
bobdenotter Apr 21, 2019
bc0b59d
Remove $type, and remove the initial `''` for $template
bobdenotter Apr 21, 2019
f9abecd
Move Snippet to Widget\Injector
Apr 21, 2019
989e5e9
Changes in Widgets
Apr 21, 2019
0f34da1
merge master
Apr 21, 2019
af1cc72
Change BaseWidget description
bobdenotter Apr 21, 2019
871a5b9
Improve Widgets
Apr 21, 2019
16b48fe
Fix HtmlInjector
Apr 21, 2019
1a444a6
fix cs
Apr 21, 2019
74ee8fd
Html Injector rewritten
Apr 21, 2019
3cc070a
Fix NewsWidget
Apr 21, 2019
17a97c5
Fix setting deps in Widget Queue Processor
Apr 21, 2019
b54a9d0
Widgets changes (#411)
JarJak Apr 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/EventSubscriber/ProcessWidgetsQueueSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Bolt\Widgets;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

Expand Down
4 changes: 2 additions & 2 deletions src/Twig/WidgetExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public function getFunctions(): array

public function renderWidgetByName(string $name): string
{
return $this->widgetRenderer->renderWidgetByName($name);
return $this->widgetRenderer->renderWidgetByName($name, func_get_args());
}

public function renderWidgetsForTarget(string $target): string
{
return $this->widgetRenderer->renderWidgetsForTarget($target);
return $this->widgetRenderer->renderWidgetsForTarget($target, func_get_args());
}

public function dummy($input = null): string
Expand Down
11 changes: 5 additions & 6 deletions src/Widget/BaseWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class BaseWidget implements WidgetInterface
protected $target = Target::NOWHERE;
protected $zone = RequestZone::EVERYWHERE;
protected $priority = 0;
protected $context = [];

/** @var string */
protected $template;
Expand Down Expand Up @@ -84,16 +83,16 @@ public function getPriority(): int
return $this->priority;
}

public function __invoke(?string $template = null): string
public function __invoke(array $params = []): string
{
if ($template === null) {
$template = $this->template;
if (array_key_exists('template', $params)) {
$this->template = $params['template'];
bobdenotter marked this conversation as resolved.
Show resolved Hide resolved
}

if ($this instanceof TwigAware) {
$output = $this->twig->render($template, $this->context);
$output = $this->twig->render($this->template, $params);
bobdenotter marked this conversation as resolved.
Show resolved Hide resolved
} else {
$output = $template;
$output = $this->template;
bobdenotter marked this conversation as resolved.
Show resolved Hide resolved
}

return sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Widget/BoltHeaderWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class BoltHeaderWidget extends BaseWidget implements ResponseAware
protected $target = Target::NOWHERE;
protected $zone = RequestZone::FRONTEND;

public function __invoke(?string $template = null): string
public function __invoke(array $params = []): string
{
$this->getResponse()->headers->set('X-Powered-By', 'Bolt', false);

Expand Down
6 changes: 3 additions & 3 deletions src/Widget/NewsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ class NewsWidget extends BaseWidget implements TwigAware, RequestAware
protected $priority = 150;
protected $template = '@bolt/widgets/news.twig';

public function __invoke(?string $template = null): string
public function __invoke(array $params = []): string
{
$news = $this->getNews();

$currentItem = $news['information'];

$this->context = [
$context = [
'title' => $currentItem['title'],
'news' => $currentItem['teaser'],
'link' => $currentItem['link'],
'datechanged' => $currentItem['datechanged'],
'datefetched' => '2019-04-04 07:25:00',
];

return parent::__invoke($template);
return parent::__invoke($context);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Widget/SnippetWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(
$this->zone = $zone;
}

public function __invoke(?string $template = null): string
public function __invoke(array $params = []): string
{
return $this->getTemplate();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Widget/WidgetInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ public function getZone(): string;

public function getSlug(): string;
bobdenotter marked this conversation as resolved.
Show resolved Hide resolved

public function __invoke(?string $template = null): string;
public function __invoke(array $params = []): string;
bobdenotter marked this conversation as resolved.
Show resolved Hide resolved
}
8 changes: 4 additions & 4 deletions src/Widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function registerWidget(WidgetInterface $widget): void
$this->queue->push($widget);
}

public function renderWidgetByName(string $name): string
public function renderWidgetByName(string $name, array $params = []): string
{
$widget = $this->queue->filter(function (WidgetInterface $widget) use ($name) {
return $widget->getName() === $name;
Expand All @@ -59,11 +59,11 @@ public function renderWidgetByName(string $name): string
$widget->setRequest($this->requestStack->getCurrentRequest());
}

return $widget();
return $widget($params);
}
}

public function renderWidgetsForTarget(string $target): string
public function renderWidgetsForTarget(string $target, array $params = []): string
{
$widgets = $this->queue->filter(function (WidgetInterface $widget) use ($target) {
return $widget->getTarget() === $target;
Expand All @@ -78,7 +78,7 @@ public function renderWidgetsForTarget(string $target): string
$widget->setRequest($this->requestStack->getCurrentRequest());
}

$output .= $widget();
$output .= $widget($params);
}

return $output;
Expand Down
23 changes: 23 additions & 0 deletions tests/php/WidgetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ public function testRenderWidget(): void
);
}


public function testRenderWidgetWithExtraParameters(): void
{
$queueprocessor = new QueueProcessor(new HtmlInjector());
$requestStack = new RequestStack();
$requestStack->push(Request::createFromGlobals());

$loader = new ArrayLoader(['weather.twig' => '[Hello, {{ foo }}!]']);
$twig = new Environment($loader);

$widgets = new Widgets($requestStack, $queueprocessor, $twig);

$weatherWidget = new WeatherWidget();
$weatherWidget->setTemplate('weather.twig');

$widgets->registerWidget($weatherWidget);

$this->assertSame(
'<div id="widget-weather-widget" name="Weather Widget">[Hello, Bar!]</div>',
$widgets->renderWidgetByName('Weather Widget', ['foo' => 'Bar'])
);
}

public function testProcessHeaderWidget(): void
{
$queueprocessor = new QueueProcessor(new HtmlInjector());
Expand Down