diff --git a/src/Factory/ServiceFactory.php b/src/Factory/ServiceFactory.php index 8ab8d8ca..4d10ea9b 100644 --- a/src/Factory/ServiceFactory.php +++ b/src/Factory/ServiceFactory.php @@ -35,55 +35,59 @@ class ServiceFactory /** * Creates and initiates the service once * @param string $serviceClass + * @param array $args * @return \Quantum\Mvc\QtService * @throws \Quantum\Exceptions\DiException * @throws \Quantum\Exceptions\ServiceException * @throws \ReflectionException */ - public function get(string $serviceClass): QtService + public function get(string $serviceClass, array $args = []): QtService { - return $this->locate($serviceClass); + return $this->locate($serviceClass, $args); } /** * Creates and initiates the service * @param string $serviceClass + * @param array $args * @return \Quantum\Mvc\QtService * @throws \Quantum\Exceptions\DiException * @throws \Quantum\Exceptions\ServiceException * @throws \ReflectionException */ - public function create(string $serviceClass): QtService + public function create(string $serviceClass, array $args = []): QtService { - return $this->instantiate($serviceClass); + return $this->instantiate($serviceClass, $args); } /** * Locates the service * @param string $serviceClass + * @param array $args * @return \Quantum\Mvc\QtService * @throws \Quantum\Exceptions\DiException * @throws \Quantum\Exceptions\ServiceException * @throws \ReflectionException */ - private function locate(string $serviceClass): QtService + private function locate(string $serviceClass, array $args = []): QtService { if (isset($this->instantiated[$serviceClass])) { return $this->instantiated[$serviceClass]; } - return $this->instantiate($serviceClass); + return $this->instantiate($serviceClass, $args); } /** * Instantiates the service * @param string $serviceClass + * @param array $args * @return \Quantum\Mvc\QtService * @throws \Quantum\Exceptions\DiException * @throws \Quantum\Exceptions\ServiceException * @throws \ReflectionException */ - private function instantiate(string $serviceClass): QtService + private function instantiate(string $serviceClass, array $args = []): QtService { if (!class_exists($serviceClass)) { throw ServiceException::serviceNotFound($serviceClass); @@ -98,45 +102,23 @@ private function instantiate(string $serviceClass): QtService $this->instantiated[$serviceClass] = $service; if (method_exists($service, '__init')) { - $service->__init(...$this->getArgs($service)); + call_user_func_array([$service, '__init'], $this->getArgs([$service, '__init'], $args)); } return $service; + } /** * Gets arguments - * @param \Quantum\Mvc\QtService $service + * @param callable $callable + * @param array $args * @return array * @throws \Quantum\Exceptions\DiException * @throws \ReflectionException */ - private function getArgs(QtService $service): array + private function getArgs(callable $callable, array $args): array { - $arguments = []; - $args = []; - - $reflection = new \ReflectionMethod($service, '__init'); - $params = $reflection->getParameters(); - - foreach ($params as $param) { - $paramType = $param->getType(); - - if ($paramType) { - switch ($paramType) { - case ModelFactory::class: - array_push($args, Di::get(ModelFactory::class)); - break; - case Loader::class: - array_push($args, Di::get(Loader::class)); - break; - default : - array_push($args, current($arguments)); - next($arguments); - } - } - } - - return $args; + return Di::autowire($callable, $args); } } diff --git a/src/Loader/Loader.php b/src/Loader/Loader.php index 4966a9e7..ccf32fea 100644 --- a/src/Loader/Loader.php +++ b/src/Loader/Loader.php @@ -9,7 +9,7 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.0.0 + * @since 2.5.0 */ namespace Quantum\Loader; @@ -18,9 +18,8 @@ use Quantum\Exceptions\LoaderException; /** - * Loader Class - * @package Quantum - * @category Loader + * Class Loader + * @package Quantum\Loader */ class Loader { @@ -56,13 +55,13 @@ class Loader /** * File System - * @var FileSystem + * @var \Quantum\Libraries\Storage\FileSystem */ private $fs; - + /** * Class constructor - * @param FileSystem $fs + * @param \Quantum\Libraries\Storage\FileSystem|null $fs */ public function __construct(FileSystem $fs = null) { @@ -74,7 +73,7 @@ public function __construct(FileSystem $fs = null) * @param Setup $setup * @return $this */ - public function setup(Setup $setup) + public function setup(Setup $setup): Loader { $this->hierarchical = $setup->getHierarchy(); $this->module = $setup->getModule(); @@ -91,10 +90,9 @@ public function setup(Setup $setup) * @param mixed $value * @return $this */ - public function set($property, $value) + public function set(string $property, $value): Loader { $this->$property = $value; - return $this; } @@ -103,7 +101,7 @@ public function set($property, $value) * @param string $dir * @throws LoaderException */ - public function loadDir($dir) + public function loadDir(string $dir) { foreach ($this->fs->glob($dir . DS . "*.php") as $filename) { $this->loadFile($filename); @@ -115,7 +113,7 @@ public function loadDir($dir) * @param string $path * @throws LoaderException */ - public function loadFile($path) + public function loadFile(string $path) { if (!$this->fs->exists($path)) { throw new LoaderException(_message($this->exceptionMessage, $path)); @@ -139,13 +137,14 @@ public function load() * @return string * @throws LoaderException */ - private function getFilePath() + public function getFilePath(): string { $filePath = modules_dir() . DS . $this->module . DS . ucfirst($this->env) . DS . $this->fileName . '.php'; - if (!file_exists($filePath)) { + if (!$this->fs->exists($filePath)) { if ($this->hierarchical) { $filePath = base_dir() . DS . $this->env . DS . $this->fileName . '.php'; + if (!$this->fs->exists($filePath)) { throw new LoaderException(_message($this->exceptionMessage, $this->fileName)); } diff --git a/src/Mvc/MvcManager.php b/src/Mvc/MvcManager.php index f567236e..b0f95d13 100644 --- a/src/Mvc/MvcManager.php +++ b/src/Mvc/MvcManager.php @@ -35,9 +35,12 @@ class MvcManager * @param \Quantum\Http\Response $response * @throws \Quantum\Exceptions\ControllerException * @throws \Quantum\Exceptions\CsrfException + * @throws \Quantum\Exceptions\DatabaseException * @throws \Quantum\Exceptions\DiException + * @throws \Quantum\Exceptions\LoaderException * @throws \Quantum\Exceptions\MiddlewareException * @throws \Quantum\Exceptions\ModelException + * @throws \Quantum\Exceptions\SessionException * @throws \ReflectionException */ public static function handle(Request $request, Response $response) @@ -120,7 +123,7 @@ private static function getAction(QtController $controller): ?string } /** - * Get Args + * Get arguments * @param callable $callable * @param array $routeArgs * @return array diff --git a/src/Mvc/QtController.php b/src/Mvc/QtController.php index f430e724..cc56bd81 100644 --- a/src/Mvc/QtController.php +++ b/src/Mvc/QtController.php @@ -24,25 +24,6 @@ class QtController extends RouteController { - /** - * Instance of QtController - * @var QtController - */ - private static $instance; - - /** - * Gets the QtController singleton instance - * @return QtController - */ - public static function getInstance(): QtController - { - if (self::$instance === null) { - self::$instance = new self(); - } - - return self::$instance; - } - /** * Handles the missing methods of the controller * @param string $method diff --git a/src/Mvc/QtService.php b/src/Mvc/QtService.php index 88be1606..f7db84f6 100644 --- a/src/Mvc/QtService.php +++ b/src/Mvc/QtService.php @@ -19,30 +19,11 @@ /** * Class QtService * @package Quantum\Mvc - * @method void __init(...$args) + * @method void __init() */ class QtService { - /** - * Instance of QtService - * @var \Quantum\Mvc\QtService - */ - private static $instance; - - /** - * Gets the QtService singleton instance - * @return \Quantum\Mvc\QtService - */ - public static function getInstance(): QtService - { - if (self::$instance === null) { - self::$instance = new static(); - } - - return self::$instance; - } - /** * Handles the missing methods of the service * @param string $method diff --git a/tests/unit/Mvc/QtControllerTest.php b/tests/unit/Mvc/QtControllerTest.php index 2f5c7d36..c83c434d 100644 --- a/tests/unit/Mvc/QtControllerTest.php +++ b/tests/unit/Mvc/QtControllerTest.php @@ -13,10 +13,8 @@ class TestController extends QtController namespace Quantum\Test\Unit { - use Mockery; use PHPUnit\Framework\TestCase; use Quantum\Exceptions\ControllerException; - use Quantum\Mvc\QtController; use Quantum\Controllers\TestController; use Quantum\Libraries\Storage\FileSystem; use Quantum\Loader\Loader; @@ -35,20 +33,13 @@ public function setUp(): void $loader->loadDir(dirname(__DIR__, 3) . DS . 'src' . DS . 'Helpers' . DS . 'functions'); } - public function testGetInstance() - { - $this->assertInstanceOf('Quantum\Mvc\QtController', QtController::getInstance()); - - $this->assertInstanceOf('Quantum\Mvc\QtController', new TestController()); - } - public function testMissingeMethods() { $this->expectException(ControllerException::class); $this->expectExceptionMessage('The method `undefinedMethod` is not defined'); - $controller = QtController::getInstance(); + $controller = new TestController(); $controller->undefinedMethod(); } diff --git a/tests/unit/Mvc/QtServiceTest.php b/tests/unit/Mvc/QtServiceTest.php index 7730d121..3baab90b 100644 --- a/tests/unit/Mvc/QtServiceTest.php +++ b/tests/unit/Mvc/QtServiceTest.php @@ -1,45 +1,53 @@ loadDir(dirname(__DIR__, 3) . DS . 'src' . DS . 'Helpers' . DS . 'functions'); - } - public function testGetInstance() + use Quantum\Mvc\QtService; + + class TestingService extends QtService { - $this->assertInstanceOf('Quantum\Mvc\QtService', QtService::getInstance()); + } +} + +namespace Quantum\Test\Unit { + + use PHPUnit\Framework\TestCase; + use Quantum\Exceptions\ServiceException; + use Quantum\Factory\ServiceFactory; + use Quantum\Libraries\Storage\FileSystem; + use Quantum\Loader\Loader; + use Quantum\Services\TestingService; + /** - * @runInSeparateProcess + * @runTestsInSeparateProcesses + * @preserveGlobalState disabled */ - public function testMissingeMethods() + class QtServiceTest extends TestCase { - $this->expectException(ServiceException::class); - $this->expectExceptionMessage('The method `undefinedMethod` is not defined'); + public function setUp(): void + { + $loader = new Loader(new FileSystem); - $service = QtService::getInstance(); + $loader->loadDir(dirname(__DIR__, 3) . DS . 'src' . DS . 'Helpers' . DS . 'functions'); + } - $service->undefinedMethod(); - } + /** + * @runInSeparateProcess + */ + public function testMissingeMethods() + { + $this->expectException(ServiceException::class); -} + $this->expectExceptionMessage('The method `undefinedMethod` is not defined'); + + $service = (new ServiceFactory)->get(TestingService::class); + + $service->undefinedMethod(); + } + + } +} \ No newline at end of file