Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Created March 24, 2024 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindplay-dk/cd5bf33533a2b7c6fe96469a1b9bd8f6 to your computer and use it in GitHub Desktop.
Save mindplay-dk/cd5bf33533a2b7c6fe96469a1b9bd8f6 to your computer and use it in GitHub Desktop.
factory.php
<?php
class UserProvider
{
public function __construct(
#[Service("user.cache-path")]
public readonly string $cache_path
) {}
#[Service("user.cache")]
public function createCache(): CacheInterface
{
return new FileCache($this->cache_path);
}
#[Service] // defaults to the return-type `UserRepository::class`
public function createUserRepository(
#[Inject("user.cache")] CacheInterface $cache
): UserRepository
{
return new UserRepository($cache);
}
#[Extension]
public function addLogger(
#[Extend("loggers")] array $loggers,
#[Inject("user.logger")] LoggerInterface $user_logger
): array
{
return [...$loggers, $user_logger, new AnotherLogger()];
}
#[Service("user.logger")]
public function createUserLogger(): LoggerInterface
{
return new SomeLogger();
}
}
@mindplay-dk
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment