From 8e75a9eea31646ebf024039d109f31d7f996ff87 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Tue, 20 Feb 2024 14:29:45 +0400 Subject: [PATCH] Init Core Introspector --- src/Core/src/Internal/Introspector.php | 56 +++++++++++++++++++ .../src/Internal/Introspector/Accessor.php | 41 ++++++++++++++ .../Internal/Introspector/CommonTest.php | 42 ++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 src/Core/src/Internal/Introspector.php create mode 100644 src/Core/src/Internal/Introspector/Accessor.php create mode 100644 src/Core/tests/Internal/Introspector/CommonTest.php diff --git a/src/Core/src/Internal/Introspector.php b/src/Core/src/Internal/Introspector.php new file mode 100644 index 000000000..595beb96b --- /dev/null +++ b/src/Core/src/Internal/Introspector.php @@ -0,0 +1,56 @@ +scope->getScopeName(); + } + + /** + * Returns list of scope names starting from the current scope to the root scope. + * + * @return list + */ + public static function scopeNames(?ContainerInterface $container = null): array + { + $scope = self::getAccessor($container)->scope; + $result = []; + do { + $result[] = $scope->getScopeName(); + $scope = $scope->getParentScope(); + } while ($scope !== null); + + return $result; + } + + /** + * @psalm-assert Container|null $container + */ + public static function getAccessor(?ContainerInterface $container = null): Accessor + { + if ($container !== null && !$container instanceof Container) { + throw new \InvalidArgumentException('Container must be an instance of ' . Container::class); + } + + $container ??= ContainerScope::getContainer(); + + if (!$container instanceof Container) { + throw new \RuntimeException('Container is not available.'); + } + + return new Accessor($container); + } +} diff --git a/src/Core/src/Internal/Introspector/Accessor.php b/src/Core/src/Internal/Introspector/Accessor.php new file mode 100644 index 000000000..4d3931370 --- /dev/null +++ b/src/Core/src/Internal/Introspector/Accessor.php @@ -0,0 +1,41 @@ + $c->$name)->call($this->publicContainer, $this->publicContainer); + } +} diff --git a/src/Core/tests/Internal/Introspector/CommonTest.php b/src/Core/tests/Internal/Introspector/CommonTest.php new file mode 100644 index 000000000..efd78a99b --- /dev/null +++ b/src/Core/tests/Internal/Introspector/CommonTest.php @@ -0,0 +1,42 @@ +invoke(static fn() => self::assertSame('root', Introspector::scopeName())); + + $container->runScope(new Scope('test'), function (ContainerInterface $container): void { + self::assertSame('test', Introspector::scopeName($container)); + self::assertSame('test', Introspector::scopeName()); + }); + } + + public function testScopeNames(): void + { + $container = new Container(); + + $container->runScope(new Scope('test'), function (Container $c): void { + $c->runScope(new Scope(), function (Container $c): void { + $c->runScope(new Scope('bar'), function (Container $c): void { + self::assertSame(['bar', null, 'test', 'root'], Introspector::scopeNames($c)); + self::assertSame(['bar', null, 'test', 'root'], Introspector::scopeNames()); + }); + }); + }); + } +}