Skip to content

Commit fe8eef7

Browse files
author
Leny BERNARD
committed
Merge pull request #355 from paulandrieux/feature/improve-cache-widgets
Feature/improve cache widgets
2 parents 2190ead + 4a1aaa6 commit fe8eef7

File tree

7 files changed

+78
-42
lines changed

7 files changed

+78
-42
lines changed

Bundle/CoreBundle/Builder/ViewCssBuilder.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,38 @@
33
namespace Victoire\Bundle\CoreBundle\Builder;
44

55
use Victoire\Bundle\CoreBundle\Entity\View;
6+
use Victoire\Bundle\CoreBundle\Template\TemplateMapper;
67
use Victoire\Bundle\WidgetBundle\Renderer\WidgetRenderer;
78

89
/**
910
* View CSS Builder
1011
* ref: victoire_core.view_css_builder.
11-
*/
12+
*
13+
* @property templating
14+
*/
1215
class ViewCssBuilder
1316
{
17+
protected $templating;
18+
protected $victoireTwigResponsive;
1419
private $widgetRenderer;
1520
private $webDir;
1621
private $viewCssDir;
1722

1823
/**
1924
* Construct.
2025
*
21-
* @param WidgetRenderer $widgetRenderer
22-
* @param $kernelRootDir
26+
* @param TemplateMapper $templating
27+
* @param $victoireTwigResponsive
28+
* @param $kernelRootDir
29+
*
30+
* @internal param WidgetRenderer $widgetRenderer
2331
*/
24-
public function __construct(WidgetRenderer $widgetRenderer, $kernelRootDir)
32+
public function __construct(TemplateMapper $templating, $victoireTwigResponsive, $kernelRootDir)
2533
{
26-
$this->widgetRenderer = $widgetRenderer;
2734
$this->webDir = '/view-css';
2835
$this->viewCssDir = $kernelRootDir.'/../web'.$this->webDir;
36+
$this->templating = $templating;
37+
$this->victoireTwigResponsive = $victoireTwigResponsive;
2938
}
3039

3140
/**
@@ -52,7 +61,14 @@ public function generateViewCss(View $view, array $widgets)
5261
$css = '';
5362

5463
foreach ($widgets as $widget) {
55-
$css .= trim($this->widgetRenderer->renderStyle($widget));
64+
$style = $this->templating->render(
65+
'VictoireCoreBundle:Widget:style/style.html.twig',
66+
[
67+
'widget' => $widget,
68+
'victoire_twig_responsive' => $this->victoireTwigResponsive,
69+
]
70+
);
71+
$css .= trim($style);
5672
}
5773

5874
if ($css !== '') {

Bundle/CoreBundle/Resources/config/services.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ services:
180180
victoire_core.view_css_builder:
181181
class: Victoire\Bundle\CoreBundle\Builder\ViewCssBuilder
182182
arguments:
183-
- @victoire_widget.widget_renderer
183+
- @victoire_templating
184+
- %victoire_twig.responsive%
184185
- '%kernel.root_dir%'
185186

186187
victoire_core.view_css_listener:

Bundle/WidgetBundle/Cache/WidgetCache.php

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace Victoire\Bundle\WidgetBundle\Cache;
44

55
use Predis\Client;
6+
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
7+
use Symfony\Component\Security\Core\SecurityContext;
68
use Victoire\Bundle\WidgetBundle\Entity\Widget;
9+
use Victoire\Bundle\WidgetBundle\Entity\WidgetSlotInterface;
710

811
/**
912
* This class handle the saving of Widgets.
@@ -16,10 +19,15 @@ class WidgetCache
1619
* @var Client
1720
*/
1821
private $redis;
22+
/**
23+
* @var AuthorizationChecker
24+
*/
25+
private $authorizationChecker;
1926

20-
public function __construct(Client $redis)
27+
public function __construct(Client $redis, AuthorizationChecker $authorizationChecker)
2128
{
2229
$this->redis = $redis;
30+
$this->authorizationChecker = $authorizationChecker;
2331
}
2432

2533
/**
@@ -53,21 +61,40 @@ public function save(Widget $widget, $content)
5361
protected function getHash(Widget $widget)
5462
{
5563
$hash = null;
56-
if ($widget->getMode() == Widget::MODE_BUSINESS_ENTITY
57-
&& ($entity = $widget->getEntity())
58-
&& method_exists($widget->getEntity(), 'getUpdatedAt')) {
59-
$hash = sprintf('%s-%s--%s-%s',
60-
$widget->getId(),
61-
$widget->getUpdatedAt()->getTimestamp(),
62-
$entity->getId(),
63-
$entity->getUpdatedAt()->getTimestamp()
64-
);
65-
} elseif ($widget->getMode() == Widget::MODE_STATIC) {
66-
$hash = sprintf('%s-%s', $widget->getId(), $widget->getUpdatedAt()->getTimestamp());
64+
if (!$widget instanceof WidgetSlotInterface) {
65+
if ($widget->getMode() == Widget::MODE_BUSINESS_ENTITY
66+
&& ($entity = $widget->getEntity())
67+
&& method_exists($widget->getEntity(), 'getUpdatedAt')) {
68+
$hash = $this->generateBusinessEntityHash($widget);
69+
} elseif ($widget->getMode() == Widget::MODE_STATIC) {
70+
$hash = $this->generateHash($widget);
71+
}
6772
}
6873

6974
return $hash;
7075
}
7176

77+
protected function generateBusinessEntityHash(Widget $widget)
78+
{
79+
return sprintf('%s-%s-%s--%s-%s-%s',
80+
$widget->getId(),
81+
$widget->getUpdatedAt()->getTimestamp(),
82+
$widget->getCurrentView()->getReference()->getId(),
83+
$widget->getEntity()->getId(),
84+
$widget->getEntity()->getUpdatedAt()->getTimestamp(),
85+
(string) $this->authorizationChecker->isGranted('ROLE_VICTOIRE')
86+
);
87+
}
88+
89+
private function generateHash($widget)
90+
{
91+
return sprintf('%s-%s-%s-%s',
92+
$widget->getId(),
93+
$widget->getUpdatedAt()->getTimestamp(),
94+
$widget->getCurrentView()->getReference()->getId(),
95+
(string) $this->authorizationChecker->isGranted('ROLE_VICTOIRE')
96+
);
97+
}
98+
7299

73100
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Victoire\Bundle\WidgetBundle\Entity;
4+
5+
/**
6+
* WidgetSlotInterface.
7+
*/
8+
interface WidgetSlotInterface
9+
{
10+
}

Bundle/WidgetBundle/Renderer/WidgetRenderer.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
class WidgetRenderer
1616
{
1717
private $container;
18-
private $victoireTwigResponsive;
1918
/**
2019
* @var WidgetCache
2120
*/
@@ -25,14 +24,12 @@ class WidgetRenderer
2524
* WidgetRenderer constructor.
2625
*
2726
* @param Container $container
28-
* @param $victoireTwigResponsive
2927
* @param Client $redis
3028
* @param WidgetCache $widgetCache
3129
*/
32-
public function __construct(Container $container, $victoireTwigResponsive, WidgetCache $widgetCache)
30+
public function __construct(Container $container, WidgetCache $widgetCache)
3331
{
3432
$this->container = $container;
35-
$this->victoireTwigResponsive = $victoireTwigResponsive;
3633
$this->widgetCache = $widgetCache;
3734
}
3835

@@ -190,21 +187,4 @@ public function getExtraCssClass(Widget $widget)
190187
return $cssClass;
191188
}
192189

193-
/**
194-
* Render the CSS style for a Widget.
195-
*
196-
* @param Widget $widget
197-
*
198-
* @return mixed
199-
*/
200-
public function renderStyle(Widget $widget)
201-
{
202-
return $this->container->get('victoire_templating')->render(
203-
'VictoireCoreBundle:Widget:style/style.html.twig',
204-
[
205-
'widget' => $widget,
206-
'victoire_twig_responsive' => $this->victoireTwigResponsive,
207-
]
208-
);
209-
}
210190
}

Bundle/WidgetBundle/Resources/config/services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ services:
2222
class: Victoire\Bundle\WidgetBundle\Renderer\WidgetRenderer
2323
arguments:
2424
- @service_container
25-
- %victoire_twig.responsive%
2625
- @victoire_widget.widget_cache
2726

2827
victoire_widget.widget_cache:
2928
class: Victoire\Bundle\WidgetBundle\Cache\WidgetCache
3029
arguments:
3130
- @snc_redis.victoire_client
31+
- @security.authorization_checker
3232

3333
victoire_widget.widget_manager:
3434
class: Victoire\Bundle\WidgetBundle\Model\WidgetManager

Bundle/WidgetMapBundle/Builder/WidgetMapBuilder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public function build(View $view, EntityManager $em = null, $updatePage = true)
6161
foreach ($__builtWidgetMap as $key => $item) {
6262
// store each widgetmap in an array with the widget id as key to make the "View::getWidgetMapByWidgetAndView"
6363
// method more efficient
64-
$builtWidgetMap[$slot][$item->getWidget()->getId()] = $item;
64+
if ($item->getWidget()) {
65+
$builtWidgetMap[$slot][$item->getWidget()->getId()] = $item;
66+
}
6567
}
6668
}
6769
if ($updatePage) {

0 commit comments

Comments
 (0)