Skip to content

Instantly share code, notes, and snippets.

@vudaltsov
Last active September 14, 2021 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save vudaltsov/4c1fe1c2c2ad0f2a3850f5ef228e00db to your computer and use it in GitHub Desktop.
Save vudaltsov/4c1fe1c2c2ad0f2a3850f5ef228e00db to your computer and use it in GitHub Desktop.
Хочу выиграть билет от канала Пых на PHPFest 2020
<?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