Example #1
0
 public function setDefault($faild = self::EXCEPTION_FAILD_MODE)
 {
     if (\Config::get("database.host") == "" and \Config::get("database.username") == "" and \Config::get("database.password") == "" and \Config::get("database.database") == "") {
         throw new DatabaseArgumentsException();
     } else {
         \Database::$default = @mysqli_connect(\Config::get("database.host"), \Config::get("database.username"), \Config::get("database.password"), \Config::get("database.database"));
         //
         if (!\Database::$default) {
             if ($faild == 2) {
                 throw new DatabaseConnectionException();
             } else {
                 if ($faild == 1) {
                     \Errors::r_db();
                 }
             }
         } else {
             mysqli_query(\Database::$default, "SET NAMES " . \Config::get("database.charset"));
             //
             \Database::$server = \Database::$default;
             //
             \Database::$serverData = ['host' => \Config::get("database.host"), "username" => \Config::get("database.username"), "password" => \Config::get("database.password"), "database" => \Config::get("database.database")];
             //
             \Database::$defaultData = ['host' => \Config::get("database.host"), "username" => \Config::get("database.username"), "password" => \Config::get("database.password"), "database" => \Config::get("database.database")];
         }
         return \Database::$default;
     }
 }
Example #2
0
 /**
  * Task to run pending migrations
  *
  * @return null
  */
 protected function _execute(array $params)
 {
     $migrations = new MigrationManager();
     Database::$default = $params['db'];
     $this->db = Database::instance();
     $db_config = Kohana::$config->load('database')->{$params['db']};
     if (!ORM::factory('Migration')->is_installed()) {
         /**
          * Get platform from database config
          */
         $platform = strtolower($db_config['type']);
         if ('mysqli' == $platform) {
             $platform = 'mysql';
         }
         /**
          * Get SQL from file for selected platform
          */
         $file = realpath(substr(__DIR__, 0, strlen(__DIR__) - strlen('classes/Task/Db')) . 'sql/' . $platform . '.sql');
         $handle = fopen($file, 'rb');
         $sql_create = fread($handle, filesize($file));
         $this->db->query(0, $sql_create);
         $msg = Minion_CLI::color("-----------------------------\n", 'green');
         $msg .= Minion_CLI::color("| Migration table create!!! |\n", 'green');
         $msg .= Minion_CLI::color("-----------------------------\n", 'green');
         Minion_CLI::write($msg);
     }
     $migrations->migrate($params['db'], $params['step']);
 }
Example #3
0
 /**
  * Rollback last executed migration.
  *
  */
 public function rollback($group = 'default', $step = 1)
 {
     Database::$default = $group;
     //Get last executed migration
     if (strtolower($step) === 'all') {
         $migrations = ORM::factory('Migration')->order_by('hash', 'DESC')->find_all();
     } else {
         $migrations = ORM::factory('Migration')->order_by('hash', 'DESC')->limit((int) $step)->find_all();
     }
     if (!count($migrations)) {
         $this->message("There's no migration to rollback");
         return;
     }
     $total_time = 0;
     foreach ($migrations as $key => $item) {
         $msg = "Migration '" . $item->name . "' with hash: " . $item->hash . ' was succefully "rollbacked"';
         $this->message($msg);
         $interval = microtime(true);
         try {
             $migration_object = $this->load_migration($item->hash, $group);
             $migration_object->down();
             $interval = microtime(true) - $interval;
             $total_time += $interval;
             $this->message(Minion_CLI::color("----------- with {$interval} s -----------\n", 'green'));
             $item->delete();
         } catch (Exception $e) {
             $this->message_error($e->getMessage());
             break;
         }
     }
     $this->message(Minion_CLI::color("\n----------- TOTAL TIME IS {$total_time} s -----------", 'green'));
 }
Example #4
0
 protected function getConnection()
 {
     $pdo_config = Kohana::config('unittest.pdo');
     Database::$default = 'unittest';
     $pdo = new PDO($pdo_config['dsn'], $pdo_config['username'], $pdo_config['password']);
     $sth = $pdo->prepare(file_get_contents(Kohana::find_file('tests', 'orm_mptt/test_data/dataset', 'sql')));
     $sth->execute();
     return $this->createDefaultDBConnection($pdo, $pdo_config['database']);
 }
Example #5
0
 /**
  * Task to rollback last executed migration
  *
  * @return null
  */
 protected function _execute(array $params)
 {
     $migrations = new MigrationManager();
     Database::$default = $params['db'];
     if (!ORM::factory('Migration')->is_installed()) {
         Minion_CLI::write('Migrations is not installed. Please Run the migrations.sql script in your mysql server');
         exit;
     }
     $migrations->rollback($params['db'], $params['step']);
 }
Example #6
0
 protected function getConnection()
 {
     $db_connection = Kohana::config('unittest.db_connection');
     $db_config = Kohana::config('database.' . $db_connection);
     Database::$default = $db_connection;
     if ($db_config['type'] == 'mysql') {
         $pdo = new PDO('mysql:host=' . $db_config['connection']['hostname'] . ';dbname=' . $db_config['connection']['database'], $db_config['connection']['username'], $db_config['connection']['password']);
         return $this->createDefaultDBConnection($pdo, $db_config['connection']['database']);
     } else {
         $db_name = substr($db_config['connection']['dsn'], strpos($db_config['connection']['dsn'], ';dbname='));
         $pdo = new PDO($db_config['dsn'], $db_config['username'], $db_config['password']);
         return $this->createDefaultDBConnection($pdo, $db_name);
     }
 }
Example #7
0
/**
 * Attach the file write to logging. Multiple writers are supported.
 */
Kohana::$log->attach(new Log_File(APPPATH . 'logs'));
/**
 * Attach a file reader to config. Multiple readers are supported.
 */
Kohana::$config->attach(new Config_File());
/**
 * Enable modules. Modules are referenced by a relative or absolute path.
 */
Kohana::modules(array('auth' => MODPATH . 'auth', 'database' => MODPATH . 'database', 'orm' => MODPATH . 'orm'));
if (Kohana::$environment === Kohana::DEVELOPMENT) {
    Database::$default = 'dev';
} else {
    Database::$default = 'default';
}
/**
 * Variable debug
 * @param mix $var
 * @param type $exit
 * @param type $echo
 * @return string 
 */
function vd($var, $exit = false, $echo = true)
{
    // format the label
    $label = '';
    // var_dump the variable into a buffer and keep the output
    ob_start();
    var_dump($var);
Example #8
0
<?php

defined('SYSPATH') or die('No direct script access.');
define('INSTALL_DATA', MODPATH . 'installer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR);
if (array_key_exists(Arr::get($_GET, 'lang'), I18n::available_langs())) {
    I18n::lang(Arr::get($_GET, 'lang'));
} else {
    I18n::lang(I18n::detect_lang());
}
try {
    date_default_timezone_get();
} catch (Exception $e) {
    date_default_timezone_set('UTC');
}
Database::$default = 'install';
Route::set('install', 'install(/<action>(/<id>))')->defaults(array('controller' => 'install', 'action' => 'error'));
Assets_Package::add('install')->js(NULL, ADMIN_RESOURCES . 'js/install.js', 'global')->css(NULL, ADMIN_RESOURCES . 'css/install.css', 'global');
Example #9
0
<?php

defined('SYSPATH') or die('No direct script access.');
// DB by env
if (class_exists('Database')) {
    Database::$default = Kohana::$environment;
}
// global create dir
function create_dir($path)
{
    if (!is_dir($path)) {
        try {
            // Create the directory
            mkdir($path, 0755, TRUE);
            // Set permissions (must be manually set to fix umask issues)
            chmod($path, 0755);
        } catch (Exception $e) {
            if (Kohana::$environment === Kohana::PRODUCTION) {
                throw new Kohana_Exception('Could not create directory :dir', array(':dir' => Debug::path($path)));
            }
        }
    }
}
if (!function_exists('dd')) {
    function dd()
    {
        echo call_user_func_array('Debug::vars', func_get_args());
        exit;
    }
}
// Auto base_url
Example #10
0
 */
Kohana::$log->attach(new Log_File(APPPATH . 'logs'));
/**
 * Attach a file reader to config. Multiple readers are supported.
 */
Kohana::$config->attach(new Config_File());
/**
 * Enable modules. Modules are referenced by a relative or absolute path.
 */
Kohana::modules(array('auth' => MODPATH . 'auth', 'database' => MODPATH . 'database', 'orm' => MODPATH . 'orm'));
if ($_SERVER['DOCUMENT_ROOT'] == '/home/michal/www') {
    Database::$default = 'slr';
} elseif ($_SERVER['SERVER_NAME'] == 'dev.socializer.pl') {
    Database::$default = 'dev_slr';
} elseif ($_SERVER['SERVER_NAME'] == 'michal.es') {
    Database::$default = 'mic';
}
/**
 * Variable debug
 * @param mix $var
 * @param type $exit
 * @param type $echo
 * @return string 
 */
function vd($var, $exit = false, $echo = true)
{
    // format the label
    $label = '';
    // var_dump the variable into a buffer and keep the output
    ob_start();
    var_dump($var);
Example #11
0
<?php

// Init Kohana
defined('APPPATH') ?: define('APPPATH', 'application/');
defined('MODPATH') ?: define('MODPATH', 'modules/');
defined('SYSPATH') ?: define('SYSPATH', 'system/');
defined('MGPATH') ?: define('MGPATH', 'modulargaming/');
defined('EXT') ?: define('EXT', '.php');
require_once APPPATH . 'bootstrap.php';
Database::$default = 'test';
Example #12
0
 *
 * - string   base_url    path, and optionally domain, of your application   NULL
 * - string   index_file  name of your index file, usually "index.php"       index.php
 * - string   charset     internal character set used for input and output   utf-8
 * - string   cache_dir   set the internal cache directory                   APPPATH/cache
 * - boolean  errors      enable or disable error handling                   TRUE
 * - boolean  profile     enable or disable internal profiling               TRUE
 * - boolean  caching     enable or disable internal caching                 FALSE
 */
Kohana::init(array('base_url' => '/', 'index_file' => ''));
/**
 * Attach the file write to logging. Multiple writers are supported.
 */
Kohana::$log->attach(new Log_File(APPPATH . 'logs'));
/**
 * Attach a file reader to config. Multiple readers are supported.
 */
Kohana::$config->attach(new Config_File());
/**
 * Enable modules. Modules are referenced by a relative or absolute path.
 */
Kohana::modules(array('user' => MODPATH . 'user', 'auth' => MODPATH . 'auth', 'database' => MODPATH . 'database', 'orm' => MODPATH . 'orm', 'pagination' => MODPATH . 'pagination', 'kohana-email' => MODPATH . 'kohana-email'));
/**
 * Set the routes. Each route must have a minimum of a name, a URI and a set of
 * defaults for the URI.
 */
Route::set('default', '(<controller>(/<action>(/<id>)))')->defaults(array('controller' => 'home', 'action' => 'index'));
$environments = array(Kohana::PRODUCTION => 'production', Kohana::STAGING => 'staging', Kohana::TESTING => 'testing');
// from http://forum.kohanaframework.org/discussion/9562/how-to-choose-database-config-in-bootstraps/p1
Database::$default = empty($environments[Kohana::$environment]) ? $_SERVER['HTTP_HOST'] : $environments[Kohana::$environment];
Example #13
0
 /**
  * 
  * @param array $post
  * @return boolean
  * @throws Installer_Exception
  */
 public function install(array $post)
 {
     if (empty($post)) {
         throw new Installer_Exception('No install data!');
     }
     if (isset($post['password_generate'])) {
         $post['password_field'] = Text::random();
     }
     if (isset($post['admin_dir_name'])) {
         $post['admin_dir_name'] = URL::title($post['admin_dir_name']);
     }
     if (isset($post['db_port'])) {
         $post['db_port'] = (int) $post['db_port'];
     }
     date_default_timezone_set($post['timezone']);
     $this->_session->set('install_data', $post);
     $this->_validation = $this->_valid($post);
     try {
         $this->_db_instance = $this->connect_to_db($post);
     } catch (Database_Exception $exc) {
         $validation = FALSE;
         switch ($exc->getCode()) {
             case 1049:
                 $this->_validation->error('db_name', 'incorrect');
                 $validation = TRUE;
                 break;
             case 2:
                 $this->_validation->error('db_server', 'incorrect')->error('db_user', 'incorrect')->error('db_password', 'incorrect');
                 $validation = TRUE;
                 break;
         }
         if ($validation === TRUE) {
             throw new Validation_Exception($this->_validation, $exc->getMessage(), NULL, $exc->getCode());
         } else {
             throw new Database_Exception($exc->getMessage(), NULL, $exc->getCode());
         }
     }
     Database::$default = 'install';
     Observer::notify('before_install', $post, $this->_validation);
     if (isset($post['empty_database'])) {
         $this->_reset();
     }
     define('TABLE_PREFIX_TMP', Arr::get($post, 'db_table_prefix', ''));
     $this->_import_shema($post);
     $this->_import_dump($post);
     $this->_install_modules($post);
     Observer::notify('install', $post);
     $this->_create_site_config($post);
     $this->_create_config_file($post);
     return TRUE;
 }