From 499f1f9e9753f2cef0342178cc1d1dd85adfeb3f Mon Sep 17 00:00:00 2001
From: Joe Springe <518362+jspringe@users.noreply.github.com>
Date: Tue, 3 Sep 2019 17:36:08 -0400
Subject: [PATCH] Start event system

---
 src/WmiScripting/Configuration/Config.php | 22 +++++++++++++++++++++-
 src/WmiScripting/Support/Events/Bus.php   | 18 ++++++++++++++++++
 src/WmiScripting/config/bootstrap.php     |  6 ++++++
 tests/Support/EventBusTest.php            | 22 ++++++++++++++++++++++
 4 files changed, 67 insertions(+), 1 deletion(-)
 create mode 100644 src/WmiScripting/Support/Events/Bus.php
 create mode 100644 tests/Support/EventBusTest.php

diff --git a/src/WmiScripting/Configuration/Config.php b/src/WmiScripting/Configuration/Config.php
index de613c0..a16f9d5 100644
--- a/src/WmiScripting/Configuration/Config.php
+++ b/src/WmiScripting/Configuration/Config.php
@@ -10,6 +10,7 @@
 use PhpWinTools\WmiScripting\Containers\Connections;
 use PhpWinTools\WmiScripting\Exceptions\InvalidConnectionException;
 use PhpWinTools\WmiScripting\Exceptions\UnresolvableClassException;
+use PhpWinTools\WmiScripting\Support\Events\Bus;
 
 class Config
 {
@@ -22,11 +23,14 @@ class Config
 
     protected $resolver;
 
+    protected $bus;
+
     protected $resolve_stack = [];
 
-    public function __construct(array $config = [], Resolver $resolver = null)
+    public function __construct(array $config = [], Resolver $resolver = null, Bus $bus = null)
     {
         $this->resolver = $resolver ?? new Resolver($this);
+        $this->bus = $bus;
         $this->boot($config);
 
         static::$instance = $this;
@@ -250,6 +254,16 @@ public function getApiObject(string $class)
         return $this->get("wmi.api_objects.{$class}");
     }
 
+    public function bus()
+    {
+        return $this->bootBus();
+    }
+
+    public function getBusClass()
+    {
+        return $this->get('bus.class');
+    }
+
     /**
      * @return array
      */
@@ -388,6 +402,7 @@ protected function boot(array $config)
 
         $this->merge(include(__DIR__ . '/../config/bootstrap.php'));
 
+        $this->bootBus();
         $this->bootConnections();
     }
 
@@ -398,6 +413,11 @@ protected function merge(array $config)
         }
     }
 
+    protected function bootBus()
+    {
+        return $this->bus = $this->bus ?? $this->resolve()->make($this->getBusClass(), $this);
+    }
+
     protected function bootConnections()
     {
         Arr::set($this->config, 'wmi.connections.servers', new Connections($this));
diff --git a/src/WmiScripting/Support/Events/Bus.php b/src/WmiScripting/Support/Events/Bus.php
new file mode 100644
index 0000000..054fafc
--- /dev/null
+++ b/src/WmiScripting/Support/Events/Bus.php
@@ -0,0 +1,18 @@
+<?php
+
+namespace PhpWinTools\WmiScripting\Support\Events;
+
+use PhpWinTools\WmiScripting\Configuration\Config;
+
+class Bus
+{
+    /** @var Config|null */
+    protected $config;
+
+    protected $registeredEvents;
+
+    public function __construct(Config $config = null)
+    {
+        $this->config = $config ?? Config::instance();
+    }
+}
diff --git a/src/WmiScripting/config/bootstrap.php b/src/WmiScripting/config/bootstrap.php
index e1517c0..89a90e1 100644
--- a/src/WmiScripting/config/bootstrap.php
+++ b/src/WmiScripting/config/bootstrap.php
@@ -3,8 +3,14 @@
 use PhpWinTools\Support\COM\ComWrapper;
 use PhpWinTools\Support\COM\VariantWrapper;
 use PhpWinTools\Support\COM\ComVariantWrapper;
+use PhpWinTools\WmiScripting\Support\Events\Bus;
 
 return [
+    'bus' => [
+        'class' => Bus::class,
+        'listeners' => [],
+    ],
+
     'com' => [
         'com_class'                 => COM::class,
         'variant_class'             => VARIANT::class,
diff --git a/tests/Support/EventBusTest.php b/tests/Support/EventBusTest.php
new file mode 100644
index 0000000..da7a4d1
--- /dev/null
+++ b/tests/Support/EventBusTest.php
@@ -0,0 +1,22 @@
+<?php
+
+namespace Tests\Support;
+
+use PhpWinTools\WmiScripting\Configuration\Config;
+use PhpWinTools\WmiScripting\Support\Events\Bus;
+use Tests\TestCase;
+
+class EventBusTest extends TestCase
+{
+    /** @test */
+    public function it_instantiates_on_config_creation()
+    {
+        $this->assertInstanceOf(Bus::class, Config::instance()->bus());
+    }
+
+//    /** @test */
+//    public function it_tracks_fired_events()
+//    {
+//
+//    }
+}
