DependencyChecker: added callback support#173
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.
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.
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.
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
9c8f2ac to
94a66aa
Compare
56ceef4 to
35d1bdf
Compare
3c05c69 to
ec380f1
Compare
1cde060 to
82206fe
Compare
This PR adds possibility to invalidate the DI container using given callback.
Currently, you can make
DependencyCheckertrack 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/diupdates and there's a cached container regardless of theVERSIONadvance. 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
callableandstring, 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).