feat: Adding BaseService::updateServicesCache. Adds ablity to update Services Cache at runtime.
Description Supersedes #8894
I need a different method to load Modules. I wanted to set it up so it was more database driven for enabling or disabling a module. This lead to problems with Services not being auto discovered by the buildServicesCache() function. As the autoloader doesn't know those files exist at the point it time it is called. Which results in any static calls to the any services class or service function would return null. So added a function that will update the service cache with and new Classes discovered since initial build.
Apologizes for previous PR. This contains the code, documentation, change log, code comments, and tests.
Checklist:
- [x] Securely signed commits
- [x] Component(s) with PHPDoc blocks, only if necessary or adds value
- [ ] Unit testing, with >80% coverage
- [x] User guide updated
- [x] Conforms to style guide
@paulbalandan In this PR branch, phpstan-codeigniter will be downgraded. Why?
(4.6-servicesreload $)$ composer update
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 installs, 1 update, 0 removals
- Downgrading codeigniter/phpstan-codeigniter (v1.4.3.70400 => v1.4.2.70400)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
- Downgrading codeigniter/phpstan-codeigniter (v1.4.3.70400 => v1.4.2.70400): Extracting archive
Generating optimized autoload files
64 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
phpstan/extension-installer: Extensions installed
> codeigniter/phpstan-codeigniter: installed
> phpstan/phpstan-strict-rules: installed
infection/extension-installer: No extensions found
> CodeIgniter\ComposerScripts::postUpdate
> composer update --working-dir=tools/phpmetrics
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
infection/extension-installer: No extensions found
No security vulnerability advisories found.
No security vulnerability advisories found.
Thank you for sending this! No worry about the previous PR. This PR is quite neat.
I wanted to set it up so it was more database driven for enabling or disabling a module.
Out of curiosity, what is your use case? Why do you need to manage modules in a database? What do the modules do?
So I'm currently designing ERP system for a customer of ours. With the project specs they gave. Everything needed to be modular and event driven for interactions between them. This is because they own multiple companies and each one has similar, but different needs and processes flows.
However they also wanted to be able to turn on or off a module at will for the companies. So easiest way I figured I could approach this was to create a service that would determine which modules should get loaded and control their event priorities.
I don't know if this best approach to fit there needs or how much of a impact it will have on the system as whole. So while setting up to do some testing. I decided put a call to this service in the routes file for right now. In the test I just used
service('autoload')->addNamespace('ModuleNameSpace', APPPATH . '../ModulePath');
Which is fine it gets the module namespace discoverable. However if that module had Config/Services file you could not use the service() helper. It would just return null.
After drilling down I saw that classname cache only ever generates once unless you reset. So just figured why not add a "refresh" to look for anything new.
I'm not sure, but isn't it better to just add a method to delete the cache?
Sorry, meant to pull the changes in to develop and missed I didn't switch my branch over. Reverted that merge.
If you empty $serviceNames, does that not break all the service functionality? Or are you talking about a different cache?
We don't use git merge in PR branches.
Use git rebase instead.
See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/workflow.md#updating-your-branch
If you empty $serviceNames, does that not break all the service functionality? Or are you talking about a different cache?
I mean, just add a method like clearServicesCache() that empties $serviceNames and sets $discovered to false.
I sent another PR #9012
Closed by #9012