-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Singleton/Prototype instances support.
- Loading branch information
Showing
8 changed files
with
524 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Ananke\Traits; | ||
|
||
use Ananke\Traits\ServiceTypeTrait; | ||
|
||
trait PrototypeServiceTrait | ||
{ | ||
use ServiceTypeTrait; | ||
|
||
/** | ||
* Register a service as prototype | ||
* | ||
* @param string $serviceName Name of the service | ||
* @return void | ||
*/ | ||
public function registerAsPrototype(string $serviceName): void | ||
{ | ||
if (array_key_exists($serviceName, $this->singletons)) { | ||
unset($this->singletons[$serviceName]); | ||
} | ||
} | ||
|
||
/** | ||
* Check if a service is registered as prototype | ||
* | ||
* @param string $serviceName Name of the service | ||
* @return bool | ||
*/ | ||
public function isPrototype(string $serviceName): bool | ||
{ | ||
return !array_key_exists($serviceName, $this->singletons); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Ananke\Traits; | ||
|
||
trait ServiceTypeTrait | ||
{ | ||
/** @var array<string, string> Service name to service type mapping */ | ||
protected array $serviceTypes = []; | ||
|
||
/** | ||
* Change service type between singleton and prototype | ||
* | ||
* @param string $serviceName Name of the service | ||
* @param string $type New service type ('singleton' or 'prototype') | ||
* @throws \InvalidArgumentException When invalid type is provided | ||
* @return void | ||
*/ | ||
public function changeServiceType(string $serviceName, string $type): void | ||
{ | ||
if (!in_array($type, ['singleton', 'prototype'])) { | ||
throw new \InvalidArgumentException("Invalid service type: $type"); | ||
} | ||
|
||
if ($type === 'prototype') { | ||
$this->registerAsPrototype($serviceName); | ||
} else { | ||
$this->registerAsSingleton($serviceName); | ||
} | ||
|
||
$this->serviceTypes[$serviceName] = $type; | ||
} | ||
|
||
/** | ||
* Get the current type of a service | ||
* | ||
* @param string $serviceName Name of the service | ||
* @return string | ||
*/ | ||
public function getServiceType(string $serviceName): string | ||
{ | ||
return $this->serviceTypes[$serviceName] ?? 'prototype'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Ananke\Traits; | ||
|
||
trait SingletonServiceTrait | ||
{ | ||
/** @var array<string, object> Singleton instances storage */ | ||
private array $singletons = []; | ||
|
||
/** | ||
* Register a service as singleton | ||
* | ||
* @param string $serviceName Name of the service | ||
* @return void | ||
*/ | ||
public function registerAsSingleton(string $serviceName): void | ||
{ | ||
if (!isset($this->singletons[$serviceName])) { | ||
$this->singletons[$serviceName] = null; | ||
} | ||
} | ||
|
||
/** | ||
* Check if a service is registered as singleton | ||
* | ||
* @param string $serviceName Name of the service | ||
* @return bool | ||
*/ | ||
public function isSingleton(string $serviceName): bool | ||
{ | ||
return array_key_exists($serviceName, $this->singletons); | ||
} | ||
|
||
/** | ||
* Get or create a singleton instance | ||
* | ||
* @param string $serviceName Name of the service | ||
* @param callable $factory Factory function to create the instance if needed | ||
* @return object | ||
*/ | ||
protected function getSingletonInstance(string $serviceName, callable $factory): object | ||
{ | ||
if (!isset($this->singletons[$serviceName])) { | ||
$this->singletons[$serviceName] = $factory(); | ||
} | ||
|
||
return $this->singletons[$serviceName]; | ||
} | ||
|
||
/** | ||
* Clear all singleton instances | ||
* | ||
* @return void | ||
*/ | ||
public function clearSingletons(): void | ||
{ | ||
$this->singletons = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Ananke\Tests\Fixtures; | ||
|
||
class ComplexService | ||
{ | ||
private string $id; | ||
private array $data; | ||
|
||
public function __construct() | ||
{ | ||
$this->id = uniqid('complex_', true); | ||
$this->data = []; | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setData(array $data): void | ||
{ | ||
$this->data = $data; | ||
} | ||
|
||
public function getData(): array | ||
{ | ||
return $this->data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Ananke\Tests\Fixtures; | ||
|
||
class SimpleService | ||
{ | ||
private string $id; | ||
|
||
public function __construct() | ||
{ | ||
$this->id = uniqid('simple_', true); | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
} |
Oops, something went wrong.