-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor template loading mechanism #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8294cf3
Refactor template loading mechanism
thekid 4a5716d
Fix error message
thekid 47b818c
Fix withTemplates() parameter type
thekid b71a0ab
Use base interface, Input
thekid c94b931
QA: Use type unions in evaluate()
thekid 03a9c75
QA: Whitespace
thekid a110a5a
Implement listing() method from WithListing
thekid 388e7bc
QA: Use type unions in render() and transform()
thekid b48e9c3
Remove WithListing interface from Templates implementations
thekid 342ce58
QA: Fix load() api docs
thekid 02cfacb
Restore BC by making Templates implement TemplateLoader
thekid 131382b
QA: Deprecation markers
thekid e732e3f
Add possibility to store compiled templates in template sources
thekid 27d17cf
Refactor $template arguments to $source, clarifying intent
thekid 40f7e9d
Refactor exists() method to base class
thekid 67d9aef
QA: Apidocs wording
thekid 9c1960a
Rename Input -> Tokens
thekid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
26 changes: 26 additions & 0 deletions
26
src/main/php/com/github/mustache/templates/Compiled.class.php
This file contains hidden or 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,26 @@ | ||
| <?php namespace com\github\mustache\templates; | ||
|
|
||
| class Compiled extends Source { | ||
| private $tokens; | ||
|
|
||
| /** @param com.github.mustache.Template */ | ||
| public function __construct($template) { | ||
| $this->template= $template; | ||
| } | ||
|
|
||
| /** @return string */ | ||
| public function code() { return (string)$this->template; } | ||
|
|
||
| /** | ||
| * Compiles this source into a template | ||
| * | ||
| * @param com.github.mustache.MustacheParser $parser | ||
| * @param string $start | ||
| * @param string $end | ||
| * @param string $indent | ||
| * @return com.github.mustache.Template | ||
| */ | ||
| public function compile($parser, $start, $end, $indent) { | ||
| return $this->template; | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
src/main/php/com/github/mustache/templates/FromLoader.class.php
This file contains hidden or 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,47 @@ | ||
| <?php namespace com\github\mustache\templates; | ||
|
|
||
| use text\StreamTokenizer; | ||
| use lang\IllegalAccessException; | ||
| use com\github\mustache\WithListing; | ||
| use com\github\mustache\templates\Tokens; | ||
|
|
||
| /** | ||
| * Adapter for TemplateLoaders | ||
| * | ||
| * @deprecated Template loaders were replaced by `Templates`. | ||
| */ | ||
| class FromLoader extends Templates { | ||
| private $loader; | ||
|
|
||
| /** @param com.github.mustache.TemplateLoader $loader */ | ||
| public function __construct($loader) { | ||
| $this->loader= $loader; | ||
| } | ||
|
|
||
| /** | ||
| * Load a template by a given name | ||
| * | ||
| * @param string $name The template name, not including the file extension | ||
| * @return com.github.mustache.TemplateSource | ||
| */ | ||
| public function source($name) { | ||
| try { | ||
| return new Tokens($name, new StreamTokenizer($this->loader->load($name))); | ||
| } catch (TemplateNotFoundException $e) { | ||
| return new NotFound($e->getMessage()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns available templates | ||
| * | ||
| * @return com.github.mustache.TemplateListing | ||
| */ | ||
| public function listing() { | ||
| if ($this->loader instanceof WithListing) { | ||
| return $this->loader->listing(); | ||
| } else { | ||
| throw new IllegalAccessException(typeof($this->loader)->toString().' does not provide listing'); | ||
| } | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/main/php/com/github/mustache/templates/NotFound.class.php
This file contains hidden or 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,33 @@ | ||
| <?php namespace com\github\mustache\templates; | ||
|
|
||
| use com\github\mustache\TemplateNotFoundException; | ||
|
|
||
| class NotFound extends Source { | ||
| private $reason; | ||
|
|
||
| /** @param string $reason */ | ||
| public function __construct($reason) { | ||
| $this->reason= $reason; | ||
| } | ||
|
|
||
| /** @return bool */ | ||
| public function exists() { return false; } | ||
|
|
||
| /** @return string */ | ||
| public function code() { | ||
| throw new TemplateNotFoundException($this->reason); | ||
| } | ||
|
|
||
| /** | ||
| * Compiles this source into a template | ||
| * | ||
| * @param com.github.mustache.MustacheParser $parser | ||
| * @param string $start | ||
| * @param string $end | ||
| * @param string $indent | ||
| * @return com.github.mustache.Template | ||
| */ | ||
| public function compile($parser, $start, $end, $indent) { | ||
| throw new TemplateNotFoundException($this->reason); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could also be:
...but the inlined version saves us a couple of nanoseconds here:)