diff --git a/src/FuzeWorks/Configurator.php b/src/FuzeWorks/Configurator.php index 0df0c01..cf1607e 100644 --- a/src/FuzeWorks/Configurator.php +++ b/src/FuzeWorks/Configurator.php @@ -151,6 +151,8 @@ class Configurator } /** + * Invokes a method on a componentClass after the Container has been created. + * * @param string $componentClass * @param string $method * @param callable|null $callable @@ -170,6 +172,21 @@ class Configurator return $this->deferredComponentClassMethods[$componentClass][] = $deferredComponentClass; } + /** + * Alias for deferComponentClassMethods + * + * @param string $componentClass + * @param string $method + * @param callable|null $callable + * @param mixed $parameters,... Parameters for the method to be invoked + * @return DeferredComponentClass + * @codeCoverageIgnore + */ + public function call(string $componentClass, string $method, callable $callable = null) + { + return call_user_func_array([$this, 'deferComponentClassMethod'], func_get_args()); + } + /* ---------------- Other Features ---------------------- */ /** @@ -311,27 +328,10 @@ class Configurator if ($debug == true) Logger::enable(); - // Invoke deferredComponentClass on FuzeWorks\Core classes - foreach ($this->deferredComponentClassMethods as $componentClass => $deferredComponentClasses) - { - // @todo Verify if system works - if ($container->instanceIsset($componentClass)) - { - // @codeCoverageIgnoreStart - foreach ($deferredComponentClasses as $deferredComponentClass) - { - $deferredComponentClass->invoke(call_user_func_array( - array($container->{$deferredComponentClass->componentClass}, $deferredComponentClass->method), - $deferredComponentClass->arguments - )); - } - // @codeCoverageIgnoreEnd - } - } - - // Add all components + // Load components foreach ($this->components as $component) { + Logger::logInfo("Adding Component: '" . $component->getName() . "'"); foreach ($component->getClasses() as $componentName => $componentClass) { if (is_object($componentClass)) @@ -345,26 +345,30 @@ class Configurator $container->setInstance($componentName, new $componentClass()); } - - // Invoke deferredComponentClass - if (isset($this->deferredComponentClassMethods[$componentName])) - { - $dfcm = $this->deferredComponentClassMethods[$componentName]; - foreach ($dfcm as $deferredComponentClass) - { - $deferredComponentClass->invoke(call_user_func_array( - array($container->{$deferredComponentClass->componentClass}, $deferredComponentClass->method), - $deferredComponentClass->arguments - )); - } - } } $component->onCreateContainer($container); } + // Invoke deferredComponentClass on FuzeWorks\Core classes + foreach ($this->deferredComponentClassMethods as $componentClass => $deferredComponentClasses) + { + if ($container->instanceIsset($componentClass)) + { + foreach ($deferredComponentClasses as $deferredComponentClass) + { + Logger::logDebug("Invoking '" . $deferredComponentClass->method . "' on component '" . $deferredComponentClass->componentClass . "'"); + $deferredComponentClass->invoke(call_user_func_array( + array($container->{$deferredComponentClass->componentClass}, $deferredComponentClass->method), + $deferredComponentClass->arguments + )); + } + } + } + // And add all directories to the components foreach ($this->directories as $component => $directories) { + Logger::logDebug("Adding directories for '" . $component . "'"); if ($component == 'app') continue; @@ -372,6 +376,7 @@ class Configurator $container->{$component}->setDirectories($directories); } + $container->init(); return $container; } } \ No newline at end of file diff --git a/src/FuzeWorks/DeferredComponentClass.php b/src/FuzeWorks/DeferredComponentClass.php index 2f980f4..abff4c2 100644 --- a/src/FuzeWorks/DeferredComponentClass.php +++ b/src/FuzeWorks/DeferredComponentClass.php @@ -77,6 +77,11 @@ class DeferredComponentClass $this->callback = $callback; } + /** + * Receives the result after being invoked. Used to save the result and send back with callable if configured. + * + * @param $result + */ public function invoke($result) { $this->return = $result; diff --git a/src/FuzeWorks/Events.php b/src/FuzeWorks/Events.php index 2c0d2a5..8877994 100644 --- a/src/FuzeWorks/Events.php +++ b/src/FuzeWorks/Events.php @@ -205,11 +205,6 @@ class Events throw new EventException('Event could not be loaded. Invalid variable provided.', 1); } - if (self::$enabled) - { - Logger::newLevel("Firing Event: '".$eventName."'"); - } - if (func_num_args() > 1) { call_user_func_array(array($event, 'init'), array_slice(func_get_args(), 1)); } @@ -219,10 +214,11 @@ class Events return $event; } - Logger::log('Checking for Listeners'); - //There are listeners for this event if (isset(self::$listeners[$eventName])) { + // Event with listeners found. Log it. + Logger::newLevel("Firing Event: '".$eventName."'. Found listeners: "); + //Loop from the highest priority to the lowest for ($priority = EventPriority::getHighestPriority(); $priority <= EventPriority::getLowestPriority(); ++$priority) { //Check for listeners in this priority @@ -251,9 +247,9 @@ class Events Logger::stopLevel(); } } - } - Logger::stopLevel(); + Logger::stopLevel(); + } return $event; } diff --git a/src/FuzeWorks/Factory.php b/src/FuzeWorks/Factory.php index 4fdd4bb..5930f57 100644 --- a/src/FuzeWorks/Factory.php +++ b/src/FuzeWorks/Factory.php @@ -78,6 +78,13 @@ class Factory */ protected static $cloneInstances = false; + /** + * Whether the Factory has been initialized or not + * + * @var bool $initialized + */ + private $initialized = false; + /** * Config Object * @var Config @@ -153,6 +160,10 @@ class Factory */ public function init(): Factory { + // If already initialized, cancel + if ($this->initialized) + return $this; + // Load the config file of the FuzeWorks core try { $cfg = $this->config->get('core'); @@ -176,6 +187,9 @@ class Factory // Initialize all plugins $this->plugins->loadHeadersFromPluginPaths(); + // Log actions + Logger::logInfo("FuzeWorks initialized. Firing coreStartEvent."); + // And fire the coreStartEvent try { Events::fireEvent('coreStartEvent'); diff --git a/src/FuzeWorks/Plugins.php b/src/FuzeWorks/Plugins.php index d6d9871..e55b9c0 100644 --- a/src/FuzeWorks/Plugins.php +++ b/src/FuzeWorks/Plugins.php @@ -123,10 +123,12 @@ class Plugins // Now go through each entry in the plugin folder foreach ($pluginPathContents as $pluginFolder) { + // @codeCoverageIgnoreStart if (!is_dir($pluginPath . DS . $pluginFolder)) { continue; } + // @codeCoverageIgnoreEnd // If a header file exists, use it $file = $pluginPath . DS . $pluginFolder . DS . 'header.php'; diff --git a/src/FuzeWorks/iComponent.php b/src/FuzeWorks/iComponent.php index d56e039..863d490 100644 --- a/src/FuzeWorks/iComponent.php +++ b/src/FuzeWorks/iComponent.php @@ -40,6 +40,7 @@ namespace FuzeWorks; interface iComponent { + public function getName(): string; public function getClasses(): array; public function onAddComponent(Configurator $configurator); public function onCreateContainer(Factory $container); diff --git a/test/bootstrap.php b/test/bootstrap.php index 77d12f3..0ef3c1c 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -50,6 +50,5 @@ $configurator->setDebugAddress('ALL'); //$configurator->setDebugEmail('example@mail.com'); $container = $configurator->createContainer(); -$container->init(); return $container; diff --git a/test/components/TestAddComponent/TestAddComponent.php b/test/components/TestAddComponent/TestAddComponent.php index f7e5921..bb98164 100644 --- a/test/components/TestAddComponent/TestAddComponent.php +++ b/test/components/TestAddComponent/TestAddComponent.php @@ -41,6 +41,11 @@ use FuzeWorks\iComponent; class TestComponent implements iComponent { + public function getName(): string + { + return 'TestComponent'; + } + public function getClasses(): array { return ['test' => 'FuzeWorks\Component\Test']; diff --git a/test/components/TestAddComponentDirectory/TestAddComponentDirectory.php b/test/components/TestAddComponentDirectory/TestAddComponentDirectory.php index 625396e..2a19595 100644 --- a/test/components/TestAddComponentDirectory/TestAddComponentDirectory.php +++ b/test/components/TestAddComponentDirectory/TestAddComponentDirectory.php @@ -55,6 +55,11 @@ class TestAddComponentDirectoryComponent implements iComponent { return $container; } + + public function getName(): string + { + return 'TestAddComponentDirectoryComponent'; + } } class TestAddComponentDirectory diff --git a/test/components/TestAddComponentFail/TestAddComponentFail.php b/test/components/TestAddComponentFail/TestAddComponentFail.php index 44cb496..32716e3 100644 --- a/test/components/TestAddComponentFail/TestAddComponentFail.php +++ b/test/components/TestAddComponentFail/TestAddComponentFail.php @@ -41,6 +41,11 @@ use FuzeWorks\iComponent; class TestAddComponentFailComponent implements iComponent { + public function getName(): string + { + return 'TestAddComponentFailComponent'; + } + public function getClasses(): array { return ['test' => 'FuzeWorks\Component\TestAddComponentNotExist']; diff --git a/test/core/core_configuratorTest.php b/test/core/core_configuratorTest.php index d5c7d78..edde115 100644 --- a/test/core/core_configuratorTest.php +++ b/test/core/core_configuratorTest.php @@ -91,7 +91,7 @@ class configuratorTest extends CoreTestAbstract $this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->addComponent($component)); // Create container and test if component is added and has known properties - $container = $this->configurator->createContainer()->init(); + $container = $this->configurator->createContainer(); $this->assertTrue(property_exists($container, 'test')); $this->assertInstanceOf('FuzeWorks\Component\Test', $container->test); $this->assertEquals(5, $container->test->variable); @@ -112,7 +112,7 @@ class configuratorTest extends CoreTestAbstract $this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->addComponent($component)); // Create container and test for variable - $container = $this->configurator->createContainer()->init(); + $container = $this->configurator->createContainer(); $this->assertEquals('value', $container->componentobject->variable); } @@ -128,7 +128,7 @@ class configuratorTest extends CoreTestAbstract $this->configurator->addComponent($component); // Create container - $this->configurator->createContainer()->init(); + $this->configurator->createContainer(); } /* ---------------------------------- Directories ----------------------------------------------- */ @@ -145,7 +145,7 @@ class configuratorTest extends CoreTestAbstract $this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->setLogDirectory(vfsStream::url('testSetLogDirectory'))); // Create container and test if properly set - $this->configurator->createContainer()->init(); + $this->configurator->createContainer(); $this->assertEquals(Core::$logDir, vfsStream::url('testSetLogDirectory')); // Create a log and write off to file @@ -178,7 +178,7 @@ class configuratorTest extends CoreTestAbstract $this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->setTempDirectory(vfsStream::url('testSetTempDirectory'))); // Create container and test if properly set - $this->configurator->createContainer()->init(); + $this->configurator->createContainer(); $this->assertEquals(Core::$tempDir, vfsStream::url('testSetTempDirectory')); } @@ -204,7 +204,7 @@ class configuratorTest extends CoreTestAbstract $this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->addDirectory(vfsStream::url('testAddAppDirectory'))); // Create container and test if properly set - $this->configurator->createContainer()->init(); + $this->configurator->createContainer(); $this->assertEquals(Core::$appDirs, [vfsStream::url('testAddAppDirectory')]); } @@ -226,7 +226,7 @@ class configuratorTest extends CoreTestAbstract $this->configurator->addDirectory(vfsStream::url('testAddComponentDirectory'), 'testaddcomponentdirectory'); // Create container and test if component is added and has known properties - $container = $this->configurator->createContainer()->init(); + $container = $this->configurator->createContainer(); $this->assertTrue(property_exists($container, 'testaddcomponentdirectory')); $this->assertInstanceOf('FuzeWorks\Component\TestAddComponentDirectory', $container->testaddcomponentdirectory); $this->assertEquals(5, $container->testaddcomponentdirectory->variable); @@ -330,7 +330,7 @@ class configuratorTest extends CoreTestAbstract $this->assertInstanceOf('FuzeWorks\Configurator', $this->configurator->setParameters(['tempDir' => 'fake_directory'])); // Create container and verify - $this->configurator->createContainer()->init(); + $this->configurator->createContainer(); $this->assertEquals('fake_directory', Core::$tempDir); } @@ -340,7 +340,7 @@ class configuratorTest extends CoreTestAbstract $this->configurator->setConfigOverride('test', 'somekey', 'somevalue'); // Create container - $this->configurator->createContainer()->init(); + $this->configurator->createContainer(); // Verify that the variable is set in the Config class $this->assertEquals(['test' => ['somekey' => 'somevalue']], \FuzeWorks\Config::$configOverrides); @@ -366,7 +366,7 @@ class configuratorTest extends CoreTestAbstract $this->assertTrue($this->configurator->isDebugMode()); // Load the container and verify that tracy runs in debug mode - $this->configurator->createContainer()->init(); + $this->configurator->createContainer(); $this->assertTrue(Logger::isEnabled()); } @@ -379,7 +379,7 @@ class configuratorTest extends CoreTestAbstract $this->assertFalse($this->configurator->enableDebugMode(false)->isDebugMode()); // Create the container and verify that tracy debug has been disabled - $this->configurator->createContainer()->init(); + $this->configurator->createContainer(); // Tracy can't be disabled once it's been enabled. Therefor this won't be tested } @@ -450,6 +450,11 @@ class configuratorTest extends CoreTestAbstract class MockComponent implements iComponent { + public function getName(): string + { + return 'MockComponent'; + } + public function getClasses(): array { }