From 3deed7a56cf9c06d420de8f16f4d683991deda91 Mon Sep 17 00:00:00 2001 From: Louis Charette Date: Sat, 17 Feb 2024 15:41:07 -0500 Subject: [PATCH] Allow sprinkle as object in Cupcake instantiation --- src/Cupcake.php | 7 ++++--- src/Sprinkle/SprinkleManager.php | 12 ++++++++---- tests/Sprinkle/SprinkleManagerTest.php | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/Cupcake.php b/src/Cupcake.php index b4df055..5dc0153 100644 --- a/src/Cupcake.php +++ b/src/Cupcake.php @@ -16,6 +16,7 @@ use DI\ContainerBuilder; use UserFrosting\ServicesProvider\FrameworkService; use UserFrosting\Sprinkle\SprinkleManager; +use UserFrosting\Sprinkle\SprinkleRecipe; /** * Base class for UserFrosting application. @@ -35,9 +36,9 @@ abstract class Cupcake /** * Constructor. * - * @param class-string<\UserFrosting\Sprinkle\SprinkleRecipe> $mainSprinkle + * @param class-string|SprinkleRecipe $mainSprinkle */ - public function __construct(protected string $mainSprinkle) + public function __construct(protected string|SprinkleRecipe $mainSprinkle) { $this->init(); } @@ -87,7 +88,7 @@ public function getContainer(): Container */ public function getMainSprinkle(): string { - return $this->mainSprinkle; + return ($this->mainSprinkle instanceof SprinkleRecipe) ? $this->mainSprinkle::class : $this->mainSprinkle; } /** diff --git a/src/Sprinkle/SprinkleManager.php b/src/Sprinkle/SprinkleManager.php index 6aa6373..0d38128 100644 --- a/src/Sprinkle/SprinkleManager.php +++ b/src/Sprinkle/SprinkleManager.php @@ -39,9 +39,9 @@ class SprinkleManager /** * Load sprinkles on construction. * - * @param class-string $mainSprinkle + * @param class-string|SprinkleRecipe $mainSprinkle */ - public function __construct(string $mainSprinkle) + public function __construct(string|SprinkleRecipe $mainSprinkle) { $this->mainSprinkle = $this->validateClassIsRecipe($mainSprinkle); $this->loadSprinkles(); @@ -145,15 +145,19 @@ protected function getDependentSprinkles(SprinkleRecipe $sprinkle): array * Instantiate a class string into an instance of SprinkleRecipe. * Provides flexibility, allowing string and object to be referenced. * - * @param class-string $class + * @param class-string|SprinkleRecipe $class * * @throws BadClassNameException If $class is not found * @throws BadInstanceOfException If $class doesn't implement SprinkleRecipe interface. * * @return SprinkleRecipe */ - protected function validateClassIsRecipe(string $class): SprinkleRecipe + protected function validateClassIsRecipe(string|SprinkleRecipe $class): SprinkleRecipe { + if (!is_string($class)) { + return $class; + } + if (!class_exists($class)) { throw new BadClassNameException("Sprinkle recipe class `$class` not found."); } diff --git a/tests/Sprinkle/SprinkleManagerTest.php b/tests/Sprinkle/SprinkleManagerTest.php index a905b1a..f912ee4 100644 --- a/tests/Sprinkle/SprinkleManagerTest.php +++ b/tests/Sprinkle/SprinkleManagerTest.php @@ -38,6 +38,24 @@ public function testGetSprinklesWithNoDependent(): void $this->assertSame('Test Sprinkle', $manager->getMainSprinkle()->getName()); } + /** + * getSprinkles|getMainSprinkle + */ + public function testGetSprinklesWithObject(): void + { + $core = new CoreStub(); + $manager = new SprinkleManager($core); + $sprinkles = $manager->getSprinkles(); + + $this->assertCount(1, $sprinkles); + $this->assertContainsOnlyInstancesOf(CoreStub::class, $sprinkles); + $this->assertSame('Test Sprinkle', $sprinkles[CoreStub::class]->getName()); + + // Test getMainSprinkle while at it + $this->assertInstanceOf(CoreStub::class, $manager->getMainSprinkle()); + $this->assertSame('Test Sprinkle', $manager->getMainSprinkle()->getName()); + } + /** * @depends testGetSprinklesWithNoDependent */