resume/app/App.php

163 lines
4.4 KiB
PHP

<?php
/*
* 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.
*/
use Sikofitt\Config\ConfigTrait;
use Sikofitt\Json\JsonFileTrait;
use Sikofitt\Json\JsonTrait;
use Silex\Application;
require '../vendor/autoload.php';
/**
* Class App
*/
class App extends Application
{
use ConfigTrait;
use JsonTrait;
use JsonFileTrait;
use Application\TwigTrait;
use Application\MonologTrait;
use Application\SwiftmailerTrait;
use Application\TranslationTrait;
use Application\UrlGeneratorTrait;
/**
* Returns the application directory.
*
* @return string
* The main application directory.
*/
public function getAppDirectory()
{
$r = new ReflectionClass($this);
return dirname($r->getFileName());
}
/**
* 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 getConfDirectory()
{
return $this->getAppDirectory() . '/config';
}
/**
* @return string
*/
public function getDataDirectory()
{
return $this->getRootDirectory() . '/data';
}
/**
* @return string
*/
public function getResumeJson()
{
return $this->getDataDirectory() . '/resume.json';
}
/**
* @return string
*/
public function getResumeSchema()
{
return $this->getDataDirectory() . '/resume.schema.json';
}
public function getLogDirectory()
{
return $this->getDataDirectory() . '/logs';
}
/**
* Registers media icons
*
* @param \Sikofitt\Image\Profile\ProfileIconInterface $icon
*/
public function registerIcon(\Sikofitt\Image\Profile\ProfileIconInterface $icon)
{
$this->config(sprintf('app.icons.%s', $icon->getName()), ['icon' => $icon->getIcon(), 'url' => $icon->getDefaultUrl()]);
}
public function setDebug()
{
$this['debug'] = null !== $this->config('app.debug') ? $this->config('app.debug') : true;
$this['env'] = getenv('PHP_ENV');
if (!$this['env']) {
$this['env'] = null !== $this->config('app.environment') ? $this->config('app.environment') : 'dev';
}
}
public function registerExtenders()
{
if (!$this['debug']) {
$this->error(function (\Exception $e, \Symfony\Component\HttpFoundation\Request $request, $code) {
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 = json_encode(['status' => 'error', 'message' => 'Method not allowed', 'allowedMethods' => $matches, 'requestedMethod' => $request->getMethod(), 'code' => $code]);
//$message = 'Sorry bout that.<br />' . $e->getMessage();
break;
default:
$message = json_encode(['status' => 'error', 'message' => $e->getMessage(), 'code' => $code]);
}
return new \Symfony\Component\HttpFoundation\Response($message, $code);
});
}
$this->extend('twig', function (\Twig_Environment $twig) {
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;
});
}
public function boot()
{
$this->registerExtenders();
// register default icons
$this->registerDefaultIcons();
return parent::boot();
}
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());
}
}