Permalink
Browse files

Cache DustPHP template file paths during runtime

  • Loading branch information...
villesiltala committed Oct 22, 2018
1 parent 3a17e52 commit 80d54128ce490d1881557e3af37dea61f0ffd015
Showing with 24 additions and 0 deletions.
  1. +5 −0 CHANGELOG.md
  2. +19 −0 dust/Dust.php
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Added
- Cache DustPHP template file paths during runtime to prevent multiple recursive directory searches for same template requests.
## [1.17.0] - 2018-10-19
### Fixed
@@ -23,6 +23,12 @@ class Dust implements \Serializable
*/
public $templates = [];
/**
* Stores found template paths for faster template loading.
* @var array
*/
protected static $dustFileCache = [];
/**
* @var array
*/
@@ -136,12 +142,18 @@ public function compileFn($source, $name = NULL) {
* @return null|string
*/
public function resolveAbsoluteDustFilePath($path, $basePath = NULL) {
//add extension if necessary
if(substr_compare($path, self::FILE_EXTENSION, -5, 5) !== 0)
{
$path .= self::FILE_EXTENSION;
}
// Get from cache if the file is already found.
if ( isset( static::$dustFileCache[ $path ] ) ) {
return static::$dustFileCache[ $path ];
}
//try the current path
$possible = realpath($path);
@@ -154,11 +166,18 @@ public function resolveAbsoluteDustFilePath($path, $basePath = NULL) {
foreach ( $this->includedDirectories as $directory ) {
foreach ( new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $directory, \RecursiveDirectoryIterator::SKIP_DOTS ) ) as $file ) {
if ( substr_compare($file, "/".$path, strlen($file)-strlen("/".$path), strlen("/".$path)) === 0 ) {
// Cache the found file.
static::$dustFileCache[ $path ] = (string) $file;
return (string)$file;
}
}
}
// Cache the not found result to prevent subsequent searches.
static::$dustFileCache[ $path ] = NULL;
return NULL;
}

0 comments on commit 80d5412

Please sign in to comment.