aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-09-06 14:13:16 +0200
committerGitHub <noreply@github.com>2020-09-06 14:13:16 +0200
commit949a0953109c2738ecd973c44cd728cddc120164 (patch)
treef27a486930ac59b62174d8219456e51d0167cae4
parenta5dd7d58d25db7ffefc7ab1b39d99da2fe6976ac (diff)
parent80b708a8780b91b092c3a372342959eeca742802 (diff)
downloadShaarli-949a0953109c2738ecd973c44cd728cddc120164.tar.gz
Shaarli-949a0953109c2738ecd973c44cd728cddc120164.tar.zst
Shaarli-949a0953109c2738ecd973c44cd728cddc120164.zip
Merge pull request #1538 from ArthurHoaro/feature/plugins-bookmark-service
Inject BookmarkServiceInterface in plugins data
-rw-r--r--application/front/controller/admin/ShaarliAdminController.php2
-rw-r--r--application/front/controller/visitor/ShaarliVisitorController.php26
-rw-r--r--application/plugin/PluginManager.php4
-rw-r--r--doc/md/Plugin-System.md68
4 files changed, 63 insertions, 37 deletions
diff --git a/application/front/controller/admin/ShaarliAdminController.php b/application/front/controller/admin/ShaarliAdminController.php
index 3b5939bb..c26c9cbe 100644
--- a/application/front/controller/admin/ShaarliAdminController.php
+++ b/application/front/controller/admin/ShaarliAdminController.php
@@ -4,9 +4,7 @@ declare(strict_types=1);
4 4
5namespace Shaarli\Front\Controller\Admin; 5namespace Shaarli\Front\Controller\Admin;
6 6
7use Shaarli\Container\ShaarliContainer;
8use Shaarli\Front\Controller\Visitor\ShaarliVisitorController; 7use Shaarli\Front\Controller\Visitor\ShaarliVisitorController;
9use Shaarli\Front\Exception\UnauthorizedException;
10use Shaarli\Front\Exception\WrongTokenException; 8use Shaarli\Front\Exception\WrongTokenException;
11use Shaarli\Security\SessionManager; 9use Shaarli\Security\SessionManager;
12use Slim\Http\Request; 10use Slim\Http\Request;
diff --git a/application/front/controller/visitor/ShaarliVisitorController.php b/application/front/controller/visitor/ShaarliVisitorController.php
index f17c8ed3..cd27455b 100644
--- a/application/front/controller/visitor/ShaarliVisitorController.php
+++ b/application/front/controller/visitor/ShaarliVisitorController.php
@@ -78,16 +78,14 @@ abstract class ShaarliVisitorController
78 'footer', 78 'footer',
79 ]; 79 ];
80 80
81 $parameters = $this->buildPluginParameters($template);
82
81 foreach ($common_hooks as $name) { 83 foreach ($common_hooks as $name) {
82 $pluginData = []; 84 $pluginData = [];
83 $this->container->pluginManager->executeHooks( 85 $this->container->pluginManager->executeHooks(
84 'render_' . $name, 86 'render_' . $name,
85 $pluginData, 87 $pluginData,
86 [ 88 $parameters
87 'target' => $template,
88 'loggedin' => $this->container->loginManager->isLoggedIn(),
89 'basePath' => $this->container->basePath,
90 ]
91 ); 89 );
92 $this->assignView('plugins_' . $name, $pluginData); 90 $this->assignView('plugins_' . $name, $pluginData);
93 } 91 }
@@ -95,19 +93,23 @@ abstract class ShaarliVisitorController
95 93
96 protected function executePageHooks(string $hook, array &$data, string $template = null): void 94 protected function executePageHooks(string $hook, array &$data, string $template = null): void
97 { 95 {
98 $params = [
99 'target' => $template,
100 'loggedin' => $this->container->loginManager->isLoggedIn(),
101 'basePath' => $this->container->basePath,
102 ];
103
104 $this->container->pluginManager->executeHooks( 96 $this->container->pluginManager->executeHooks(
105 $hook, 97 $hook,
106 $data, 98 $data,
107 $params 99 $this->buildPluginParameters($template)
108 ); 100 );
109 } 101 }
110 102
103 protected function buildPluginParameters(?string $template): array
104 {
105 return [
106 'target' => $template,
107 'loggedin' => $this->container->loginManager->isLoggedIn(),
108 'basePath' => $this->container->basePath,
109 'bookmarkService' => $this->container->bookmarkService
110 ];
111 }
112
111 /** 113 /**
112 * Simple helper which prepend the base path to redirect path. 114 * Simple helper which prepend the base path to redirect path.
113 * 115 *
diff --git a/application/plugin/PluginManager.php b/application/plugin/PluginManager.php
index 2d93cb3a..7881e3be 100644
--- a/application/plugin/PluginManager.php
+++ b/application/plugin/PluginManager.php
@@ -112,6 +112,10 @@ class PluginManager
112 $data['_BASE_PATH_'] = $params['basePath']; 112 $data['_BASE_PATH_'] = $params['basePath'];
113 } 113 }
114 114
115 if (isset($params['bookmarkService'])) {
116 $data['_BOOKMARK_SERVICE_'] = $params['bookmarkService'];
117 }
118
115 foreach ($this->loadedPlugins as $plugin) { 119 foreach ($this->loadedPlugins as $plugin) {
116 $hookFunction = $this->buildHookName($hook, $plugin); 120 $hookFunction = $this->buildHookName($hook, $plugin);
117 121
diff --git a/doc/md/Plugin-System.md b/doc/md/Plugin-System.md
index f264e873..87a2638d 100644
--- a/doc/md/Plugin-System.md
+++ b/doc/md/Plugin-System.md
@@ -73,6 +73,26 @@ Every hook function has a `$data` parameter. Its content differs for each hooks.
73 73
74 return $data; 74 return $data;
75 75
76#### Special data
77
78Special additional data are passed to every hook through the
79`$data` parameter to give you access to additional context, and services.
80
81Complete list:
82
83 * `_PAGE_` (string): if the current hook is used to render a template, its name is passed through this additional parameter.
84 * `_LOGGEDIN_` (bool): whether the user is logged in or not.
85 * `_BASE_PATH_` (string): if Shaarli instance is hosted under a subfolder, contains the subfolder path to `index.php` (e.g. `https://domain.tld/shaarli/` -> `/shaarli/`).
86 * `_BOOKMARK_SERVICE_` (`BookmarkServiceInterface`): bookmark service instance, for advanced usage.
87
88Example:
89
90```php
91if ($data['_PAGE_'] === TemplatePage::LINKLIST && $data['LOGGEDIN'] === true) {
92 // Do something for logged in users when the link list is rendered
93}
94```
95
76#### Filling templates placeholder 96#### Filling templates placeholder
77 97
78Template placeholders are displayed in template in specific places. 98Template placeholders are displayed in template in specific places.
@@ -95,7 +115,7 @@ When a page is displayed, every variable send to the template engine is passed t
95 115
96The data contained by this array can be altered before template rendering. 116The data contained by this array can be altered before template rendering.
97 117
98For exemple, in linklist, it is possible to alter every title: 118For example, in linklist, it is possible to alter every title:
99 119
100```php 120```php
101// mind the reference if you want $data to be altered 121// mind the reference if you want $data to be altered
@@ -156,8 +176,7 @@ Allow plugin to add content in page headers.
156 176
157`$data` is an array containing: 177`$data` is an array containing:
158 178
159- `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.). 179 - [Special data](#special-data)
160- `_LOGGEDIN_`: true if user is logged in, false otherwise.
161 180
162##### Template placeholders 181##### Template placeholders
163 182
@@ -185,8 +204,7 @@ Allow plugin to include their own CSS files.
185 204
186`$data` is an array containing: 205`$data` is an array containing:
187 206
188- `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.). 207 - [Special data](#special-data)
189- `_LOGGEDIN_`: true if user is logged in, false otherwise.
190 208
191##### Template placeholders 209##### Template placeholders
192 210
@@ -208,8 +226,7 @@ Allow plugin to add content in page footer and include their own JS files.
208 226
209`$data` is an array containing: 227`$data` is an array containing:
210 228
211- `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.). 229 - [Special data](#special-data)
212- `_LOGGEDIN_`: true if user is logged in, false otherwise.
213 230
214##### Template placeholders 231##### Template placeholders
215 232
@@ -236,8 +253,8 @@ It allows to add content at the begining and end of the page, after every link d
236 253
237`$data` is an array containing: 254`$data` is an array containing:
238 255
239- `_LOGGEDIN_`: true if user is logged in, false otherwise. 256 - All templates data, including links.
240- All templates data, including links. 257 - [Special data](#special-data)
241 258
242##### Template placeholders 259##### Template placeholders
243 260
@@ -271,7 +288,8 @@ Allow to add fields in the form, or display elements.
271 288
272`$data` is an array containing: 289`$data` is an array containing:
273 290
274- All templates data. 291 - All templates data.
292 - [Special data](#special-data)
275 293
276##### Template placeholders 294##### Template placeholders
277 295
@@ -293,7 +311,8 @@ Allow to add content at the end of the page.
293 311
294`$data` is an array containing: 312`$data` is an array containing:
295 313
296- All templates data. 314 - All templates data.
315 - [Special data](#special-data)
297 316
298##### Template placeholders 317##### Template placeholders
299 318
@@ -315,8 +334,8 @@ Allow to add content at the top and bottom of the page.
315 334
316`$data` is an array containing: 335`$data` is an array containing:
317 336
318- `_LOGGEDIN_`: true if user is logged in, false otherwise. 337 - All templates data.
319- All templates data. 338 - [Special data](#special-data)
320 339
321##### Template placeholders 340##### Template placeholders
322 341
@@ -339,8 +358,8 @@ Allow to add content at the top and bottom of the page.
339 358
340`$data` is an array containing: 359`$data` is an array containing:
341 360
342- `_LOGGEDIN_`: true if user is logged in, false otherwise. 361 - All templates data.
343- All templates data. 362 - [Special data](#special-data)
344 363
345##### Template placeholders 364##### Template placeholders
346 365
@@ -368,8 +387,8 @@ Allow to add content at the top and bottom of the page.
368 387
369`$data` is an array containing: 388`$data` is an array containing:
370 389
371- `_LOGGEDIN_`: true if user is logged in, false otherwise. 390 - All templates data.
372- All templates data. 391 - [Special data](#special-data)
373 392
374##### Template placeholders 393##### Template placeholders
375 394
@@ -394,8 +413,8 @@ Allow to add content at the top and bottom of the page, the bottom of each link
394 413
395`$data` is an array containing: 414`$data` is an array containing:
396 415
397- `_LOGGEDIN_`: true if user is logged in, false otherwise. 416 - All templates data, including links.
398- All templates data, including links. 417 - [Special data](#special-data)
399 418
400##### Template placeholders 419##### Template placeholders
401 420
@@ -420,9 +439,8 @@ Allow to add tags in the feed, either in the header or for each items. Items (li
420 439
421`$data` is an array containing: 440`$data` is an array containing:
422 441
423- `_LOGGEDIN_`: true if user is logged in, false otherwise. 442 - All templates data, including links.
424- `_PAGE_`: containing either `rss` or `atom`. 443 - [Special data](#special-data)
425- All templates data, including links.
426 444
427##### Template placeholders 445##### Template placeholders
428 446
@@ -456,6 +474,8 @@ Allow to alter the link being saved in the datastore.
456- created 474- created
457- updated 475- updated
458 476
477Also [special data](#special-data).
478
459 479
460#### delete_link 480#### delete_link
461 481
@@ -465,7 +485,7 @@ Allow to execute any action before the link is actually removed from the datasto
465 485
466##### Data 486##### Data
467 487
468`$data` is an array containing the link being saved: 488`$data` is an array containing the link being deleted:
469 489
470- id 490- id
471- title 491- title
@@ -477,6 +497,7 @@ Allow to execute any action before the link is actually removed from the datasto
477- created 497- created
478- updated 498- updated
479 499
500Also [special data](#special-data).
480 501
481#### save_plugin_parameters 502#### save_plugin_parameters
482 503
@@ -492,6 +513,7 @@ For example it is used to update the CSS file of the `default_colors` plugins.
492So if the plugin has a parameter called `MYPLUGIN_PARAMETER`, 513So if the plugin has a parameter called `MYPLUGIN_PARAMETER`,
493the array will contain an entry with `MYPLUGIN_PARAMETER` as a key. 514the array will contain an entry with `MYPLUGIN_PARAMETER` as a key.
494 515
516Also [special data](#special-data).
495 517
496## Guide for template designer 518## Guide for template designer
497 519