Plugin.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace support;
  3. class Plugin
  4. {
  5. public static function install($event)
  6. {
  7. static::findHepler();
  8. $operation = $event->getOperation();
  9. $autoload = method_exists($operation, 'getPackage') ? $operation->getPackage()->getAutoload() : $operation->getTargetPackage()->getAutoload();
  10. if (!isset($autoload['psr-4'])) {
  11. return;
  12. }
  13. $namespace = key($autoload['psr-4']);
  14. $install_function = "\\{$namespace}Install::install";
  15. $plugin_const = "\\{$namespace}Install::WEBMAN_PLUGIN";
  16. if (defined($plugin_const) && is_callable($install_function)) {
  17. $install_function();
  18. }
  19. }
  20. public static function update($event)
  21. {
  22. static::install($event);
  23. }
  24. public static function uninstall($event)
  25. {
  26. static::findHepler();
  27. $autoload = $event->getOperation()->getPackage()->getAutoload();
  28. if (!isset($autoload['psr-4'])) {
  29. return;
  30. }
  31. $namespace = key($autoload['psr-4']);
  32. $uninstall_function = "\\{$namespace}Install::uninstall";
  33. $plugin_const = "\\{$namespace}Install::WEBMAN_PLUGIN";
  34. if (defined($plugin_const) && is_callable($uninstall_function)) {
  35. $uninstall_function();
  36. }
  37. }
  38. protected static function findHepler()
  39. {
  40. // Plugin.php in vendor
  41. $file = __DIR__ . '/../../../../../support/helpers.php';
  42. if (is_file($file)) {
  43. require_once $file;
  44. return;
  45. }
  46. // Plugin.php in webman
  47. require_once __DIR__ . '/helpers.php';
  48. }
  49. }