resume/app/App.php

194 lines
5.6 KiB
PHP
Raw 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-17 11:05:32 -07:00
use Symfony\Component\HttpFoundation\Request;
2016-07-03 20:54:55 -07:00
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-05 14:41:50 -07:00
/**
* Returns the application directory.
*
* @return string
* The main application directory.
*/
2016-07-08 14:58:30 -07:00
public function getAppDirectory()
{
$r = new ReflectionClass($this);
return dirname($r->getFileName());
2016-07-05 14:41:50 -07:00
}
2016-07-03 20:54:55 -07:00
2016-07-05 14:41:50 -07:00
/**
* Returns the root directory of the application.
*
* @return string
* The root directory of the application.
*/
2016-07-08 14:58:30 -07:00
public function getRootDirectory()
{
return dirname($this->getAppDirectory());
2016-07-05 14:41:50 -07:00
}
2016-07-03 20:54:55 -07:00
2016-07-05 14:41:50 -07:00
/**
* @return string
*/
2016-07-08 14:58:30 -07:00
public function getConfDirectory()
{
return $this->getAppDirectory() . '/config';
2016-07-05 14:41:50 -07:00
}
2016-07-03 20:54:55 -07:00
2016-07-05 14:41:50 -07:00
/**
* @return string
*/
2016-07-08 14:58:30 -07:00
public function getDataDirectory()
{
return $this->getRootDirectory() . '/data';
2016-07-05 14:41:50 -07:00
}
2016-07-03 20:54:55 -07:00
2016-07-05 14:41:50 -07:00
/**
* @return string
*/
2016-07-08 14:58:30 -07:00
public function getResumeJson()
{
return $this->getDataDirectory() . '/resume.json';
2016-07-05 14:41:50 -07:00
}
2016-07-03 20:54:55 -07:00
2016-07-05 14:41:50 -07:00
/**
* @return string
*/
2016-07-08 14:58:30 -07:00
public function getResumeSchema()
{
2016-07-15 14:41:32 -07:00
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-05 14:41:50 -07:00
/**
* Registers media icons
*
* @param \Sikofitt\Image\Profile\ProfileIconInterface $icon
2016-07-05 14:41:50 -07:00
*/
public function registerIcon(\Sikofitt\Image\Profile\ProfileIconInterface $icon)
{
$this->config(sprintf('app.icons.%s', $icon->getName()), ['icon' => $icon->getIcon(), 'url' => $icon->getDefaultUrl()]);
}
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
public function getDebug()
{
return $this['debug'];
}
2016-07-09 21:37:39 -07:00
public function registerExtenders()
{
if (!$this['debug']) {
2016-07-15 10:02:18 -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-08 14:58:30 -07:00
switch ($code) {
case 405:
preg_match('/\(Allow\:(.+)\)/', $e->getMessage(), $matches);
2016-07-09 21:37:39 -07:00
if (isset($matches[1])) {
$matches = trim($matches[1]);
} elseif (isset($matches[0])) {
$matches = trim($matches[0]);
2016-07-08 14:58:30 -07:00
} else {
2016-07-09 21:37:39 -07:00
$matches = 'Available methods are unknown.';
2016-07-08 14:58:30 -07:00
}
2016-07-15 14:41:32 -07:00
$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);
2016-07-15 10:02:18 -07:00
break;
case 500:
$message = json_encode(['status' => 'error', 'message' => 'Critical Error', 'code' => $code]);
$this->log($e->getMessage(), ['code' => $code], \Symfony\Bridge\Monolog\Logger::CRITICAL);
2016-07-08 14:58:30 -07:00
break;
default:
2016-07-15 14:41:32 -07:00
$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);
2016-07-15 10:02:18 -07:00
break;
2016-07-08 14:58:30 -07:00
}
2016-07-08 14:58:30 -07:00
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-08 14:58:30 -07:00
if ($this['debug']) {
2016-07-09 21:37:39 -07:00
$twig->enableDebug();
2016-07-08 14:58:30 -07:00
}
$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-09 21:37:39 -07:00
}
2016-07-08 14:58:30 -07:00
public function boot()
{
2016-07-09 21:37:39 -07:00
$this->registerExtenders();
2016-07-08 14:58:30 -07:00
// register default icons
$this->registerDefaultIcons();
return parent::boot();
}
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());
}
}