Skip to content

Commit

Permalink
Invalidate cache on file namespaces (#402)
Browse files Browse the repository at this point in the history
* Allow to invalidate cache on file changes in namespaces

* Invalidate cache on file changes in namespaces
  • Loading branch information
dgafka authored Oct 26, 2024
1 parent 0ad5db1 commit d671fef
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ private function init(string $rootProjectDir, array $namespacesToUse, string $ca

$registeredClasses = array_merge(
$registeredClasses,
$this->getRegisteredClassesForNamespaces($rootProjectDir, $autoloadNamespaceParser, $namespacesToUse)
self::getRegisteredClassesForNamespaces($rootProjectDir, $autoloadNamespaceParser, $namespacesToUse)
);

$this->registeredClasses = array_unique($registeredClasses);
}

private function getDirContents(string $dir, array &$results = []): array
private static function getDirContents(string $dir, array &$results = []): array
{
if (! is_dir($dir)) {
return [];
Expand All @@ -157,7 +157,7 @@ private function getDirContents(string $dir, array &$results = []): array
$results[] = $fullPath;
}
} elseif ($value != '.' && $value != '..') {
$this->getDirContents($fullPath, $results);
self::getDirContents($fullPath, $results);
}
}

Expand All @@ -170,7 +170,7 @@ private function getDirContents(string $dir, array &$results = []): array
*
* @return bool
*/
private function isInAvailableNamespaces(array $namespaces, $namespace): bool
private static function isInAvailableNamespaces(array $namespaces, $namespace): bool
{
foreach ($namespaces as $namespaceToUse) {
if (strpos($namespace, $namespaceToUse) === 0) {
Expand Down Expand Up @@ -385,18 +385,18 @@ public function getAnnotationsForProperty(string $className, string $propertyNam
* @param string[]|null $namespacesToUse
* @param string[] $classes
*/
private function getClassesIn(array $paths, ?array $namespacesToUse): array
private static function getClassesIn(array $paths, ?array $namespacesToUse): array
{
$classes = [];
foreach ($paths as $path) {
$files = $this->getDirContents($path);
$files = self::getDirContents($path);

foreach ($files as $file) {
if (preg_match_all(self::CLASS_NAMESPACE_REGEX, file_get_contents($file), $results)) {
$namespace = isset($results[1][0]) ? trim($results[1][0]) : '';
$namespace = trim($namespace, "\t\n\r\\");

if (is_null($namespacesToUse) || $this->isInAvailableNamespaces($namespacesToUse, $namespace)) {
if (is_null($namespacesToUse) || self::isInAvailableNamespaces($namespacesToUse, $namespace)) {
$classes[] = $namespace . '\\' . basename($file, '.php');
}
}
Expand All @@ -405,7 +405,11 @@ private function getClassesIn(array $paths, ?array $namespacesToUse): array
return $classes;
}

private function getRegisteredClassesForNamespaces(string $rootProjectDir, AutoloadNamespaceParser $autoloadNamespaceParser, array $namespacesToUse): array
public static function getRegisteredClassesForNamespaces(
string $rootProjectDir,
AutoloadNamespaceParser $autoloadNamespaceParser,
array $namespacesToUse
): array
{
if ($namespacesToUse === []) {
return [];
Expand All @@ -421,7 +425,7 @@ private function getRegisteredClassesForNamespaces(string $rootProjectDir, Autol
return array_values(
\array_filter(
array_keys($autoloader->getClassMap()),
fn (string $className) => $this->isInAvailableNamespaces($namespacesToUse, $className)
fn (string $className) => self::isInAvailableNamespaces($namespacesToUse, $className)
)
);
} else {
Expand All @@ -434,7 +438,7 @@ private function getRegisteredClassesForNamespaces(string $rootProjectDir, Autol

$paths = array_unique($paths);

return $this->getClassesIn($paths, $namespacesToUse);
return self::getClassesIn($paths, $namespacesToUse);
}
}

Expand Down
24 changes: 20 additions & 4 deletions packages/Ecotone/src/Lite/EcotoneLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Ecotone\Lite;

use Ecotone\AnnotationFinder\FileSystem\AutoloadFileNamespaceParser;
use Ecotone\AnnotationFinder\FileSystem\FileSystemAnnotationFinder;
use Ecotone\AnnotationFinder\FileSystem\RootCatalogNotFound;
use Ecotone\Dbal\Configuration\DbalConfiguration;
Expand Down Expand Up @@ -189,6 +190,15 @@ private static function getFileNameBasedOnConfig(
// get file contents based on class names, configuration and configuration variables
$fileSha = '';

if ($serviceConfiguration->getNamespaces()) {
$classes = FileSystemAnnotationFinder::getRegisteredClassesForNamespaces($pathToRootCatalog, new AutoloadFileNamespaceParser(), $serviceConfiguration->getNamespaces());

foreach ($classes as $class) {
$filePath = (new ReflectionClass($class))->getFileName();
$fileSha .= sha1_file($filePath);
}
}

if (file_exists($pathToRootCatalog . 'composer.lock')) {
$fileSha .= sha1_file($pathToRootCatalog . 'composer.lock');
}
Expand Down Expand Up @@ -245,11 +255,15 @@ private static function prepareConfiguration(ContainerInterface|array $container
);
$configurationVariableService = InMemoryConfigurationVariableService::create($configurationVariables);
$definitionHolder = null;
$messagingSystemCachePath = null;

if ($serviceCacheConfiguration->shouldUseCache()) {
$messagingSystemCachePath = $serviceCacheConfiguration->getPath() . DIRECTORY_SEPARATOR . self::getFileNameBasedOnConfig($pathToRootCatalog, $useCachedVersion, $classesToResolve, $serviceConfiguration, $configurationVariables, $enableTesting);

$messagingSystemCachePath = $serviceCacheConfiguration->getPath() . DIRECTORY_SEPARATOR . self::getFileNameBasedOnConfig($pathToRootCatalog, $useCachedVersion, $classesToResolve, $serviceConfiguration, $configurationVariables, $enableTesting);
if ($serviceCacheConfiguration->shouldUseCache() && file_exists($messagingSystemCachePath)) {
/** It may fail on deserialization, then return `false` and we can build new one */
$definitionHolder = unserialize(file_get_contents($messagingSystemCachePath));
if (file_exists($messagingSystemCachePath)) {
/** It may fail on deserialization, then return `false` and we can build new one */
$definitionHolder = unserialize(file_get_contents($messagingSystemCachePath));
}
}

if (! $definitionHolder) {
Expand All @@ -263,6 +277,8 @@ private static function prepareConfiguration(ContainerInterface|array $container
$definitionHolder = ContainerConfig::buildDefinitionHolder($messagingConfiguration);

if ($serviceCacheConfiguration->shouldUseCache()) {
Assert::notNull($messagingSystemCachePath, 'Cache path should be defined');

MessagingSystemConfiguration::prepareCacheDirectory($serviceCacheConfiguration);
file_put_contents($messagingSystemCachePath, serialize($definitionHolder));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function test_bootstraping_with_given_set_of_classes()

public function test_bootstraping_with_namespace()
{
$ecotoneTestSupport = EcotoneLite::bootstrapForTesting(
$ecotoneTestSupport = EcotoneLite::bootstrapFlowTesting(
[],
[new OrderService(), new PlaceOrderConverter(), new OrderWasPlacedConverter()],
ServiceConfiguration::createWithDefaults()
Expand All @@ -68,9 +68,9 @@ public function test_bootstraping_with_namespace()
);

$orderId = 'someId';
$ecotoneTestSupport->getCommandBus()->sendWithRouting('order.register', new PlaceOrder($orderId));
$ecotoneTestSupport->sendCommandWithRoutingKey('order.register', new PlaceOrder($orderId));

$this->assertNotEmpty($ecotoneTestSupport->getQueryBus()->sendWithRouting('order.getOrders'));
$this->assertNotEmpty($ecotoneTestSupport->sendQueryWithRouting('order.getOrders'));
}

public function test_bootstraping_with_given_set_of_classes_and_asynchronous_module()
Expand Down

0 comments on commit d671fef

Please sign in to comment.