View source
<?php
namespace Drupal\minisite;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Database\Database;
use Drupal\minisite\Exception\AssetException;
use Drupal\minisite\Exception\InvalidExtensionValidatorException;
use Drupal\minisite\Exception\PageProcessorException;
class Asset implements AssetInterface {
protected $id;
protected $entityType;
protected $entityBundle;
protected $entityId;
protected $entityLanguage;
protected $mimeType;
protected $size;
protected $fieldName;
protected $urlBag;
public function __construct($entity_type, $entity_bundle, $entity_id, $entity_language, $field_name, $file_uri, $file_mime = NULL, $file_size = NULL) {
$this->entityType = $entity_type;
$this->entityBundle = $entity_bundle;
$this->entityId = $entity_id;
$this->entityLanguage = $entity_language;
$this->fieldName = $field_name;
$this->urlBag = new UrlBag($file_uri);
$this
->setMimeType($file_mime);
$this
->setSize($file_size);
}
public static function fromValues(array $values) {
$required_fields = [
'entity_type',
'entity_bundle',
'entity_id',
'entity_language',
'field_name',
'source',
];
if (count(array_diff_key(array_flip($required_fields), array_filter($values))) > 0) {
throw new AssetException('Unable to instantiate Asset instance from the provided values as required values are missing');
}
$values += [
'filemime' => NULL,
'filesize' => NULL,
];
$instance = new self($values['entity_type'], $values['entity_bundle'], $values['entity_id'], $values['entity_language'], $values['field_name'], $values['source'], $values['filemime'], $values['filesize']);
if (!empty($values['id'])) {
$instance
->setId($values['id']);
}
if (!empty($values['alias'])) {
$instance
->setAlias($values['alias']);
}
return $instance;
}
public static function load($id) {
$values = Database::getConnection()
->select('minisite_asset', 'ma')
->fields('ma')
->condition('id', $id)
->orderBy('id', 'DESC')
->execute()
->fetchAssoc();
if (empty($values)) {
return NULL;
}
return self::fromValues($values);
}
public static function loadByUri($uri) {
if (!LegacyWrapper::isValidUri($uri)) {
return NULL;
}
$values = Database::getConnection()
->select('minisite_asset', 'ma')
->fields('ma')
->condition('source', $uri)
->orderBy('id', 'DESC')
->execute()
->fetchAssoc();
if (empty($values)) {
return NULL;
}
return self::fromValues($values);
}
public static function loadByAlias($alias) {
if (!UrlHelper::isValid($alias)) {
return NULL;
}
$values = Database::getConnection()
->select('minisite_asset', 'ma')
->fields('ma')
->condition('alias', $alias)
->orderBy('id', 'DESC')
->execute()
->fetchAssoc();
if (empty($values)) {
return NULL;
}
return self::fromValues($values);
}
public static function loadAll() {
$values = Database::getConnection()
->select('minisite_asset', 'ma')
->fields('ma')
->orderBy('id', 'DESC')
->execute()
->fetchAllAssoc('id', \PDO::FETCH_ASSOC);
$assets = [];
foreach ($values as $value) {
$assets[] = self::fromValues($value);
}
return $assets;
}
public function save() {
$fields = [
'entity_type' => $this->entityType,
'entity_bundle' => $this->entityBundle,
'entity_id' => $this->entityId,
'entity_language' => $this->entityLanguage,
'field_name' => $this->fieldName,
'source' => $this
->getUri(),
'alias' => !empty($this
->getAlias()) ? $this
->getAlias() : '',
'filemime' => $this
->getMimeType(),
'filesize' => $this
->getSize(),
];
if (!empty($this->id)) {
$fields['id'] = $this->id;
}
if (empty($fields['id'])) {
$id = Database::getConnection()
->insert('minisite_asset')
->fields($fields)
->execute();
}
else {
$id = Database::getConnection()
->update('minisite_asset')
->fields($fields)
->condition('id', $fields['id'])
->execute();
}
$this
->setId($id);
return $id;
}
public function delete() {
if (isset($this->id)) {
Database::getConnection()
->delete('minisite_asset')
->condition('id', $this
->id())
->execute();
$this->id = NULL;
}
$fs = \Drupal::service('file_system');
$fs
->deleteRecursive($this->urlBag
->getUri());
$dirname = $this->urlBag
->getUri();
while (($dirname = $fs
->dirname($dirname)) && $dirname != Minisite::getCommonAssetDir()) {
if (empty(LegacyWrapper::scanDirectory($dirname, '/.*/'))) {
$fs
->deleteRecursive($dirname);
}
}
}
public function render() {
$file_uri = $this->urlBag
->getUri();
if (!is_readable($file_uri)) {
throw new AssetException(sprintf('Unable to render file "%s" as it does not exist', $file_uri));
}
$content = file_get_contents($file_uri);
if ($this
->isDocument()) {
try {
$processor = new PageProcessor($content, $this->urlBag);
$processor
->process();
$content = $processor
->content();
} catch (PageProcessorException $exception) {
}
}
return $content;
}
public function id() {
return isset($this->id) ? $this->id : NULL;
}
public function setId($id) {
$this->id = $id;
}
public function getUri() {
return $this->urlBag
->getUri();
}
public function getUrl() {
$alias = $this
->getAlias();
return !empty($alias) ? $alias : $this->urlBag
->getUrl();
}
public function getAlias() {
return $this->urlBag
->getAlias();
}
public function setAlias($alias) {
$this->urlBag
->setAlias($alias);
}
public function setAliasPrefix($parent_alias) {
$this->urlBag
->setParentAlias($parent_alias);
}
public function getLanguage() {
return $this->entityLanguage;
}
public function getMimeType() {
return $this->mimeType;
}
public function setMimeType($mime_type) {
if (!empty($mime_type)) {
$this->mimeType = $mime_type;
}
else {
$this->mimeType = $this
->guessMimeType($this
->getUri());
}
}
public function getSize() {
return $this->size;
}
public function setSize($size) {
if (!empty($size)) {
$this->size = $size;
}
else {
$size = $this
->calcFileSize($this
->getUri());
if ($size) {
$this->size = $size;
}
}
}
public function isIndex() {
if (!UrlValidator::urlIsIndex($this->urlBag
->getUri(), self::INDEX_FILE)) {
return FALSE;
}
$path = $this->urlBag
->getPathInArchive();
$in_root = strpos($path, DIRECTORY_SEPARATOR) == FALSE;
return $in_root;
}
public function getHeaders() {
$headers = [];
if ($this
->isDocument()) {
$headers['Content-Language'] = $this
->getLanguage();
$headers['Content-Type'] = $this
->getMimeType() . '; charset=UTF-8';
}
else {
$type = Unicode::mimeHeaderEncode($this
->getMimeType());
$headers['Content-Type'] = $type;
$headers['Accept-Ranges'] = 'bytes';
$size = $this
->getSize();
if ($size) {
$headers['Content-Length'] = $size;
}
}
return $headers;
}
public function isDocument() {
try {
FileValidator::validateFileExtension($this->urlBag
->getUri(), [
'html',
'htm',
]);
return TRUE;
} catch (InvalidExtensionValidatorException $exception) {
}
return FALSE;
}
public function getCacheMaxAge() {
return 2628000;
}
public function guessMimeType($uri) {
return \Drupal::service('file.mime_type.guesser')
->guess($uri);
}
public function calcFileSize($uri) {
if (is_readable($uri)) {
return @filesize($uri);
}
return NULL;
}
}