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 ;
require '../vendor/autoload.php' ;
/**
* 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-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 ()
{
return $this -> getDataDirectory () . '/resume.schema.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
/**
2016-07-07 14:50:58 -07:00
* Registers media icons
*
* @ param \Sikofitt\Image\Profile\ProfileIconInterface $icon
2016-07-05 14:41:50 -07:00
*/
2016-07-07 14:50:58 -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 ()
{
$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 :
2016-07-07 14:50:58 -07:00
2016-07-08 14:58:30 -07:00
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 = $this [ 'twig' ] -> render ( 'error500.html.twig' );
}
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 ;
});
2016-07-05 14:41:50 -07:00
}
2016-07-08 14:58:30 -07:00
public function boot ()
{
$this -> registerExtenders ();
// 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 ());
}
}