<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def apply(self, event, document, *args, **kwargs):
 """ Applies all middleware functions registered against the given event in order to the given document. """

        for fn in self._handler_dict.get(event, []):
            fn(document, *args, **kwargs)