yamaha-av/web/index.php

94 lines
3.0 KiB
PHP

<?php declare(strict_types=1);
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
require __DIR__.'/../vendor/autoload.php';
$env = 'dev';
$app = new Application();
$redis = new Redis();
$redis->connect('localhost');
$app['debug'] = 'dev' === $env;
$app
->register(new \Silex\Provider\HttpFragmentServiceProvider())
->register(new Silex\Provider\SerializerServiceProvider())
->register(new Silex\Provider\ServiceControllerServiceProvider())
->register(new \Silex\Provider\VarDumperServiceProvider(), [
'var_dumper.dump_destination' => fopen('php://stdout', 'w'),
])
->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array(
'driver' => 'pdo_sqlite',
'path' => __DIR__.'/../config/app.db',
'charset' => 'utf8mb4',
'collate' => 'utf8mb4_unicode_ci',
),
))
->register(new \Silex\Provider\MonologServiceProvider(), [
'monolog.logfile' => __DIR__.'/../logs/app.log',
'monolog.level' => 'dev' === $env ? \Monolog\Logger::DEBUG : \Monolog\Logger::INFO,
])
->register(new Moust\Silex\Provider\CacheServiceProvider(), array(
'cache.options' => array(
'driver' => 'redis',
'redis' => $redis,
)
))
;
$app->extend('monolog', function(Monolog\Logger $monolog, $app) use($redis) {
$monolog->pushHandler(new Monolog\Handler\RotatingFileHandler(__DIR__.'/../logs/app.log',20,Monolog\Logger::DEBUG));
$monolog->pushHandler(new \Monolog\Handler\SyslogHandler('app'));
$monolog->pushHandler(new \Monolog\Handler\RedisHandler($redis, 'app'));
return $monolog;
});
\Doctrine\DBAL\Types\Type::addType('uuid', \Ramsey\Uuid\Doctrine\UuidType::class);
/** @var \Doctrine\DBAL\Connection $db */
$db = $app['db'];
if('dev' !== $env) {
$app->register(new \Silex\Provider\HttpCacheServiceProvider(),[
'http_cache.cache_dir' => __DIR__.'/../cache/http/',
'http_cache.esi' => null,
]);
}
$app->error(function (\Exception $e, Request $request, int $code) {
return new JsonResponse([
'success' => false,
'status' => Response::$statusTexts[$code],
'code' => $code,
'message' => $e->getMessage(),
], $code);
});
$app['user.repository'] = function() use($app) {
return new \Sikofitt\Yamaha\AV\Repository\UserRepository($app['db']);
};
$app['system.controller'] = function() use($app) {
return new \Sikofitt\Yamaha\AV\Controller\SystemController($app['user.repository']);
};
$app->post('/system/features', 'system.controller:statusCommand');
/** @var Request $request */
//$request = $app['request_stack']->getMasterRequest();
$app->options('/', function() {
$response = new Response();
$response->setStatusCode(Response::HTTP_NO_CONTENT);
$response->headers->set('Allow', 'GET, OPTIONS');
return $response;
});
Request::setTrustedProxies(array('127.0.0.1'));
if('dev' !== $env) {
$app['http_cache']->run();
} else {
$app->run();
}