1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MIT License |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerEco\Yves\FactFinder\Plugin; |
9
|
|
|
|
10
|
|
|
use Silex\Application; |
11
|
|
|
use Silex\ServiceProviderInterface; |
12
|
|
|
use Spryker\Shared\Log\LoggerTrait; |
13
|
|
|
use Spryker\Yves\Kernel\AbstractPlugin; |
14
|
|
|
use Twig_Environment; |
15
|
|
|
use Twig_SimpleFunction; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @method \SprykerEco\Yves\FactFinder\FactFinderFactory getFactory() |
19
|
|
|
*/ |
20
|
|
|
class FactFinderTwigServiceProvider extends AbstractPlugin implements ServiceProviderInterface |
21
|
|
|
{ |
22
|
|
|
use LoggerTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Registers services on the given app. |
26
|
|
|
* |
27
|
|
|
* This method should only be used to configure services and parameters. |
28
|
|
|
* It should not get services. |
29
|
|
|
* |
30
|
|
|
* @param \Silex\Application $app |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function register(Application $app) |
35
|
|
|
{ |
36
|
|
|
$app['twig'] = $app->share( |
|
|
|
|
37
|
|
|
$app->extend('twig', function (Twig_Environment $twig) use ($app) { |
38
|
|
|
$twig->addFunction( |
39
|
|
|
'fact_finder_recommendations', |
40
|
|
|
$this->createRecommendationsFunction($twig, $app) |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
return $twig; |
44
|
|
|
}) |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
$app['twig'] = $app->share( |
|
|
|
|
48
|
|
|
$app->extend('twig', function (Twig_Environment $twig) use ($app) { |
49
|
|
|
$twig->addFunction( |
50
|
|
|
'fact_finder_product_campaigns', |
51
|
|
|
$this->createProductCampaignsFunction($twig, $app) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
return $twig; |
55
|
|
|
}) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$app['twig'] = $app->share( |
|
|
|
|
59
|
|
|
$app->extend('twig', function (Twig_Environment $twig) use ($app) { |
60
|
|
|
$twig->addFunction( |
61
|
|
|
'fact_finder_shopping_cart_campaigns', |
62
|
|
|
$this->createShoppingCartCampaignsFunction($twig, $app) |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
return $twig; |
66
|
|
|
}) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param \Twig_Environment $twig |
72
|
|
|
* @param \Silex\Application $app |
73
|
|
|
* |
74
|
|
|
* @return \Twig_SimpleFunction |
75
|
|
|
*/ |
76
|
|
|
protected function createRecommendationsFunction(Twig_Environment $twig, Application $app) |
77
|
|
|
{ |
78
|
|
|
$options = ['is_safe' => ['html']]; |
79
|
|
|
|
80
|
|
|
return new Twig_SimpleFunction('fact_finder_recommendations', function (array $params, $templatePath) use ($twig, $app) { |
81
|
|
|
|
82
|
|
|
$recommendationsDataProvider = $this->getFactory() |
83
|
|
|
->createRecommendationsDataProvider(); |
84
|
|
|
|
85
|
|
|
return $twig->render( |
86
|
|
|
$templatePath, |
87
|
|
|
[ |
88
|
|
|
'productRecommendations' => $recommendationsDataProvider->buildTemplateData($params, $app['request_stack']->getCurrentRequest()), |
89
|
|
|
'params' => $params, |
90
|
|
|
] |
91
|
|
|
); |
92
|
|
|
}, $options); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param \Twig_Environment $twig |
97
|
|
|
* @param \Silex\Application $app |
98
|
|
|
* |
99
|
|
|
* @return \Twig_SimpleFunction |
100
|
|
|
*/ |
101
|
|
|
protected function createProductCampaignsFunction(Twig_Environment $twig, Application $app) |
102
|
|
|
{ |
103
|
|
|
$options = ['is_safe' => ['html']]; |
104
|
|
|
|
105
|
|
|
return new Twig_SimpleFunction('fact_finder_product_campaigns', function (array $params, $templatePath) use ($twig, $app) { |
106
|
|
|
|
107
|
|
|
$productCampaignsDataProvider = $this->getFactory() |
108
|
|
|
->createProductCampaignsDataProvider(); |
109
|
|
|
|
110
|
|
|
return $twig->render( |
111
|
|
|
$templatePath, |
112
|
|
|
[ |
113
|
|
|
'campaigns' => $productCampaignsDataProvider->buildTemplateData($params, $app['request_stack']->getCurrentRequest()), |
114
|
|
|
'params' => $params, |
115
|
|
|
] |
116
|
|
|
); |
117
|
|
|
}, $options); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param \Twig_Environment $twig |
122
|
|
|
* @param \Silex\Application $app |
123
|
|
|
* |
124
|
|
|
* @return \Twig_SimpleFunction |
125
|
|
|
*/ |
126
|
|
|
protected function createShoppingCartCampaignsFunction(Twig_Environment $twig, Application $app) |
127
|
|
|
{ |
128
|
|
|
$options = ['is_safe' => ['html']]; |
129
|
|
|
|
130
|
|
|
return new Twig_SimpleFunction('fact_finder_shopping_cart_campaigns', function (array $params, $templatePath) use ($twig, $app) { |
131
|
|
|
|
132
|
|
|
$shoppingCartCampaignsDataProvider = $this->getFactory() |
133
|
|
|
->createShoppingCartCampaignsDataProvider(); |
134
|
|
|
|
135
|
|
|
return $twig->render( |
136
|
|
|
$templatePath, |
137
|
|
|
[ |
138
|
|
|
'campaigns' => $shoppingCartCampaignsDataProvider->buildTemplateData($params, $app['request_stack']->getCurrentRequest()), |
139
|
|
|
'params' => $params, |
140
|
|
|
] |
141
|
|
|
); |
142
|
|
|
}, $options); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Bootstraps the application. |
147
|
|
|
* |
148
|
|
|
* This method is called after all services are registered |
149
|
|
|
* and should be used for "dynamic" configuration (whenever |
150
|
|
|
* a service must be requested). |
151
|
|
|
* |
152
|
|
|
* @param \Silex\Application $app |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
public function boot(Application $app) |
157
|
|
|
{ |
158
|
|
|
// TODO: Implement boot() method. |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.