Skip to content

Commit 91639ac

Browse files
Add namespace loader
1 parent 5645d7b commit 91639ac

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

src/Latte/Loaders/FileLoader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function getReferredName(string $file, string $referringFile): string
7575
*/
7676
public function getUniqueId(string $file): string
7777
{
78-
return $this->baseDir . strtr($file, '/', DIRECTORY_SEPARATOR);
78+
return $this->baseDir . strtr($file, '/', DIRECTORY_SEPARATOR);
7979
}
8080

8181

src/Latte/Loaders/NamespaceLoader.php

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Latte (https://latte.nette.org)
5+
* Copyright (c) 2008 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Latte\Loaders;
11+
12+
use Latte;
13+
14+
15+
/**
16+
* Template loader.
17+
*/
18+
class NamespaceLoader implements Latte\Loader
19+
{
20+
/**
21+
* @var Latte\Loader[]
22+
*/
23+
private array $loaders;
24+
25+
public function __construct(array $loaders)
26+
{
27+
$this->loaders = $loaders;
28+
}
29+
30+
/**
31+
* Returns template source code.
32+
*/
33+
public function getContent(string $name): string
34+
{
35+
[$loader, $name] = $this->extractLoaderAndName($name);
36+
37+
return $loader->getContent($name);
38+
}
39+
40+
41+
public function isExpired(string $file, int $time): bool
42+
{
43+
[$loader, $name] = $this->extractLoaderAndName($file);
44+
45+
return $loader->isExpired($name, $time);
46+
}
47+
48+
49+
/**
50+
* Returns referred template name.
51+
*/
52+
public function getReferredName(string $name, string $referringName): string
53+
{
54+
[$loader, $name] = $this->extractLoaderAndName($name);
55+
56+
return $loader->getReferredName($name, $referringName);
57+
}
58+
59+
60+
/**
61+
* Returns unique identifier for caching.
62+
*/
63+
public function getUniqueId(string $name): string
64+
{
65+
[$loader, $name] = $this->extractLoaderAndName($name);
66+
67+
return $loader->getUniqueId($name);
68+
}
69+
70+
71+
private function extractLoaderAndName(string $name): array
72+
{
73+
$namespaceParts = \explode('::', $name, 2);
74+
75+
if (count($namespaceParts) === 2) {
76+
return [
77+
$this->loaders[$namespaceParts[0]],
78+
$namespaceParts[1],
79+
];
80+
}
81+
82+
return [
83+
$this->loaders[''],
84+
$name,
85+
];
86+
}
87+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* Test: StringLoader
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Latte\Loaders\StringLoader;
10+
use Latte\Loaders\NamespaceLoader;
11+
use Tester\Assert;
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
test('', function () {
17+
$defaultLoader = new StringLoader(['main' => 'defaultcontent']);
18+
$appLoader = new StringLoader(['main' => 'appcontent']);
19+
$otherLoader = new StringLoader(['main' => 'othercontent']);
20+
21+
$loader = new NamespaceLoader([
22+
'' => $defaultLoader,
23+
'app' => $appLoader,
24+
'other' => $otherLoader,
25+
]);
26+
27+
Assert::same('defaultcontent', $loader->getContent('main'));
28+
Assert::same('appcontent', $loader->getContent('app::main'));
29+
Assert::same('othercontent', $loader->getContent('other::main'));
30+
});

0 commit comments

Comments
 (0)