Skip to content

Commit

Permalink
Allow sprinkle as object in Cupcake instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Feb 17, 2024
1 parent 4178f9c commit 3deed7a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/Cupcake.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use DI\ContainerBuilder;
use UserFrosting\ServicesProvider\FrameworkService;
use UserFrosting\Sprinkle\SprinkleManager;
use UserFrosting\Sprinkle\SprinkleRecipe;

/**
* Base class for UserFrosting application.
Expand All @@ -35,9 +36,9 @@ abstract class Cupcake
/**
* Constructor.
*
* @param class-string<\UserFrosting\Sprinkle\SprinkleRecipe> $mainSprinkle
* @param class-string<SprinkleRecipe>|SprinkleRecipe $mainSprinkle
*/
public function __construct(protected string $mainSprinkle)
public function __construct(protected string|SprinkleRecipe $mainSprinkle)
{
$this->init();
}
Expand Down Expand Up @@ -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;
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/Sprinkle/SprinkleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class SprinkleManager
/**
* Load sprinkles on construction.
*
* @param class-string<SprinkleRecipe> $mainSprinkle
* @param class-string<SprinkleRecipe>|SprinkleRecipe $mainSprinkle
*/
public function __construct(string $mainSprinkle)
public function __construct(string|SprinkleRecipe $mainSprinkle)
{
$this->mainSprinkle = $this->validateClassIsRecipe($mainSprinkle);
$this->loadSprinkles();
Expand Down Expand Up @@ -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.");
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Sprinkle/SprinkleManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 3deed7a

Please sign in to comment.