diff --git a/composer.lock b/composer.lock index fa428d6..7a3209d 100644 --- a/composer.lock +++ b/composer.lock @@ -68,16 +68,16 @@ }, { "name": "lion/helpers", - "version": "v3.1.0", + "version": "v3.1.1", "source": { "type": "git", "url": "https://github.com/lion-packages/helpers.git", - "reference": "34799142861db5292263e0d6ca5728b581a7973c" + "reference": "5ccb8bee5a466be868ead096c594beb7a43f7be1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/lion-packages/helpers/zipball/34799142861db5292263e0d6ca5728b581a7973c", - "reference": "34799142861db5292263e0d6ca5728b581a7973c", + "url": "https://api.github.com/repos/lion-packages/helpers/zipball/5ccb8bee5a466be868ead096c594beb7a43f7be1", + "reference": "5ccb8bee5a466be868ead096c594beb7a43f7be1", "shasum": "" }, "require": { @@ -100,9 +100,9 @@ "description": "easy to use helpers for any type of use", "support": { "issues": "https://github.com/lion-packages/helpers/issues", - "source": "https://github.com/lion-packages/helpers/tree/v3.1.0" + "source": "https://github.com/lion-packages/helpers/tree/v3.1.1" }, - "time": "2024-01-19T02:47:44+00:00" + "time": "2024-02-13T14:48:04+00:00" }, { "name": "php-di/invoker", @@ -289,16 +289,16 @@ "packages-dev": [ { "name": "lion/test", - "version": "v1.4.0", + "version": "v1.5.0", "source": { "type": "git", - "url": "https://github.com/lion-packages/test.git", - "reference": "c0eed0c33478b27a521258c94b053dd68d8e7bbc" + "url": "https://github.com/Sleon4/test.git", + "reference": "b087d55b4a18eb4e50afe52d4a747122e7d9f933" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/lion-packages/test/zipball/c0eed0c33478b27a521258c94b053dd68d8e7bbc", - "reference": "c0eed0c33478b27a521258c94b053dd68d8e7bbc", + "url": "https://api.github.com/repos/Sleon4/test/zipball/b087d55b4a18eb4e50afe52d4a747122e7d9f933", + "reference": "b087d55b4a18eb4e50afe52d4a747122e7d9f933", "shasum": "" }, "require": { @@ -319,10 +319,10 @@ ], "description": "library to implement testing with helpers that allow easy testing with PHPUnit", "support": { - "issues": "https://github.com/lion-packages/test/issues", - "source": "https://github.com/lion-packages/test/tree/v1.4.0" + "issues": "https://github.com/Sleon4/test/issues", + "source": "https://github.com/Sleon4/test/tree/v1.5.0" }, - "time": "2024-01-09T14:44:37+00:00" + "time": "2024-02-13T21:08:53+00:00" }, { "name": "myclabs/deep-copy", @@ -385,16 +385,16 @@ }, { "name": "nikic/php-parser", - "version": "v5.0.0", + "version": "v5.0.1", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "4a21235f7e56e713259a6f76bf4b5ea08502b9dc" + "reference": "2218c2252c874a4624ab2f613d86ac32d227bc69" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/4a21235f7e56e713259a6f76bf4b5ea08502b9dc", - "reference": "4a21235f7e56e713259a6f76bf4b5ea08502b9dc", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/2218c2252c874a4624ab2f613d86ac32d227bc69", + "reference": "2218c2252c874a4624ab2f613d86ac32d227bc69", "shasum": "" }, "require": { @@ -437,9 +437,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v5.0.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.0.1" }, - "time": "2024-01-07T17:17:35+00:00" + "time": "2024-02-21T19:24:10+00:00" }, { "name": "phar-io/manifest", @@ -875,16 +875,16 @@ }, { "name": "phpunit/phpunit", - "version": "10.5.7", + "version": "10.5.11", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "e5c5b397a95cb0db013270a985726fcae93e61b8" + "reference": "0d968f6323deb3dbfeba5bfd4929b9415eb7a9a4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/e5c5b397a95cb0db013270a985726fcae93e61b8", - "reference": "e5c5b397a95cb0db013270a985726fcae93e61b8", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/0d968f6323deb3dbfeba5bfd4929b9415eb7a9a4", + "reference": "0d968f6323deb3dbfeba5bfd4929b9415eb7a9a4", "shasum": "" }, "require": { @@ -956,7 +956,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.7" + "source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.11" }, "funding": [ { @@ -972,7 +972,7 @@ "type": "tidelift" } ], - "time": "2024-01-14T16:40:30+00:00" + "time": "2024-02-25T14:05:00+00:00" }, { "name": "sebastian/cli-parser", diff --git a/src/DependencyInjection/Container.php b/src/DependencyInjection/Container.php index ae18070..da2af86 100644 --- a/src/DependencyInjection/Container.php +++ b/src/DependencyInjection/Container.php @@ -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 + */ public function getFiles(string $folder): array { $files = []; @@ -36,7 +82,7 @@ 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); } } } @@ -44,6 +90,15 @@ public function getFiles(string $folder): array 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); @@ -51,7 +106,15 @@ public function getNamespace(string $file, string $namespace, string $split = '/ 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 = []; @@ -70,6 +133,15 @@ 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); @@ -77,6 +149,14 @@ public function injectDependenciesMethod(object $object, string $method, array $ 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); @@ -84,6 +164,15 @@ public function injectDependenciesCallback(Closure $closure, array $params = []) 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) { @@ -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(); diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index d52544f..9de768d 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -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']; @@ -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); diff --git a/tests/Provider/ContainerProviderTrait.php b/tests/Provider/ContainerProviderTrait.php new file mode 100644 index 0000000..454133c --- /dev/null +++ b/tests/Provider/ContainerProviderTrait.php @@ -0,0 +1,22 @@ + './Provider/CustomClass.php', + 'return' => './Provider/CustomClass.php' + ], + [ + 'path' => '.\Provider\CustomClass.php', + 'return' => './Provider/CustomClass.php' + ] + ]; + } +}