Skip to content

Commit

Permalink
Cleanup CachedContainer invalidation (#3867)
Browse files Browse the repository at this point in the history
* Cleanup CachedContainer invalidation

* Update AbstractTestCase.php

* Update CachedContainerBuilder.php

* cleanup

* cleanup

* fix

* Update CacheInvalidatingContainer.php

* Update CacheInvalidatingContainer.php

* Add StaleContainerCacheException

* move
  • Loading branch information
staabm committed May 17, 2023
1 parent a8fdf00 commit f0141af
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 11 deletions.
10 changes: 1 addition & 9 deletions packages/Testing/PHPUnit/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,7 @@ protected function getService(string $type): object
);
}

try {
$object = self::$currentContainer->get($type);
} catch (Throwable $throwable) {
// clear compiled container cache, to trigger re-discovery
RectorKernel::clearCache();

throw $throwable;
}

$object = self::$currentContainer->get($type);
if ($object === null) {
$message = sprintf('Service "%s" was not found', $type);
throw new ShouldNotHappenException($message);
Expand Down
7 changes: 7 additions & 0 deletions src/Exception/Cache/StaleContainerCacheException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Exception\Cache;

final class StaleContainerCacheException extends \RuntimeException {}
67 changes: 67 additions & 0 deletions src/Kernel/CacheInvalidatingContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Kernel;

use Rector\Core\Exception\Cache\StaleContainerCacheException;
use Symfony\Component\DependencyInjection\ContainerInterface;

final class CacheInvalidatingContainer implements ContainerInterface {
public function __construct(
private readonly ContainerInterface $wrapped
)
{}

public function set(string $id, ?object $service): void
{
$this->wrapped->set($id, $service);
}

public function get(string $id, int $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE): ?object
{
try {
return $this->wrapped->get($id, $invalidBehavior);
} catch (\Throwable $throwable) {
// clear compiled container cache, to trigger re-discovery
RectorKernel::clearCache();

throw new StaleContainerCacheException(
'Container cache is outdated and was cleared. please re-run the command.',
0,
$throwable
);
}
}

public function has(string $id): bool
{
return $this->wrapped->has($id);
}

public function initialized(string $id): bool
{
return $this->wrapped->initialized($id);
}

/**
* @return array<mixed>|bool|float|int|string|\UnitEnum|null
*/
public function getParameter(string $name)
{
return $this->wrapped->getParameter($name);
}

public function hasParameter(string $name): bool
{
return $this->wrapped->hasParameter($name);
}

/**
* @param \UnitEnum|float|array<mixed>|bool|int|string|null $value
*/
public function setParameter(string $name, \UnitEnum|float|array|bool|int|string|null $value): void
{
$this->wrapped->setParameter($name, $value);
}
}
5 changes: 3 additions & 2 deletions src/Kernel/CachedContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ public function build(array $configFiles, string $hash, callable $containerBuild
if (file_exists($file)) {
require_once $file;
$className = '\\' . __NAMESPACE__ . '\\' . $className;
$container = new $className();
if (! $container instanceof ContainerInterface) {
$cachedContainer = new $className();
if (! $cachedContainer instanceof ContainerInterface) {
throw new ShouldNotHappenException();
}
$container = new CacheInvalidatingContainer($cachedContainer);
} else {
$container = ($containerBuilderCallback)($configFiles);

Expand Down

0 comments on commit f0141af

Please sign in to comment.