PluginStrategy.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @author Open Mind
  4. * @date 21/06/2018
  5. * @time 12:34
  6. */
  7. namespace OpenMind\System;
  8. /**
  9. * Interface IPluginStrategy
  10. * Main interface for plugin system, it use Strategy Pattern.
  11. *
  12. * @package OpenMind\System
  13. */
  14. interface IPluginStrategy
  15. {
  16. public function __construct();
  17. /**
  18. * Class version
  19. * @return string
  20. */
  21. public function getVersion(): string;
  22. /**
  23. * Class name
  24. * @return string
  25. */
  26. public function getName(): string;
  27. /**
  28. * Set method route
  29. *
  30. * @param array|string $pattern
  31. * @param string $handler
  32. * @return mixed
  33. */
  34. public function AddRoute($pattern, string $handler);
  35. }
  36. /**
  37. * Class PluginStrategy
  38. * Basically every plugin must extend this class to work
  39. *
  40. */
  41. class PluginStrategy implements IPluginStrategy
  42. {
  43. public function __construct()
  44. {
  45. throw new \Exception("Plugin " . $this->getName() . ' doesn\'t have a __constructor() function. Please provide one.');
  46. }
  47. /**
  48. * Return default version for plugins that doesn't have one
  49. *
  50. * @return string
  51. */
  52. public function getVersion(): string
  53. {
  54. return '1.0';
  55. }
  56. /**
  57. * Return Class name, usefull when a list of installed plugins is need
  58. *
  59. * @return string
  60. */
  61. public function getName(): string
  62. {
  63. return get_class($this);
  64. }
  65. public function AddRoute($pattern, string $handler)
  66. {
  67. \F3::route($pattern, $handler, 0); // TTL 60s
  68. }
  69. }