Skip to content
Permalink
Browse files

Refactor caching part2

  • Loading branch information...
redaxmedia committed Nov 16, 2018
1 parent 1ba0cde commit 0b38348574220019a1f1171bc612c0eadd23104c
Showing with 48 additions and 8 deletions.
  1. +32 −1 includes/Filesystem/Filesystem.php
  2. +6 −1 modules/PageCache/Config.php
  3. +10 −6 modules/PageCache/PageCache.php
@@ -12,6 +12,7 @@
use function in_array;
use function is_array;
use function is_dir;
use function iterator_count;
use function sort;
/**
@@ -70,16 +71,46 @@ class Filesystem
* @param string $root value of the root
* @param bool $recursive recursive flag
* @param array $filterArray array to be filtered
*
* @return self
*/
public function init(string $root = null, bool $recursive = false, array $filterArray = [])
public function init(string $root = null, bool $recursive = false, array $filterArray = []) : self
{
$this->_root = $root;
$this->_recursive = $recursive;
if (is_array($filterArray))
{
$this->_filterArray = array_merge($this->_filterArray, $filterArray);
}
return $this;
}
/**
* copy the filesystem
*
* @since 4.0.0
*
* @return self
*/
public function copy() : self
{
return clone $this;
}
/**
* count the filesystem iterator
*
* @since 4.0.0
*
* @return int|null
*/
public function countIterator() : ?int
{
$iterator = $this->getIterator();
return $iterator ? iterator_count($iterator) : null;
}
/**
@@ -23,7 +23,12 @@ class Config extends Module\Notification
protected $_configArray =
[
'directory' => 'cache/pages',
'directory' =>
[
'scripts' => 'cache/scripts',
'styles' => 'cache/styles',
'pages' => 'cache/pages'
],
'extension' => 'phtml',
'lifetime' => 3600,
'tokenPlaceholder' => '%TOKEN%'
@@ -47,13 +47,13 @@ class PageCache extends Config
public function adminNotification() : ?array
{
if (!is_dir($this->_configArray['directory']) && !mkdir($this->_configArray['directory']))
if (!is_dir($this->_configArray['directory']['pages']) && !mkdir($this->_configArray['directory']['pages']))
{
$this->setNotification('error', $this->_language->get('directory_not_found') . $this->_language->get('colon') . ' ' . $this->_configArray['directory'] . $this->_language->get('point'));
$this->setNotification('error', $this->_language->get('directory_not_found') . $this->_language->get('colon') . ' ' . $this->_configArray['directory']['pages'] . $this->_language->get('point'));
}
else if (!chmod($this->_configArray['directory'], 0777))
else if (!chmod($this->_configArray['directory']['pages'], 0777))
{
$this->setNotification('error', $this->_language->get('directory_permission_grant') . $this->_language->get('colon') . ' ' . $this->_configArray['directory'] . $this->_language->get('point'));
$this->setNotification('error', $this->_language->get('directory_permission_grant') . $this->_language->get('colon') . ' ' . $this->_configArray['directory']['pages'] . $this->_language->get('point'));
}
return $this->getNotification();
}
@@ -68,17 +68,21 @@ public function adminNotification() : ?array
public function renderTemplate() : ?array
{
$fileSystem = new Filesystem\Filesystem();
$stylesFileSystem = $fileSystem->copy()->init($this->_configArray['directory']['styles']);
$scriptsFileSystem = $fileSystem->copy()->init($this->_configArray['directory']['scripts']);
/* prevent as needed */
if ($this->_request->getPost() || $this->_registry->get('noCache'))
if ($stylesFileSystem->countIterator() === 0 || $scriptsFileSystem->countIterator() === 0 || $this->_request->getPost() || $this->_registry->get('noCache'))
{
return null;
}
/* cache as needed */
$cacheFilesystem = new Filesystem\Cache();
$cacheFilesystem->init($this->_configArray['directory'], $this->_configArray['extension']);
$cacheFilesystem->init($this->_configArray['directory']['pages'], $this->_configArray['extension']);
$bundle = $this->_registry->get('root') . $this->_registry->get('fullRoute') . '/' . $this->_registry->get('template') . '/' . $this->_registry->get('language');
$token = $this->_registry->get('token');

0 comments on commit 0b38348

Please sign in to comment.
You can’t perform that action at this time.