diff --git a/src/App/Actions/Action_Base.php b/src/App/Actions/Action_Base.php index 6e54ea9..de3f7c9 100644 --- a/src/App/Actions/Action_Base.php +++ b/src/App/Actions/Action_Base.php @@ -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 { @@ -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; } /** @@ -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}; } } \ No newline at end of file diff --git a/src/App/Application.php b/src/App/Application.php index 7461be1..9fcd250 100644 --- a/src/App/Application.php +++ b/src/App/Application.php @@ -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 @@ -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"); + } } \ No newline at end of file diff --git a/src/App/Core/Event_Dispatcher.php b/src/App/Core/Event_Dispatcher.php index 1e7ac2b..d191606 100644 --- a/src/App/Core/Event_Dispatcher.php +++ b/src/App/Core/Event_Dispatcher.php @@ -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 $data the argument data to pass * @return void */ public function dispatch(string $event_name, array $data = []) { @@ -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; diff --git a/src/App/Injector/Injector.php b/src/App/Injector/Injector.php index e76b9af..e1052b4 100644 --- a/src/App/Injector/Injector.php +++ b/src/App/Injector/Injector.php @@ -4,21 +4,55 @@ namespace Clyde\Injector; use ReflectionClass; -use Exception; use Clyde\Application; -class Injector { +class Injector +{ + /** + * Resolved Classes + * + * @var array + */ protected array $resolved_classes = []; + + /** + * Registered classes + * + * @var array + */ 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 $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 $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]; } @@ -26,26 +60,51 @@ public function resolve(string $class_name, array $dependencies = []): object { 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 $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 $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); @@ -63,5 +122,4 @@ protected function createFreshInstance(string $class_name, array $dependencies): return $instance; } - } \ No newline at end of file diff --git a/src/App/Request/Request_Response.php b/src/App/Request/Request_Response.php index d54b107..d0c3c22 100644 --- a/src/App/Request/Request_Response.php +++ b/src/App/Request/Request_Response.php @@ -2,7 +2,7 @@ namespace Clyde\Request; /** - * @template RRes of array{'body'?: mixed, 'error'?: mixed} + * @template T of array */ class Request_Response { @@ -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; diff --git a/src/App/Tasks/Task_Base.php b/src/App/Tasks/Task_Base.php index ae7bdcb..e2f5720 100644 --- a/src/App/Tasks/Task_Base.php +++ b/src/App/Tasks/Task_Base.php @@ -9,6 +9,8 @@ /** * @property Injector $Injector + * @property Event_Dispatcher $Event_Dispatcher + * @property Printer $Printer */ abstract class Task_Base { @@ -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; } /** @@ -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}; } } \ No newline at end of file diff --git a/src/App/Tasks/Task_Response.php b/src/App/Tasks/Task_Response.php index 4ef4cf1..1f24a8c 100644 --- a/src/App/Tasks/Task_Response.php +++ b/src/App/Tasks/Task_Response.php @@ -1,6 +1,9 @@ success = $success; $this->message = $message; + $this->data = $data; } } \ No newline at end of file