We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Dynamically loaded plugin based on file existence

I would like to create a plugin system where a plugin is simply a file located in specific directory. Contradicting the autoloading principle, I want all files in that directory to be include-d with corresponding class (still 1 file 1 class, implementing an interface I created) instance created and registered to events the class wants to listen to by using EventsManager.

In my previous project, I use glob() to iterate all files found, then use file_get_contents() to extract the contents and finally preg_match() to get the class name along with checking to see if the class implements the interface (regex: class\s+(\w+)\s+implements\s+TheInterfaceClass). This is not only ugly and cumbersome, but also doesn't cover all possible legal coding styles.

So, I would like to ask if there's either any better way to get what I want, or another plugin system approach where (un)installation of a plugin is a matter of moving the plugin file out of a specific directory. Thank you.

edited Nov '16

How about creating Composer hook and get all Plugin bootstraps from \Some\Standard\Plugin\Namespace ?

I think I can make a good use of get_declared_classes() for the latter, while still using glob() to iterate the files but this time I'll just blindly include all of them instead of using composer hooks. At least you give me a cleaner way of doing the hack. Thanks.