-
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.
- added renderer bridge `LatteRendererBridge` - added default template for all types of positions in Latte - added integration of renderers into the `AmpClientExtension` - added tests
- Loading branch information
Showing
37 changed files
with
1,008 additions
and
128 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
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\AmpClient\Bridge\Nette\DI\Config; | ||
|
||
use Nette\DI\Definitions\Statement; | ||
|
||
final class RendererConfig | ||
{ | ||
/** @var string|Statement|null */ | ||
public $bridge = null; | ||
|
||
/** @var array<string, string> */ | ||
public array $templates = []; | ||
} |
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\AmpClient\Renderer\Latte; | ||
|
||
use Closure; | ||
use Latte\Engine; | ||
use function assert; | ||
|
||
final class ClosureLatteFactory implements LatteFactoryInterface | ||
{ | ||
/** @var Closure(): Engine */ | ||
private Closure $factory; | ||
|
||
/** | ||
* @param Closure(): Engine $factory | ||
*/ | ||
public function __construct(Closure $factory) | ||
{ | ||
$this->factory = $factory; | ||
} | ||
|
||
public function create(): Engine | ||
{ | ||
$latte = ($this->factory)(); | ||
|
||
assert($latte instanceof Engine); | ||
|
||
return $latte; | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\AmpClient\Renderer\Latte; | ||
|
||
use Latte\Engine; | ||
|
||
interface LatteFactoryInterface | ||
{ | ||
public function create(): Engine; | ||
} |
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,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\AmpClient\Renderer\Latte; | ||
|
||
use Latte\Engine; | ||
use SixtyEightPublishers\AmpClient\Renderer\Latte\Templates\MultipleTemplate; | ||
use SixtyEightPublishers\AmpClient\Renderer\Latte\Templates\NotFoundTemplate; | ||
use SixtyEightPublishers\AmpClient\Renderer\Latte\Templates\RandomTemplate; | ||
use SixtyEightPublishers\AmpClient\Renderer\Latte\Templates\SingleTemplate; | ||
use SixtyEightPublishers\AmpClient\Renderer\RendererBridgeInterface; | ||
use SixtyEightPublishers\AmpClient\Renderer\Templates; | ||
use SixtyEightPublishers\AmpClient\Response\ValueObject\Banner; | ||
use SixtyEightPublishers\AmpClient\Response\ValueObject\Position; | ||
|
||
final class LatteRendererBridge implements RendererBridgeInterface | ||
{ | ||
private LatteFactoryInterface $latteFactory; | ||
|
||
private Templates $templates; | ||
|
||
private ?Engine $latte = null; | ||
|
||
public function __construct(LatteFactoryInterface $latteFactory) | ||
{ | ||
$this->latteFactory = $latteFactory; | ||
$this->templates = new Templates([ | ||
Templates::TemplateSingle => __DIR__ . '/Templates/single.latte', | ||
Templates::TemplateRandom => __DIR__ . '/Templates/random.latte', | ||
Templates::TemplateMultiple => __DIR__ . '/Templates/multiple.latte', | ||
Templates::TemplateNotFound => __DIR__ . '/Templates/notFound.latte', | ||
]); | ||
} | ||
|
||
public static function fromEngine(Engine $engine): self | ||
{ | ||
return new self( | ||
new ClosureLatteFactory(static fn (): Engine => $engine), | ||
); | ||
} | ||
|
||
public function overrideTemplates(Templates $templates): self | ||
{ | ||
$renderer = clone $this; | ||
$renderer->templates = $this->templates->override($templates); | ||
|
||
return $renderer; | ||
} | ||
|
||
public function renderNotFound(Position $position): string | ||
{ | ||
return $this->getLatte()->renderToString( | ||
$this->templates->getTemplateFile(Templates::TemplateNotFound), | ||
new NotFoundTemplate($position), | ||
); | ||
} | ||
|
||
public function renderSingle(Position $position, ?Banner $banner): string | ||
{ | ||
return $this->getLatte()->renderToString( | ||
$this->templates->getTemplateFile(Templates::TemplateSingle), | ||
new SingleTemplate($position, $banner), | ||
); | ||
} | ||
|
||
public function renderRandom(Position $position, ?Banner $banner): string | ||
{ | ||
return $this->getLatte()->renderToString( | ||
$this->templates->getTemplateFile(Templates::TemplateRandom), | ||
new RandomTemplate($position, $banner), | ||
); | ||
} | ||
|
||
public function renderMultiple(Position $position, array $banners): string | ||
{ | ||
return $this->getLatte()->renderToString( | ||
$this->templates->getTemplateFile(Templates::TemplateMultiple), | ||
new MultipleTemplate($position, $banners), | ||
); | ||
} | ||
|
||
private function getLatte(): Engine | ||
{ | ||
if (null === $this->latte) { | ||
$this->latte = $this->latteFactory->create(); | ||
} | ||
|
||
return $this->latte; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\AmpClient\Renderer\Latte\Templates; | ||
|
||
use SixtyEightPublishers\AmpClient\Response\ValueObject\Banner; | ||
use SixtyEightPublishers\AmpClient\Response\ValueObject\Position; | ||
|
||
final class MultipleTemplate | ||
{ | ||
public Position $position; | ||
|
||
/** @var array<int, Banner> */ | ||
public array $banners; | ||
|
||
/** | ||
* @param array<int, Banner> $banners | ||
*/ | ||
public function __construct( | ||
Position $position, | ||
array $banners | ||
) { | ||
$this->position = $position; | ||
$this->banners = $banners; | ||
} | ||
} |
Oops, something went wrong.