/**
  * Resolve new csrf token against provided action
  * @param $action
  * @return mixed
  */
 public static function getNewToken($action)
 {
     // create new csrf model instance, set its action, rest would be auto-populated using default validators
     $csrf = new Csrf();
     $csrf->action = $action;
     if (!$csrf->save(true, false)) {
         WebApplication::exitWithException(new \Exception('Unable to generate csrf token', 400));
     }
     return $csrf->key;
 }
 /**
  * @param $value
  */
 public function resolvePDODatabaseTypeByValue($value)
 {
     if (!is_scalar($value) && !is_null($value)) {
         \GGS\Components\WebApplication::exitWithException(new \Exception('Provided value can not be mapped to PDO data type', 400));
     }
     $dataType = \PDO::PARAM_STR;
     if (is_numeric($value)) {
         $dataType = \PDO::PARAM_INT;
     } else {
         if (is_bool($value)) {
             $dataType = \PDO::PARAM_BOOL;
         } else {
             if (is_null($value)) {
                 $dataType = \PDO::PARAM_NULL;
             }
         }
     }
     return $dataType;
 }
 /**
  * Save current model object
  * @param bool $validate
  * @param bool $throwExceptionOnFailure
  */
 public function save($validate = true, $throwExceptionOnFailure = true)
 {
     if ($validate && !$this->validate()) {
         // wanted to validate but validation failed? bail out.
         return false;
     }
     // call beforeSave hook to handle special processes
     $this->beforeSave();
     // call create() if record is new, update() if it already exists
     $saved = $this->isNew() ? $this->create() : $this->update();
     if (!$saved) {
         if ($throwExceptionOnFailure) {
             // woah, database bailed on app or was there an issue with the query?
             WebApplication::exitWithException(new \Exception("Unable to save record"));
         } else {
             return false;
         }
     }
     // call the after save hook only if record has been saved.
     $this->afterSave();
     // return the primary key value
     return $this->getPkValue();
 }
 /**
  * A wrapper around WebApplication's function with same name but with different arguments
  * @param $message
  * @param null $code
  */
 protected static function exitWithException($message, $code = null)
 {
     WebApplication::exitWithException(new \Exception($message, $code));
 }
<?php

// this causes a lot of mess when enabled. Even though we handle the special case in Controller's beforeAction
// lets not try to burden the system.
ini_set('magic_quotes_gpc', false);
// some useful constants.
define('ROOT_PATH', __DIR__);
define('APP_PATH', ROOT_PATH . DIRECTORY_SEPARATOR . 'app');
define('VENDOR_PATH', ROOT_PATH . DIRECTORY_SEPARATOR . 'vendor');
// get the psr4 autoloader running
require_once VENDOR_PATH . DIRECTORY_SEPARATOR . 'autoload.php';
// get the system config
$config = (require_once APP_PATH . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'main.php');
// now do some real work
\GGS\Components\WebApplication::run($config);