* initial commit

This commit is contained in:
Danyi Dávid
2018-01-15 20:50:40 +01:00
commit b79cea9fb2
57 changed files with 5005 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Action;
use App\Service\CameraPictureManagerService;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Zend\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ServerRequestInterface;
class GetLatestCameraPicturesAction implements ServerMiddlewareInterface
{
/** @var CameraPictureManagerService */
private $cameraPictureManager;
public function __construct(CameraPictureManagerService $cameraPictureManager)
{
$this->cameraPictureManager = $cameraPictureManager;
}
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
return new JsonResponse($this->cameraPictureManager->getLatestCameraPictures());
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Action;
use App\Service\CameraPictureManagerService;
use Psr\Container\ContainerInterface;
class GetLatestCameraPicturesFactory
{
/**
* @param ContainerInterface $container
* @return GetLatestCameraPicturesAction
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container)
{
$cameraPictureManager = $container->get(CameraPictureManagerService::class);
return new GetLatestCameraPicturesAction($cameraPictureManager);
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace App\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Config\Config;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Expressive\Router;
use Zend\Expressive\Template;
class HomePageAction implements ServerMiddlewareInterface
{
/** @var Router\RouterInterface */
private $router;
/** @var Template\TemplateRendererInterface */
private $template;
/** @var Config */
private $config;
public function __construct(
Router\RouterInterface $router,
Template\TemplateRendererInterface $template,
Config $config
) {
$this->router = $router;
$this->template = $template;
$this->config = $config;
}
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$data = [
'changeDelay' => $this->config->get("changeDelay", 10000),
'refreshInterval' => $this->config->get("refreshInterval", 10000),
];
return new HtmlResponse($this->template->render('app::home-page', $data));
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Action;
use Psr\Container\ContainerInterface;
use Zend\Config\Config;
use Zend\Expressive\Router\RouterInterface;
use Zend\Expressive\Template\TemplateRendererInterface;
class HomePageFactory
{
/**
* @param ContainerInterface $container
* @return HomePageAction
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container)
{
$router = $container->get(RouterInterface::class);
$template = $container->get(TemplateRendererInterface::class);
$config = $container->get("config");
return new HomePageAction($router, $template, new Config($config['application']));
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Action;
use App\Service\CameraPictureManagerService;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
class ShowCameraImageAction implements ServerMiddlewareInterface
{
/** @var CameraPictureManagerService */
private $cameraPictureManager;
public function __construct(CameraPictureManagerService $cameraPictureManager)
{
$this->cameraPictureManager = $cameraPictureManager;
}
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$camera = $request->getAttribute('camera');
$image = $request->getAttribute('image');
return $this->cameraPictureManager->getCameraImage($camera, $image);
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Action;
use App\Service\CameraPictureManagerService;
use Psr\Container\ContainerInterface;
class ShowCameraImageFactory
{
/**
* @param ContainerInterface $container
* @return ShowCameraImageAction
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container)
{
$cameraPictureManager = $container->get(CameraPictureManagerService::class);
return new ShowCameraImageAction($cameraPictureManager);
}
}

View File

@@ -0,0 +1,62 @@
<?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' => [],
'factories' => [
Action\HomePageAction::class => Action\HomePageFactory::class,
Action\GetLatestCameraPicturesAction::class => Action\GetLatestCameraPicturesFactory::class,
Action\ShowCameraImageAction::class => Action\ShowCameraImageFactory::class,
Service\CameraPictureManagerService::class => Service\CameraPictureManagerServiceFactory::class,
],
];
}
/**
* Returns the templates configuration
*
* @return array
*/
public function getTemplates()
{
return [
'paths' => [
'app' => ['templates/app'],
'error' => ['templates/error'],
'layout' => ['templates/layout'],
],
];
}
}

View File

@@ -0,0 +1,108 @@
<?php
namespace App\Entity;
class CameraImage implements \JsonSerializable
{
/** @var string */
private $camera;
/** @var string */
private $imageName;
/** @var \DateTime */
private $createdAt;
/** @var string */
private $text;
/**
* @return string
*/
public function getCamera(): string
{
return $this->camera;
}
/**
* @param string $camera
* @return CameraImage
*/
public function setCamera(string $camera): CameraImage
{
$this->camera = $camera;
return $this;
}
/**
* @return string
*/
public function getImageName(): string
{
return $this->imageName;
}
/**
* @param string $imageName
* @return CameraImage
*/
public function setImageName(string $imageName): CameraImage
{
$this->imageName = $imageName;
return $this;
}
/**
* @return \DateTime
*/
public function getCreatedAt(): \DateTime
{
return $this->createdAt;
}
/**
* @param \DateTime $createdAt
* @return CameraImage
*/
public function setCreatedAt(\DateTime $createdAt): CameraImage
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return string
*/
public function getText(): ?string
{
return $this->text;
}
/**
* @param string $text
* @return CameraImage
*/
public function setText(string $text): CameraImage
{
$this->text = $text;
return $this;
}
/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize()
{
return [
'camera' => $this->getCamera(),
'imageName' => $this->getImageName(),
'createdAt' => $this->getCreatedAt()->format("Y-m-d H:i:s"),
'text' => $this->getText(),
];
}
}

View File

@@ -0,0 +1,89 @@
<?php
namespace App\Service;
use App\Entity\CameraImage;
use Zend\Config\Config;
use Zend\Diactoros\Response;
use Zend\Diactoros\Stream;
class CameraPictureManagerService
{
/** @var Config */
private $config;
public function __construct(Config $config)
{
$this->config = $config;
}
/**
* @return array
*/
public function getLatestCameraPictures()
{
$cameraImageBaseFolder = $this->config->get('cameraImageBaseFolder', false);
if (!is_dir($cameraImageBaseFolder)) {
throw new \InvalidArgumentException("Wrong cameraImageBaseFolder setting.");
}
$cameraImages = [];
$cameraImageFolders = array_diff(scandir($cameraImageBaseFolder), ['.', '..']);
foreach ($cameraImageFolders as $cameraImageFolder) {
$cameraImages[] = $this->getLatestImageInDirectory("{$cameraImageBaseFolder}/{$cameraImageFolder}");
}
return $cameraImages;
}
/**
* @param string $camera
* @param string $image
* @return Response
*/
public function getCameraImage(string $camera, string $image): Response
{
$cameraImageBaseFolder = $this->config->get('cameraImageBaseFolder');
$imageFile = "{$cameraImageBaseFolder}/{$camera}/{$image}";
if (!file_exists($imageFile)) {
throw new \InvalidArgumentException("");
}
$imageStream = new Stream(fopen($imageFile, 'r'));
$response = new Response();
return $response->withBody($imageStream)
->withHeader('Content-Type', (new \finfo(FILEINFO_MIME))->file($imageFile))
->withHeader('Content-Disposition', 'inline; filename=' . basename($imageFile))
->withHeader('Content-Transfer-Encoding', 'Binary')
->withHeader('Content-Description', 'File Transfer')
->withHeader('Pragma', 'public')
->withHeader('Content-Length', $imageStream->getSize());
}
/**
* @param string $directory
* @return CameraImage
*/
private function getLatestImageInDirectory(string $directory): CameraImage
{
$cameraImage = new CameraImage();
$cameraImage->setCamera(basename($directory));
$allImages = glob("{$directory}/*.{png,jpeg,jpg}", GLOB_NOSORT | GLOB_BRACE);
$latestImage = array_pop($allImages);
$cameraImage->setImageName(pathinfo($latestImage, PATHINFO_BASENAME))
->setCreatedAt(new \DateTime(sprintf("@%s", filemtime($latestImage))));
$textFile = sprintf("{$directory}/%s.txt", pathinfo($latestImage, PATHINFO_FILENAME));
if (file_exists($textFile)) {
$cameraImage->setText(file_get_contents($textFile));
} elseif (file_exists("{$directory}/overlay.txt")) {
$cameraImage->setText(file_get_contents("{$directory}/overlay.txt"));
}
return $cameraImage;
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Service;
use Psr\Container\ContainerInterface;
use Zend\Config\Config;
class CameraPictureManagerServiceFactory
{
/**
* @param ContainerInterface $container
* @return CameraPictureManagerService
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container)
{
$config = $container->get("config");
return new CameraPictureManagerService(new Config($config['application']));
}
}