Skip to content

Commit

Permalink
refactor: minor code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuseppe Eletto committed Oct 25, 2022
1 parent 53b9b22 commit b4092dc
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 33 deletions.
30 changes: 18 additions & 12 deletions src/Syringe/Injector/ComponentInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@

namespace Syringe\Injector;

use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use Syringe\Attribute\{Inject, Qualifier};
use Syringe\Exception\SyringeException;
use Syringe\Repository\{Component, SyringeRepositoryFactory};
use WeakMap;

class ComponentInjector implements SyringeInjector {
/**
* @var \WeakMap
* @var WeakMap
*/
protected \WeakMap $instances;
protected WeakMap $instances;

public function __construct() {
$this->instances = new \WeakMap();
$this->instances = new WeakMap();
}

/**
* @inheritDoc
* @throws \ReflectionException
* @throws ReflectionException
* @throws SyringeException
*/
public function spawnClass(string $class): object {
$reflector = $parent = new \ReflectionClass($class);
$reflector = $parent = new ReflectionClass($class);
$instance = $reflector->newInstanceWithoutConstructor();

$properties = $reflector->getProperties();
Expand Down Expand Up @@ -50,10 +54,10 @@ public function spawnClass(string $class): object {

/**
* @inheritDoc
* @throws \ReflectionException
* @throws ReflectionException
* @throws SyringeException
*/
public function invokeMethod(object $class, \ReflectionMethod $method): mixed {
public function invokeMethod(object $class, ReflectionMethod $method): mixed {
$parameters = $method->getParameters();
$paramValues = [];

Expand All @@ -68,11 +72,12 @@ public function invokeMethod(object $class, \ReflectionMethod $method): mixed {
}

/**
* @param string $class
* @param string $class
* @param string|null $name
*
* @return object
* @throws SyringeException
* @throws \ReflectionException
* @throws ReflectionException
*/
protected function getComponentInstance(string $class, ?string $name): object {
$component = SyringeRepositoryFactory::getInstance()?->getComponent($class, $name);
Expand All @@ -95,17 +100,18 @@ protected function getComponentInstance(string $class, ?string $name): object {

/**
* @param Component $component
*
* @return object
* @throws SyringeException
* @throws \ReflectionException
* @throws ReflectionException
*/
protected function newComponentInstance(Component $component): object {
$reflector = $component->getReflector();
if ($reflector instanceof \ReflectionMethod) {
if ($reflector instanceof ReflectionMethod) {
$configInstance = $this->spawnClass($reflector->class);
return $this->invokeMethod($configInstance, $reflector);
}
if ($reflector instanceof \ReflectionClass) {
if ($reflector instanceof ReflectionClass) {
return $this->spawnClass($reflector->getName());
}
throw new SyringeException("Invalid \"{$component->getName()}\" component.");
Expand Down
10 changes: 7 additions & 3 deletions src/Syringe/Injector/SyringeInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

namespace Syringe\Injector;

use ReflectionMethod;

interface SyringeInjector {
/**
* @param string $class
*
* @return object
*/
public function spawnClass(string $class): object;

/**
* @param object $class
* @param \ReflectionMethod $method
* @param object $class
* @param ReflectionMethod $method
*
* @return mixed
*/
public function invokeMethod(object $class, \ReflectionMethod $method): mixed;
public function invokeMethod(object $class, ReflectionMethod $method): mixed;
}
12 changes: 7 additions & 5 deletions src/Syringe/Repository/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@

class Component {
/**
* @param bool $primary
* @param bool $primary
* @param string|null $name
* @param bool $singleton
* @param Reflector $reflector
* @param bool $singleton
* @param Reflector $reflector
*/
public function __construct(
protected bool $primary,
protected ?string $name,
protected bool $singleton,
protected Reflector $reflector
) { }
) {
}

/**
* @return bool
Expand Down Expand Up @@ -73,8 +74,9 @@ public function getReflector(): Reflector {
}

/**
* @param Provides $provides
* @param Provides $provides
* @param Reflector $reflection
*
* @return Component
*/
public static function fromProvidesAttribute(Provides $provides, Reflector $reflection): self {
Expand Down
6 changes: 4 additions & 2 deletions src/Syringe/Repository/ComponentBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Syringe\Repository;

use Syringe\Exception\ComponentNotFoundException;
use Syringe\Exception\{ComponentNotFoundException, SyringeException};

class ComponentBucket {
/**
Expand All @@ -22,7 +22,8 @@ public function __construct() {

/**
* @param Component $component
* @throws \Syringe\Exception\SyringeException
*
* @throws SyringeException
*/
public function addComponent(Component $component): void {
if (!count($this->components)) {
Expand All @@ -39,6 +40,7 @@ public function addComponent(Component $component): void {

/**
* @param string|null $name
*
* @return Component
* @throws ComponentNotFoundException
*/
Expand Down
16 changes: 10 additions & 6 deletions src/Syringe/Repository/ComponentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Syringe\Repository;

use ReflectionClass;
use ReflectionException;
use Syringe\Attribute\Provides;
use Syringe\Exception\ComponentNotFoundException;
use Syringe\Exception\{ComponentNotFoundException, SyringeException};

class ComponentRepository implements SyringeRepository {
/**
Expand All @@ -17,8 +19,9 @@ public function __construct() {

/**
* @param string[] $classes
* @throws \ReflectionException
* @throws \Syringe\Exception\SyringeException
*
* @throws ReflectionException
* @throws SyringeException
*/
public function addConfigurations(string ...$classes): void {
foreach ($classes as $config) {
Expand All @@ -28,11 +31,12 @@ public function addConfigurations(string ...$classes): void {

/**
* @param string $class
* @throws \ReflectionException
* @throws \Syringe\Exception\SyringeException
*
* @throws ReflectionException
* @throws SyringeException
*/
public function addConfiguration(string $class): void {
$reflector = new \ReflectionClass($class);
$reflector = new ReflectionClass($class);
$methods = $reflector->getMethods();

foreach ($methods as $method) {
Expand Down
3 changes: 2 additions & 1 deletion src/Syringe/Repository/SyringeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

interface SyringeRepository {
/**
* @param string $class
* @param string $class
* @param string|null $name
*
* @return Component
*/
public function getComponent(string $class, ?string $name): Component;
Expand Down
11 changes: 7 additions & 4 deletions src/Syringe/Syringe.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Syringe;

use Syringe\Exception\SyringeException;
use ReflectionException;
use Syringe\Exception\{ComponentNotFoundException, SyringeException};
use Syringe\Injector\{ComponentInjector, SyringeInjector};
use Syringe\Repository\{SyringeRepository, SyringeRepositoryFactory};

Expand All @@ -14,10 +15,11 @@ class Syringe {

/**
* @param string $class
*
* @return object
* @throws \Syringe\Exception\ComponentNotFoundException
* @throws ComponentNotFoundException
* @throws SyringeException
* @throws \ReflectionException
* @throws ReflectionException
*/
public static function new(string $class): object {
if (!self::$injector) {
Expand All @@ -27,8 +29,9 @@ public static function new(string $class): object {
}

/**
* @param SyringeRepository $repository
* @param SyringeRepository $repository
* @param SyringeInjector|null $injector
*
* @return void
*/
public static function initialize(SyringeRepository $repository, ?SyringeInjector $injector = null): void {
Expand Down

0 comments on commit b4092dc

Please sign in to comment.