2015-11-26 03:19:43 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Assets\Flysystem;
|
2015-11-26 03:19:43 +01:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use League\Flysystem\Filesystem;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple Flysystem implementation of GeneratedAssetHandler for storing generated content
|
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
class GeneratedAssetHandler implements \SilverStripe\Assets\Storage\GeneratedAssetHandler {
|
2015-11-26 03:19:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flysystem store for files
|
|
|
|
*
|
|
|
|
* @var Filesystem
|
|
|
|
*/
|
|
|
|
protected $assetStore = null;
|
|
|
|
|
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* Assign the asset backend. This must be a filesystem
|
|
|
|
* with an adapter of type {@see PublicAdapter}.
|
2015-11-26 03:19:43 +01:00
|
|
|
*
|
|
|
|
* @param Filesystem $store
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setFilesystem(Filesystem $store) {
|
|
|
|
$this->assetStore = $store;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the asset backend
|
|
|
|
*
|
|
|
|
* @return Filesystem
|
2016-08-19 00:51:35 +02:00
|
|
|
* @throws Exception
|
2015-11-26 03:19:43 +01:00
|
|
|
*/
|
|
|
|
public function getFilesystem() {
|
2016-03-01 06:30:52 +01:00
|
|
|
if(!$this->assetStore) {
|
|
|
|
throw new Exception("Filesystem misconfiguration error");
|
|
|
|
}
|
2015-11-26 03:19:43 +01:00
|
|
|
return $this->assetStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getContentURL($filename, $callback = null) {
|
|
|
|
$result = $this->checkOrCreate($filename, $callback);
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$result) {
|
|
|
|
return null;
|
2015-11-26 03:19:43 +01:00
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var PublicAdapter $adapter */
|
|
|
|
$adapter = $this
|
|
|
|
->getFilesystem()
|
|
|
|
->getAdapter();
|
|
|
|
return $adapter->getPublicUrl($filename);
|
2015-11-26 03:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getContent($filename, $callback = null) {
|
|
|
|
$result = $this->checkOrCreate($filename, $callback);
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$result) {
|
|
|
|
return null;
|
2015-11-26 03:19:43 +01:00
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return $this
|
|
|
|
->getFilesystem()
|
|
|
|
->read($filename);
|
2015-11-26 03:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the file exists or that the $callback provided was able to regenerate it.
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @param callable $callback
|
|
|
|
* @return bool Whether or not the file exists
|
|
|
|
* @throws Exception If an error has occurred during save
|
|
|
|
*/
|
2015-11-30 03:03:46 +01:00
|
|
|
protected function checkOrCreate($filename, $callback = null) {
|
2015-11-26 03:19:43 +01:00
|
|
|
// Check if there is an existing asset
|
|
|
|
if ($this->getFilesystem()->has($filename)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$callback) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke regeneration and save
|
|
|
|
$content = call_user_func($callback);
|
|
|
|
$this->setContent($filename, $content);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setContent($filename, $content) {
|
|
|
|
// Store content
|
|
|
|
$result = $this
|
|
|
|
->getFilesystem()
|
|
|
|
->put($filename, $content);
|
|
|
|
|
|
|
|
if(!$result) {
|
|
|
|
throw new Exception("Error regenerating file \"{$filename}\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeContent($filename) {
|
|
|
|
if($this->getFilesystem()->has($filename)) {
|
|
|
|
$handler = $this->getFilesystem()->get($filename);
|
|
|
|
$handler->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|