Inject BookmarkServiceInterface in plugins data

Related discussion: ilesinge/shaarli-related#7
This commit is contained in:
ArthurHoaro 2020-09-01 13:33:50 +02:00
parent 0e60b7f174
commit 80b708a878
4 changed files with 63 additions and 37 deletions

View file

@ -4,9 +4,7 @@
namespace Shaarli\Front\Controller\Admin; namespace Shaarli\Front\Controller\Admin;
use Shaarli\Container\ShaarliContainer;
use Shaarli\Front\Controller\Visitor\ShaarliVisitorController; use Shaarli\Front\Controller\Visitor\ShaarliVisitorController;
use Shaarli\Front\Exception\UnauthorizedException;
use Shaarli\Front\Exception\WrongTokenException; use Shaarli\Front\Exception\WrongTokenException;
use Shaarli\Security\SessionManager; use Shaarli\Security\SessionManager;
use Slim\Http\Request; use Slim\Http\Request;

View file

@ -78,16 +78,14 @@ protected function executeDefaultHooks(string $template): void
'footer', 'footer',
]; ];
$parameters = $this->buildPluginParameters($template);
foreach ($common_hooks as $name) { foreach ($common_hooks as $name) {
$pluginData = []; $pluginData = [];
$this->container->pluginManager->executeHooks( $this->container->pluginManager->executeHooks(
'render_' . $name, 'render_' . $name,
$pluginData, $pluginData,
[ $parameters
'target' => $template,
'loggedin' => $this->container->loginManager->isLoggedIn(),
'basePath' => $this->container->basePath,
]
); );
$this->assignView('plugins_' . $name, $pluginData); $this->assignView('plugins_' . $name, $pluginData);
} }
@ -95,19 +93,23 @@ protected function executeDefaultHooks(string $template): void
protected function executePageHooks(string $hook, array &$data, string $template = null): void protected function executePageHooks(string $hook, array &$data, string $template = null): void
{ {
$params = [
'target' => $template,
'loggedin' => $this->container->loginManager->isLoggedIn(),
'basePath' => $this->container->basePath,
];
$this->container->pluginManager->executeHooks( $this->container->pluginManager->executeHooks(
$hook, $hook,
$data, $data,
$params $this->buildPluginParameters($template)
); );
} }
protected function buildPluginParameters(?string $template): array
{
return [
'target' => $template,
'loggedin' => $this->container->loginManager->isLoggedIn(),
'basePath' => $this->container->basePath,
'bookmarkService' => $this->container->bookmarkService
];
}
/** /**
* Simple helper which prepend the base path to redirect path. * Simple helper which prepend the base path to redirect path.
* *

View file

@ -112,6 +112,10 @@ public function executeHooks($hook, &$data, $params = array())
$data['_BASE_PATH_'] = $params['basePath']; $data['_BASE_PATH_'] = $params['basePath'];
} }
if (isset($params['bookmarkService'])) {
$data['_BOOKMARK_SERVICE_'] = $params['bookmarkService'];
}
foreach ($this->loadedPlugins as $plugin) { foreach ($this->loadedPlugins as $plugin) {
$hookFunction = $this->buildHookName($hook, $plugin); $hookFunction = $this->buildHookName($hook, $plugin);

View file

@ -73,6 +73,26 @@ Every hook function has a `$data` parameter. Its content differs for each hooks.
return $data; return $data;
#### Special data
Special additional data are passed to every hook through the
`$data` parameter to give you access to additional context, and services.
Complete list:
* `_PAGE_` (string): if the current hook is used to render a template, its name is passed through this additional parameter.
* `_LOGGEDIN_` (bool): whether the user is logged in or not.
* `_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/`).
* `_BOOKMARK_SERVICE_` (`BookmarkServiceInterface`): bookmark service instance, for advanced usage.
Example:
```php
if ($data['_PAGE_'] === TemplatePage::LINKLIST && $data['LOGGEDIN'] === true) {
// Do something for logged in users when the link list is rendered
}
```
#### Filling templates placeholder #### Filling templates placeholder
Template placeholders are displayed in template in specific places. Template 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
The data contained by this array can be altered before template rendering. The data contained by this array can be altered before template rendering.
For exemple, in linklist, it is possible to alter every title: For example, in linklist, it is possible to alter every title:
```php ```php
// mind the reference if you want $data to be altered // mind the reference if you want $data to be altered
@ -156,8 +176,7 @@ Allow plugin to add content in page headers.
`$data` is an array containing: `$data` is an array containing:
- `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.). - [Special data](#special-data)
- `_LOGGEDIN_`: true if user is logged in, false otherwise.
##### Template placeholders ##### Template placeholders
@ -185,8 +204,7 @@ Allow plugin to include their own CSS files.
`$data` is an array containing: `$data` is an array containing:
- `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.). - [Special data](#special-data)
- `_LOGGEDIN_`: true if user is logged in, false otherwise.
##### Template placeholders ##### Template placeholders
@ -208,8 +226,7 @@ Allow plugin to add content in page footer and include their own JS files.
`$data` is an array containing: `$data` is an array containing:
- `_PAGE_`: current target page (eg: `linklist`, `picwall`, etc.). - [Special data](#special-data)
- `_LOGGEDIN_`: true if user is logged in, false otherwise.
##### Template placeholders ##### Template placeholders
@ -236,8 +253,8 @@ It allows to add content at the begining and end of the page, after every link d
`$data` is an array containing: `$data` is an array containing:
- `_LOGGEDIN_`: true if user is logged in, false otherwise. - All templates data, including links.
- All templates data, including links. - [Special data](#special-data)
##### Template placeholders ##### Template placeholders
@ -271,7 +288,8 @@ Allow to add fields in the form, or display elements.
`$data` is an array containing: `$data` is an array containing:
- All templates data. - All templates data.
- [Special data](#special-data)
##### Template placeholders ##### Template placeholders
@ -293,7 +311,8 @@ Allow to add content at the end of the page.
`$data` is an array containing: `$data` is an array containing:
- All templates data. - All templates data.
- [Special data](#special-data)
##### Template placeholders ##### Template placeholders
@ -315,8 +334,8 @@ Allow to add content at the top and bottom of the page.
`$data` is an array containing: `$data` is an array containing:
- `_LOGGEDIN_`: true if user is logged in, false otherwise. - All templates data.
- All templates data. - [Special data](#special-data)
##### Template placeholders ##### Template placeholders
@ -339,8 +358,8 @@ Allow to add content at the top and bottom of the page.
`$data` is an array containing: `$data` is an array containing:
- `_LOGGEDIN_`: true if user is logged in, false otherwise. - All templates data.
- All templates data. - [Special data](#special-data)
##### Template placeholders ##### Template placeholders
@ -368,8 +387,8 @@ Allow to add content at the top and bottom of the page.
`$data` is an array containing: `$data` is an array containing:
- `_LOGGEDIN_`: true if user is logged in, false otherwise. - All templates data.
- All templates data. - [Special data](#special-data)
##### Template placeholders ##### Template placeholders
@ -394,8 +413,8 @@ Allow to add content at the top and bottom of the page, the bottom of each link
`$data` is an array containing: `$data` is an array containing:
- `_LOGGEDIN_`: true if user is logged in, false otherwise. - All templates data, including links.
- All templates data, including links. - [Special data](#special-data)
##### Template placeholders ##### Template placeholders
@ -420,9 +439,8 @@ Allow to add tags in the feed, either in the header or for each items. Items (li
`$data` is an array containing: `$data` is an array containing:
- `_LOGGEDIN_`: true if user is logged in, false otherwise. - All templates data, including links.
- `_PAGE_`: containing either `rss` or `atom`. - [Special data](#special-data)
- All templates data, including links.
##### Template placeholders ##### Template placeholders
@ -456,6 +474,8 @@ Allow to alter the link being saved in the datastore.
- created - created
- updated - updated
Also [special data](#special-data).
#### delete_link #### delete_link
@ -465,7 +485,7 @@ Allow to execute any action before the link is actually removed from the datasto
##### Data ##### Data
`$data` is an array containing the link being saved: `$data` is an array containing the link being deleted:
- id - id
- title - title
@ -477,6 +497,7 @@ Allow to execute any action before the link is actually removed from the datasto
- created - created
- updated - updated
Also [special data](#special-data).
#### save_plugin_parameters #### save_plugin_parameters
@ -492,6 +513,7 @@ For example it is used to update the CSS file of the `default_colors` plugins.
So if the plugin has a parameter called `MYPLUGIN_PARAMETER`, So if the plugin has a parameter called `MYPLUGIN_PARAMETER`,
the array will contain an entry with `MYPLUGIN_PARAMETER` as a key. the array will contain an entry with `MYPLUGIN_PARAMETER` as a key.
Also [special data](#special-data).
## Guide for template designer ## Guide for template designer