Skip to content

Commit

Permalink
Revert Revert Cleanup CachedContainer invalidation (#3867) (#3880) (#…
Browse files Browse the repository at this point in the history
…3881)

* Revert Revert Cleanup CachedContainer invalidation (#3867) (#3880)

This reverts commit 5e3afcb.

* update
  • Loading branch information
samsonasik committed May 17, 2023
1 parent 5e3afcb commit 41aabcb
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 12 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"symfony/string": "^6.2.2",
"symplify/easy-parallel": "^11.1",
"symplify/rule-doc-generator-contracts": "^11.1",
"symplify/smart-file-system": "^11.2",
"tracy/tracy": "^2.9",
"webmozart/assert": "^1.11"
},
Expand Down
11 changes: 1 addition & 10 deletions packages/Testing/PHPUnit/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Kernel\RectorKernel;
use Rector\Core\Util\FileHasher;
use Throwable;

abstract class AbstractTestCase extends TestCase
{
Expand Down Expand Up @@ -60,15 +59,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
8 changes: 8 additions & 0 deletions src/Exception/Cache/StaleContainerCacheException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Exception\Cache;

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

declare(strict_types=1);

namespace Rector\Core\Kernel;

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

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

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

public function get(string $id, int $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE): ?object
{
try {
return $this->container->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->container->has($id);
}

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

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

public function hasParameter(string $name): bool
{
return $this->container->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->container->setParameter($name, $value);
}
}
6 changes: 4 additions & 2 deletions src/Kernel/CachedContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ 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 41aabcb

Please sign in to comment.