Skip to content

Commit

Permalink
misc updates and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dvnc0 committed Sep 15, 2023
1 parent b3e77ff commit f5698f6
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 70 deletions.
34 changes: 7 additions & 27 deletions src/App/Actions/Action_Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
use Clyde\Request\Request;
use Clyde\Request\Request_Response;
use Clyde\Tools\Printer;
use Exception;
use Clyde\Injector\Injector;

/**
* @property Injector $Injector
* @property Event_Dispatcher $Event_Dispatcher
* @property Printer $Printer
*/
abstract class Action_Base
{
Expand All @@ -20,30 +22,13 @@ abstract class Action_Base
*/
protected Application $Application;

/**
* Event dispatcher
*
* @var Event_Dispatcher
*/
protected Event_Dispatcher $Event_Dispatcher;

/**
* Printer
*
* @var Printer
*/
protected Printer $Printer;

/**
* Construct
*
* @param Application $Application The Application instance
* @param Event_Dispatcher $Event_Dispatcher The Event Dispatcher
* @param Application $Application The Application instance
*/
public function __construct(Application $Application, Event_Dispatcher $Event_Dispatcher) {
$this->Application = $Application;
$this->Event_Dispatcher = $Event_Dispatcher;
$this->Printer = $this->Application->Printer;
public function __construct(Application $Application) {
$this->Application = $Application;
}

/**
Expand Down Expand Up @@ -72,11 +57,6 @@ abstract public function execute(Request $Request): Request_Response;
* @return mixed
*/
public function __get(string $name) {
$method = "get{$name}";
if (method_exists($this->Application, $method)) {
return $this->Application->$method();
}

throw new Exception("Method {$method} does not exist");
return $this->Application->{$name};
}
}
40 changes: 39 additions & 1 deletion src/App/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
use Exception;

/**
* @property Injector $Injector
* @property Event_Dispatcher $Event_Dispatcher
* @property Printer $Printer
*
* @phpstan-import-type CommandObject from \Clyde\Objects\Command_Object
*/
class Application
Expand Down Expand Up @@ -325,7 +329,41 @@ public function run(): void {
*
* @return Injector
*/
public function getInjector(): Injector {
protected function getInjector(): Injector {
return $this->Injector;
}

/**
* Get the Application Event Dispatcher instance
*
* @return Event_Dispatcher
*/
protected function getEventDispatcher(): Event_Dispatcher {
return $this->Event_Dispatcher;
}

/**
* Get the Application Printer instance
*
* @return Printer
*/
protected function getPrinter(): Printer {
return $this->Printer;
}

/**
* Magic method __get
*
* @param string $name property name
* @return mixed
*/
public function __get(string $name) {
$name = str_replace('_', '', $name);
$method = "get{$name}";
if (method_exists($this, $method)) {
return $this->{$method}();
}

throw new Exception("Method {$method} does not exist");
}
}
10 changes: 5 additions & 5 deletions src/App/Core/Event_Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function __construct(Application $Application) {
/**
* Dispatch an event
*
* @param string $event_name the name of the event to dispatch
* @param array $data the argument data to pass
* @param non-empty-string $event_name the name of the event to dispatch
* @param array<string> $data the argument data to pass
* @return void
*/
public function dispatch(string $event_name, array $data = []) {
Expand All @@ -46,11 +46,11 @@ public function dispatch(string $event_name, array $data = []) {
}

foreach ($this->events[$event_name] as $Command) {
if (is_callable($Command)) {
call_user_func($Command, $data);
if (is_callable($Command->action)) {
call_user_func($Command->action, $data);
continue;
}

if (get_parent_class($Command->action) === Action_Base::class) {
$action = $Command->action;
$Request = new Request;
Expand Down
80 changes: 69 additions & 11 deletions src/App/Injector/Injector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,107 @@
namespace Clyde\Injector;

use ReflectionClass;
use Exception;
use Clyde\Application;

class Injector {
class Injector
{

/**
* Resolved Classes
*
* @var array<class-string, object>
*/
protected array $resolved_classes = [];

/**
* Registered classes
*
* @var array<class-string, object>
*/
protected array $registered_classes = [];

/**
* Application
*
* @var Application
*/
protected Application $Application;

public function __construct(Application $Application, array $registered_classes = [], ) {
$this->Application = $Application;
/**
* Construct
*
* @param Application $Application The Application instance
* @param array<class-string, object> $registered_classes The registered classes
*/
public function __construct(Application $Application, array $registered_classes = []) {
$this->Application = $Application;
$this->registered_classes = $registered_classes;
}

/**
* Register a class
*
* @param string $class_name The class name
* @param array<string> $dependencies The dependencies
* @return object
*/
public function resolve(string $class_name, array $dependencies = []): object {
if (!empty($this->registered_classes[$class_name])) {
return $this->registered_classes[$class_name];
}

if (!empty($this->resolved_classes[$class_name])) {
return $this->resolved_classes[$class_name];
}

return $this->createFreshInstance($class_name, $dependencies);
}

public function resolveFresh(string $class_name, array $dependencies = []) {
return $this->createFreshInstance($class_name, $dependencies);
/**
* Register a class
*
* @param class-string $class_name The class name
* @param object $instance The instance
* @return void
*/
public function registerClass(string $class_name, object $instance): void {
$this->registered_classes[$class_name] = $instance;
}

/**
* Resolve a fresh instance
*
* @param class-string $class_name The class name
* @param array<string> $dependencies The dependencies
* @return object
*/
public function resolveFresh(string $class_name, array $dependencies = []): object {
return $this->createFreshInstance($class_name, $dependencies);
}

/**
* Create a fresh instance
*
* @param class-string $class_name The class name
* @param array<string> $dependencies The dependencies
* @return object
*/
protected function createFreshInstance(string $class_name, array $dependencies):object {
$dependencies[Application::class] = $this->Application;
$dependencies[Injector::class] = $this;
$reflection = new ReflectionClass($class_name);
$dependencies[Injector::class] = $this;

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

if (empty($constructor)) {
$instance = new $class_name();
} else {
$constructor_params = $constructor->getParameters();
$constructor_args = [];
$constructor_args = [];

foreach ($constructor_params as $param) {
$param_class = $param->getType()->getName(); // @phpstan-ignore-line
$param_name = $param->getName();
$param_name = $param->getName();

if (!empty($param_class) && class_exists($param_class)) {
$constructor_args[] = $this->resolve($param_class);
Expand All @@ -63,5 +122,4 @@ protected function createFreshInstance(string $class_name, array $dependencies):

return $instance;
}

}
4 changes: 2 additions & 2 deletions src/App/Request/Request_Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Clyde\Request;

/**
* @template RRes of array{'body'?: mixed, 'error'?: mixed}
* @template T of array
*/
class Request_Response
{
Expand Down Expand Up @@ -32,7 +32,7 @@ class Request_Response
*
* @param boolean $success successful
* @param string $message message to attach
* @param RRes $data any additional data
* @param T $data any additional data
*/
public function __construct(bool $success, string $message = '', array $data = []) {
$this->success = $success;
Expand Down
27 changes: 4 additions & 23 deletions src/App/Tasks/Task_Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

/**
* @property Injector $Injector
* @property Event_Dispatcher $Event_Dispatcher
* @property Printer $Printer
*/
abstract class Task_Base
{
Expand All @@ -26,29 +28,13 @@ abstract class Task_Base
*/
protected Application $Application;

/**
* Event dispatch
*
* @var Event_Dispatcher
*/
protected Event_Dispatcher $Event_Dispatcher;

/**
* Printer
*
* @var Printer
*/
protected Printer $Printer;

/**
* construct
*
* @param Application $Application Application Instance
*/
public function __construct(Application $Application) {
$this->Application = $Application;
$this->Event_Dispatcher = $this->Application->Event_Dispatcher;
$this->Printer = $this->Application->Printer;
$this->Application = $Application;
}

/**
Expand All @@ -65,11 +51,6 @@ abstract public function execute(): Task_Response;
* @return mixed
*/
public function __get(string $name) {
$method = "get{$name}";
if (method_exists($this->Application, $method)) {
return $this->Application->$method();
}

throw new Exception("Method {$method} does not exist");
return $this->Application->{$name};
}
}
14 changes: 13 additions & 1 deletion src/App/Tasks/Task_Response.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace Clyde\Tasks;

/**
* @template T of array
*/
class Task_Response
{
/**
Expand All @@ -17,14 +20,23 @@ class Task_Response
*/
public string $message;

/**
* data
*
* @var array
*/
public array $data;

/**
* construct
*
* @param boolean $success was task successful
* @param string $message message to attach
* @param T $data any additional data
*/
public function __construct(bool $success, string $message = '') {
public function __construct(bool $success, string $message = '', array $data = []) {
$this->success = $success;
$this->message = $message;
$this->data = $data;
}
}

0 comments on commit f5698f6

Please sign in to comment.