Ejemplo n.º 1
0
 public function initDatabase()
 {
     if (option('db_conn') === null) {
         throw new Exception('Database connection not found.');
     }
     $schema = new Fz_Db_Schema(option('root_dir') . '/config/db');
     $schema->migrate();
 }
Ejemplo n.º 2
0
/**
 * configuring Filez
 */
function before()
{
    if (fz_config_get('app', 'use_url_rewriting')) {
        option('base_uri', option('base_path'));
    }
    // error handling
    if (fz_config_get('app', 'debug', false)) {
        ini_set('display_errors', true);
        option('debug', true);
        option('env', ENV_DEVELOPMENT);
    } else {
        ini_set('display_errors', false);
        option('debug', false);
    }
    // I18N
    Zend_Locale::setDefault(fz_config_get('app', 'default_locale', 'fr'));
    $currentLocale = new Zend_Locale('auto');
    $translate = new Zend_Translate('gettext', option('root_dir') . DIRECTORY_SEPARATOR . 'i18n', $currentLocale, array('scan' => Zend_Translate::LOCALE_DIRECTORY));
    option('translate', $translate);
    option('locale', $currentLocale);
    Zend_Registry::set('Zend_Locale', $currentLocale);
    // Execute DB configuration only if Filez is configured
    if (!option('installing')) {
        // check log dir
        if (!is_writable(fz_config_get('app', 'log_dir'))) {
            trigger_error('Log dir is not writeable "' . fz_config_get('app', 'log_dir') . '"', E_USER_WARNING);
        }
        // check upload dir
        if (!is_writable(fz_config_get('app', 'upload_dir'))) {
            trigger_error('Upload dir is not writeable "' . fz_config_get('app', 'upload_dir') . '"', E_USER_ERROR);
        }
        // Database configuration
        try {
            $db = new PDO(fz_config_get('db', 'dsn'), fz_config_get('db', 'user'), fz_config_get('db', 'password'));
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $db->exec('SET NAMES \'utf8\'');
            option('db_conn', $db);
        } catch (Exception $e) {
            halt(SERVER_ERROR, 'Can\'t connect to the database');
        }
        // Initialise and save the user factory
        $factoryClass = fz_config_get('app', 'user_factory_class');
        $userFactory = new $factoryClass();
        $userFactory->setOptions(fz_config_get('user_factory_options', null, array()));
        option('userFactory', $userFactory);
        // Check the database version and migrate if necessary
        $dbSchema = new Fz_Db_Schema(option('root_dir') . '/config/db');
        if ($dbSchema->isOutdated()) {
            fz_log('Migration needed (db_version: ' . $dbSchema->getCurrentVersion() . '), executing the scripts...');
            $dbSchema->migrate();
        }
    }
}