Skip to content

DependencyChecker: added callback support #173

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/DI/DependencyChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class DependencyChecker
{
use Nette\SmartObject;

public const VERSION = 1;
public const VERSION = 2;

/** @var array of ReflectionClass|\ReflectionFunctionAbstract|string */
/** @var array of ReflectionClass|\ReflectionFunctionAbstract|string|array */
private $dependencies = [];


Expand All @@ -44,11 +44,14 @@ public function add(array $deps)
*/
public function export(): array
{
$files = $phpFiles = $classes = $functions = [];
$files = $phpFiles = $classes = $functions = $callbacks = [];
foreach ($this->dependencies as $dep) {
if (is_string($dep)) {
$files[] = $dep;

} elseif (is_array($dep) && isset($dep[0]) && Nette\Utils\Callback::isStatic($dep[0])) {
$callbacks[] = $dep;

} elseif ($dep instanceof ReflectionClass) {
if (empty($classes[$name = $dep->getName()])) {
$all = [$name] + class_parents($name) + class_implements($name);
Expand All @@ -70,28 +73,30 @@ public function export(): array

$classes = array_keys($classes);
$functions = array_unique($functions, SORT_REGULAR);
$hash = self::calculateHash($classes, $functions);
$reflectionHash = self::calculateReflectionHash($classes, $functions);
$callbackHash = self::calculateCallbackHash($callbacks);
$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, $reflectionHash, $callbacks, $callbackHash];
}


/**
* Are dependencies expired?
*/
public static function isExpired(int $version, array $files, array &$phpFiles, array $classes, array $functions, string $hash): bool
public static function isExpired(int $version, array $files, array &$phpFiles, array $classes, array $functions, string $reflectionHash, array $callbacks = [], string $callbackHash = ''): bool
{
$current = @array_map('filemtime', array_combine($tmp = array_keys($files), $tmp)); // @ - files may not exist
$origPhpFiles = $phpFiles;
$phpFiles = @array_map('filemtime', array_combine($tmp = array_keys($phpFiles), $tmp)); // @ - files may not exist
return $version !== self::VERSION
|| $files !== $current
|| ($phpFiles !== $origPhpFiles && $hash !== self::calculateHash($classes, $functions));
|| ($phpFiles !== $origPhpFiles && $reflectionHash !== self::calculateReflectionHash($classes, $functions))
|| $callbackHash !== self::calculateCallbackHash($callbacks);
}


private static function calculateHash(array $classes, array $functions): ?string
private static function calculateReflectionHash(array $classes, array $functions): ?string
{
$hash = [];
foreach ($classes as $name) {
Expand Down Expand Up @@ -155,6 +160,18 @@ class_uses($name),
}


private static function calculateCallbackHash(array $callbacks): string
{
$hash = [];

foreach ($callbacks as $callback) {
$hash[] = [$callback, call_user_func(...$callback)];
}

return md5(serialize($hash));
}


private static function hashParameters(\ReflectionFunctionAbstract $method): array
{
$res = [];
Expand Down
30 changes: 27 additions & 3 deletions tests/DI/Compiler.dependencies.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,50 @@ require __DIR__ . '/../bootstrap.php';
$compiler = new DI\Compiler;

Assert::same(
[DependencyChecker::VERSION, [], [], [], [], '40cd750bba9870f18aada2478b24840a'],
[DependencyChecker::VERSION, [], [], [], [], '40cd750bba9870f18aada2478b24840a', [], '40cd750bba9870f18aada2478b24840a'],
$compiler->exportDependencies()
);
Assert::false(DependencyChecker::isExpired(...$compiler->exportDependencies()));


$compiler->addDependencies(['file1', __FILE__]);
Assert::same(
[DependencyChecker::VERSION, ['file1' => false, __FILE__ => filemtime(__FILE__)], [], [], [], '40cd750bba9870f18aada2478b24840a'],
[DependencyChecker::VERSION, ['file1' => false, __FILE__ => filemtime(__FILE__)], [], [], [], '40cd750bba9870f18aada2478b24840a', [], '40cd750bba9870f18aada2478b24840a'],
$compiler->exportDependencies()
);
Assert::false(DependencyChecker::isExpired(...$compiler->exportDependencies()));


$compiler->addDependencies(['file1', null, 'file3']);
Assert::same(
[DependencyChecker::VERSION, ['file1' => false, __FILE__ => filemtime(__FILE__), 'file3' => false], [], [], [], '40cd750bba9870f18aada2478b24840a'],
[DependencyChecker::VERSION, ['file1' => false, __FILE__ => filemtime(__FILE__), 'file3' => false], [], [], [], '40cd750bba9870f18aada2478b24840a', [], '40cd750bba9870f18aada2478b24840a'],
$compiler->exportDependencies()
);

$res = $compiler->exportDependencies();
$res[1]['file4'] = 123;
Assert::true(DependencyChecker::isExpired(...$res));

$compiler = new DI\Compiler;
$compiler->addDependencies([['CustomDependencyChecker::check', 0]]);
$res = $compiler->exportDependencies();

Assert::same(
[DependencyChecker::VERSION, [], [], [], [], '40cd750bba9870f18aada2478b24840a', [['CustomDependencyChecker::check', 0]], '75ea70c3b123324ae9c9ccb31bd7a6c0'],
$res
);

Assert::false(DependencyChecker::isExpired(...$res));
CustomDependencyChecker::$state++;
Assert::true(DependencyChecker::isExpired(...$res));

class CustomDependencyChecker
{
public static $state = 0;


public static function check($state)
{
return self::$state === $state;
}
}