View source
<?php
namespace Drupal\micon\Entity;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Component\Serialization\Json;
use Drupal\micon\MiconIcon;
class Micon extends ConfigEntityBase implements MiconInterface {
protected $id;
protected $label;
protected $type = 'font';
protected $info = [];
protected $icons = [];
protected $directory = 'public://micon';
public function setAsSvg() {
$this->type = 'image';
}
public function type() {
return $this->type;
}
public function getInfo() {
if (empty($this->info)) {
$this->info = [];
$path = $this
->getDirectory() . '/selection.json';
if (file_exists($path)) {
$data = file_get_contents($path);
$this->info = Json::decode($data);
}
}
return $this->info;
}
public function getName() {
$info = $this
->getInfo();
return isset($info['metadata']['name']) ? $info['metadata']['name'] : str_replace('-', '', $this
->getPrefix());
}
public function getPrefix() {
$info = $this
->getInfo();
return $info['preferences'][$this
->type() . 'Pref']['prefix'];
}
public function getIcons() {
if (empty($this->icons) && ($info = $this
->getInfo())) {
$this->icons = [];
foreach ($info['icons'] as $icon) {
$icon['name'] = $icon['properties']['name'];
$icon['tags'] = implode(",", $icon['icon']['tags']);
$icon['prefix'] = $this
->getPrefix();
$icon['directory'] = file_create_url($this
->getDirectory());
$icon['package_id'] = $this
->id();
$icon['package_label'] = $this
->label();
$micon_icon = new MiconIcon($this
->type(), $icon);
$this->icons[$micon_icon
->getSelector()] = $micon_icon;
}
}
return $this->icons;
}
public function setArchive($zip_path) {
$data = strtr(base64_encode(addslashes(gzcompress(serialize(file_get_contents($zip_path)), 9))), '+/=', '-_,');
$parts = str_split($data, 200000);
$this
->set('archive', $parts);
}
public function getArchive() {
$data = implode('', $this
->get('archive'));
return unserialize(gzuncompress(stripslashes(base64_decode(strtr($data, '-_,', '+/=')))));
}
public function getStylesheet() {
$path = $this
->getDirectory() . '/style.css';
return file_exists($path) ? $path : NULL;
}
protected function getDirectory() {
return $this->directory . '/' . $this
->id();
}
public static function loadActive() {
return Micon::loadMultiple(Micon::loadActiveIds());
}
public static function loadActiveIds() {
$query = \Drupal::entityQuery('micon')
->condition('status', 1);
return $query
->execute();
}
public static function loadActiveLabels() {
$labels = [];
foreach (Micon::loadActive() as $micon) {
$labels[$micon
->id()] = $micon
->label();
}
return $labels;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (!$this
->isNew()) {
$original = $storage
->loadUnchanged($this
->getOriginalId());
}
if (!$this
->get('archive')) {
throw new EntityMalformedException('IcoMoon icon package is required.');
}
if ($this
->isNew() || $original
->get('archive') !== $this
->get('archive')) {
$this
->archiveDecode();
}
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
parent::preDelete($storage, $entities);
foreach ($entities as $entity) {
\Drupal::service('file_system')
->deleteRecursive($entity
->getDirectory());
@rmdir($entity->directory);
}
}
protected function archiveDecode() {
$data = $this
->getArchive();
$zip_path = 'temporary://' . $this
->id() . '.zip';
file_put_contents($zip_path, $data);
$this
->archiveExtract($zip_path);
}
public function archiveExtract($zip_path) {
$file_system = \Drupal::service('file_system');
$archiver = \Drupal::service('plugin.manager.archiver')
->getInstance([
'filepath' => $file_system
->realpath($zip_path),
]);
if (!$archiver) {
throw new \Exception(t('Cannot extract %file, not a valid archive.', [
'%file' => $zip_path,
]));
}
$directory = $this
->getDirectory();
$file_system
->deleteRecursive($directory);
$file_system
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$archiver
->extract($directory);
$file_system
->deleteRecursive($directory . '/demo-files');
$file_system
->deleteRecursive($directory . '/demo.html');
$file_system
->delete($directory . '/Read Me.txt');
if (file_exists($directory . '/symbol-defs.svg')) {
$this
->setAsSvg();
$file_path = $directory . '/symbol-defs.svg';
$file_contents = file_get_contents($file_path);
$file_contents = str_replace($this
->getPrefix(), $this
->id() . '-', $file_contents);
file_put_contents($file_path, $file_contents);
}
else {
$font_directory = $directory . '/fonts';
$files_to_rename = $font_directory . '/*.*';
foreach (glob($file_system
->realpath($files_to_rename)) as $file_to_rename_path) {
$file_new_path = str_replace('fonts/' . $this
->getName(), 'fonts/' . $this
->id(), $file_to_rename_path);
if ($file_to_rename_path !== $file_new_path) {
$file_system
->move($file_to_rename_path, $file_new_path, FileSystemInterface::EXISTS_REPLACE);
}
}
}
$file_path = $directory . '/selection.json';
$file_contents = file_get_contents($file_path);
$file_contents = str_replace('"icons":', 'MICONSIcons', $file_contents);
$file_contents = str_replace('"icon":', 'MICONIcon', $file_contents);
$file_contents = str_replace('iconIdx', 'MICONIdx', $file_contents);
$file_contents = str_replace($this
->getPrefix(), 'MICONPrefix', $file_contents);
$file_contents = str_replace($this
->getName(), $this
->id(), $file_contents);
$file_contents = str_replace('MICONSIcons', '"icons":', $file_contents);
$file_contents = str_replace('MICONIcon', '"icon":', $file_contents);
$file_contents = str_replace('MICONIdx', 'iconIdx', $file_contents);
$file_contents = str_replace('MICONPrefix', $this
->id() . '-', $file_contents);
file_put_contents($file_path, $file_contents);
$file_path = $directory . '/style.css';
$file_contents = file_get_contents($file_path);
$file_contents = preg_replace('(\\?[a-zA-Z0-9#\\-\\_]*)', '', $file_contents);
$file_contents = str_replace($this
->getPrefix(), 'MICON', $file_contents);
$file_contents = str_replace($this
->getName(), $this
->id(), $file_contents);
$file_contents = str_replace('MICON', $this
->id() . '-', $file_contents);
file_put_contents($file_path, $file_contents);
}
}