Skip to content

Commit 0029036

Browse files
authored
Merge pull request #53 from sanmai/virtual-fs
Virtual FS
2 parents 9e65b6a + e3369d8 commit 0029036

File tree

7 files changed

+238
-137
lines changed

7 files changed

+238
-137
lines changed

src/Liquid/FileSystem/Local.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Liquid package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* @package Liquid
10+
*/
11+
12+
namespace Liquid\FileSystem;
13+
14+
use Liquid\FileSystem;
15+
use Liquid\LiquidException;
16+
use Liquid\Regexp;
17+
use Liquid\Liquid;
18+
19+
/**
20+
* This implements an abstract file system which retrieves template files named in a manner similar to Rails partials,
21+
* ie. with the template name prefixed with an underscore. The extension ".liquid" is also added.
22+
*
23+
* For security reasons, template paths are only allowed to contain letters, numbers, and underscore.
24+
*/
25+
class Local implements FileSystem
26+
{
27+
/**
28+
* The root path
29+
*
30+
* @var string
31+
*/
32+
private $root;
33+
34+
/**
35+
* Constructor
36+
*
37+
* @param string $root The root path for templates
38+
*/
39+
public function __construct($root) {
40+
// since root path can only be set from constructor, we check it once right here
41+
if (!empty($root)) {
42+
$realRoot = realpath($root);
43+
if ($realRoot === false) {
44+
throw new LiquidException("Root path could not be found: '$root'");
45+
}
46+
$root = $realRoot;
47+
}
48+
49+
$this->root = $root;
50+
}
51+
52+
/**
53+
* Retrieve a template file
54+
*
55+
* @param string $templatePath
56+
*
57+
* @return string template content
58+
*/
59+
public function readTemplateFile($templatePath) {
60+
return file_get_contents($this->fullPath($templatePath));
61+
}
62+
63+
/**
64+
* Resolves a given path to a full template file path, making sure it's valid
65+
*
66+
* @param string $templatePath
67+
*
68+
* @throws LiquidException
69+
* @return string
70+
*/
71+
public function fullPath($templatePath) {
72+
if (empty($templatePath)) {
73+
throw new LiquidException("Empty template name");
74+
}
75+
76+
$nameRegex = Liquid::get('INCLUDE_ALLOW_EXT')
77+
? new Regexp('/^[^.\/][a-zA-Z0-9_\.\/]+$/')
78+
: new Regexp('/^[^.\/][a-zA-Z0-9_\/]+$/');
79+
80+
if (!$nameRegex->match($templatePath)) {
81+
throw new LiquidException("Illegal template name '$templatePath'");
82+
}
83+
84+
$templateDir = dirname($templatePath);
85+
$templateFile = basename($templatePath);
86+
87+
if (!Liquid::get('INCLUDE_ALLOW_EXT')) {
88+
$templateFile = Liquid::get('INCLUDE_PREFIX') . $templateFile . '.' . Liquid::get('INCLUDE_SUFFIX');
89+
}
90+
91+
$fullPath = join(DIRECTORY_SEPARATOR, array($this->root, $templateDir, $templateFile));
92+
93+
$realFullPath = realpath($fullPath);
94+
if ($realFullPath === false) {
95+
throw new LiquidException("File not found: $fullPath");
96+
}
97+
98+
if (strpos($realFullPath, $this->root) !== 0) {
99+
throw new LiquidException("Illegal template full path: {$realFullPath} not under {$this->root}");
100+
}
101+
102+
return $realFullPath;
103+
}
104+
}

src/Liquid/FileSystem/Virtual.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Liquid package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* @package Liquid
10+
*/
11+
12+
namespace Liquid\FileSystem;
13+
14+
use Liquid\FileSystem;
15+
use Liquid\LiquidException;
16+
17+
/**
18+
* This implements a virtual file system with actual code used to find files injected from outside thus achieving inversion of control.
19+
*/
20+
class Virtual implements FileSystem
21+
{
22+
/**
23+
* @var callable
24+
*/
25+
private $callback;
26+
27+
/**
28+
* Constructor
29+
*
30+
* @param callable $callback Callback is responsible for providing content of requested templates. Should return template's text.
31+
* @throws LiquidException
32+
*/
33+
public function __construct($callback) {
34+
// Since a callback can only be set from the constructor, we check it once right here.
35+
if (!is_callable($callback)) {
36+
throw new LiquidException("Not a callback provided");
37+
}
38+
39+
$this->callback = $callback;
40+
}
41+
42+
/**
43+
* Retrieve a template file
44+
*
45+
* @param string $templatePath
46+
*
47+
* @return string template content
48+
*/
49+
public function readTemplateFile($templatePath) {
50+
return call_user_func($this->callback, $templatePath);
51+
}
52+
}

src/Liquid/LocalFileSystem.php

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -12,88 +12,6 @@
1212
namespace Liquid;
1313

1414
/**
15-
* This implements an abstract file system which retrieves template files named in a manner similar to Rails partials,
16-
* ie. with the template name prefixed with an underscore. The extension ".liquid" is also added.
17-
*
18-
* For security reasons, template paths are only allowed to contain letters, numbers, and underscore.
15+
* @deprecated Left for backward compatibility reasons. Use \Liquid\FileSystem\Local instead.
1916
*/
20-
class LocalFileSystem implements FileSystem
21-
{
22-
/**
23-
* The root path
24-
*
25-
* @var string
26-
*/
27-
private $root;
28-
29-
/**
30-
* Constructor
31-
*
32-
* @param string $root The root path for templates
33-
*/
34-
public function __construct($root) {
35-
// since root path can only be set from constructor, we check it once right here
36-
if (!empty($root)) {
37-
$realRoot = realpath($root);
38-
if ($realRoot === false) {
39-
throw new LiquidException("Root path could not be found: '$root'");
40-
}
41-
$root = $realRoot;
42-
}
43-
44-
$this->root = $root;
45-
}
46-
47-
/**
48-
* Retrieve a template file
49-
*
50-
* @param string $templatePath
51-
*
52-
* @return string template content
53-
*/
54-
public function readTemplateFile($templatePath) {
55-
return file_get_contents($this->fullPath($templatePath));
56-
}
57-
58-
/**
59-
* Resolves a given path to a full template file path, making sure it's valid
60-
*
61-
* @param string $templatePath
62-
*
63-
* @throws LiquidException
64-
* @return string
65-
*/
66-
public function fullPath($templatePath) {
67-
if (empty($templatePath)) {
68-
throw new LiquidException("Empty template name");
69-
}
70-
71-
$nameRegex = Liquid::get('INCLUDE_ALLOW_EXT')
72-
? new Regexp('/^[^.\/][a-zA-Z0-9_\.\/]+$/')
73-
: new Regexp('/^[^.\/][a-zA-Z0-9_\/]+$/');
74-
75-
if (!$nameRegex->match($templatePath)) {
76-
throw new LiquidException("Illegal template name '$templatePath'");
77-
}
78-
79-
$templateDir = dirname($templatePath);
80-
$templateFile = basename($templatePath);
81-
82-
if (!Liquid::get('INCLUDE_ALLOW_EXT')) {
83-
$templateFile = Liquid::get('INCLUDE_PREFIX') . $templateFile . '.' . Liquid::get('INCLUDE_SUFFIX');
84-
}
85-
86-
$fullPath = join(DIRECTORY_SEPARATOR, array($this->root, $templateDir, $templateFile));
87-
88-
$realFullPath = realpath($fullPath);
89-
if ($realFullPath === false) {
90-
throw new LiquidException("File not found: $fullPath");
91-
}
92-
93-
if (strpos($realFullPath, $this->root) !== 0) {
94-
throw new LiquidException("Illegal template full path: {$realFullPath} not under {$this->root}");
95-
}
96-
97-
return $realFullPath;
98-
}
99-
}
17+
class LocalFileSystem extends \Liquid\FileSystem\Local {}

tests/Liquid/Tag/LiquidTestFileSystem.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

tests/Liquid/Tag/TagExtendsTest.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,31 @@
1414
use Liquid\TestCase;
1515
use Liquid\Template;
1616
use Liquid\Cache\Local;
17+
use Liquid\FileSystem\Virtual;
1718

1819
/**
1920
* @see TagExtends
2021
*/
2122
class TagExtendsTest extends TestCase
2223
{
24+
private $fs;
25+
26+
protected function setUp() {
27+
$this->fs = new Virtual(function ($templatePath) {
28+
if ($templatePath == 'base') {
29+
return "{% block content %}{% endblock %}{% block footer %}{% endblock %}";
30+
}
31+
32+
if ($templatePath == 'sub-base') {
33+
return "{% extends 'base' %}{% block content %}{% endblock %}{% block footer %} Boo! {% endblock %}";
34+
}
35+
});
36+
}
37+
2338
public function testBasicExtends()
2439
{
2540
$template = new Template();
26-
$template->setFileSystem(new LiquidTestFileSystem());
41+
$template->setFileSystem($this->fs);
2742
$template->parse("{% extends 'base' %}{% block content %}{{ hello }}{% endblock %}");
2843
$output = $template->render(array("hello" => "Hello!"));
2944
$this->assertEquals("Hello!", $output);
@@ -32,25 +47,25 @@ public function testBasicExtends()
3247
public function testDefaultContentExtends()
3348
{
3449
$template = new Template();
35-
$template->setFileSystem(new LiquidTestFileSystem());
50+
$template->setFileSystem($this->fs);
3651
$template->parse("{% block content %}{{ hello }}{% endblock %}\n{% extends 'sub-base' %}");
3752
$output = $template->render(array("hello" => "Hello!"));
3853
$this->assertEquals("Hello!\n Boo! ", $output);
3954
}
4055

41-
4256
public function testDeepExtends()
4357
{
4458
$template = new Template();
45-
$template->setFileSystem(new LiquidTestFileSystem());
59+
$template->setFileSystem($this->fs);
4660
$template->parse('{% extends "sub-base" %}{% block content %}{{ hello }}{% endblock %}{% block footer %} I am a footer.{% endblock %}');
61+
4762
$output = $template->render(array("hello" => "Hello!"));
4863
$this->assertEquals("Hello! I am a footer.", $output);
4964
}
5065

5166
public function testWithCache() {
5267
$template = new Template();
53-
$template->setFileSystem(new LiquidTestFileSystem());
68+
$template->setFileSystem($this->fs);
5469
$template->setCache(new Local());
5570

5671
foreach (array("Before cache", "With cache") as $type) {

0 commit comments

Comments
 (0)