Initial commit
This commit is contained in:
27
src/App/Action/GetGalleryImagesAction.php
Normal file
27
src/App/Action/GetGalleryImagesAction.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use App\Service\GalleryService;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class GetGalleryImagesAction implements ServerMiddlewareInterface
|
||||
{
|
||||
private $galleryService;
|
||||
|
||||
public function __construct(GalleryService $galleryService)
|
||||
{
|
||||
$this->galleryService = $galleryService;
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
|
||||
{
|
||||
$slug = $request->getAttribute('slug', false);
|
||||
return new JsonResponse([
|
||||
'images' => $this->galleryService->getGallery($slug),
|
||||
]);
|
||||
}
|
||||
}
|
||||
15
src/App/Action/GetGalleryImagesFactory.php
Normal file
15
src/App/Action/GetGalleryImagesFactory.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use App\Service\GalleryService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class GetGalleryImagesFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$galleryService = $container->get(GalleryService::class);
|
||||
return new GetGalleryImagesAction($galleryService);
|
||||
}
|
||||
}
|
||||
27
src/App/Action/GetImageAction.php
Normal file
27
src/App/Action/GetImageAction.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use App\Service\GalleryService;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class GetImageAction implements ServerMiddlewareInterface
|
||||
{
|
||||
private $galleryService;
|
||||
|
||||
public function __construct(GalleryService $galleryService)
|
||||
{
|
||||
$this->galleryService = $galleryService;
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
|
||||
{
|
||||
$slug = $request->getAttribute('slug', false);
|
||||
return new JsonResponse([
|
||||
'images' => $this->galleryService->getGallery($slug),
|
||||
]);
|
||||
}
|
||||
}
|
||||
15
src/App/Action/GetImageFactory.php
Normal file
15
src/App/Action/GetImageFactory.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use App\Service\GalleryService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class GetImageFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$galleryService = $container->get(GalleryService::class);
|
||||
return new GetImageAction($galleryService);
|
||||
}
|
||||
}
|
||||
63
src/App/Action/HomePageAction.php
Normal file
63
src/App/Action/HomePageAction.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\HtmlResponse;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Zend\Expressive\Router;
|
||||
use Zend\Expressive\Template;
|
||||
use Zend\Expressive\Plates\PlatesRenderer;
|
||||
use Zend\Expressive\Twig\TwigRenderer;
|
||||
use Zend\Expressive\ZendView\ZendViewRenderer;
|
||||
|
||||
class HomePageAction implements ServerMiddlewareInterface
|
||||
{
|
||||
private $router;
|
||||
|
||||
private $template;
|
||||
|
||||
public function __construct(Router\RouterInterface $router, Template\TemplateRendererInterface $template = null)
|
||||
{
|
||||
$this->router = $router;
|
||||
$this->template = $template;
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
|
||||
{
|
||||
if (! $this->template) {
|
||||
return new JsonResponse([
|
||||
'welcome' => 'Congratulations! You have installed the zend-expressive skeleton application.',
|
||||
'docsUrl' => 'https://docs.zendframework.com/zend-expressive/',
|
||||
]);
|
||||
}
|
||||
|
||||
$data = [];
|
||||
|
||||
if ($this->router instanceof Router\AuraRouter) {
|
||||
$data['routerName'] = 'Aura.Router';
|
||||
$data['routerDocs'] = 'http://auraphp.com/packages/2.x/Router.html';
|
||||
} elseif ($this->router instanceof Router\FastRouteRouter) {
|
||||
$data['routerName'] = 'FastRoute';
|
||||
$data['routerDocs'] = 'https://github.com/nikic/FastRoute';
|
||||
} elseif ($this->router instanceof Router\ZendRouter) {
|
||||
$data['routerName'] = 'Zend Router';
|
||||
$data['routerDocs'] = 'https://docs.zendframework.com/zend-router/';
|
||||
}
|
||||
|
||||
if ($this->template instanceof PlatesRenderer) {
|
||||
$data['templateName'] = 'Plates';
|
||||
$data['templateDocs'] = 'http://platesphp.com/';
|
||||
} elseif ($this->template instanceof TwigRenderer) {
|
||||
$data['templateName'] = 'Twig';
|
||||
$data['templateDocs'] = 'http://twig.sensiolabs.org/documentation';
|
||||
} elseif ($this->template instanceof ZendViewRenderer) {
|
||||
$data['templateName'] = 'Zend View';
|
||||
$data['templateDocs'] = 'https://docs.zendframework.com/zend-view/';
|
||||
}
|
||||
|
||||
return new HtmlResponse($this->template->render('app::home-page', $data));
|
||||
}
|
||||
}
|
||||
20
src/App/Action/HomePageFactory.php
Normal file
20
src/App/Action/HomePageFactory.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Zend\Expressive\Router\RouterInterface;
|
||||
use Zend\Expressive\Template\TemplateRendererInterface;
|
||||
|
||||
class HomePageFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$router = $container->get(RouterInterface::class);
|
||||
$template = $container->has(TemplateRendererInterface::class)
|
||||
? $container->get(TemplateRendererInterface::class)
|
||||
: null;
|
||||
|
||||
return new HomePageAction($router, $template);
|
||||
}
|
||||
}
|
||||
26
src/App/Action/ListGalleriesAction.php
Normal file
26
src/App/Action/ListGalleriesAction.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use App\Service\GalleryService;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class ListGalleriesAction implements ServerMiddlewareInterface
|
||||
{
|
||||
private $galleryService;
|
||||
|
||||
public function __construct(GalleryService $galleryService)
|
||||
{
|
||||
$this->galleryService = $galleryService;
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
|
||||
{
|
||||
return new JsonResponse([
|
||||
'galleries' => $this->galleryService->listGalleries()
|
||||
]);
|
||||
}
|
||||
}
|
||||
15
src/App/Action/ListGalleriesFactory.php
Normal file
15
src/App/Action/ListGalleriesFactory.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use App\Service\GalleryService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class ListGalleriesFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$galleryService = $container->get(GalleryService::class);
|
||||
return new ListGalleriesAction($galleryService);
|
||||
}
|
||||
}
|
||||
16
src/App/Action/PingAction.php
Normal file
16
src/App/Action/PingAction.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class PingAction implements ServerMiddlewareInterface
|
||||
{
|
||||
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
|
||||
{
|
||||
return new JsonResponse(['ack' => time()]);
|
||||
}
|
||||
}
|
||||
63
src/App/ConfigProvider.php
Normal file
63
src/App/ConfigProvider.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
/**
|
||||
* The configuration provider for the App module
|
||||
*
|
||||
* @see https://docs.zendframework.com/zend-component-installer/
|
||||
*/
|
||||
class ConfigProvider
|
||||
{
|
||||
/**
|
||||
* Returns the configuration array
|
||||
*
|
||||
* To add a bit of a structure, each section is defined in a separate
|
||||
* method which returns an array with its configuration.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function __invoke()
|
||||
{
|
||||
return [
|
||||
'dependencies' => $this->getDependencies(),
|
||||
'templates' => $this->getTemplates(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the container dependencies
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDependencies()
|
||||
{
|
||||
return [
|
||||
'invokables' => [
|
||||
Action\PingAction::class => Action\PingAction::class,
|
||||
],
|
||||
'factories' => [
|
||||
Action\HomePageAction::class => Action\HomePageFactory::class,
|
||||
Action\ListGalleriesAction::class => Action\ListGalleriesFactory::class,
|
||||
Action\GetGalleryImagesAction::class => Action\GetGalleryImagesFactory::class,
|
||||
Action\GetImageAction::class => Action\GetImageFactory::class,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the templates configuration
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplates()
|
||||
{
|
||||
return [
|
||||
'paths' => [
|
||||
'app' => ['templates/app'],
|
||||
'error' => ['templates/error'],
|
||||
'layout' => ['templates/layout'],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
104
src/App/Service/GalleryService.php
Normal file
104
src/App/Service/GalleryService.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
|
||||
use Imagine\Gd\Imagine;
|
||||
use Imagine\Image\Box;
|
||||
use Imagine\Image\ImageInterface;
|
||||
use Symfony\Component\Yaml\Parser;
|
||||
use Zend\Config\Reader\Yaml;
|
||||
|
||||
class GalleryService
|
||||
{
|
||||
|
||||
const CONFIG_FILE = 'data/galleries/gallery.yaml';
|
||||
const IMAGE_BASEDIR = 'data/galleries/%s/';
|
||||
const THUMBNAIL_SIZE = 250;
|
||||
|
||||
protected $contentTypeMap = [
|
||||
'jpg' => 'image/jpeg',
|
||||
'jpeg' => 'image/jpeg',
|
||||
'png' => 'image/png',
|
||||
];
|
||||
protected $config;
|
||||
|
||||
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,
|
||||
];
|
||||
}
|
||||
|
||||
public function getImage(string $slug, string $image, bool $size)
|
||||
{
|
||||
$config = $this->getConfig();
|
||||
$galleryDir = sprintf(self::IMAGE_BASEDIR, $config['galleries'][$slug]['dir']);
|
||||
$imageFileName = $size
|
||||
? $galleryDir . $image
|
||||
: $this->getResizedImageName($galleryDir, $image, $size);
|
||||
|
||||
return fopen($imageFileName, 'r');
|
||||
}
|
||||
|
||||
protected function getResizedImageName($galleryDirectory, $imageName, $size)
|
||||
{
|
||||
$numericSize = $size == 'thumb' ? self::THUMBNAIL_SIZE : $size;
|
||||
|
||||
$thumbPath = $galleryDirectory . "thumb_" . $numericSize;
|
||||
@mkdir($thumbPath);
|
||||
$thumbImageName = $thumbPath . "/" . $imageName;
|
||||
|
||||
if (!file_exists($thumbImageName)) {
|
||||
$thumbSize = new Box($numericSize, $numericSize);
|
||||
$imagine = new Imagine();
|
||||
$image = $imagine->open($galleryDirectory . $imageName)
|
||||
->thumbnail($thumbSize, ImageInterface::THUMBNAIL_OUTBOUND);
|
||||
$image->effects()->sharpen();
|
||||
$image->save($thumbImageName);
|
||||
}
|
||||
|
||||
return $thumbImageName;
|
||||
}
|
||||
|
||||
protected function getConfig()
|
||||
{
|
||||
if (null === $this->config) {
|
||||
$parser = new Parser();
|
||||
$configReader = new Yaml([$parser, 'parse']);
|
||||
$this->config = $configReader->fromFile(self::CONFIG_FILE);
|
||||
}
|
||||
return $this->config;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user