Created
March 24, 2024 16:53
-
-
Save mindplay-dk/cd5bf33533a2b7c6fe96469a1b9bd8f6 to your computer and use it in GitHub Desktop.
factory.php
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
<?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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Discussion here:
https://groups.google.com/g/php-fig/c/SWIfYEkw89I/m/HIw_nmUdAAAJ