diff --git a/src/MapCache/MapCache.php b/src/MapCache/MapCache.php index 2eddf97..2fed3cc 100644 --- a/src/MapCache/MapCache.php +++ b/src/MapCache/MapCache.php @@ -3,6 +3,8 @@ namespace StaticMapLite\MapCache; use StaticMapLite\Canvas\CanvasInterface; +use StaticMapLite\Output\CacheOutput; +use StaticMapLite\Output\OutputInterface; use StaticMapLite\Printer\PrinterInterface; class MapCache @@ -32,7 +34,6 @@ class MapCache public function checkMapCache(): bool { - return false; $this->mapCacheID = md5($this->serializeParams()); $filename = $this->getFilename(); @@ -73,21 +74,22 @@ class MapCache return $filename; } - public function cache(CanvasInterface $canvas) + public function cache(CanvasInterface $canvas): void { $this->mkdir_recursive(dirname($this->mapCacheIDToFilename()), 0777); imagepng($canvas->getImage(), $this->mapCacheIDToFilename(), 9); - $this->sendHeader(); - if (file_exists($this->mapCacheIDToFilename())) { - return file_get_contents($this->mapCacheIDToFilename()); - } else { - return imagepng($canvas->getImage()); - } + + $output = new CacheOutput(); + $output->setFilename($this->mapCacheIDToFilename()) + ->sendHeader() + ->sendImage() + ; } - public function mkdir_recursive($pathname, $mode) + public function mkdir_recursive($pathname, $mode): bool { is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode); + return is_dir($pathname) || @mkdir($pathname, $mode); } @@ -95,14 +97,4 @@ class MapCache { return $this->mapCacheIDToFilename(); } - - public function sendHeader() - { - header('Content-Type: image/png'); - $expires = 60 * 60 * 24 * 14; - header("Pragma: public"); - header("Cache-Control: maxage=" . $expires); - header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT'); - } - } diff --git a/src/Output/CacheOutput.php b/src/Output/CacheOutput.php new file mode 100644 index 0000000..05ae895 --- /dev/null +++ b/src/Output/CacheOutput.php @@ -0,0 +1,31 @@ +filename = $filename; + + return $this; + } + + public function sendHeader(): OutputInterface + { + header('Content-Type: image/png'); + $expires = 60 * 60 * 24 * 14; + header('Pragma: public'); + header('Cache-Control: maxage=' . $expires); + header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT'); + + return $this; + } + + public function sendImage(): void + { + echo file_get_contents($this->filename); + } +} diff --git a/src/Output/ImageOutput.php b/src/Output/ImageOutput.php new file mode 100644 index 0000000..3a86cbb --- /dev/null +++ b/src/Output/ImageOutput.php @@ -0,0 +1,31 @@ +image = $image; + + return $this; + } + + public function sendHeader(): OutputInterface + { + header('Content-Type: image/png'); + $expires = 60 * 60 * 24 * 14; + header('Pragma: public'); + header('Cache-Control: maxage=' . $expires); + header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT'); + + return $this; + } + + public function sendImage(): bool + { + return imagepng($this->image); + } +} diff --git a/src/Output/OutputInterface.php b/src/Output/OutputInterface.php new file mode 100644 index 0000000..1bbbca6 --- /dev/null +++ b/src/Output/OutputInterface.php @@ -0,0 +1,8 @@ +mapCache) { - // use map cache, so check cache for map if (!$this->mapCache->checkMapCache()) { - // map is not in cache, needs to be build $this->makeMap(); + $this->mapCache->cache($this->canvas); } else { - // map is in cache - $this->sendHeader(); - return file_get_contents($this->mapCache->getFilename()); + $output = new CacheOutput(); + $output + ->setFilename($this->mapCache->getFilename()) + ->sendHeader() + ->sendImage() + ; } - } else { // no cache, make map, send headers and deliver png $this->makeMap(); - $this->sendHeader(); - return imagepng($this->canvas->getImage()); + $output = new ImageOutput(); + $output + ->setImage($this->image) + ->sendHeader() + ->sendImage() + ; } } }