Skip to content

Commit

Permalink
Merge pull request #14 from delatbabel/slimdown
Browse files Browse the repository at this point in the history
Slim down StringBladeCompiler code
  • Loading branch information
TerrePorter committed Mar 4, 2016
2 parents 8e1ffc5 + 04cc352 commit 547a857
Show file tree
Hide file tree
Showing 16 changed files with 208 additions and 1,336 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
/.netbeans/
/bkp/
/bkp/
.directory
composer.lock
/vendor/
/.idea/
/documents/
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@
],
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.1.*"
"illuminate/filesystem": "5.1.*",
"illuminate/support": "5.1.*",
"illuminate/view": "5.1.*"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"mockery/mockery": "^0.9.4"
},
"autoload": {
"psr-4": {
"Wpb\\String_Blade_Compiler\\": "src/"
}
}
},
"minimum-stability": "dev"
}
25 changes: 25 additions & 0 deletions runtests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

#
# Command line runner for unit tests for composer projects
# (c) Del 2015 http://www.babel.com.au/
# No Rights Reserved
#

#
# Clean up after any previous test runs
#
mkdir -p documents
rm -rf documents/coverage-html-new
rm -f documents/coverage.xml

#
# Run phpunit
#
vendor/bin/phpunit --coverage-html documents/coverage-html-new --coverage-clover documents/coverage.xml

if [ -d documents/coverage-html-new ]; then
rm -rf documents/coverage-html
mv documents/coverage-html-new documents/coverage-html
fi

10 changes: 7 additions & 3 deletions src/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public function setEscapedContentTags($openTag, $closeTag)
* @param bool $escaped
* @return void
*/
public function setContentTagsEscaped($escaped = true) {
public function setContentTagsEscaped($escaped = true)
{
$this->contentTagsEscaped = $escaped;
}

Expand All @@ -104,7 +105,8 @@ public function setContentTagsEscaped($escaped = true) {
* @param bool $recompile
* @return void
*/
public function setForceTemplateRecompile($recompile = true) {
public function setForceTemplateRecompile($recompile = true)
{
$this->forceTemplateRecompile = $recompile;
}

Expand Down Expand Up @@ -134,5 +136,7 @@ public function isExpired($path)
* @param string $format
* @return void *
*/
public function setEchoFormat($format) {}
public function setEchoFormat($format)
{
}
}
101 changes: 49 additions & 52 deletions src/Compilers/StringBladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,74 +5,71 @@
use Config;
use Illuminate\View\Compilers\CompilerInterface;

class StringBladeCompiler extends BladeCompiler implements CompilerInterface {
class StringBladeCompiler extends BladeCompiler implements CompilerInterface
{

/**
* Compile the view at the given path.
*
* @param object $viewData
* @return void
*/
public function compile($viewData)
{
/**
* Compile the view at the given path.
*
* @param object $viewData
* @return void
*/
public function compile($viewData)
{

// get the template data
$string = $viewData->template;

// Compile to PHP
$contents = $this->compileString($string);
// Compile to PHP
$contents = $this->compileString($string);

// check/save cache
if ( ! is_null($this->cachePath))
{
$this->files->put($this->getCompiledPath($viewData), $contents);

}
}

/**
* Get the path to the compiled version of a view.
*
* @param object $viewData
* @return string
*/
public function getCompiledPath($viewData)
{
/*
* A unique path for the given model instance must be generated
* so the view has a place to cache. The following generates a
* path using almost the same logic as Blueprint::createIndexName()
*/
return $this->cachePath.'/'.$viewData->cache_key;
}
if (! is_null($this->cachePath)) {
$this->files->put($this->getCompiledPath($viewData), $contents);
}
}

/**
* Determine if the view at the given path is expired.
*
* @param string $viewData
* @return bool
*/
public function isExpired($viewData)
{
/**
* Get the path to the compiled version of a view.
*
* @param object $viewData
* @return string
*/
public function getCompiledPath($viewData)
{
/*
* A unique path for the given model instance must be generated
* so the view has a place to cache. The following generates a
* path using almost the same logic as Blueprint::createIndexName()
*/
return $this->cachePath.'/'.$viewData->cache_key;
}

$compiled = $this->getCompiledPath($viewData);
/**
* Determine if the view at the given path is expired.
*
* @param string $viewData
* @return bool
*/
public function isExpired($viewData)
{
$compiled = $this->getCompiledPath($viewData);

// If the compiled file doesn't exist we will indicate that the view is expired
// so that it can be re-compiled. Else, we will verify the last modification
// of the views is less than the modification times of the compiled views.
if ( ! $this->cachePath || ! $this->files->exists($compiled))
{
return true;
}
// If the compiled file doesn't exist we will indicate that the view is expired
// so that it can be re-compiled. Else, we will verify the last modification
// of the views is less than the modification times of the compiled views.
if (! $this->cachePath || ! $this->files->exists($compiled)) {
return true;
}

// If set to 0, then return cache has expired
if ($viewData->secondsTemplateCacheExpires==0) {
if ($viewData->secondsTemplateCacheExpires == 0) {
return true;
}

// Note: The lastModified time for a file on homestead will use the time from the host system.
// This means the vm time could be off, so setting the timeout to seconds may not work as expected.

return time() >= ($this->files->lastModified($compiled) + $viewData->secondsTemplateCacheExpires) ;
}
return time() >= ($this->files->lastModified($compiled) + $viewData->secondsTemplateCacheExpires);
}
}
15 changes: 8 additions & 7 deletions src/Engines/CompilerEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Wpb\String_Blade_Compiler\Engines;

use ErrorException;
use Illuminate\View\Compilers\CompilerInterface;

/**
* Class CompilerEngine
*
* Extends Laravel CompilerEngine to allow StringView classes.
*/
class CompilerEngine extends \Illuminate\View\Engines\CompilerEngine
{
/**
Expand All @@ -17,7 +19,7 @@ class CompilerEngine extends \Illuminate\View\Engines\CompilerEngine
/**
* Get the evaluated contents of the view.
*
* @param string $path
* @param string|object $path
* @param array $data
* @return string
*/
Expand All @@ -43,8 +45,6 @@ public function get($path, array $data = [])
// which have been rendered for right exception messages to be generated.
$results = $this->evaluatePath($compiled, $data);



array_pop($this->lastCompiled);

return $results;
Expand All @@ -55,7 +55,8 @@ public function get($path, array $data = [])
*
* @param bool $delete
*/
public function setDeleteViewCacheAfterRender($delete = true) {
public function setDeleteViewCacheAfterRender($delete = true)
{
$this->deleteViewCacheAfterRender = $delete;
}
}
Loading

0 comments on commit 547a857

Please sign in to comment.