Initializing the container is no longer required. Use call method to call componentClasses after creating the container.

This commit is contained in:
Abel Hoogeveen 2019-01-17 13:54:37 +01:00
parent b87a35ecbc
commit 172bed55b9
No known key found for this signature in database
GPG Key ID: 96C2234920BF4292
11 changed files with 95 additions and 53 deletions

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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');

View File

@ -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';

View File

@ -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);

View File

@ -50,6 +50,5 @@ $configurator->setDebugAddress('ALL');
//$configurator->setDebugEmail('example@mail.com');
$container = $configurator->createContainer();
$container->init();
return $container;

View File

@ -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'];

View File

@ -55,6 +55,11 @@ class TestAddComponentDirectoryComponent implements iComponent
{
return $container;
}
public function getName(): string
{
return 'TestAddComponentDirectoryComponent';
}
}
class TestAddComponentDirectory

View File

@ -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'];

View File

@ -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
{
}