resume/app/App.php

210 lines
6.6 KiB
PHP
Raw Permalink Normal View History

2016-07-03 20:54:55 -07:00
<?php
2016-07-08 14:58:30 -07:00
/*
* This file is part of Resume.PHP.
*
* (copyleft) R. Eric Wheeler <sikofitt@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2016-07-03 20:54:55 -07:00
use Sikofitt\Config\ConfigTrait;
2016-07-08 14:58:30 -07:00
use Sikofitt\Json\JsonFileTrait;
use Sikofitt\Json\JsonTrait;
2016-07-03 20:54:55 -07:00
use Silex\Application;
2016-07-11 14:56:40 -07:00
require __DIR__ . '/../vendor/autoload.php';
2016-07-03 20:54:55 -07:00
/**
* Class App
*/
2016-07-08 14:58:30 -07:00
class App extends Application
{
2016-07-03 20:54:55 -07:00
2016-07-08 14:58:30 -07:00
use ConfigTrait;
use JsonTrait;
use JsonFileTrait;
use Application\TwigTrait;
use Application\MonologTrait;
use Application\SwiftmailerTrait;
use Application\TranslationTrait;
use Application\UrlGeneratorTrait;
2016-07-03 20:54:55 -07:00
2016-07-11 14:56:40 -07:00
private $debug;
2016-07-17 17:02:58 -07:00
/**
* @return string
*/
public function getConfDirectory()
{
return $this->getAppDirectory() . '/config';
}
/**
* Returns the application directory.
*
* @return string
* The main application directory.
*/
public function getAppDirectory()
{
$r = new ReflectionClass($this);
return dirname($r->getFileName());
}
/**
* @return string
*/
public function getResumeJson()
{
return $this->getDataDirectory() . '/resume.json';
2016-07-17 17:02:58 -07:00
}
/**
* @return string
*/
public function getDataDirectory()
{
return $this->getRootDirectory() . '/data';
}
/**
* Returns the root directory of the application.
*
* @return string
* The root directory of the application.
*/
public function getRootDirectory()
{
return dirname($this->getAppDirectory());
}
/**
* @return string
*/
public function getResumeSchema()
{
return $this->getDataDirectory() . '/schema/schema.v1.json';
}
2016-07-05 14:41:50 -07:00
2016-07-08 14:58:30 -07:00
public function getLogDirectory()
{
return $this->getDataDirectory() . '/logs';
}
2016-07-17 17:02:58 -07:00
public function getDebug()
{
return $this['debug'];
}
2016-07-08 14:58:30 -07:00
public function setDebug()
{
2016-07-13 14:48:30 -07:00
$this['debug'] = (null !== $this->config('app.debug') ? $this->config('app.debug') : true);
2016-07-08 14:58:30 -07:00
$this['env'] = getenv('PHP_ENV');
if (!$this['env']) {
$this['env'] = null !== $this->config('app.environment') ? $this->config('app.environment') : 'dev';
}
}
2016-07-11 14:56:40 -07:00
2016-07-17 17:02:58 -07:00
public function boot()
2016-07-11 14:56:40 -07:00
{
2016-07-17 17:02:58 -07:00
$this->registerExtenders();
// register default icons
$this->registerDefaultIcons();
return parent::boot();
2016-07-11 14:56:40 -07:00
}
2016-07-17 17:02:58 -07:00
2016-07-09 21:37:39 -07:00
public function registerExtenders()
{
if (!$this['debug']) {
2016-07-17 17:02:58 -07:00
$this->log('In Error handler.');
2016-07-09 21:37:39 -07:00
$this->error(function (\Exception $e, \Symfony\Component\HttpFoundation\Request $request, $code) {
2016-07-17 17:02:58 -07:00
switch ($code) {
case 405:
preg_match('/\(Allow\:(.+)\)/', $e->getMessage(), $matches);
if (isset($matches[1])) {
$matches = trim($matches[1]);
} elseif (isset($matches[0])) {
$matches = trim($matches[0]);
} else {
$matches = 'Available methods are unknown.';
}
$message = [
'status' => 'error',
'message' => 'Method not allowed',
'allowedMethods' => $matches,
'requestedMethod' => $request->getMethod(),
'code' => $code,
];
if ($request->isXmlHttpRequest()) {
$message = json_encode($message);
} else {
$message = $this->renderView('error.405.html.twig', $message);
}
$this->log($e->getMessage(), ['code' => $code], \Symfony\Bridge\Monolog\Logger::WARNING);
break;
case 500:
$message = ['status' => 'error', 'message' => 'Critical Error', 'code' => $code];
if ($request->isXmlHttpRequest()) {
$message = json_decode($message);
} else {
$message = $this->renderView('error.html.twig', $message);
}
$this->log($e->getMessage(), ['code' => $code], \Symfony\Bridge\Monolog\Logger::CRITICAL);
break;
default:
$message = ['status' => 'error', 'message' => $e->getMessage(), 'code' => $code, 'requestUri' => $request->getRequestUri()];
if ($request->isXmlHttpRequest()) {
$message = json_decode($message);
} else {
$message = $this->renderView('error.html.twig', $message);
}
$this->log($e->getMessage(), ['code' => $code], \Symfony\Bridge\Monolog\Logger::ERROR);
break;
}
return new \Symfony\Component\HttpFoundation\Response($message, $code);
});
2016-07-09 21:37:39 -07:00
}
$this->extend('twig', function (\Twig_Environment $twig) {
2016-07-17 17:02:58 -07:00
if ($this['debug']) {
$twig->enableDebug();
}
$twig->addExtension(new Twig_Extensions_Extension_Date());
$twig->addExtension(new Sikofitt\Twig\Date());
$twig->addExtension(new Sikofitt\Twig\RenderProfile());
$twig->addGlobal('config', $this->config('all'));
return $twig;
});
2016-07-08 14:58:30 -07:00
}
2016-07-03 20:54:55 -07:00
2016-07-08 14:58:30 -07:00
public function registerDefaultIcons()
{
$this->registerIcon(new \Sikofitt\Image\Profile\TwitterProfileIcon());
$this->registerIcon(new \Sikofitt\Image\Profile\FacebookProfileIcon());
$this->registerIcon(new \Sikofitt\Image\Profile\GithubProfileIcon());
$this->registerIcon(new \Sikofitt\Image\Profile\GitlabProfileIcon());
$this->registerIcon(new \Sikofitt\Image\Profile\LinkedinProfileIcon());
}
2016-07-17 17:02:58 -07:00
/**
* Registers media icons
*
* @param \Sikofitt\Image\Profile\ProfileIconInterface $icon
*/
public function registerIcon(\Sikofitt\Image\Profile\ProfileIconInterface $icon)
{
$r = new ReflectionClass($icon);
if($r->hasMethod('getClass')) {
$classes = $icon->getClass();
} else {
$classes = null;
}
$this->config(sprintf('app.icons.%s', $icon->getName()), ['icon' => $icon->getIcon(), 'url' => $icon->getDefaultUrl(), 'class' => $classes]);
2016-07-17 17:02:58 -07:00
}
2016-07-08 14:58:30 -07:00
}