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
3 changes: 2 additions & 1 deletion src/Facades/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace Rudra\Container\Facades;

use Rudra\Container\{Traits\FacadeTrait, Interfaces\ContainerInterface};
use Rudra\Container\Traits\FacadeTrait;
use Rudra\Container\Interfaces\ContainerInterface;

/**
* @method static ContainerInterface get()
Expand Down
16 changes: 7 additions & 9 deletions src/Facades/Rudra.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@

namespace Rudra\Container\Facades;

use Rudra\Container\{
Cookie,
Session,
Interfaces\RudraInterface,
Interfaces\RequestInterface,
Interfaces\ResponseInterface,
Interfaces\ContainerInterface
};
use Rudra\Container\Cookie;
use Rudra\Container\Session;
use Rudra\Container\Interfaces\RudraInterface;
use Rudra\Container\Interfaces\RequestInterface;
use Rudra\Container\Interfaces\ResponseInterface;
use Rudra\Container\Interfaces\ContainerInterface;

/**
* @method static Cookie cookie()
Expand All @@ -35,7 +33,7 @@
* @method static ContainerInterface waiting(array $services = [])
* @method static ContainerInterface binding(array $contracts = [])
* @method static mixed autowire($object, string $method, ?array $params = null)
* @method static array getParamsIoC(ReflectionMethod $constructor, ?array $params)
* @method static array getParamsIoC(\ReflectionMethod $constructor, ?array $params)
*
* @see \Rudra\Container\Rudra
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Request implements RequestInterface
/**
* @return ContainerInterface
*/
#[\Override]
public function get(): ContainerInterface
{
return $this->containerize("get", Container::class, $_GET);
Expand All @@ -33,6 +34,7 @@ public function get(): ContainerInterface
/**
* @return ContainerInterface
*/
#[\Override]
public function post(): ContainerInterface
{
return $this->containerize("post", Container::class, $_POST);
Expand All @@ -41,6 +43,7 @@ public function post(): ContainerInterface
/**
* @return ContainerInterface
*/
#[\Override]
public function put(): ContainerInterface
{
return $this->containerize("put", Container::class);
Expand All @@ -49,6 +52,7 @@ public function put(): ContainerInterface
/**
* @return ContainerInterface
*/
#[\Override]
public function patch(): ContainerInterface
{
return $this->containerize("patch", Container::class);
Expand All @@ -57,6 +61,7 @@ public function patch(): ContainerInterface
/**
* @return ContainerInterface
*/
#[\Override]
public function delete(): ContainerInterface
{
return $this->containerize("delete", Container::class);
Expand All @@ -65,6 +70,7 @@ public function delete(): ContainerInterface
/**
* @return ContainerInterface
*/
#[\Override]
public function server(): ContainerInterface
{
return $this->containerize("server", Container::class, $_SERVER);
Expand All @@ -73,6 +79,7 @@ public function server(): ContainerInterface
/**
* @return ContainerInterface
*/
#[\Override]
public function files(): ContainerInterface
{
return $this->containerize("files", Container::class, $_FILES);
Expand Down
9 changes: 7 additions & 2 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ class Response implements ResponseInterface
* @param array $data
* @return void
*/
public function json(array $data): void
#[\Override]
public function json(array $data, int $code = 200): void
{
header("Content-Type: application/json");
if (!headers_sent()) {
http_response_code($code);
header("Content-Type: application/json");
}

print $this->getJson($data);
}

Expand Down
91 changes: 46 additions & 45 deletions src/Rudra.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,43 @@

namespace Rudra\Container;

use Closure;
use ReflectionClass;
use ReflectionMethod;
use Rudra\Exceptions\{
LogicException,
NotFoundException
};
use Rudra\Container\{
Interfaces\RudraInterface,
Traits\InstantiationsTrait,
Interfaces\FactoryInterface,
};
use Rudra\Exceptions\LogicException;
use Rudra\Exceptions\NotFoundException;
use Rudra\Container\Interfaces\RudraInterface;
use Rudra\Container\Traits\InstantiationsTrait;
use Rudra\Container\Interfaces\FactoryInterface;
use Psr\Container\ContainerInterface;

/**
* @method waiting() Возвращает контейнер для временных данных (waiting).
* @method binding() Возвращает контейнер для связываний (binding).
* @method services() Возвращает контейнер для сервисов (services).
* @method waiting() Returns a container for temporary data / Возвращает контейнер для временных данных
* @method binding() Returns a container for bindings / Возвращает контейнер для связываний
* @method services() Returns a container for services / Возвращает контейнер для сервисов
*/
class Rudra implements RudraInterface, ContainerInterface
{
use InstantiationsTrait;

public static ?RudraInterface $rudra = null;

protected array $allowedContainersMap = [
'waiting' => true,
'binding' => true,
'services' => true,
'shared' => true,
'config' => true
];

protected array $allowedInstances = [
'request' => Request::class,
'response' => Response::class,
'cookie' => Cookie::class,
'session' => Session::class
];
protected readonly array $allowedContainersMap;
protected readonly array $allowedInstances;

public function __construct()
{
$this->allowedContainersMap = [
'waiting' => true,
'binding' => true,
'services' => true,
'shared' => true,
'config' => true
];
$this->allowedInstances = [
'request' => Request::class,
'response' => Response::class,
'cookie' => Cookie::class,
'session' => Session::class
];
}

/**
* Handles dynamic method calls for the class.
Expand All @@ -68,9 +66,9 @@ class Rudra implements RudraInterface, ContainerInterface
*
* @param string $method
* @param array $parameters
* @return void
* @return mixed
*/
public function __call(string $method, array $parameters = [])
public function __call(string $method, array $parameters = []): mixed
{
if (isset($this->allowedContainersMap[$method])) {
$data = $parameters[0] ?? [];
Expand All @@ -81,7 +79,7 @@ public function __call(string $method, array $parameters = [])
return $this->init($this->allowedInstances[$method]);
}

throw new LogicException("{$method}' is not allowed.");
throw new LogicException("Method '{$method}' is not allowed.");
}

/**
Expand All @@ -93,6 +91,7 @@ public function __call(string $method, array $parameters = [])
*
* @return RudraInterface
*/
#[\Override]
public static function run(): RudraInterface
{
if (!isset(static::$rudra)) {
Expand All @@ -116,6 +115,7 @@ public static function run(): RudraInterface
* @param string $id
* @return mixed
*/
#[\Override]
public function get(string $id): mixed
{
if ($this->has($id)) {
Expand All @@ -133,7 +133,7 @@ public function get(string $id): mixed

$waiting = $waitingStorage->get($id);

if ($waiting instanceof Closure) {
if ($waiting instanceof \Closure) {
return $waiting();
}

Expand Down Expand Up @@ -218,7 +218,7 @@ private function handleArrayObject(string $key, array $object): void
*/
private function resolveSetValue(mixed $value): mixed
{
if ($value instanceof Closure) {
if ($value instanceof \Closure) {
return $value();
}

Expand Down Expand Up @@ -250,6 +250,7 @@ private function isFactoryImplementation(mixed $value): bool
* @param string $id
* @return boolean
*/
#[\Override]
public function has(string $id): bool
{
return $this->services()->has($id);
Expand Down Expand Up @@ -297,7 +298,7 @@ private function setObject(string $key, string|object $object): void
*/
private function iOc(string $key, string $object, ?array $params = null): void
{
$reflection = new ReflectionClass($object);
$reflection = new \ReflectionClass($object);
$constructor = $reflection->getConstructor();

if ($constructor && $constructor->getNumberOfParameters() > 0) {
Expand Down Expand Up @@ -329,15 +330,15 @@ public function new(string $object, ?array $params = null): object
throw new LogicException("Class {$object} does not exist");
}

$reflection = new ReflectionClass($object);
$reflection = new \ReflectionClass($object);
$constructor = $reflection->getConstructor();

if ($constructor && $constructor->getNumberOfParameters() > 0) {
$args = $this->getParamsIoC($constructor, $params);
return $reflection->newInstanceArgs($args);
}

return $reflection->newInstanceWithoutConstructor();
return new $object();
}

/**
Expand All @@ -349,14 +350,14 @@ public function new(string $object, ?array $params = null): object
* Использует рефлексию для анализа параметров метода и разрешает их с помощью `getParamsIoC`.
* Если метод не имеет параметров, он вызывается напрямую. В противном случае разрешённые аргументы передаются при вызове.
*
* @param $object
* @param string $method
* @param array|null $params
* @param object|string $object
* @param string $method
* @param array|null $params
* @return mixed
*/
public function autowire($object, string $method, ?array $params = null): mixed
public function autowire(object|string$object, string $method, ?array $params = null): mixed
{
$reflectionMethod = new ReflectionMethod($object, $method);
$reflectionMethod = new \ReflectionMethod($object, $method);

if ($reflectionMethod->getNumberOfParameters() === 0) {
return $reflectionMethod->invoke($object);
Expand All @@ -376,11 +377,11 @@ public function autowire($object, string $method, ?array $params = null): mixed
* Обрабатывает каждый параметр конструктора, разрешая зависимости с использованием привязок, имён классов или значений по умолчанию.
* Если параметр не может быть разрешён, используется предоставленный массив `$params`.
*
* @param ReflectionMethod $constructor
* @param \ReflectionMethod $constructor
* @param array|null $params
* @return array
*/
public function getParamsIoC(ReflectionMethod $constructor, ?array $params): array
public function getParamsIoC(\ReflectionMethod $constructor, ?array $params): array
{
$i = 0;
$params = is_array($params) ? array_values($params) : [$params];
Expand Down Expand Up @@ -432,7 +433,7 @@ public function getParamsIoC(ReflectionMethod $constructor, ?array $params): arr
*/
private function resolveDependency($className): object
{
if ($className instanceof Closure) {
if ($className instanceof \Closure) {
return $className();
}

Expand Down
5 changes: 5 additions & 0 deletions src/Traits/SetRudraContainersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Rudra\Container\Traits;

use Rudra\Exceptions\LogicException;
use Rudra\Container\Interfaces\RudraInterface;

trait SetRudraContainersTrait
Expand All @@ -25,6 +26,10 @@ public function __construct(private RudraInterface $rudra) {}
*/
public function rudra(): RudraInterface
{
if (!isset($this->rudra)) {
throw new LogicException('Rudra instance not initialized. Did you override __construct()?');
}

return $this->rudra;
}
}
5 changes: 4 additions & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Rudra\Container\Facades\Rudra;
use Rudra\Container\Facades\Response;
use Rudra\Exceptions\NotFoundException;

if (!function_exists('data')) {
function data(mixed $data = null): mixed
Expand Down Expand Up @@ -41,7 +42,9 @@ function config(?string $key, ?string $subKey = null): mixed
return $data;
}

return is_array($data) && isset($data[$subKey]) ? $data[$subKey] : false;
return (is_array($data) && array_key_exists($subKey, $data))
? $data[$subKey]
: throw new NotFoundException("Конфигурационный ключ \"$key.$subKey\" не найден.");
}
}

Expand Down
15 changes: 11 additions & 4 deletions tests/ConfigFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use PHPUnit\Framework\TestCase;
use Rudra\Container\Facades\Rudra;
use Rudra\Exceptions\NotFoundException;

class ConfigFunctionTest extends TestCase
{
Expand Down Expand Up @@ -47,14 +48,20 @@ public function testReturnsSubKeyWhenPresent()
$this->assertSame($value, config('app', 'name'));
}

public function testReturnsFalseWhenSubKeyDoesNotExist()
public function testThrowsExceptionWhenSubKeyDoesNotExist(): void
{
$this->assertFalse(config('app', 'non_existing_key'));
$this->expectException(NotFoundException::class);
$this->expectExceptionMessage('Конфигурационный ключ "app.non_existing_key" не найден.');

config('app', 'non_existing_key');
}

public function testReturnsFalseWhenValueIsNotArray()
public function testThrowsExceptionWhenValueIsNotArray(): void
{
// Предположим, что 'version' — это строка, а не массив
$this->assertFalse(config('version', 'subkey'));
$this->expectException(NotFoundException::class);
$this->expectExceptionMessage('Конфигурационный ключ "version.subkey" не найден.');

config('version', 'subkey');
}
}
Loading