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
52 changes: 17 additions & 35 deletions src/Factory/ServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
27 changes: 13 additions & 14 deletions src/Loader/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @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;
Expand All @@ -18,9 +18,8 @@
use Quantum\Exceptions\LoaderException;

/**
* Loader Class
* @package Quantum
* @category Loader
* Class Loader
* @package Quantum\Loader
*/
class Loader
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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();
Expand All @@ -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;
}

Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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));
}
Expand Down
5 changes: 4 additions & 1 deletion src/Mvc/MvcManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -120,7 +123,7 @@ private static function getAction(QtController $controller): ?string
}

/**
* Get Args
* Get arguments
* @param callable $callable
* @param array $routeArgs
* @return array
Expand Down
19 changes: 0 additions & 19 deletions src/Mvc/QtController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 1 addition & 20 deletions src/Mvc/QtService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 1 addition & 10 deletions tests/unit/Mvc/QtControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down
Loading