-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f8324c
commit f18ec04
Showing
2 changed files
with
117 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Latte (https://latte.nette.org) | ||
* Copyright (c) 2008 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Latte\Loaders; | ||
|
||
use Latte; | ||
|
||
|
||
/** | ||
* Template loader. | ||
*/ | ||
class NamespaceLoader implements Latte\Loader | ||
{ | ||
/** | ||
* @var Latte\Loader[] | ||
*/ | ||
private array $loaders; | ||
|
||
public function __construct(array $loaders) | ||
{ | ||
$this->loaders = $loaders; | ||
} | ||
|
||
/** | ||
* Returns template source code. | ||
*/ | ||
public function getContent(string $name): string | ||
{ | ||
[$loader, $name] = $this->extractLoaderAndName($name); | ||
|
||
return $loader->getContent($name); | ||
} | ||
|
||
|
||
public function isExpired(string $file, int $time): bool | ||
{ | ||
[$loader, $name] = $this->extractLoaderAndName($file); | ||
|
||
return $loader->isExpired($name, $time); | ||
} | ||
|
||
|
||
/** | ||
* Returns referred template name. | ||
*/ | ||
public function getReferredName(string $name, string $referringName): string | ||
{ | ||
[$loader, $name] = $this->extractLoaderAndName($name); | ||
|
||
return $loader->getReferredName($name, $referringName); | ||
} | ||
|
||
|
||
/** | ||
* Returns unique identifier for caching. | ||
*/ | ||
public function getUniqueId(string $name): string | ||
{ | ||
[$loader, $name] = $this->extractLoaderAndName($name); | ||
|
||
return $loader->getUniqueId($name); | ||
} | ||
|
||
|
||
private function extractLoaderAndName(string $name): array | ||
{ | ||
$namespaceParts = \explode('::', $name, 2); | ||
|
||
if (count($namespaceParts) === 2) { | ||
return [ | ||
$this->loaders[$namespaceParts[0]], | ||
$namespaceParts[1], | ||
]; | ||
} | ||
|
||
return [ | ||
$this->loaders[''], | ||
$name, | ||
]; | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
/** | ||
* Test: StringLoader | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Latte\Loaders\StringLoader; | ||
use Latte\Loaders\NamespaceLoader; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
test('', function () { | ||
$defaultLoader = new StringLoader(['main' => 'defaultcontent']); | ||
$appLoader = new StringLoader(['main' => 'appcontent']); | ||
$otherLoader = new StringLoader(['main' => 'othercontent']); | ||
|
||
$loader = new NamespaceLoader([ | ||
'' => $defaultLoader, | ||
'app' => $appLoader, | ||
'other' => $otherLoader, | ||
]); | ||
|
||
Assert::same('defaultcontent', $loader->getContent('main')); | ||
Assert::same('appcontent', $loader->getContent('app::main')); | ||
Assert::same('othercontent', $loader->getContent('other::main')); | ||
}); |