Skip to content

Commit

Permalink
Merge pull request #7 from lion-packages/bug
Browse files Browse the repository at this point in the history
Fix to get routes on Windows operating systems
  • Loading branch information
Sleon4 authored Feb 25, 2024
2 parents 8f9c644 + 01bbdd5 commit 72a0aca
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 34 deletions.
52 changes: 26 additions & 26 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 100 additions & 3 deletions src/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,66 @@
use Lion\Helpers\Str;
use ReflectionClass;
use ReflectionFunction;
use ReflectionMethod;
use ReflectionFunctionAbstract;
use ReflectionParameter;

/**
* Container to generate dependency injection
*
* @package Lion\DependencyInjection
*/
class Container
{
/**
* [Object of class DIContainer]
*
* @var DIContainer $container
*/
private DIContainer $container;

/**
* [Object of class Str]
*
* @var Str $str
*/
private Str $str;

/**
* Class constructor
*/
public function __construct()
{
$this->container = (new ContainerBuilder())->useAutowiring(true)->useAttributes(true)->build();
$this->str = new Str();
}

/**
* Normalize routes depending on OS type
*
* @param string $path [Defined route]
*
* @return string
*/
public function normalizePath(string $path): string
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$path = str_replace('/', '\\', $path);
$path = str_replace("\\\\", "\\", $path);
} else {
$path = str_replace('\\', '/', $path);
$path = str_replace('//', '/', $path);
}

return $path;
}

/**
* Get files from a defined path
*
* @param string $folder [Defined route]
*
* @return array<string>
*/
public function getFiles(string $folder): array
{
$files = [];
Expand All @@ -36,22 +82,39 @@ public function getFiles(string $folder): array
if (is_dir($path)) {
$files = array_merge($files, $this->getFiles($path));
} else {
$files[] = realpath($path);
$files[] = $this->normalizePath($path);
}
}
}

return $files;
}

/**
* Gets the namespace of a class through a defined path
*
* @param string $file [File path]
* @param string $namespace [Namespace for the file]
* @param string $split [Separator to obtain the namespace]
*
* @return string
*/
public function getNamespace(string $file, string $namespace, string $split = '/'): string
{
$splitFile = explode($split, $file);

return $this->str->of("{$namespace}{$splitFile[1]}")->replace("/", "\\")->replace('.php', '')->trim()->get();
}

private function getParameters(ReflectionMethod|ReflectionFunction $method, array $params = []): array
/**
* Gets the parameters of a function
*
* @param ReflectionFunctionAbstract $method [Method obtained]
* @param array $params [Array of defined parameters]
*
* @return array
*/
private function getParameters(ReflectionFunctionAbstract $method, array $params = []): array
{
$args = [];

Expand All @@ -70,20 +133,46 @@ private function getParameters(ReflectionMethod|ReflectionFunction $method, arra
return $args;
}

/**
* Inject dependencies into a method of a class
*
* @param object $object [Class object]
* @param string $method [Method name]
* @param array $params [Array of defined parameters]
*
* @return mixed
*/
public function injectDependenciesMethod(object $object, string $method, array $params = []): mixed
{
$method = (new ReflectionClass($object))->getMethod($method);

return $method->invoke($object, ...$this->getParameters($method, $params));
}

/**
* Inject dependencies to a callback
*
* @param Closure $closure [Defined callback]
* @param array $params [Array of defined parameters]
*
* @return mixed
*/
public function injectDependenciesCallback(Closure $closure, array $params = []): mixed
{
$method = new ReflectionFunction($closure);

return $method->invoke(...$this->getParameters($method, $params));
}

/**
* Inject dependencies to methods of a class that have the annotation
* '@required'
*
* @param object $object [Class object]
* @param array $params [Array of defined parameters]
*
* @return object
*/
public function injectDependencies(object $object, array $params = []): object
{
foreach ((new ReflectionClass($object))->getMethods() as $method) {
Expand All @@ -99,6 +188,14 @@ public function injectDependencies(object $object, array $params = []): object
return $object;
}

/**
* Gets the data type of the parameters obtained
*
* @param ReflectionParameter $parameter [Defined parameter of type
* ReflectionParameter]
*
* @return null|string
*/
private function getParameterClassName(ReflectionParameter $parameter): ?string
{
$type = $parameter->getType();
Expand Down
25 changes: 20 additions & 5 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@
use ReflectionMethod;
use ReflectionParameter;
use Tests\Provider\ClassProvider;
use Tests\Provider\ContainerProviderTrait;
use Tests\Provider\CustomClass;
use Tests\Provider\ExtendsProvider;
use Tests\Provider\FactoryProvider;

class ContainerTest extends Test
{
use ContainerProviderTrait;

const STR = 'test';
const FOLDER = './tests/';
const PATH_FILE = './Provider/CustomClass.php';
const FILES = [
'/var/www/html/tests/ContainerTest.php',
'/var/www/html/tests/Provider/ClassProvider.php',
'/var/www/html/tests/Provider/CustomClass.php',
'/var/www/html/tests/Provider/ExtendsProvider.php',
'/var/www/html/tests/Provider/FactoryProvider.php'
'./tests/ContainerTest.php',
'./tests/Provider/ClassProvider.php',
'./tests/Provider/ContainerProviderTrait.php',
'./tests/Provider/CustomClass.php',
'./tests/Provider/ExtendsProvider.php',
'./tests/Provider/FactoryProvider.php'
];
const REFLECTION_PARAMETERS = [CustomClass::class, 'setFactoryProvider'];

Expand All @@ -46,6 +50,17 @@ public function testConstruct(): void
$this->assertInstanceOf(Str::class, $this->getPrivateProperty('str'));
}

/**
* @dataProvider normalizePathProvider
*/
public function testNormalizePath(string $path, string $return): void
{
$filePath = $this->container->normalizePath($path);

$this->assertIsString($filePath);
$this->assertSame($return, $filePath);
}

public function testGetFiles(): void
{
$files = $this->container->getFiles(self::FOLDER);
Expand Down
22 changes: 22 additions & 0 deletions tests/Provider/ContainerProviderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Tests\Provider;

trait ContainerProviderTrait
{
public static function normalizePathProvider(): array
{
return [
[
'path' => './Provider/CustomClass.php',
'return' => './Provider/CustomClass.php'
],
[
'path' => '.\Provider\CustomClass.php',
'return' => './Provider/CustomClass.php'
]
];
}
}

0 comments on commit 72a0aca

Please sign in to comment.