PluginStrategy.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * @author Open Mind
  4. * @date 21/06/2018
  5. * @time 12:34
  6. */
  7. namespace OpenMind\System;
  8. use Exception;
  9. /**
  10. * Interface IPluginStrategy
  11. * Main interface for plugin system, it use Strategy Pattern.
  12. *
  13. * @package OpenMind\System
  14. */
  15. interface IPluginStrategy
  16. {
  17. public function __construct();
  18. /**
  19. * Class version
  20. * @return string
  21. */
  22. public function getVersion(): string;
  23. /**
  24. * Class name
  25. * @return string
  26. */
  27. public function getName(): string;
  28. /**
  29. * Set method route
  30. *
  31. * @param array|string $pattern
  32. * @param string $handler
  33. * @return mixed
  34. */
  35. public function AddRoute($pattern, string $handler);
  36. }
  37. /**
  38. * Class PluginStrategy
  39. * Basically every plugin must extend this class to work
  40. *
  41. */
  42. class PluginStrategy implements IPluginStrategy
  43. {
  44. static private $timeLoadStart = null;
  45. public function __construct()
  46. {
  47. throw new Exception("Plugin " . $this->getName() . ' doesn\'t have a __constructor() function. Please provide one.');
  48. }
  49. /**
  50. * Return Class name, useful when a list of installed plugins is need
  51. *
  52. * @return string
  53. */
  54. public function getName(): string
  55. {
  56. return get_class($this);
  57. }
  58. static function setTsStartLoad()
  59. {
  60. self::$timeLoadStart = round(microtime(true) * 1000);
  61. }
  62. public function __destruct()
  63. {
  64. global $f3, $frameworkUtils;
  65. if ($f3->get('DEBUG') > 2) {
  66. $timeEnd = round(microtime(true) * 1000);
  67. $totalMS = $timeEnd - self::$timeLoadStart;
  68. $className = get_called_class();
  69. $frameworkUtils->getLogger()->write("Class $className loaded in {$totalMS}ms", 'd/m/Y H:i:s');
  70. }
  71. }
  72. /**
  73. * Return default version for plugins that doesn't have one
  74. *
  75. * @return string
  76. */
  77. public function getVersion(): string
  78. {
  79. return '1.0';
  80. }
  81. /**
  82. * Add URL routing to the Route chain
  83. *
  84. * @param array|string $pattern
  85. * @param string $handler
  86. */
  87. public function AddRoute($pattern, string $handler)
  88. {
  89. global $f3;
  90. $f3->route($pattern, $handler, 0); // TTL 60s
  91. }
  92. /**
  93. * Add menu to application for the plugin, if another menu with same $menuName is present, the last added has priority on other.
  94. *
  95. * @param string $menuName Name of menu to add
  96. * @param array $options the struct of OMLink Menu
  97. *
  98. * <br/><br/>
  99. * [<br/>
  100. * &nbsp;&nbsp;'menu_name' => 'Bind',<br/>
  101. * &nbsp;&nbsp;'url' => '/bind',<br/>
  102. * &nbsp;&nbsp;'isDisabled' => false<br/>
  103. * ]
  104. */
  105. public function AddMenuItem(string $menuName, array $options)
  106. {
  107. global $f3;
  108. $defaults = [
  109. 'url' => '',
  110. 'isDisabled' => false,
  111. 'image' => '',
  112. 'permissionName' => null,
  113. 'order' => 99999
  114. ];
  115. $newMenu = array_merge($defaults, $options);
  116. $globalMenuUpdated = $f3->get('GLOBALMENU');
  117. $globalMenuUpdated[$menuName] = $newMenu;
  118. // If user logged check permission
  119. $user = User::Restore();
  120. if (!$user->dry()) {
  121. $hasMenuPermissionRequirements = !is_null($newMenu['permissionName']);
  122. if (!$hasMenuPermissionRequirements || ($hasMenuPermissionRequirements && $user->hasPermission($newMenu['permissionName']))) {
  123. $f3->set('GLOBALMENU', $globalMenuUpdated);
  124. }
  125. }
  126. }
  127. }