-
-
Save vudaltsov/4c1fe1c2c2ad0f2a3850f5ef228e00db to your computer and use it in GitHub Desktop.
Хочу выиграть билет от канала Пых на PHPFest 2020
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 | |
final class Data | |
{ | |
// ... | |
} | |
final class Report | |
{ | |
// ... | |
} | |
interface ReportFactory | |
{ | |
public function createReport(Data $data): Report; | |
} | |
/** | |
* Registered as a shared DI service | |
*/ | |
final class HeavyReportFactory implements ReportFactory | |
{ | |
public function createReport(Data $data): Report | |
{ | |
// ... | |
return new Report(); | |
} | |
} | |
/** | |
* Registered as a shared DI service | |
*/ | |
final class InMemoryCachingReportFactory implements ReportFactory | |
{ | |
private ReportFactory $reportFactory; | |
/** | |
* @var SplObjectStorage<Data, Report> | |
*/ | |
private SplObjectStorage $reports; | |
public function __construct(ReportFactory $reportFactory) | |
{ | |
$this->reportFactory = $reportFactory; | |
$this->reports = new SplObjectStorage(); | |
} | |
public function createReport(Data $data): Report | |
{ | |
return $this->reports[$data] ??= $this->reportFactory->createReport($data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment