9 namespace Fossology\Lib\Plugin;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpFoundation\Response;
18 use Symfony\Component\HttpFoundation\Session\Session;
23 const PERMISSION =
"permission";
24 const REQUIRES_LOGIN =
"requiresLogin";
25 const ENABLE_MENU =
"ENABLE_MENU";
26 const LEVEL =
"level";
27 const DEPENDENCIES =
"dependencies";
28 const INIT_ORDER =
"initOrder";
29 const MENU_LIST =
"menuList";
30 const MENU_ORDER =
"menuOrder";
31 const MENU_TARGET =
"menuTarget";
32 const TITLE =
"title";
49 private $version =
"1.0";
55 private $requiresLogin =
true;
57 private $PluginLevel = 10;
59 private $dependencies = array();
60 private $InitOrder = 0;
62 private $MenuList = NULL;
63 private $MenuOrder = 0;
64 private $MenuTarget = NULL;
66 public function __construct($name, $parameters = array())
68 if ($name ===
null || $name ===
"") {
69 throw new \InvalidArgumentException(
"plugin requires a name");
72 foreach ($parameters as $key => $value) {
73 $this->setParameter($key, $value);
77 $this->container = $container;
78 $this->session = $this->
getObject(
'session');
79 $this->renderer = $this->
getObject(
'twig.environment');
80 $this->logger = $this->
getObject(
'logger');
82 $this->microMenu = $this->
getObject(
'ui.component.micromenu');
85 private function setParameter($key, $value)
89 $this->title = $value;
92 case self::PERMISSION:
93 $this->permission = $value;
96 case self::REQUIRES_LOGIN:
97 $this->requiresLogin = $value;
101 $this->PluginLevel = $value;
104 case self::DEPENDENCIES:
105 $this->dependencies = $value;
108 case self::INIT_ORDER:
109 $this->InitOrder = $value;
112 case self::MENU_LIST:
113 $this->MenuList = $value;
116 case self::MENU_ORDER:
117 $this->MenuOrder = $value;
120 case self::MENU_TARGET:
121 $this->MenuTarget = $value;
125 throw new \Exception(
"unhandled parameter $key in module " . $this->name);
142 return $this->version;
158 return $this->requiresLogin;
166 return $this->dependencies;
174 return $this->PluginLevel;
182 return $this->permission;
190 return PLUGIN_STATE_READY;
198 return $this->InitOrder;
202 public function getNoMenu()
212 if (isset($this->MenuList) && (!$this->requiresLogin || $this->isLoggedIn())) {
213 menu_insert(
"Main::" . $this->MenuList, $this->MenuOrder, $this->name, $this->name);
222 $request = Request::createFromGlobals();
223 $request->setSession($this->session);
225 $this->checkPrerequisites();
227 $startTime = microtime(
true);
228 $response = $this->
handle($request);
229 $response->prepare($request);
230 $this->logger->debug(sprintf(
"handle request in %.3fs", microtime(
true) - $startTime));
240 return $this->container->get($name);
243 public function preInstall()
248 public function postInstall()
252 public function unInstall()
256 public function execute()
258 $startTime = microtime(
true);
262 $this->logger->debug(sprintf(
"prepare response in %.3fs", microtime(
true) - $startTime));
271 protected abstract function handle(Request $request);
279 protected function render($templateName, $vars =
null, $headers =
null)
281 if ($this->requiresLogin && !$this->isLoggedIn()) {
282 new Response(
"permission denied", Response::HTTP_FORBIDDEN, array(
"contentType" =>
"text/plain"));
285 $startTime = microtime(
true);
287 $content = $this->renderer->load($templateName)
290 $this->logger->debug(sprintf(
"%s: render response in %.3fs", get_class($this), microtime(
true) - $startTime));
298 public function isLoggedIn()
303 private function checkPrerequisites()
305 if ($this->requiresLogin && !$this->isLoggedIn()) {
306 throw new \Exception(
"not allowed without login");
309 foreach ($this->dependencies as $dependency) {
310 $id = plugin_find_id($dependency);
313 throw new \Exception(
"unsatisfied dependency '$dependency' in module '" . $this->
getName() .
"'");
324 'Content-type' =>
'text/html',
325 'Pragma' =>
'no-cache',
326 'Cache-Control' =>
'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0',
327 'Expires' =>
'Expires: Thu, 19 Nov 1981 08:52:00 GMT');
337 $metadata =
"<meta name='description' content='The study of Open Source'>\n";
338 $metadata .=
"<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>\n";
339 $metadata .=
"<meta name='viewport' content='width=device-width,initial-scale=1.0'>\n";
341 $vars[
'metadata'] = $metadata;
343 if (!empty($this->title)) {
344 $vars[self::TITLE] = htmlentities($this->title);
347 $styles =
"<link rel='stylesheet' href='css/jquery-ui.css'>\n";
348 $styles .=
"<link rel='stylesheet' href='css/select2.min.css'>\n";
349 $styles .=
"<link rel='stylesheet' href='css/jquery.dataTables.css'>\n";
350 $styles .=
"<link rel='stylesheet' href='css/fossology.css'>\n";
351 $styles .=
"<link rel='stylesheet' href='css/bootstrap/bootstrap.min.css'>\n";
352 $styles .=
"<link rel='stylesheet' href='css/bootstrap-icons.css'>\n";
353 $styles .=
"<link rel='icon' type='image/x-icon' href='favicon.ico'>\n";
354 $styles .=
"<link rel='shortcut icon' type='image/x-icon' href='favicon.ico'>\n";
356 $styles .= $this->
menu->OutputCSS();
358 $vars[
'styles'] = $styles;
360 $vars[
'menu'] = $this->
menu->Output($this->title);
363 if (array_key_exists(
'BUILD', $SysConf)) {
364 $vars[
'versionInfo'] = array(
365 'version' => $SysConf[
'BUILD'][
'VERSION'],
366 'buildDate' => $SysConf[
'BUILD'][
'BUILD_DATE'],
367 'commitHash' => $SysConf[
'BUILD'][
'COMMIT_HASH'],
368 'commitDate' => $SysConf[
'BUILD'][
'COMMIT_DATE'],
369 'branchName' => $SysConf[
'BUILD'][
'BRANCH']
376 protected function mergeWithDefault($vars)
381 protected function flushContent($content)
383 return $this->
render(
"include/base.html.twig",$this->mergeWithDefault(array(
"content"=>$content)));
393 if (method_exists($this, ($method =
'get' . ucwords($name)))) {
394 return $this->$method();
396 throw new \Exception(
"property '$name' not found in module " . $this->name);
400 function __toString()
Contains the constants and helpers for authentication of user.
RegisterMenus()
Customize submenus.
render($templateName, $vars=null, $headers=null)
getStringRepresentation($vars, $classname)