diff options
author | Arron Woods <aw@chartblocks.com> | 2015-04-23 16:52:50 +0100 |
---|---|---|
committer | Arron Woods <aw@chartblocks.com> | 2015-04-23 16:52:58 +0100 |
commit | a12d873a6a8230dd2ef1255fedd5753068ce86ee (patch) | |
tree | 8b8cedba08b540689009861225b1108fc5b3460f | |
parent | 320d49f6af9fd66ab1ba72da22fd9fa37fcad0e4 (diff) | |
download | php-ssrs-a12d873a6a8230dd2ef1255fedd5753068ce86ee.zip php-ssrs-a12d873a6a8230dd2ef1255fedd5753068ce86ee.tar.gz php-ssrs-a12d873a6a8230dd2ef1255fedd5753068ce86ee.tar.bz2 |
Cache streams from report output
-rwxr-xr-x | library/SSRS/Object/RenderStream.php | 8 | ||||
-rwxr-xr-x | library/SSRS/Object/ReportOutput.php | 30 | ||||
-rw-r--r-- | library/SSRS/Report/CachedStreamResource.php | 43 |
3 files changed, 78 insertions, 3 deletions
diff --git a/library/SSRS/Object/RenderStream.php b/library/SSRS/Object/RenderStream.php index 58482b6..02cbb33 100755 --- a/library/SSRS/Object/RenderStream.php +++ b/library/SSRS/Object/RenderStream.php @@ -7,9 +7,11 @@ class RenderStream extends ObjectAbstract { public $Result; public $MimeType; - public function __construct(\stdClass $stream) { - $this->Result = $stream->Result; - $this->MimeType = $stream->MimeType; + public function __construct(\stdClass $stream = null) { + if ($stream) { + $this->Result = $stream->Result; + $this->MimeType = $stream->MimeType; + } } public function __toString() { diff --git a/library/SSRS/Object/ReportOutput.php b/library/SSRS/Object/ReportOutput.php index 0500525..ac17fe4 100755 --- a/library/SSRS/Object/ReportOutput.php +++ b/library/SSRS/Object/ReportOutput.php @@ -2,8 +2,26 @@ namespace SSRS\Object; +use SSRS\Report; +use SSRS\Report\CachedStreamResource; + class ReportOutput extends ObjectAbstract { + public function preCacheStreams(Report $report, $localCachePath, $format = 'HTML4.0') { + $this->verifyCachePath($localCachePath); + + $rootPath = rtrim($localCachePath, '/'); + foreach ($this->StreamIds->string as $streamId) { + $path = $rootPath . '/' . $streamId; + $stream = $report->renderStream($format, $streamId); + + $cachedResource = new CachedStreamResource($streamId, $path); + $cachedResource->store($stream); + } + + return $this; + } + public function download($filename) { header("Cache-control: max-age=3600, must-revalidate"); header("Pragma: public"); @@ -22,4 +40,16 @@ class ReportOutput extends ObjectAbstract { return (string) $this->Result; } + protected function verifyCachePath($path) { + if (false === file_exists($path) || false === is_dir($path)) { + throw new \RuntimeException('Stream cache path does not exist'); + } + + if (false === is_writable($path)) { + throw new \RuntimeException('Stream cache path is not writeable'); + } + + return $this; + } + } diff --git a/library/SSRS/Report/CachedStreamResource.php b/library/SSRS/Report/CachedStreamResource.php new file mode 100644 index 0000000..08e9dbb --- /dev/null +++ b/library/SSRS/Report/CachedStreamResource.php @@ -0,0 +1,43 @@ +<?php + +namespace SSRS\Report; + +use \SSRS\Object\RenderStream; + +class CachedStreamResource { + + public $streamId; + public $filePath; + + public function __construct($streamId, $filePath) { + $this->streamId = $streamId; + $this->filePath = $filePath; + } + + public function read() { + $data = file_get_contents($this->filePath); + $parts = explode("\n", $data, 2); + + $stream = new RenderStream(); + $stream->MimeType = $parts[0]; + $stream->Result = $parts[1]; + + return $stream; + } + + public function store(RenderStream $renderStream) { + $data = trim($renderStream->MimeType) . "\n"; + $data .= $renderStream->Result; + + file_put_contents($this->filePath, $data); + return $this; + } + + public function send() { + $stream = $this->read(); + + header('Content-Type: ' . $stream->MimeType); + echo $stream->Result; + } + +} |