Skip to content

Commit

Permalink
Merge pull request #35 from abmmhasan/feature/improvement-j
Browse files Browse the repository at this point in the history
Updated make
  • Loading branch information
abmmhasan committed Jan 29, 2024
2 parents 09e68ba + f0fd92f commit 4af3a99
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 21 deletions.
13 changes: 8 additions & 5 deletions docs/di/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Simply, initialize using either of these lines,

.. code-block:: php
$container = AbmmHasan\InterMix\container();
$container = AbmmHasan\InterMix\container(); // recommended
$container = AbmmHasan\InterMix\DI\Container::instance();
$container = new AbmmHasan\InterMix\DI\Container();
Expand Down Expand Up @@ -83,10 +83,13 @@ cached. In case of ``call()`` returns from method/closure are never cached (but
make(string $class, string|bool $method = false)
------------------------------------------------

This library only builds single instance per class (Singleton). But sometime we may need class instance independently.
``container()`` have 2nd parameter for completely different container instance for that. But what if we need only one
new instance for a class but other dependencies from cached? well, that is what ``make()`` is for! Also, as the definition
it supports class only.
This library only builds single instance per class (Singleton). But sometime we may need class instance/values independently.

Though ``container()`` have 2nd parameter for completely different container instance but what if we need only one
new instance for a single class but other dependencies from existing?
Well, that is what ``make()`` is for! Also, it supports classes only.

Providing 2nd parameter will get return from method execution. If not provided, will get the class instance only.

registerClass(string $class, array $parameters = [])
----------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
Expand All @@ -11,4 +12,7 @@
$rectorConfig->sets([
constant("Rector\Set\ValueObject\LevelSetList::UP_TO_PHP_82")
]);
$rectorConfig->skip([
ReadOnlyPropertyRector::class
]);
};
5 changes: 3 additions & 2 deletions src/DI/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class Container implements ContainerInterface
{
protected static array $instances;
protected readonly Repository $repository;
protected Repository $repository;
protected string $resolver = InjectedCall::class;

/**
Expand Down Expand Up @@ -253,7 +253,8 @@ public function call(
*/
public function make(string $class, string|bool $method = false): mixed
{
return (new $this->resolver($this->repository))->classSettler($class, $method, true);
$resolved = (new $this->resolver($this->repository))->classSettler($class, $method, true);
return $method ? $resolved['returned'] : $resolved['instance'];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Resolver/ClassResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ClassResolver
* @param DefinitionResolver $definitionResolver The definition resolver object.
*/
public function __construct(
private readonly Repository $repository,
private Repository $repository,
private readonly ParameterResolver $parameterResolver,
private readonly PropertyResolver $propertyResolver,
private readonly DefinitionResolver $definitionResolver
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Resolver/DefinitionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DefinitionResolver
* @param Repository $repository The repository object.
*/
public function __construct(
private readonly Repository $repository
private Repository $repository
) {
}

Expand Down
6 changes: 3 additions & 3 deletions src/DI/Resolver/ParameterResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ParameterResolver
* @param DefinitionResolver $definitionResolver The definition resolver object.
*/
public function __construct(
private readonly Repository $repository,
private Repository $repository,
private readonly DefinitionResolver $definitionResolver
) {
$this->stdClass = new StdClass();
Expand Down Expand Up @@ -180,7 +180,7 @@ private function resolveAssociativeParameters(
continue;
}

$class = $this->getResolvableClassReflection($reflector, $classParameter, $type, $processed);
$class = $this->getResolvableReflection($reflector, $classParameter, $type, $processed);
if ($class) {
$name = $class->isInterface() ? $class->getName() : $parameterName;
$processed[$parameterName] = $this->resolveClassDependency(
Expand Down Expand Up @@ -233,7 +233,7 @@ private function resolveAssociativeParameters(
* @return ReflectionClass|null The reflection class of the resolvable parameter, or null if not resolvable.
* @throws ContainerException|ReflectionException circular dependency or multiple instances for the same class
*/
private function getResolvableClassReflection(
private function getResolvableReflection(
ReflectionFunctionAbstract $reflector,
ReflectionParameter $parameter,
string $type,
Expand Down
5 changes: 1 addition & 4 deletions src/DI/Resolver/PropertyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PropertyResolver
* @param ParameterResolver $parameterResolver The parameter resolver object.
*/
public function __construct(
private readonly Repository $repository,
private Repository $repository,
private readonly ParameterResolver $parameterResolver
) {
}
Expand Down Expand Up @@ -96,9 +96,6 @@ private function resolveProperties(ReflectionClass $class, array $properties, ob
continue;
}

// required for PHP 8.0 only
$property->setAccessible(true);

$values = $this->resolveValue($property, $classPropertyValues, $classInstance);
!$values ?: $property->setValue(...$values);
}
Expand Down
13 changes: 13 additions & 0 deletions src/DI/Resolver/Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use AbmmHasan\InterMix\DI\Reflection\ReflectionResource;
use Closure;
use ReflectionClass;
use ReflectionEnum;
use ReflectionException;
use ReflectionFunction;

Expand All @@ -22,6 +23,18 @@ private function reflectedClass(string|object $class): ReflectionClass
return $this->repository->resolvedResource[$class]['reflection'] ??= ReflectionResource::getForClass($class);
}

/**
* Returns a ReflectionEnum object based on the given name.
*
* @param string $name The name of the enum to reflect
* @return ReflectionEnum The reflection object for the given enum name
* @throws ReflectionException If the ReflectionClass cannot be obtained.
*/
private function reflectedEnum(string $name): ReflectionEnum
{
return $this->repository->resolvedEnum[$name]['reflection'] ??= new ReflectionEnum($name);
}

/**
* Returns a ReflectionFunction object based on the provided closure and name.
*
Expand Down
1 change: 1 addition & 0 deletions src/DI/Resolver/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Repository
public array $closureResource = [];
public array $resolved = [];
public array $resolvedResource = [];
public array $resolvedEnum = [];
public array $resolvedFunction = [];
public array $resolvedDefinition = [];
public bool $enablePropertyAttribute = false;
Expand Down
10 changes: 5 additions & 5 deletions tests/Feature/InjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
$randomInitConstructor1 = $instance1['instance']->getValues()['random'];
$instance2 = $classInit->call(ClassInit::class);
$randomInitConstructor2 = $instance2['instance']->getValues()['random'];
$instance3 = $classInit->make(ClassInit::class);
$randomInitConstructor3 = $instance3['instance']->getValues()['random'];
$instance4 = $classInit->make(ClassInit::class);
$randomInitConstructor4 = $instance4['instance']->getValues()['random'];
$instance3 = $classInit->make(ClassInit::class, 'getValues');
$randomInitConstructor3 = $instance3['random'];
$instance4 = $classInit->make(ClassInit::class, 'getValues');
$randomInitConstructor4 = $instance4['random'];
$instance5 = $classInit->call(ClassInit::class);
$randomInitConstructor5 = $instance5['instance']->getValues()['random'];

Expand All @@ -46,4 +46,4 @@ function () use ($randomInitConstructor1, $randomInitConstructor3, $randomInitCo
->and($randomInitConstructor3)->not->toEqual($randomInitConstructor4)
->and($randomInitConstructor4)->not->toEqual($randomInitConstructor1);
}
);
);

0 comments on commit 4af3a99

Please sign in to comment.