Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding BaseService::updateServicesCache. Adds ablity to update Services Cache at runtime. #8896

Open
wants to merge 4 commits into
base: 4.6
Choose a base branch
from

Conversation

pyromanci
Copy link

@pyromanci pyromanci commented May 15, 2024

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:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added 4.6 enhancement PRs that improve existing functionalities labels May 16, 2024
@kenjis
Copy link
Member

kenjis commented May 16, 2024

@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.

@@ -75,6 +75,9 @@ Helpers and Functions

Others
======
- **Services:** Added ``BaseService::updateServicesCache`` method to allow
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **Services:** Added ``BaseService::updateServicesCache`` method to allow
+
- **Services:** Added ``BaseService::updateServicesCache()`` method to allow

Comment on lines +175 to +176
There may be times when you need to have Service Discovery refresh it's cache after the inital autoload proccess. This can be done by running :php:meth:`Config\\Services::updateServicesCache()`.
This will force the service discovery to re-scan the directories for any new services files.
Copy link
Member

@kenjis kenjis May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this?

Suggested change
There may be times when you need to have Service Discovery refresh it's cache after the inital autoload proccess. This can be done by running :php:meth:`Config\\Services::updateServicesCache()`.
This will force the service discovery to re-scan the directories for any new services files.
Updating Services Cache
=======================
.. versionadded:: 4.6.0
When Services is first called fairly early in the framework initialization process,
the Services classes discovered by auto-discovery is cached in a property.
If modules are dynamically loaded later, this cache must be updated.
This can be done by running ``Config\Services::updateServicesCache()``. This will
force the service discovery to re-scan the directories for any new Services files.

:php:meth:Config\\Services::updateServicesCache() is okay if there is the class reference for the method. But in this case, there is no class reference to link.

@kenjis
Copy link
Member

kenjis commented May 16, 2024

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?

@pyromanci
Copy link
Author

pyromanci commented May 16, 2024

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.

@kenjis
Copy link
Member

kenjis commented May 16, 2024

I'm not sure, but isn't it better to just add a method to delete the cache?

@pyromanci
Copy link
Author

pyromanci commented May 16, 2024

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?

@kenjis
Copy link
Member

kenjis commented May 17, 2024

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

@kenjis
Copy link
Member

kenjis commented May 17, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.6 enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants