You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.4 KiB

3 years ago
  1. <?php
  2. namespace App\Handlers;
  3. use Exception;
  4. /**
  5. * Factory to getting Handler-Classes
  6. *
  7. *
  8. */
  9. class HandlerFactory
  10. {
  11. /**
  12. * getting handler class if exists
  13. *
  14. * @param String $className
  15. * @return Mixed
  16. *
  17. */
  18. private static function getHandlerClass(String $prefix, String $className)
  19. {
  20. $result = NULL;
  21. // create class
  22. $class = 'App\\Handlers\\'.$prefix.'\\'.$className);
  23. // if not found check for custom handlers
  24. if (class_exists($class)) {
  25. $result = new $class();
  26. } else {
  27. // create class
  28. $class = 'CustomHandlers\\'.$prefix.'\\'.$className;
  29. if (class_exists($class)) {
  30. $result = new $class();
  31. }
  32. }
  33. // if there is not class throw Exception
  34. if ($result === NULL) {
  35. thrown new Exception($prefix.'\\'.$className.' not Found!');
  36. }
  37. return $result;
  38. }
  39. /**
  40. *
  41. *
  42. * @param String $className
  43. * @return Mixed
  44. *
  45. */
  46. public static function getResponseHandlerClass(String $className)
  47. {
  48. return self::getHandlerClass('Response', String $className)
  49. }
  50. /**
  51. *
  52. *
  53. * @param String $className
  54. * @return Mixed
  55. *
  56. */
  57. public static function getActionHandlerClass(String $className)
  58. {
  59. return self::getHandlerClass('Actions', String $className)
  60. }
  61. }