Skip to content

Commit

Permalink
wip for transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
koriym committed Nov 27, 2024
1 parent 6eb1d76 commit c516381
Show file tree
Hide file tree
Showing 30 changed files with 131 additions and 109 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"autoload": {
"psr-4": {
"Ray\\Compiler\\": ["src", "src-deprecated"]
"Ray\\Compiler\\": ["src"]
}
},
"autoload-dev": {
Expand Down
2 changes: 0 additions & 2 deletions docs/exmaple/DevInjectorContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Doctrine\Common\Cache\ApcuCache;
use Doctrine\Common\Cache\CacheProvider;
use Ray\Compiler\AbstractInjectorContext;
use Ray\Compiler\DiCompileModule;
use Ray\Compiler\FakeCarModule;
use Ray\Di\AbstractModule;
use Ray\Di\NullCache;

Expand Down
2 changes: 1 addition & 1 deletion src/AopCode.php → src-deprecated/AopCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
use Ray\Di\Dependency;
use Ray\Di\Name;
use Ray\Di\NewInstance;

use function array_merge;
use function array_splice;
use function is_array;

/** @deprecated Use CompileInjector instead */
final class AopCode
{
/** @var PrivateProperty */
Expand Down
1 change: 1 addition & 0 deletions src/Code.php → src-deprecated/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;

/** @deprecated Use CompileInjector instead */
final class Code
{
/** @var bool */
Expand Down
3 changes: 1 addition & 2 deletions src/DependencyCode.php → src-deprecated/DependencyCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
use Ray\Di\SetContextInterface;
use Ray\Di\SetterMethod;
use Ray\Di\SetterMethods;

use function get_class;
use function is_a;

/** @deprecated This is subcomponent of deprecated DiCompiler */
final class DependencyCode implements SetContextInterface
{
/** @var BuilderFactory */
Expand Down Expand Up @@ -150,7 +150,6 @@ private function getProviderCode(DependencyProvider $provider): Code
$isSingleton = $prop($provider, 'isSingleton');
$node[] = $this->getIsSingletonCode($isSingleton);
$node[] = new Stmt\Return_(new MethodCall(new Expr\Variable('instance'), 'get'));
/** @psalm-trace $node */
$codeNode = $this->factory->namespace('Ray\Di\Compiler')->addStmts($node)->getNode();
$qualifier = $this->qualifier;
$this->qualifier = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@

use LogicException;
use ReflectionClass;

use function file_exists;
use function is_dir;
use function mkdir;
use function serialize;
use function sprintf;
use function str_replace;

use const PHP_EOL;

/** @psalm-import-type ScriptDir from CompileInjector */
/**
* @deprecated use CompileInjector instead
* @psalm-import-type ScriptDir from CompileInjector
*/
final class DependencySaver
{
/** @var ScriptDir */
Expand Down
3 changes: 3 additions & 0 deletions src/FactoryCode.php → src-deprecated/FactoryCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Ray\Di\Name;
use Ray\Di\SetterMethod;

/**
* @deprecated Use CompileInjector instead
*/
final class FactoryCode
{
/** @var Container */
Expand Down
2 changes: 1 addition & 1 deletion src/FunctionCode.php → src-deprecated/FunctionCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
use ReflectionClass;
use ReflectionMethod;
use ReflectionParameter;

use function assert;
use function is_bool;

/** @deprecated Use CompilerInjector instead */
final class FunctionCode
{
/** @var Container */
Expand Down
7 changes: 4 additions & 3 deletions src/GraphDumper.php → src-deprecated/GraphDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Koriym\Printo\Printo;
use Ray\Di\Container;
use Ray\Di\Name;

use function assert;
use function class_exists;
use function explode;
Expand All @@ -16,10 +15,12 @@
use function interface_exists;
use function mkdir;
use function str_replace;

use const LOCK_EX;

/** @psalm-import-type ScriptDir from CompileInjector */
/**
* @psalm-import-type ScriptDir from CompileInjector
* @deprecated No longer supported. There is no alternative available.
*/
final class GraphDumper
{
/** @var Container */
Expand Down
2 changes: 1 addition & 1 deletion src/IpQualifier.php → src-deprecated/IpQualifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Ray\Compiler;

use ReflectionParameter;

use function serialize;

/** @deprecated */
final class IpQualifier
{
/** @var ReflectionParameter */
Expand Down
2 changes: 1 addition & 1 deletion src/NodeFactory.php → src-deprecated/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
use Ray\Di\InjectorInterface;
use Ray\Di\SetterMethod;
use ReflectionClass;

use function assert;
use function is_bool;
use function is_string;

/** @deprecated */
final class NodeFactory
{
/** @var InjectorInterface|null */
Expand Down
2 changes: 1 addition & 1 deletion src/Normalizer.php → src-deprecated/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpParser\Node\Scalar;
use Ray\Compiler\Exception\InvalidInstance;
use Ray\Di\InjectorInterface;

use function is_array;
use function is_bool;
use function is_float;
Expand All @@ -20,6 +19,7 @@
use function serialize;

/**
* @deprecated This is subcomponent of
* Value to code(Node) converter
*/
final class Normalizer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@
use Ray\Di\Bind;
use Ray\Di\Dependency;
use Ray\Di\Exception\NotFound;

use function assert;
use function error_reporting;
use function explode;
use function file_exists;
use function file_get_contents;
use function is_bool;
use function unserialize;

use const E_NOTICE;

/**
* @psalm-import-type ScriptDir from CompileInjector
* @psalm-type Pointcuts = list<Pointcut>
* @deprecated
*/
final class OnDemandCompiler
{
Expand Down Expand Up @@ -70,7 +69,7 @@ public function __invoke(string $dependencyIndex): void

($this->compiler)($containerObject, $this->scriptDir);
$dependency = $containerArray[$dependencyIndex];
/** @var Pointcuts $pointCuts */
/** @var Pointcut $pointCuts */
$pointCuts = $this->loadPointcuts();
$isWeaverable = $dependency instanceof Dependency && ! empty($pointCuts);
if ($isWeaverable) {
Expand All @@ -81,7 +80,7 @@ public function __invoke(string $dependencyIndex): void
(new DependencySaver($this->scriptDir))($dependencyIndex, $code);
}

/** @return Pointcuts */
/** @return Pointcut */
private function loadPointcuts(): array
{
$pointcutsPath = $this->scriptDir . ScriptInjector::AOP;
Expand All @@ -92,7 +91,7 @@ private function loadPointcuts(): array
$serialized = file_get_contents($pointcutsPath);
assert(! is_bool($serialized));
$er = error_reporting(error_reporting() ^ E_NOTICE);
/** @var Pointcuts $pointcuts */
/** @var Pointcut $pointcuts */
$pointcuts = unserialize($serialized, ['allowed_classes' => true]);
error_reporting($er);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ReflectionProperty;
use Throwable;

/** @deprecated */
final class PrivateProperty
{
/**
Expand Down
3 changes: 1 addition & 2 deletions src/ScriptInjector.php → src-deprecated/ScriptInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Ray\Di\NullModule;
use Ray\Di\ProviderSetModule;
use ReflectionParameter;

use function assert;
use function count;
use function error_log;
Expand All @@ -35,13 +34,13 @@
use function str_replace;
use function unlink;
use function unserialize;

use const DIRECTORY_SEPARATOR;
use const E_NOTICE;

/**
* @psalm-import-type ScriptDir from CompileInjector
* @psalm-import-type Ip from CompileInjector
* @deprecated Use CompileInjector instead
*/
final class ScriptInjector implements ScriptInjectorInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use Ray\Di\AbstractModule;
use Ray\Di\InjectorInterface;

/** @psalm-import-type ScriptDir from CompileInjector */
/**
* @psalm-import-type ScriptDir from CompileInjector
* @deprecated
*/
class ScriptInjectorModule extends AbstractModule
{
/** @var ScriptDir */
Expand Down
25 changes: 17 additions & 8 deletions src/AirInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
/**
* @psalm-import-type ScriptDir from CompileInjector
* @psalm-import-type Ip from CompileInjector
* @psalm-import-type Singleton from CompileInjector
* @psalm-import-type Prottype from CompileInjector
* @psalm-import-type InjectionPoint from CompileInjector
* @psalm-import-type Injector from CompileInjector
* @psalm-type Injector = callable(): InjectorInterface
*/
final class AirInjector implements InjectorInterface
{
Expand Down Expand Up @@ -69,12 +74,13 @@ public function __wakeup()
*/
public function getInstance($interface, $name = Name::ANY)
{
static $injectionPoint;
static $injector;
static $prototype;
static $singleton;
static $injector;
static $injectionPoint;

if ($prototype === null) {
/** @var InjectionPoint $injectionPoint */ // @phpstan-ignore-next-line
$injectionPoint = function (): InjectionPoint {
if ($this->ip[0] === '') {
throw new InjectionPointUnbound();
Expand All @@ -89,30 +95,33 @@ public function getInstance($interface, $name = Name::ANY)
);
};

/** @var Injector $injector */
// @phpstan-ignore-next-line
$injector = function (): self {
return $this;
};

$prototype =
/** @var Prottype $prototype */
$prototype = // @phpstan-ignore-line
/**
* @param array{0: string, 1: string, 2: string} $ip
* @param Ip $ip
*
* @return mixed
*/
function (string $dependencyIndex, array $ip = ['', '', '']) {
// @phpstan-ignore-line
$this->ip = $ip; // @phpstan-ignore-line

return require $this->getInstanceFile($dependencyIndex);
};
$singleton =

/** @var Singleton $singleton */
$singleton = // @phpstan-ignore-line
/**
* @param array{0: string, 1: string, 2: string} $ip
* @param Ip $ip
*
* @return mixed
*/
function (string $dependencyIndex, $ip = ['', '', '']) {
// @phpstan-ignore-line
if (isset($this->singletons[$dependencyIndex])) {
return $this->singletons[$dependencyIndex];
}
Expand Down
3 changes: 2 additions & 1 deletion src/CompileInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ public function compile(): void
$module = (new InstallBuiltinModule())(($this->lazyModule)());
(new FilePutContents())(sprintf('%s/_bindings.log', $this->scriptDir), (string) $module);
(new Bind($module->getContainer(), ''))->annotatedWith(ScriptDir::class)->toInstance($this->scriptDir);
(new DiCompiler($module, $this->scriptDir))->compileContainer();
(new Bind($module->getContainer(), InjectorInterface::class))->toInstance(new AirInjector($this->scriptDir));
(new Compiler())->compile($module, $this->scriptDir);
}
}
43 changes: 12 additions & 31 deletions src/InjectionPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
use Ray\Aop\ReflectionClass;
use Ray\Aop\ReflectionMethod;
use Ray\Di\Annotation\ScriptDir;
use Ray\Di\Di\Qualifier;
use Ray\Di\InjectionPointInterface;
use Ray\ServiceLocator\ServiceLocator;
use ReflectionParameter;
use RuntimeException;

use function assert;
use function class_exists;
use function file_exists;
use function file_get_contents;
use function is_bool;
use function sprintf;
use function str_replace;
use function unserialize;

/** @psalm-import-type ScriptDir from CompileInjector */
final class InjectionPoint implements InjectionPointInterface
Expand Down Expand Up @@ -88,31 +83,17 @@ public function getQualifiers(): array
*/
public function getQualifier()
{
$class = $this->parameter->getDeclaringClass();
assert($class instanceof \ReflectionClass);

$qualifierFile = sprintf(
ScriptInjector::QUALIFIER,
$this->scriptDir,
str_replace('\\', '_', $class->name),
$this->parameter->getDeclaringFunction()->name,
$this->parameter->name
);
// @codeCoverageIgnoreStart
if (! file_exists($qualifierFile)) {
return null;
$reader = ServiceLocator::getReader();
$annotations = $reader->getMethodAnnotations($this->getMethod());
foreach ($annotations as $annotation) {
$maybeQualifers = $reader->getClassAnnotations(new \ReflectionClass($annotation));
foreach ($maybeQualifers as $maybeQualifer) {
if ($maybeQualifer instanceof Qualifier) {
return $annotation;
}
}
}

// @codeCoverageIgnoreEnd

$qualifierString = file_get_contents($qualifierFile);
if (is_bool($qualifierString)) {
throw new RuntimeException(); // @codeCoverageIgnore
}

/** @var ?object $qualifier */
$qualifier = unserialize($qualifierString, ['allowed_classes' => true]);

return $qualifier;
return null;
}
}
Loading

0 comments on commit c516381

Please sign in to comment.