Details

    • Type: Story
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 5.3.0-b1
    • Component/s: None
    • Labels:

      Description

      Our current events (e.g. Services::add) typically only trigger after the service is created.

      We should add an option for events to be triggered Before an action and After an action. Existing events will then need to be deprecated and setup to perform the Before or After action to maintain backward compatibility.

      1. We will need Before and After events available for all of the current events

        Activity

        Hide
        jonathan Jonathan Reissmueller added a comment -

        I'd love to see:

        1. A simplified way to call events
        2. Plugins having more control over the input and output of methods with events

        To that end I was thinking we could add the following method to the AppModel class:

            /**
             * Executes the given event
             *
             * @param string $event_name The name of the event to trigger
             * @param array $params The list of parameters to submit to the event handlers
             * @return array The list of parameters that were submitted along with any modifications made to them
             *  by the event handlers.  In addition a __return__ item is included with the return array.
             */
            protected function executeAndParseEvent($event_name, $params) {
                $eventFactory = $this->getFromContainer('util.events');
                $eventListener = $eventFactory->listener();
                $eventListener->register($event_name);
                $event = $eventListener->trigger(
                    $eventFactory->event($event_name, $params)
                );
        
                $return = ['__return__' => $event->getReturnValue()];
                $event_params = $event->getParams();
                if (is_array($event_params)) {
                    foreach ($event_params as $event_param => $data) {
                        if (array_key_exists($event_param, $params)) {
                            $return[$event_param] = $data;
                        }
                    }
                }
        
                return $return;
            }
        

        Then to trigger an event and allow it to update the submitted data, an event could be called as such:

         		extract($this->executeAndParseEvent('Packages.add', compact(['package_id', 'vars'])));
        

        The call to extract() would override the submitted variables with the updated version (or the same thing if no updates were made).

        Show
        jonathan Jonathan Reissmueller added a comment - I'd love to see: A simplified way to call events Plugins having more control over the input and output of methods with events To that end I was thinking we could add the following method to the AppModel class: /** * Executes the given event * * @param string $event_name The name of the event to trigger * @param array $params The list of parameters to submit to the event handlers * @ return array The list of parameters that were submitted along with any modifications made to them * by the event handlers. In addition a __return__ item is included with the return array. */ protected function executeAndParseEvent($event_name, $params) { $eventFactory = $ this ->getFromContainer('util.events'); $eventListener = $eventFactory->listener(); $eventListener->register($event_name); $event = $eventListener->trigger( $eventFactory->event($event_name, $params) ); $ return = ['__return__' => $event->getReturnValue()]; $event_params = $event->getParams(); if (is_array($event_params)) { foreach ($event_params as $event_param => $data) { if (array_key_exists($event_param, $params)) { $ return [$event_param] = $data; } } } return $ return ; } Then to trigger an event and allow it to update the submitted data, an event could be called as such: extract($ this ->executeAndParseEvent('Packages.add', compact(['package_id', 'vars']))); The call to extract() would override the submitted variables with the updated version (or the same thing if no updates were made).

          People

          • Assignee:
            jonathan Jonathan Reissmueller
            Reporter:
            tyson Tyson Phillips (Inactive)
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Fix Release Date:
              27/Jan/22

              Time Tracking

              Estimated:
              Original Estimate - Not Specified
              Not Specified
              Remaining:
              Remaining Estimate - 0 minutes
              0m
              Logged:
              Time Spent - 7 hours, 11 minutes
              7h 11m

                Agile