• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CodeIgniter4 - Event pass by ???

#1
Started to test a few things in CI4 and was wondering what the best way to handle modifying the input to a Event.
Seems to me perhaps just passing the values by reference might solve the problem?

PHP Code:
public static function trigger($eventName,&...$arguments): bool 

For example if I have:


PHP Code:
$a 'this is something a trigger needs to modify maybe (let the trigger figure it out)';

$boolean = events::trigger('modify_text',$a); 


What would be the best way to modify the $a text value? What if I have multiple modify_text triggers with multiple priority levels? Shouldn't everyone in "priority" be able to take a shot at it? maybe 1 trigger adds <p> if it contains "this" as the first word and then later down the event "list" it adds <b> if it doesn't already start with a html element. 

Like I wrote about prefixing the trigger functions ...$arguments with & should do the trick???

I look forward to your input.

DMyers
Reply

#2
I figured I could simply extend the Events Class to add this feature (https://bcit-ci.github.io/CodeIgniter4/e...re-classes) but it looks like this library is autoloaded using the PSR4 classmap array. 

Simply adding:


PHP Code:
$classmap = [
 ...
 
'CodeIgniter\Events\Events' => APPPATH 'Libraries/Events.php',
 ...
]; 

to the configuration file Autoload (/application/Config/Autoload.php)

causes a issue because when I extend \CodeIgniter\Events\Events with my Events class


PHP Code:
<?php 

namespace App\Libraries;

class 
Events extends \CodeIgniter\Events\Events {




It can't find \CodeIgniter\Events\Events because the /application/Config/Autoload.php classmap already points to my new APPPATH . 'Libraries/Events.php' file?

How would one extend the Events or really any class which users are loading using the 


PHP Code:
use CodeIgniter\Events\Events

Syntax?

DMyers
Reply

#3
try to require the \CodeIgniter\Events\Events file on your lib class file
Reply

#4
Actually, there is some cleanup on that Autoload classmap file that needs to be done. Like most of it needs to be ripped out, I believe. It was originally an experiment to see if it increased the performance at all having them all listed, but it doesn't seem to.

In general, though, you can't really override any class loaded that way. That's why we provide the Services for most things. We might need to re-look at how Events is loaded in.

However, there's nothing saying you have to extend it for your own use. If you've got something that's replacing it, just replace and use that for your own event system, leaving Events to CodeIgniter's internal hooks, if that works for you.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#5
(10-02-2018, 02:07 PM)kilishan Wrote: Actually, there is some cleanup on that Autoload classmap file that needs to be done. Like most of it needs to be ripped out, I believe. It was originally an experiment to see if it increased the performance at all having them all listed, but it doesn't seem to.

In general, though, you can't really override any class loaded that way. That's why we provide the Services for most things. We might need to re-look at how Events is loaded in.

However, there's nothing saying you have to extend it for your own use. If you've got something that's replacing it, just replace and use that for your own event system, leaving Events to CodeIgniter's internal hooks, if that works for you.

kilishan, thank you for your excellent reply and thank you for all of your hardwork on CodeIgniter 4.
What I was getting at with making events  arguments pass by reference was that instead of a simply triggering something like hooks are today. 
Adding the pass by reference would allow other CodeIgniter 4 developers to use events in more of a universal way to modify arguments sent in without the need to download another "events" class to provide this feature.

DMyers
Reply

#6
(10-03-2018, 07:18 AM)dmyers Wrote: kilishan, thank you for your excellent reply and thank you for all of your hardwork on CodeIgniter 4.
What I was getting at with making events  arguments pass by reference was that instead of a simply triggering something like hooks are today. 
Adding the pass by reference would allow other CodeIgniter 4 developers to use events in more of a universal way to modify arguments sent in without the need to download another "events" class to provide this feature.

DMyers

That's a good idea. Something worth looking into.

Not sure what you're using it for, but would Controller Filters help out.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#7
(10-03-2018, 10:32 AM)kilishan Wrote:
(10-03-2018, 07:18 AM)dmyers Wrote: kilishan, thank you for your excellent reply and thank you for all of your hardwork on CodeIgniter 4.
What I was getting at with making events  arguments pass by reference was that instead of a simply triggering something like hooks are today. 
Adding the pass by reference would allow other CodeIgniter 4 developers to use events in more of a universal way to modify arguments sent in without the need to download another "events" class to provide this feature.

DMyers

That's a good idea. Something worth looking into.

Not sure what you're using it for, but would Controller Filters help out.

Kilishan, I was thinking more along the lines of Jamie Rumbelow's old MY_Model code where you would call a before_update or after_update event which could modify it's input.

https://github.com/jamierumbelow/codeign..._Model.php

Another "event" I personally use in my applications is:


PHP Code:
ci('event')->trigger('nav.library.html',$array); 

Which allows other listeners the opportunity to modify the html as needed  


PHP Code:
$this->event->register('nav.library.html',function(&$array){
     if (
$array['user'] == 'nobody') {
        
$array['user'] == 'guest';
     }
  });


This example could probably be rolled into a after filter (not sure how you would get the array)??? but, then how would you dynamically add it to the application/Config/Filters.php file only on pages where needed.

I'm also not sure removeListener(...) works with a Closure? Which you can use as per line 18.


I updated my CodeIgniter 3 Library to add some of your great ideas. You can see it here.

https://github.com/ProjectOrangeBox/oran.../Event.php

I like a few of your ideas


Before I added them I had this. 

https://github.com/ProjectOrangeBox/oran.../Event.php
Reply

#8
Well - we have part of that covered anyway. Seems docs are pretty lacking on it though.

I would definitely welcome a PR to make Events pass by reference. Or at least an issue created for that so I remember to get to it lol.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#9
(10-03-2018, 01:09 PM)kilishan Wrote: Well - we have part of that covered anyway. Seems docs are pretty lacking on it though.

I would definitely welcome a PR to make Events pass by reference. Or at least an issue created for that so I remember to get to it lol.

Kilishan, I will download, change the code and send a pull request. I will also try to check the unit tests and see if I can add the needed work.

Don Myers
Reply

#10
Sorry Kilishan, I tried to do the update myself but ran into problems signing my code. I did fill a bug report on github coving the issue and the fix.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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