| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- /**
- * @author Open Mind
- * @date 21/06/2018
- * @time 12:34
- */
- namespace OpenMind\System;
- use Exception;
- /**
- * Interface IPluginStrategy
- * Main interface for plugin system, it use Strategy Pattern.
- *
- * @package OpenMind\System
- */
- interface IPluginStrategy
- {
- public function __construct();
- /**
- * Class version
- * @return string
- */
- public function getVersion(): string;
- /**
- * Class name
- * @return string
- */
- public function getName(): string;
- /**
- * Set method route
- *
- * @param array|string $pattern
- * @param string $handler
- * @return mixed
- */
- public function AddRoute($pattern, string $handler);
- }
- /**
- * Class PluginStrategy
- * Basically every plugin must extend this class to work
- *
- */
- class PluginStrategy implements IPluginStrategy
- {
- static private $timeLoadStart = null;
- public function __construct()
- {
- throw new Exception("Plugin " . $this->getName() . ' doesn\'t have a __constructor() function. Please provide one.');
- }
- /**
- * Return Class name, useful when a list of installed plugins is need
- *
- * @return string
- */
- public function getName(): string
- {
- return get_class($this);
- }
- static function setTsStartLoad()
- {
- self::$timeLoadStart = round(microtime(true) * 1000);
- }
- public function __destruct()
- {
- global $f3, $frameworkUtils;
- if ($f3->get('DEBUG') > 2) {
- $timeEnd = round(microtime(true) * 1000);
- $totalMS = $timeEnd - self::$timeLoadStart;
- $className = get_called_class();
- $frameworkUtils->getLogger()->write("Class $className loaded in {$totalMS}ms", 'd/m/Y H:i:s');
- }
- }
- /**
- * Return default version for plugins that doesn't have one
- *
- * @return string
- */
- public function getVersion(): string
- {
- return '1.0';
- }
- /**
- * Add URL routing to the Route chain
- *
- * @param array|string $pattern
- * @param string $handler
- */
- public function AddRoute($pattern, string $handler)
- {
- global $f3;
- $f3->route($pattern, $handler, 0); // TTL 60s
- }
- /**
- * Add menu to application for the plugin, if another menu with same $menuName is present, the last added has priority on other.
- *
- * @param string $menuName Name of menu to add
- * @param array $options the struct of OMLink Menu
- *
- * <br/><br/>
- * [<br/>
- * 'menu_name' => 'Bind',<br/>
- * 'url' => '/bind',<br/>
- * 'isDisabled' => false<br/>
- * ]
- */
- public function AddMenuItem(string $menuName, array $options)
- {
- global $f3;
- $defaults = [
- 'url' => '',
- 'isDisabled' => false,
- 'image' => '',
- 'permissionName' => null,
- 'order' => 99999
- ];
- $newMenu = array_merge($defaults, $options);
- $globalMenuUpdated = $f3->get('GLOBALMENU');
- $globalMenuUpdated[$menuName] = $newMenu;
- // If user logged check permission
- $user = User::Restore();
- if (!$user->dry()) {
- $hasMenuPermissionRequirements = !is_null($newMenu['permissionName']);
- if (!$hasMenuPermissionRequirements || ($hasMenuPermissionRequirements && $user->hasPermission($newMenu['permissionName']))) {
- $f3->set('GLOBALMENU', $globalMenuUpdated);
- }
- }
- }
- }
|