diff --git a/src/FuzeWorks/Core.php b/src/FuzeWorks/Core.php index 5aef261..f9e89ac 100644 --- a/src/FuzeWorks/Core.php +++ b/src/FuzeWorks/Core.php @@ -86,6 +86,13 @@ class Core */ protected static $errorHandlers = []; + /** + * Array of all classMaps which can be autoloaded. + * + * @var array + */ + protected static $autoloadMap = []; + /** * Initializes the core. * @@ -251,8 +258,6 @@ class Core } } - protected static $autoloadMap = []; - /** * @param string $nameSpacePrefix * @param string $filePath @@ -292,6 +297,17 @@ class Core } } + /** + * Clears the autoloader to its original state. + * + * Not intended for use by developer. Only for use during testing + * @internal + */ + public static function clearAutoloader() + { + self::$autoloadMap = []; + } + /** * Tests for file writability * diff --git a/src/FuzeWorks/Libraries.php b/src/FuzeWorks/Libraries.php index 52fccda..0420808 100644 --- a/src/FuzeWorks/Libraries.php +++ b/src/FuzeWorks/Libraries.php @@ -37,7 +37,10 @@ namespace FuzeWorks; use FuzeWorks\Exception\ConfigException; +use FuzeWorks\Exception\CoreException; use FuzeWorks\Exception\LibraryException; +use ReflectionClass; +use ReflectionException; class Libraries { @@ -196,12 +199,27 @@ class Libraries } // Load the class object + /** @var iLibrary $classObject */ $classObject = new $libraryClass($parameters); - // @todo Check if library has all required methods (iLibrary) - // @todo Implement autoloading for libraries + // If not instance of iLibrary, refuse the library + if (!$classObject instanceof iLibrary) + throw new LibraryException("Could not initiate library. Library is not instance of iLibrary."); - // Check if the address is already reserved, if it is, we can presume that a new instance is requested. + // Add the library files to the autoloader + try { + $headerReflection = new ReflectionClass(get_class($classObject)); + $filePath = dirname($headerReflection->getFileName()) . (!is_null($classObject->getSourceDirectory()) ? DS . $classObject->getSourceDirectory() : '' ); + $prefix = $classObject->getClassesPrefix(); + if (!is_null($filePath) && !is_null($prefix)) + Core::addAutoloadMap($prefix, $filePath); + } catch (ReflectionException $e) { + throw new LibraryException("Could not initiate library. ReflectionClass threw exception."); + } catch (CoreException $e) { + throw new LibraryException("Could not initiate library. Failed to add to autoloader."); + } + + // @todo Check if the address is already reserved, if it is, we can presume that a new instance is requested. // Otherwise this code would not be reached // Now load the class diff --git a/src/FuzeWorks/iLibrary.php b/src/FuzeWorks/iLibrary.php new file mode 100644 index 0000000..598c4f5 --- /dev/null +++ b/src/FuzeWorks/iLibrary.php @@ -0,0 +1,64 @@ +config->discardConfigFiles(); + Factory::getInstance('config')->discardConfigFiles(); // Re-enable events, in case they have been disabled Events::enable(); // Remove Config overrides Config::$configOverrides = []; + + // Remove autoloader + Core::clearAutoloader(); } } diff --git a/test/core/core_configuratorTest.php b/test/core/core_configuratorTest.php index 1b52349..3ec6063 100644 --- a/test/core/core_configuratorTest.php +++ b/test/core/core_configuratorTest.php @@ -64,6 +64,8 @@ class configuratorTest extends CoreTestAbstract public function tearDown() { + parent::tearDown(); + Core::$tempDir = dirname(__DIR__) . '/temp'; Core::$logDir = dirname(__DIR__) . '/temp'; } diff --git a/test/core/core_factoryTest.php b/test/core/core_factoryTest.php index 680496a..df25b51 100644 --- a/test/core/core_factoryTest.php +++ b/test/core/core_factoryTest.php @@ -310,6 +310,8 @@ class factoryTest extends CoreTestAbstract public function tearDown() { + parent::tearDown(); + $factory = Factory::getInstance(); if (isset($factory->mock)) $factory->removeInstance('mock'); diff --git a/test/core/core_libraryTest.php b/test/core/core_libraryTest.php index 256ed35..123513e 100644 --- a/test/core/core_libraryTest.php +++ b/test/core/core_libraryTest.php @@ -195,8 +195,36 @@ class libraryTest extends CoreTestAbstract $this->libraries->addLibraryClass('LibraryClassFail', '\Case\Not\Exist'); } + /** + * @depends testAddLibraryClass + * @covers ::initLibrary + */ + public function testAddLibraryWithAutoloader() + { + // First assert the extra class can't be autoloaded + $this->assertFalse(class_exists('FuzeWorks\Test\TestAddLibraryWithAutoloader\SomeExtraClass', true)); + + // Load the library and test the instance type + $this->assertInstanceOf('Application\Library\TestAddLibraryWithAutoloader', $this->libraries->get('TestAddLibraryWithAutoloader')); + + // Afterwards test if the loader has been correctly added + $this->assertTrue(class_exists('FuzeWorks\Test\TestAddLibraryWithAutoloader\SomeExtraClass', true)); + } + + /** + * @depends testAddLibraryWithAutoloader + * @covers ::initLibrary + * @expectedException \FuzeWorks\Exception\LibraryException + */ + public function testAddBadAutoloader() + { + $this->assertInstanceOf('Application\Library\TestAddBadAutoloader', $this->libraries->get('TestAddBadAutoloader')); + } + public function tearDown() { + parent::tearDown(); + Factory::getInstance()->config->getConfig('error')->revert(); } diff --git a/test/core/core_loggerTest.php b/test/core/core_loggerTest.php index 0a9bb51..69f9577 100644 --- a/test/core/core_loggerTest.php +++ b/test/core/core_loggerTest.php @@ -213,6 +213,8 @@ class loggerTest extends CoreTestAbstract public function tearDown() { + parent::tearDown(); + Logger::disable(); Logger::$logs = array(); } diff --git a/test/core/core_pluginsTest.php b/test/core/core_pluginsTest.php index 1793f43..29beb1a 100644 --- a/test/core/core_pluginsTest.php +++ b/test/core/core_pluginsTest.php @@ -198,6 +198,8 @@ class pluginTest extends CoreTestAbstract public function tearDown() { + parent::tearDown(); + $factory = Factory::getInstance(); $factory->config->plugins->revert(); } diff --git a/test/libraries/TestAddBadAutoloader/TestAddBadAutoloader.php b/test/libraries/TestAddBadAutoloader/TestAddBadAutoloader.php new file mode 100644 index 0000000..a647f52 --- /dev/null +++ b/test/libraries/TestAddBadAutoloader/TestAddBadAutoloader.php @@ -0,0 +1,52 @@ +parameters = $parameters; } + public function getClassesPrefix(): ?string + { + return null; + } + + public function getSourceDirectory(): ?string + { + return null; + } + } \ No newline at end of file