<?php

/*
 * This file is part of the Access to Memory (AtoM) software.
 *
 * Access to Memory (AtoM) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Access to Memory (AtoM) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Access to Memory (AtoM).  If not, see <http://www.gnu.org/licenses/>.
 */

class sfInstallDatabaseConfigHandler extends sfDatabaseConfigHandler
{
  public static
    $options;

  public static function getConfiguration(array $files)
  {
    // Basic parameters
    $config = array(
      'all' => array(
        'propel' => array(
          'class' => 'sfPropelDatabase',
          'param' => array(
            'encoding'   => 'utf8',
            'persistent' => true,
            'pooling'    => true
          )
        )
      ),
      'dev' => array(
        'propel' => array(
          'param' => array(
            'classname' => 'DebugPDO',
            'debug' => array(
              'realmemoryusage' => true,
              'details' => array(
                'time'     => array('enabled' => true),
                'slow'     => array('enabled' => true, 'threshold' => 0.1),
                'mem'      => array('enabled' => true),
                'mempeak'  => array('enabled' => true),
                'memdelta' => array('enabled' => true)
              )
            )
          )
        )
      ),
      'test' => array(
        'propel' => array(
          'param' => array(
            'classname' => 'DebugPDO'
          )
        )
      )
    );

    // DSN config
    $config['all']['propel']['param']['dsn'] = 'mysql:dbname='.self::$options['databaseName'];

    if (isset(self::$options['databaseHost'])
      && 'localhost' != self::$options['databaseHost'])
    {
      $config['all']['propel']['param']['dsn'] .= ';host='.self::$options['databaseHost'];
    }

    // Database config
    if (isset(self::$options['databasePort']))
    {
      $config['all']['propel']['param']['dsn'] .= ';port='.self::$options['databasePort'];
    }

    if (isset(self::$options['databaseUsername']))
    {
      $config['all']['propel']['param']['username'] = self::$options['databaseUsername'];

      if (isset(self::$options['databasePassword']))
      {
        $config['all']['propel']['param']['password'] = self::$options['databasePassword'];
      }
    }

    return $config;
  }

  public function execute($configFiles)
  {
    $includes = array();
    $config = self::getConfiguration( $configFiles);

    // compile data
    return sprintf("<?php\n".
                      "// auto-generated by sfInstall::configureDatabase()\n".
                      "// date: %s\n%s\nreturn %s;\n",
                      date('Y/m/d H:i:s'), implode("\n", $includes), var_export($config, true));
  }
}
