<?php
//debug
ini_set('display_errors', "On");
error_reporting(E_ALL);
$debug = new Phalcon\Debug();
$debug->listen();
try {
  date_default_timezone_set('Europe/Warsaw');
  $config = new \Phalcon\Config\Adapter\Ini('../app/lib/config/configuration.ini');
  $loader = new \Phalcon\Loader();
  $loader->registerDirs(array(
      $config->directories->controllers,
      $config->directories->models,
      $config->directories->lib,
      $config->directories->validation,
      'tcpdf' => '../app/lib/assets/tcpdf/',
  ))->register();
  $di = new \Phalcon\DI\FactoryDefault();
  $di->set('view', function() use ($config) {
    $view = new \Phalcon\Mvc\View();
    $version = new Version($config, TRUE);
    $view->setVar('version', $version->getVersion());
    $view->setViewsDir('../app/views/');
    return $view;
  });
  $di->setShared('session', function() {
    $session = new Phalcon\Session\Adapter\Files();
    $session->start();
    return $session;
  });
  $di->set('dispatcher', function() {
    $eventsManager = new \Phalcon\Events\Manager();
    $eventsManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) {
      if ($exception instanceof DispatchException) {
        return false;
      }
      if ($event->getType() == 'beforeException') {
        switch ($exception->getCode()) {
          case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
          case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
            $dispatcher->forward(array(
                'controller' => 'index',
                'action' => 'notExists'
            ));
            return false;
        }
      }
    });
    $dispatcher = new \Phalcon\Mvc\Dispatcher();
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
  }, true);
  $app = new \Phalcon\Mvc\Application($di);
  echo $app->handle()->getContent();
} catch (\Phalcon\Exception $e) {
  echo '<pre>';
  echo get_class($e), ": ", $e->getMessage(), "<br />";
  echo " File=", $e->getFile(), "<br />";
  echo " Line=", $e->getLine(), "<br />";
  echo " Stack Trace: <br />";
  foreach (explode("#", $e->getTraceAsString()) as $i => $trace) {
    if ($i != 0)
      echo ' #' . $trace;
  }
  echo '</pre>';
}