Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module Redmine
  19. module Hook
  20. @@listener_classes = []
  21. @@listeners = nil
  22. @@hook_listeners = {}
  23. class << self
  24. # Adds a listener class.
  25. # Automatically called when a class inherits from Redmine::Hook::Listener.
  26. def add_listener(klass)
  27. unless klass.included_modules.include?(Singleton)
  28. raise "Hooks must include Singleton module."
  29. end
  30. @@listener_classes << klass
  31. clear_listeners_instances
  32. end
  33. # Returns all the listener instances.
  34. def listeners
  35. @@listeners ||= @@listener_classes.collect {|listener| listener.instance}
  36. end
  37. # Returns the listener instances for the given hook.
  38. def hook_listeners(hook)
  39. @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)}
  40. end
  41. # Clears all the listeners.
  42. def clear_listeners
  43. @@listener_classes = []
  44. clear_listeners_instances
  45. end
  46. # Clears all the listeners instances.
  47. def clear_listeners_instances
  48. @@listeners = nil
  49. @@hook_listeners = {}
  50. end
  51. # Calls a hook.
  52. # Returns the listeners response.
  53. def call_hook(hook, context={})
  54. [].tap do |response|
  55. hls = hook_listeners(hook)
  56. if hls.any?
  57. hls.each {|listener| response << listener.send(hook, context)}
  58. end
  59. end
  60. end
  61. end
  62. # Helper module included in ApplicationHelper and ActionController so that
  63. # hooks can be called in views like this:
  64. #
  65. # <%= call_hook(:some_hook) %>
  66. # <%= call_hook(:another_hook, :foo => 'bar') %>
  67. #
  68. # Or in controllers like:
  69. # call_hook(:some_hook)
  70. # call_hook(:another_hook, :foo => 'bar')
  71. #
  72. # Hooks added to views will be concatenated into a string. Hooks added to
  73. # controllers will return an array of results.
  74. #
  75. # Several objects are automatically added to the call context:
  76. #
  77. # * project => current project
  78. # * request => Request instance
  79. # * controller => current Controller instance
  80. # * hook_caller => object that called the hook
  81. #
  82. module Helper
  83. def call_hook(hook, context={})
  84. if is_a?(ActionController::Base)
  85. default_context = {:controller => self, :project => @project, :request => request, :hook_caller => self}
  86. Redmine::Hook.call_hook(hook, default_context.merge(context))
  87. else
  88. default_context = {:project => @project, :hook_caller => self}
  89. default_context[:controller] = controller if respond_to?(:controller)
  90. default_context[:request] = request if respond_to?(:request)
  91. Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ').html_safe
  92. end
  93. end
  94. end
  95. end
  96. end