-
-
Notifications
You must be signed in to change notification settings - Fork 71
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
DependencyChecker: added callback support #173
base: master
Are you sure you want to change the base?
Conversation
I would consider sytanx for static method call as |
Yup, here's what I have been thinking:
I don't really mind anyway, if you want, I'll edit the PR. |
Probably |
(Also note that support for |
I've allowed the callback to be specified using array syntax. Something about performace - would it be OK to use part of hash for files and other part for callbacks? I think there's no need for entirely separated hash. |
I think Callback::isStatic() may help https://api.nette.org/2.4/Nette.Utils.Callback.html |
It must be separated. Hashes for callbacks will be generated for each request and it should not influence generating hashes for files. |
Updates:
|
src/DI/DependencyChecker.php
Outdated
$files = @array_map('filemtime', array_combine($files, $files)); // @ - file may not exist | ||
$phpFiles = @array_map('filemtime', array_combine($phpFiles, $phpFiles)); // @ - file may not exist | ||
return [self::VERSION, $files, $phpFiles, $classes, $functions, $hash]; | ||
return [self::VERSION, $files, $phpFiles, $classes, $functions, $callbacks, $hash]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe simpler solution will be better, ie. $reflectionHash = self::calculateReflectionHash(); $callbackHash = self::calculateCallbackHash();
and then return [self::VERSION, $files, $phpFiles, $classes, $functions, $callbacks, $reflectionHash, $callbackHash]
, what do you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that was exactly the original solution (linked above). I'll split them, if you wish. How about compatibility with cached container, should I drop it or keep it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compatibility is not needed, ie. it is enough to have compatible isExpired declaration:
public static function isExpired(int $version, array $files, array &$phpFiles, array $classes, array $functions, string $reflectionHash, array $callbacks = [], string $callbackHash = '')
Now I've been thinking about this - in practice, it'd be useful to be able alter cached data without rebuilding the container. For example, if a class file changes, but there's no interesting annotation in it, I don't want to discard the container, but next time, I would have to load and inspect the file again. Not sure if done properly, but here's a patch which excludes parameters from hashing and when they change (using reference), they'll get stored without rebuilding, much like class files do. |
That makes sense, but it must be done a little differently. I'll try write more later. |
8224b32
to
d51ca9e
Compare
1fc7645
to
485a875
Compare
41deac3
to
191506d
Compare
680bc12
to
5066242
Compare
b109822
to
7f11e6e
Compare
ef39d2d
to
f729b1e
Compare
a43bb2c
to
71a91be
Compare
51cfed7
to
9c4af52
Compare
0a1f0ab
to
8c392ab
Compare
5b1fffa
to
f9ca71d
Compare
8655bcb
to
59cf699
Compare
This PR adds possibility to invalidate the DI container using given callback.
Currently, you can make
DependencyChecker
track specific files (and classes or methods). Using callback, we could invalidate the container, for instance:I'm not sure about proper implementation - open to discussion. What concerns me is
isExpired()
method; it used to callcalculateHash()
only when a file was changed, but now it gets called always when there is a callback as well (which causes tracked files to be loaded and inspected unnecessarily). An optimization would require a separate hash for callbacks.EDIT: This actually probably breaks when
nette/di
updates and there's a cached container regardless of theVERSION
advance. Could be fixed using optional parameters inisExpired()
signature, if that's an issue.Usage
Dependency is supposed to be defined like this:
First argument must be a
callable
andstring
, which limits the callback to a function or a static method. Remaining arguments are stored within the callback and are always passed to it (I can explain what's that good for if it's not obvious).