Skip to content

Commit

Permalink
Merge pull request #417 from bearsunday/check-cache
Browse files Browse the repository at this point in the history
Validate injector cache saved and optimize local cache selection
  • Loading branch information
koriym authored Apr 21, 2023
2 parents 2e6d717 + 27900ae commit 2bb73ef
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 11 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ on:

jobs:
ci:
uses: ray-di/.github/.github/workflows/continuous-integration.yml@next_stable
uses: ray-di/.github/.github/workflows/continuous-integration.yml@v1
with:
old_stable: '["8.0"]'
current_stable: 8.1
next_stable: 8.2
old_stable: '["8.0", "8.1"]'
current_stable: 8.2
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"doctrine/cache": "^1.10 || ^2.0",
"doctrine/annotations": "^1.11",
"koriym/http-constants": "^1.1",
"ray/psr-cache-module": "^1.3",
"ray/psr-cache-module": "^1.3.2",
"symfony/cache": "^5.3",
"psr/cache": "^1.0",
"koriym/attributes": "^1.0",
Expand Down
5 changes: 2 additions & 3 deletions src/Injector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use BEAR\Package\Injector\PackageInjector;
use Ray\Di\AbstractModule;
use Ray\Di\InjectorInterface;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Ray\PsrCacheModule\LocalCacheProvider;
use Symfony\Contracts\Cache\CacheInterface;

use function str_replace;
Expand All @@ -26,7 +25,7 @@ public static function getInstance(string $appName, string $context, string $app
{
$meta = new Meta($appName, $context, $appDir);
$cacheNamespace = str_replace('/', '_', $appDir) . $context;
$cache ??= ApcuAdapter::isSupported() ? new ApcuAdapter($cacheNamespace) : new FilesystemAdapter('', 0, $meta->tmpDir . '/injector');
$cache ??= (new LocalCacheProvider($meta->tmpDir . '/injector', $cacheNamespace))->get();

return PackageInjector::getInstance($meta, $context, $cache);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Injector/PackageInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
use function is_dir;
use function mkdir;
use function str_replace;
use function trigger_error;

use const E_USER_WARNING;

final class PackageInjector
{
Expand Down Expand Up @@ -51,6 +54,10 @@ public static function getInstance(AbstractAppMeta $meta, string $context, Cache
if (! $isCacheableInjector) {
$injector = self::factory($meta, $context);
$cache->save($cache->getItem($injectorId)->set([$injector, new FileUpdate($meta)]));
// Check the cache
if ($cache->getItem($injectorId)->get() === null) {
trigger_error('Failed to verify the injector cache. See https://github.com/bearsunday/BEAR.Package/issues/418', E_USER_WARNING);
}
}

self::$instances[$injectorId] = $injector;
Expand Down
10 changes: 7 additions & 3 deletions tests/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ public function testInvoke(): void
$this->assertFileExists($compiledFile3);
}

/** @depends testInvoke */
public function testInvokeAgain(): void
{
$compiled = __DIR__ . '/Fake/fake-app/var/tmp/prod-cli-app/di/compiled';
$compiledFile1 = __DIR__ . '/Fake/fake-app/var/tmp/prod-cli-app/di/FakeVendor_HelloWorld_Resource_Page_Index-.php';
$compiledFile3 = __DIR__ . '/Fake/fake-app/var/tmp/prod-cli-app/di/FakeVendor_HelloWorld_FakeFoo-.php';
@unlink($compiledFile1);
@unlink($compiledFile3);
@unlink($compiled);
unlink($compiledFile1);
unlink($compiledFile3);
$compiler = new Compiler('FakeVendor\HelloWorld', 'prod-cli-app', __DIR__ . '/Fake/fake-app', false);
$compiler->compile();
$status = $compiler->compile();
$this->assertSame(0, $status);
$compiler->dumpAutoload();
$this->assertFileExists($compiledFile1);
$this->assertFileExists($compiledFile3);
Expand Down
29 changes: 29 additions & 0 deletions tests/Fake/fake-app/src/Module/BadModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace FakeVendor\HelloWorld\Module;

use BEAR\Sunday\Extension\Error\ThrowableHandlerInterface;
use BEAR\Sunday\Extension\Router\RouterMatch;
use Ray\Di\AbstractModule;
use Throwable;

class BadModule extends AbstractModule
{
/**
* {@inheritdoc}
*/
protected function configure(): void
{
// Invalid. You can not to bind unserializable object.
$this->bind(ThrowableHandlerInterface::class)->toInstance(new class implements ThrowableHandlerInterface {
public function handle(Throwable $e, RouterMatch $request): ThrowableHandlerInterface
{
}
public function transfer(): void
{
}
});
}
}
19 changes: 19 additions & 0 deletions tests/Injector/PackageInjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@

namespace BEAR\Package\Injector;

use BEAR\AppMeta\Meta;
use BEAR\Package\Injector;
use BEAR\Resource\ResourceInterface;
use Exception;
use FakeVendor\HelloWorld\FakeDep;
use FakeVendor\HelloWorld\FakeDep2;
use FakeVendor\HelloWorld\FakeDepInterface;
use FakeVendor\HelloWorld\Resource\Page\Injection;
use PHPUnit\Framework\TestCase;
use Ray\Di\AbstractModule;
use Ray\Di\InjectorInterface;
use Symfony\Component\Cache\Adapter\NullAdapter;

use function assert;
use function dirname;
use function restore_error_handler;
use function set_error_handler;

use const E_USER_WARNING;

class PackageInjectorTest extends TestCase
{
Expand Down Expand Up @@ -43,4 +51,15 @@ protected function configure(): void
assert($page instanceof Injection);
$this->assertInstanceOf(FakeDep2::class, $page->foo);
}

public function testUnserializableRootObject(): void
{
set_error_handler(static function (int $errno, string $errstr): void {
throw new Exception($errstr, $errno);
}, E_USER_WARNING);
$this->expectExceptionMessage('Failed to verify the injector cache.');
$injector = PackageInjector::getInstance(new Meta('FakeVendor\HelloWorld'), 'bad-app', new NullAdapter());
$this->assertInstanceOf(InjectorInterface::class, $injector);
restore_error_handler();
}
}

0 comments on commit 2bb73ef

Please sign in to comment.