• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
triggerFilter in Event

#1
I added below new method code in Events\Events.php

PHP Code:
   /**
     * Runs through all subscribed methods running them one at a time,
     * until either:
     *  a) All subscribers have finished or
     *  b) a method returns false, at which point execution of subscribers stops.
     *
     * @param $eventName
     * @param $value
     * @param $arguments
     *
     * @return mixed
     */
 
   public static function triggerFilter($eventName$value, ...$arguments)
 
   {
 
       // Read in our Config/events file so that we have them all!
 
       if ( ! self::$initialized) {
 
           self::initialize();
 
       }

 
       $listeners self::listeners($eventName);

 
       foreach ($listeners as $listener) {
 
           $start microtime(true);

 
           $value = static::$simulate === false
                
$listener($value, ...$arguments)
 
               true;

 
           if (CI_DEBUG) {
 
               static::$performanceLog[] = [
 
                   'start' => $start,
 
                   'end' => microtime(true),
 
                   'event' => strtolower($eventName)
 
               ];
 
           }

 
           if ($value === false) {
 
               return false;
 
           }
 
       }

 
       return $value;
 
   


PHP Code:
    public function test_method()
    {
    
    $data = [
            'raw1' => 'Before Text',
            'raw2' => 'aaaa1234',
        ];

    
    \CodeIgniter\Events\Events::on('test_event1', function ($value$data2$data3) {
            $value['raw1'] = 'After event~!';
            $value['raw2'] = strtoupper($value['raw2']) . $data2;

            return $value;
        }, EVENT_PRIORITY_HIGH);

        \CodeIgniter\Events\Events::on('test_event1', function ($value$data2$data3) {
            $value['raw1'] = $value['raw1'] . ' // One more!';
            $value['raw2'] = $value['raw2'] . $data3;

            return $value;
        }, EVENT_PRIORITY_NORMAL);

    
    $data2 = \CodeIgniter\Events\Events::triggerFilter('test_event1'$data'_TITLE''_SUBJECT');

    
    var_dump$data );
    
    echo '<br>';
    
    var_dump$data2 );

    
    ///////////////

        \CodeIgniter\Events\Events::on('another_event', function ($value) {
            $value['raw1'] = 'After event~!';
            $value['raw2'] = strtoupper($value['raw2']) . ' First';

            return $value;
        }, EVENT_PRIORITY_HIGH);

        \CodeIgniter\Events\Events::on('another_event', function ($value) {
            $value['raw1'] = $value['raw1'] . ' // One More!';
            $value['raw2'] = $value['raw2'] . ' Second';

            return $value;
        }, EVENT_PRIORITY_NORMAL);

        $data2 = \CodeIgniter\Events\Events::triggerFilter('another_event'$data);

        echo '<br>';
        var_dump$data );
        echo '<br>';
        var_dump$data2 );
    


Because I need something similar to the filter that Wordpress uses.
What do you think about this?  Please, let me know your opinions.
Reply

#2
Interesting idea, but it has a very real problem that filters expect to be able to modify the request/response or return their own response, and calling a filter within an event negates all of those abilities.

The filter you are trying to replicate from Wordpress is fundamentally different enough from the Controller Filters that CI provides that I don't think they're compatible.

To allow filters on content, you'd be better served creating something new and specific for that use, I think.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#3
(12-03-2018, 07:56 AM)kilishan Wrote: Interesting idea, but it has a very real problem that filters expect to be able to modify the request/response or return their own response, and calling a filter within an event negates all of those abilities.

The filter you are trying to replicate from Wordpress is fundamentally different enough from the Controller Filters that CI provides that I don't think they're compatible.

To allow filters on content, you'd be better served creating something new and specific for that use, I think.

Thank you for your opinion.

For now, I think I'll have to use above code I added. 
That is a quite simple way, if there isn't another simple awesome way.

By the way, CI4 Events concept is very nice. I think this is similar to Action (Hook) concept of WordPress.
So, I just added that triggerFilter (Filter Hook) function.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.