--- /dev/null
+<?php
+
+namespace App\Fluidbook\Compiler;
+
+trait Cache
+{
+ protected $_compiledCacheDate = null;
+ protected $_compositionCached = null;
+ protected $_compositionTimestamp = null;
+
+
+ /**
+ * @return bool
+ * @throws \Exception
+ */
+ public function compositionCached()
+ {
+ if (null === $this->_compositionCached) {
+ $compiled = $this->getCompiledCacheDate();
+ if ($compiled === 0) {
+ $this->_compositionCached = false;
+ } else {
+
+ $this->_compositionCached = $this->getCompositionTimestamp() <= $compiled;
+ }
+ }
+ return $this->_compositionCached;
+ }
+
+ public function getCompositionTimestamp()
+ {
+ if (null === $this->_compositionTimestamp) {
+ $this->_compositionTimestamp = (new \DateTime($this->getFluidbook()->composition_updated_at))->getTimestamp();
+ }
+ return $this->_compositionTimestamp;
+ }
+
+ public function setCompiledCacheDate()
+ {
+ \Illuminate\Support\Facades\Cache::set('compiler_cached_' . $this->getFluidbook()->id, time(), 7200);
+ }
+
+ protected function getCompiledCacheDate()
+ {
+ if (null === $this->_compiledCacheDate) {
+ $this->_compiledCacheDate = \Illuminate\Support\Facades\Cache::get('compiler_cached_' . $this->getFluidbook()->id, 0);
+ }
+ return $this->_compiledCacheDate;
+ }
+
+ /**
+ * @param $key
+ * @return bool
+ * @throws \Exception
+ */
+ public function isCached($key)
+ {
+ if (!$this->compositionCached()) {
+ return false;
+ }
+ return \Illuminate\Support\Facades\Cache::has($this->_cacheKey($key));
+ }
+
+ public function getCacheValue($key)
+ {
+ return \Illuminate\Support\Facades\Cache::get($this->_cacheKey($key));
+ }
+
+ public function setCacheValue($key, $value)
+ {
+ return \Illuminate\Support\Facades\Cache::set($this->_cacheKey($key), $value, 86400);
+ }
+
+ protected function _cacheKey($key)
+ {
+ return 'compiler_cached_' . $this->getFluidbook()->id . '_' . $this->getCompositionTimestamp() . '_' . $key;
+ }
+
+
+}
use Images;
use Sound;
use Articles;
+ use Cache;
protected static $uaPrefixes = array('-moz-', '-webkit-', '-o-', '-ms-', '');
$this->logtime = $currenttime;
}
$time = $currenttime - $this->logtime;
- $log = $step . ' | ' . round($time, 3) . 's' . "\n";
+ $log = $step . ' | ' . round($time, 4) . 's' . "\n";
fwrite($this->logfp, $log);
fflush($this->logfp);
$this->logtime = $currenttime;
public function handle()
{
- $this->log('Preprocess images');
- (new FluidbookImagesPreprocess($this->book_id))->handle();
+ if (!$this->compositionCached()) {
+ $this->log('Preprocess images');
+ (new FluidbookImagesPreprocess($this->book_id))->handle();
+ }
$this->log('Start compile process');
// Raw copy of some directories
$this->log('Files Synced');
// $f=rtrim(str_replace('/html5/', '/compiletime/', $this->dir));
// touch($f);
+
+ $this->setCompiledCacheDate();
}
protected function writeSlider()
protected function writeImages()
{
+ $compositionCached = $this->compositionCached();
+
+
set_time_limit(0);
switch ($this->fluidbookSettings->mobileVersion) {
case 'html5-desktop':
break;
}
- $rasterizePages = $this->config->rasterizePages;
- $this->config->pagesDimensions = [];
- if ($this->fluidbookSettings->mobileNavigationType === 'mobilefirst') {
- $imdir = 'mf';
- } else {
- $imdir = 'html';
- }
+ if (!$this->isCached('imagesToCopy')) {
+ $filesToCopy = [];
+
+ $rasterizePages = $this->config->rasterizePages;
+
+ if ($this->fluidbookSettings->mobileNavigationType === 'mobilefirst') {
+ $imdir = 'mf';
+ } else {
+ $imdir = 'html';
+ }
+
+ $thumbs = array();
+ foreach ($this->pages as $page => $infos) {
+ $thisrasterize = in_array($page, $rasterizePages);
+ $thisimagesvg = !$thisrasterize && $this->svg;
+ $thisbackgroundPrefix = $thisrasterize ? [true] : $this->backgroundsPrefix;
+
+ foreach ($this->getResolutions() as $r) {
+ foreach ($thisbackgroundPrefix as $backgroundsPrefix) {
+ $source = $this->getFluidbook()->getFile($page, $this->imageFormat, $r, $backgroundsPrefix, true, $imdir);
- $thumbs = array();
- foreach ($this->pages as $page => $infos) {
- $thisrasterize = in_array($page, $rasterizePages);
- $thisimagesvg = !$thisrasterize && $this->svg;
- $thisbackgroundPrefix = $thisrasterize ? [true] : $this->backgroundsPrefix;
-
- foreach ($this->getResolutions() as $r) {
- foreach ($thisbackgroundPrefix as $backgroundsPrefix) {
- $source = $this->getFluidbook()->getFile($page, $this->imageFormat, $r, $backgroundsPrefix, true, $imdir);
- if ($r === $this->maxRes) {
- $this->getPageDimension($page);
+ $filesToCopy[$source] = 'data/background/' . $r . '/' . ($backgroundsPrefix ? 't' : 'p') . $page . '.' . $this->imageFormat;
}
- $this->vdir->copy($source, 'data/background/' . $r . '/' . ($backgroundsPrefix ? 't' : 'p') . $page . '.' . $this->imageFormat);
}
- }
- if ($thisimagesvg) {
- $this->vdir->copy(
- $this->getFluidbook()->getFile($page, 'svg', 150, true,
- in_array($page, $this->config->vectorPages), 'html')
- , 'data/contents/p' . $page . '.svg');
+ if ($thisimagesvg) {
+ $source = $this->getFluidbook()->getFile($page, 'svg', 150, true,
+ in_array($page, $this->config->vectorPages), 'html');
+ $filesToCopy[$source] = 'data/contents/p' . $page . '.svg';
+ }
+
+ $t = $this->getFluidbook()->getThumbFile($page, $this->imageFormat);
+ $filesToCopy[$t] = 'data/thumbnails/p' . $page . '.' . $this->imageFormat;
+ $this->log('Made image page ' . $page);
}
- $t = $this->getFluidbook()->getThumbFile($page, $this->imageFormat);
- $this->vdir->copy($t, 'data/thumbnails/p' . $page . '.' . $this->imageFormat);
- $this->log('Made image page ' . $page);
- }
+ $this->_makeCover($this->getFluidbook()->getFile(1, 'jpg', 150, true, true));
+ $this->log('Made cover for apps');
+ $this->setCacheValue('imagesToCopy', $filesToCopy);
+ } else {
+ $filesToCopy = $this->getCacheValue('imagesToCopy');
+ }
- $this->_makeCover($this->getFluidbook()->getFile(1, 'jpg', 150, true, true));
- $this->log('Made cover for apps');
+ foreach ($filesToCopy as $source => $dest) {
+ $this->vdir->copy($source, $dest, false, false);
+ }
$this->log('Made images');
+
+ $this->getPageDimensions();
+ }
+
+ protected function getPageDimensions()
+ {
+ if (!$this->isCached('pagesDimensions')) {
+ $this->config->pagesDimensions = [];
+ foreach ($this->pages as $page => $infos) {
+ $this->getPageDimension($page);
+ }
+ $this->setCacheValue('pagesDimensions', $this->config->pagesDimensions);
+ } else {
+ $this->config->pagesDimensions = $this->getCacheValue('pagesDimensions');
+ }
+ $this->log('Got page dimensions');
}
protected function _makeCover($orig)