Skip to content

Commit

Permalink
Merge pull request #405 from bearsunday/remove_module_cache
Browse files Browse the repository at this point in the history
Remove module cache in getOverrideInstance
  • Loading branch information
koriym authored Sep 27, 2022
2 parents 0b5b1b0 + 5c4d2aa commit 77ab2d1
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 17 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
},
"config": {
"allow-plugins": {
"bamarni/composer-bin-plugin": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ parameters:
- tests/tmp/*
- tests/Fake/*
- tests/script/*
- tests/Injector/Fake/fake-app/var/tmp/*
2 changes: 0 additions & 2 deletions src/Compiler/CompileDiScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Ray\Di\InjectorInterface;

use function assert;
use function class_exists;

final class CompileDiScripts
{
Expand All @@ -38,7 +37,6 @@ public function __invoke(AbstractAppMeta $appMeta): void
$metas = $appMeta->getResourceListGenerator();
foreach ($metas as $meta) {
[$className] = $meta;
assert(class_exists($className));
$this->scanClass($reader, $namedParams, $className);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Compiler/FakeRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public function __invoke(): void
($bootstrap)($this->appMeta->name, $this->context, $GLOBALS, $_SERVER); // @phpstan-ignore-line
$_SERVER['REQUEST_METHOD'] = 'DELETE';
$app = $this->injector->getInstance(AppInterface::class);
assert(property_exists($app, 'resource')); // @phpstan-ignore-line
assert(property_exists($app, 'responder')); // @phpstan-ignore-line
assert(property_exists($app, 'resource'));
assert(property_exists($app, 'responder'));
$ro = $this->injector->getInstance(NullPage::class);
$ro->uri = new Uri('app://self/'); // @phpstan-ignore-line
$ro->uri = new Uri('app://self/');
/** @var NullPage $ro */
$ro = $app->resource->get->object($ro)(['required' => 'string']); // @phpstan-ignore-line
assert($app->responder instanceof TransferInterface); // @phpstan-ignore-line
$ro = $app->resource->get->object($ro)(['required' => 'string']);
assert($app->responder instanceof TransferInterface);
ob_start();
$ro->transfer($app->responder, []); // @phpstan-ignore-line
$ro->transfer($app->responder, []);
ob_end_clean();
class_exists(HttpCacheInterface::class);
class_exists(HttpCache::class);
Expand Down
1 change: 1 addition & 0 deletions src/Context/ProdModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private function installCacheModule(): void
{
$this->install(new ProdQueryRepositoryModule());
$this->install(new Psr6ApcuModule());
/** @psalm-suppress DeprecatedClass */
$this->bind(CacheItemInterface::class)->annotatedWith(EtagPool::class)->toProvider(LocalCacheProvider::class);
$this->bind(Reader::class)->toConstructor(
PsrCachedReader::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Injector/FileUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private function getFiles(string $path, string $regex): array

$files = [];
/** @var RegexIterator<string, SplFileInfo> $iterator */
foreach ($iterator as $fileName => $fileInfo) {
foreach ($iterator as $fileName => $fileInfo) { // @phpstan-ignore-line
if ($fileInfo->isFile() && $fileInfo->getFilename()[0] !== '.') {
$files[] = $fileName;
}
Expand Down
9 changes: 1 addition & 8 deletions src/Injector/PackageInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ final class PackageInjector
*/
private static array $instances;

/** @var array<string, AbstractModule> */
private static array $modules;

/**
* @codeCoverageIgnore
*/
Expand Down Expand Up @@ -66,12 +63,8 @@ public static function factory(AbstractAppMeta $meta, string $context, ?Abstract
{
$scriptDir = $meta->tmpDir . '/di';
! is_dir($scriptDir) && ! @mkdir($scriptDir) && ! is_dir($scriptDir);
$moduleId = $meta->appDir . $context;
if (! isset(self::$modules[$moduleId])) {
self::$modules[$moduleId] = (new Module())($meta, $context);
}
$module = (new Module())($meta, $context);

$module = self::$modules[$moduleId];
if ($overideModule instanceof AbstractModule) {
$module->override($overideModule);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Fake/fake-app/src/FakeDep2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace FakeVendor\HelloWorld;

class FakeDep2 implements FakeDepInterface
{
public function foo()
{
}
}
19 changes: 19 additions & 0 deletions tests/Fake/fake-app/src/Resource/Page/Injection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace FakeVendor\HelloWorld\Resource\Page;

use BEAR\Resource\ResourceObject;
use FakeVendor\HelloWorld\FakeDepInterface;
use FooName\FooInterface;


class Injection extends ResourceObject
{
public FakeDepInterface $foo;
public function __construct(FakeDepInterface $foo)
{
$this->foo = $foo;
}
}
48 changes: 48 additions & 0 deletions tests/Injector/PackageInjectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace BEAR\Package\Injector;

use BEAR\Package\Injector;
use BEAR\Resource\ResourceInterface;
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 function assert;
use function dirname;

class PackageInjectorTest extends TestCase
{
public function testOriginalBind(): void
{
$injector = Injector::getInstance('FakeVendor\HelloWorld', 'app', dirname(__DIR__) . '/Fake/fake-app');
$resource = $injector->getInstance(ResourceInterface::class);
assert($resource instanceof ResourceInterface);
$page = $resource->newInstance('page://self/injection');
assert($page instanceof Injection);
$this->assertInstanceOf(FakeDep::class, $page->foo);
}

/**
* @depends testOriginalBind
*/
public function testGetOverrideInstance(): void
{
$injector = Injector::getOverrideInstance('FakeVendor\HelloWorld', 'app', dirname(__DIR__) . '/Fake/fake-app', new class extends AbstractModule{
protected function configure()
{
$this->bind(FakeDepInterface::class)->to(FakeDep2::class);
}
});
$resource = $injector->getInstance(ResourceInterface::class);
assert($resource instanceof ResourceInterface);
$page = $resource->newInstance('page://self/injection');
assert($page instanceof Injection);
$this->assertInstanceOf(FakeDep2::class, $page->foo);
}
}

0 comments on commit 77ab2d1

Please sign in to comment.