Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

hook.rb 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2017 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. raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton)
  28. @@listener_classes << klass
  29. clear_listeners_instances
  30. end
  31. # Returns all the listener instances.
  32. def listeners
  33. @@listeners ||= @@listener_classes.collect {|listener| listener.instance}
  34. end
  35. # Returns the listener instances for the given hook.
  36. def hook_listeners(hook)
  37. @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)}
  38. end
  39. # Clears all the listeners.
  40. def clear_listeners
  41. @@listener_classes = []
  42. clear_listeners_instances
  43. end
  44. # Clears all the listeners instances.
  45. def clear_listeners_instances
  46. @@listeners = nil
  47. @@hook_listeners = {}
  48. end
  49. # Calls a hook.
  50. # Returns the listeners response.
  51. def call_hook(hook, context={})
  52. [].tap do |response|
  53. hls = hook_listeners(hook)
  54. if hls.any?
  55. hls.each {|listener| response << listener.send(hook, context)}
  56. end
  57. end
  58. end
  59. end
  60. # Helper module included in ApplicationHelper and ActionController so that
  61. # hooks can be called in views like this:
  62. #
  63. # <%= call_hook(:some_hook) %>
  64. # <%= call_hook(:another_hook, :foo => 'bar') %>
  65. #
  66. # Or in controllers like:
  67. # call_hook(:some_hook)
  68. # call_hook(:another_hook, :foo => 'bar')
  69. #
  70. # Hooks added to views will be concatenated into a string. Hooks added to
  71. # controllers will return an array of results.
  72. #
  73. # Several objects are automatically added to the call context:
  74. #
  75. # * project => current project
  76. # * request => Request instance
  77. # * controller => current Controller instance
  78. # * hook_caller => object that called the hook
  79. #
  80. module Helper
  81. def call_hook(hook, context={})
  82. if is_a?(ActionController::Base)
  83. default_context = {:controller => self, :project => @project, :request => request, :hook_caller => self}
  84. Redmine::Hook.call_hook(hook, default_context.merge(context))
  85. else
  86. default_context = { :project => @project, :hook_caller => self }
  87. default_context[:controller] = controller if respond_to?(:controller)
  88. default_context[:request] = request if respond_to?(:request)
  89. Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ').html_safe
  90. end
  91. end
  92. end
  93. end
  94. end