Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ You can register callables in your di. For example for static inits of classes,

You can just register them by using the ```registerCallable(string $className, callable $callback)``` method of the Container.

You can load the entry by using the ```loadCallable(string $className)``` from the container. Note the method executes the callable method.
You can load the entry by using the ```load(string $className)``` from the container. Note the method executes the callable method.

### 11. Register classes with static parameters
You can register classes with static parameters for example defaultUserId or something else.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
},
"config": {
"platform": {
"php": "8.2"
"php": "8.3"
}
},
"require": {
"php": ">=8.2",
"php": ">=8.3",
"phpunit/phpunit": "^9.6",
"phpspec/prophecy-phpunit": "^1.1.0"
},
Expand Down
1,165 changes: 775 additions & 390 deletions composer.lock

Large diffs are not rendered by default.

26 changes: 19 additions & 7 deletions lib/DependencyInjection/DependencyInjectionContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Therion86\Framework\DependencyInjection;

use JetBrains\PhpStorm\Deprecated;
use Therion86\Framework\Exceptions\ClassNotRegisteredException;
use Therion86\Framework\Exceptions\ConstructorParameterTypeNotFoundException;
use ReflectionClass;
Expand All @@ -30,7 +31,7 @@ public function register(string $classLabel, ?string $className = null, array $p

public function registerCallable(string $className, callable $callable): void
{
$this->statics[$className] = $callable;
$this->container[$className] = $callable;
}

/**
Expand All @@ -44,23 +45,34 @@ public function registerCallable(string $className, callable $callable): void
public function load(string $className): ?object
{
if (in_array($className, [DependencyInjection::class, HttpDependencyInjection::class, CliDependencyInjection::class])) {
return $this->dependencyInjection;
$di = $this->dependencyInjection;
/** @var T $di */
return $di;
}
if (!isset($this->container[$className])) {
throw new ClassNotRegisteredException('Class ' . $className . ' was not registered');
}
if (is_callable($this->container[$className])) {
return $this->container[$className]();
}
if (isset($this->parameters[$className])) {
return $this->getObjectWithParameters($className);
}
return $this->getDependenciesByReflection($className);
}

public function loadCallable(string $className): object
/**
* @deprecated use load() instead
* @template T
* @param class-string<T> $className
* @return T|null
* @throws ReflectionException
* @throws ClassNotRegisteredException
* @throws ConstructorParameterTypeNotFoundException
*/
#[Deprecated(reason: 'Use load() instead')]public function loadCallable(string $className): object
{
if (!isset($this->statics[$className])) {
throw new ClassNotRegisteredException('Class ' . $className . ' was not registered');
}
return $this->statics[$className]();
return $this->load($className);
}


Expand Down
4 changes: 2 additions & 2 deletions lib/DependencyInjection/HttpDependencyInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getRouter(): HttpRouterInterface
public function generateResponse(string $body = '', int $statusCode = 200, array $headers = []): ResponseInterface
{
try {
$response = $this->getContainer()->loadCallable(ResponseInterface::class);
$response = $this->getContainer()->load(ResponseInterface::class);
} catch (Throwable) {
$response = new HttpResponse('');
}
Expand All @@ -90,7 +90,7 @@ public function generateResponse(string $body = '', int $statusCode = 200, array
public function getRequest(): HttpRequestInterface
{
try {
$request = $this->getContainer()->loadCallable(HttpRequestInterface::class);
$request = $this->getContainer()->load(HttpRequestInterface::class);
} catch (Throwable) {
// Default use HttpRequest if no request was set in di container
$request = HttpRequest::fromGlobals();
Expand Down
8 changes: 5 additions & 3 deletions tests/DependencyInjection/CliDependencyInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Exception;
use Therion86\Framework\DependencyInjection\CliDependencyInjection;
use stdClass;
use Therion86\Framework\Request\HttpRequest;
use Therion86\Framework\Response\HttpResponse;


/**
Expand Down Expand Up @@ -58,11 +60,11 @@ public function testCallableServicesWhereAddedToContainer(): void
public function testServicesWhereAddedToContainerWithParam(): void
{
$_SERVER['argv'] = ['index.php', 'example', '--name=Test'];
$di = new CliDependencyInjection([], [\Therion86\Framework\Request\HttpRequest::class => ['', '', [], [], '']]);
$di = new CliDependencyInjection([], [HttpResponse::class => ['', 200,[]]]);

$this->assertInstanceOf(
\Therion86\Framework\Request\HttpRequest::class,
$di->getContainer()->load(\Therion86\Framework\Request\HttpRequest::class)
HttpResponse::class,
$di->getContainer()->load(HttpResponse::class)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Therion86\Framework\Exceptions\ConstructorParameterTypeNotFoundException;
use Therion86\Framework\Request\HttpRequest;
use PHPUnit\Framework\TestCase;
use Therion86\Framework\Response\HttpResponse;

/**
* @covers \Therion86\Framework\DependencyInjection\DependencyInjectionContainer
Expand All @@ -31,9 +32,9 @@ public function testRegisterWithParams(): void
{
$di = new HttpDependencyInjection([], []);
$dic = new DependencyInjectionContainer($di);
$dic->register(HttpRequest::class, HttpRequest::class, ['', '', [],[],'']);
$dic->register(HttpResponse::class, HttpResponse::class, ['', 200, []]);

$this->assertInstanceOf(HttpRequest::class, $dic->load(HttpRequest::class));
$this->assertInstanceOf(HttpResponse::class, $dic->load(HttpResponse::class));
}


Expand Down
8 changes: 5 additions & 3 deletions tests/DependencyInjection/HttpDependencyInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Therion86\Framework\Request\HttpRequest;
use PHPUnit\Framework\TestCase;
use stdClass;
use Therion86\Framework\Response\HttpResponse;

/**
* @covers \Therion86\Framework\DependencyInjection\HttpDependencyInjection
Expand All @@ -37,6 +38,7 @@ public function testModulesWhereAddedToContainer(): void
$_SERVER['REQUEST_URI'] = '/1';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['REQUEST_METHOD'] = 'get';
$_SERVER['HTTP_HOST'] = 'test';

$di = new HttpDependencyInjection([Factory::class], []);
$di->getRouter()->route();
Expand Down Expand Up @@ -72,11 +74,11 @@ public function testServicesWhereAddedToContainerWithParams(): void
$_SERVER['REQUEST_URI'] = '/1';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['REQUEST_METHOD'] = 'get';
$di = new HttpDependencyInjection([], [HttpRequest::class => ['', '', [], [], '']]);
$di = new HttpDependencyInjection([], [HttpResponse::class => ['', 200, []]]);

$this->assertInstanceOf(
HttpRequest::class,
$di->getContainer()->load(HttpRequest::class)
HttpResponse::class,
$di->getContainer()->load(HttpResponse::class)
);
}

Expand Down