* doctrine db cache backend added

This commit is contained in:
Danyi Dávid
2017-08-12 22:56:25 +02:00
parent 36fa6c3297
commit 771db00c4f
22 changed files with 2368 additions and 205 deletions

View File

@@ -4,6 +4,8 @@
namespace App\Service;
use App\Entity\Image;
use Doctrine\ORM\EntityManager;
use Imagine\Gd\Imagine;
use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
@@ -18,19 +20,29 @@ class GalleryService
const THUMBNAIL_SIZE = 500;
protected $contentTypeMap = [
'jpg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'png' => 'image/png',
];
protected $config;
/**
* @var EntityManager
*/
protected $em;
public function __construct(EntityManager $entityManager)
{
$this->em = $entityManager;
}
public function loadGalleryData($includeHidden = false)
{
$config = $this->getConfig();
$slugs = [];
$albums = [];
foreach ($config['galleries'] as $id => $data) {
if ($includeHidden || $data['public']) {
$slugs[] = [
$albums[] = [
'slug' => $id,
'name' => $data['name'],
'coverImage' => isset($data['cover'])
@@ -44,7 +56,7 @@ class GalleryService
];
}
}
return $slugs;
return $albums;
}
private function getGalleryDate(string $dir): string
@@ -56,73 +68,53 @@ class GalleryService
/**
* @param string $dir
* @return array
* @return Image[]
* @todo implement label for image in some way
*/
private function loadGalleryImages(string $dir)
private function loadGalleryImages(string $dir): array
{
$images = array_map('basename', glob(
sprintf(self::IMAGE_BASEDIR . "*.{jpg,jpeg,png}", $dir),
GLOB_BRACE
));
return array_map(function ($image) {
return [
'label' => '',
$imagine = new Imagine();
return array_map(function ($image) use ($dir, $imagine) {
$imageEntity = $this->em->getRepository(Image::class)->findOneBy([
'dir' => $dir,
'path' => $image,
];
]);
if (null === $imageEntity) {
$imageEntity = new Image();
$imageEntity->setLabel("")
->setDir($dir)
->setPath($image);
$this->processImageSizes($dir, $imageEntity, $imagine);
$this->em->persist($imageEntity);
$this->em->flush();
}
return $imageEntity;
}, $images);
}
private function processImageSizes(string $dir, Image $image, Imagine $imagine)
{
$img = $imagine->open(sprintf(self::IMAGE_BASEDIR, $dir) . $image->getPath());
$imgSize = $img->getSize();
$image->setWidth($imgSize->getWidth())
->setHeight($imgSize->getHeight());
}
/**
* @param string $image
* @return array
* @return Image
*/
private function getCoverImage(string $image)
private function getCoverImage(string $image): Image
{
return [
'label' => '',
'path' => $image,
];
}
public function listGalleries($includeHidden = false)
{
$config = $this->getConfig();
$slugs = [];
foreach ($config['galleries'] as $id => $data) {
if ($includeHidden || $data['public']) {
$slugs[] = [
'id' => $id,
'label' => $data['name'],
'new' => $data['new'],
];
}
}
return $slugs;
}
public function getGallery(string $slug)
{
$galleryList = $this->listGalleries(true);
$slugs = array_map(function ($item) {
return $item['id'];
}, $galleryList);
$config = $this->getConfig();
$images = [];
if (in_array($slug, $slugs)) {
$dir = $config['galleries'][$slug]['dir'];
$images = array_map('basename', glob(
sprintf(self::IMAGE_BASEDIR . "*.{jpg,jpeg,png}", $dir),
GLOB_BRACE
));
}
return [
'name' => $config['galleries'][$slug]['name'],
'type' => $config['galleries'][$slug]['type'],
'images' => $images,
];
$img = new Image();
return $img->setLabel("")
->setPath($image)
->setWidth(0)
->setHeight(0);
}
public function getImage(string $slug, string $image, $size = false): string