Skip to content
Permalink
Browse files

Code optimization

  • Loading branch information
cedx committed Apr 25, 2020
1 parent 5a6a5d5 commit 77f87843830e96fd14acd88fb0bbb3ba58b4010b
Showing with 24 additions and 15 deletions.
  1. +14 −8 src/Cache.php
  2. +9 −6 src/Loader.php
  3. +1 −1 test/index.php
@@ -7,7 +7,7 @@
class Cache extends BaseObject implements \Mustache_Cache {

/** @var ViewRenderer The instance used to render the views. */
public ViewRenderer $viewRenderer;
public ?ViewRenderer $viewRenderer = null;

/**
* Caches and loads a compiled view.
@@ -16,11 +16,14 @@ class Cache extends BaseObject implements \Mustache_Cache {
*/
function cache($key, $value): void {
assert(is_string($key) && mb_strlen($key) > 0);
if (!$this->viewRenderer->enableCaching) eval("?>$value");

/** @var ViewRenderer $viewRenderer */
$viewRenderer = $this->viewRenderer;
if (!$viewRenderer->enableCaching) eval("?>$value");
else {
/** @var \yii\caching\Cache $cache */
$cache = $this->viewRenderer->cache;
$cache->set([__CLASS__, $key], $value, $this->viewRenderer->cachingDuration);
$cache = $viewRenderer->cache;
$cache->set([__CLASS__, $key], $value, $viewRenderer->cachingDuration);
$this->load($key);
}
}
@@ -31,7 +34,7 @@ function cache($key, $value): void {
*/
function init(): void {
parent::init();
if (!$this->viewRenderer instanceof ViewRenderer) throw new InvalidConfigException('The view renderer is not initialized.');
if (!$this->viewRenderer) throw new InvalidConfigException('The view renderer is not initialized.');
}

/**
@@ -41,11 +44,14 @@ function init(): void {
*/
function load($key): bool {
assert(is_string($key) && mb_strlen($key) > 0);
$cacheKey = [__CLASS__, $key];

/** @var ViewRenderer $viewRenderer */
$viewRenderer = $this->viewRenderer;

/** @var \yii\caching\Cache $cache */
$cache = $this->viewRenderer->cache;
if (!$this->viewRenderer->enableCaching || !$cache->exists($cacheKey)) return false;
$cache = $viewRenderer->cache;
$cacheKey = [__CLASS__, $key];
if (!$viewRenderer->enableCaching || !$cache->exists($cacheKey)) return false;

eval("?>{$cache->get($cacheKey)}");
return true;
@@ -8,7 +8,7 @@
class Loader extends BaseObject implements \Mustache_Loader {

/** @var ViewRenderer The instance used to render the views. */
public ViewRenderer $viewRenderer;
public ?ViewRenderer $viewRenderer = null;

/** @var string[] The loaded views. */
private array $views = [];
@@ -19,7 +19,7 @@ class Loader extends BaseObject implements \Mustache_Loader {
*/
function init(): void {
parent::init();
if (!$this->viewRenderer instanceof ViewRenderer) throw new InvalidConfigException('The view renderer is not initialized.');
if (!$this->viewRenderer) throw new InvalidConfigException('The view renderer is not initialized.');
}

/**
@@ -38,14 +38,17 @@ function load($name): string {
}

if (!isset($this->views[$name])) {
/** @var ViewRenderer $viewRenderer */
$viewRenderer = $this->viewRenderer;

/** @var \yii\caching\Cache $cache */
$cache = $this->viewRenderer->cache;
$cache = $viewRenderer->cache;
$cacheKey = [__METHOD__, $name];

if ($this->viewRenderer->enableCaching && $cache->exists($cacheKey)) $output = $cache->get($cacheKey);
if ($viewRenderer->enableCaching && $cache->exists($cacheKey)) $output = $cache->get($cacheKey);
else {
/** @var View $view */
$view = $this->viewRenderer->view;
$view = $viewRenderer->view;
$path = $findViewFile->invoke($view, $name, $view->context);
if ($view->theme) {
/** @var \yii\base\Theme $theme */
@@ -58,7 +61,7 @@ function load($name): string {

$fileObject = new \SplFileObject(FileHelper::localize($fileInfo->getPathname()));
$output = (string) $fileObject->fread($fileObject->getSize());
if ($this->viewRenderer->enableCaching) $cache->set($cacheKey, $output, $this->viewRenderer->cachingDuration);
if ($viewRenderer->enableCaching) $cache->set($cacheKey, $output, $viewRenderer->cachingDuration);
}

$this->views[$name] = $output;
@@ -6,7 +6,7 @@
define('YII_ENV', 'test');

// Load the class library.
$rootPath = dirname(__DIR__);
$rootPath = (new SplFileInfo(__DIR__))->getPath();
require_once "$rootPath/vendor/autoload.php";
require_once "$rootPath/vendor/yiisoft/yii2/Yii.php";
Yii::setAlias('@root', $rootPath);

0 comments on commit 77f8784

Please sign in to comment.