* * 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 __DIR__ . '/../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; private $debug; /** * @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'; } /** * @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'; } public function getLogDirectory() { return $this->getDataDirectory() . '/logs'; } public function getDebug() { return $this['debug']; } 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 boot() { $this->registerExtenders(); // register default icons $this->registerDefaultIcons(); return parent::boot(); } public function registerExtenders() { if (!$this['debug']) { $this->log('In Error handler.'); $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 = [ '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); }); } $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 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()); } /** * 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()]); } }