<commit_before><?php
/**
 * =============================================================================
 * Copyright (c) 2010, <NAME>
 * All rights reserved.
 *
 * This file is part of Conductor and is licensed by the Copyright holder under
 * the 3-clause BSD License.  The full text of the license can be found in the
 * LICENSE.txt file included in the root directory of this distribution or at
 * the link below.
 * =============================================================================
 *
 * @license http://www.opensource.org/licenses/bsd-license.php
 */
namespace zpt\cdt;

use \oboe\head\Javascript;
use \oboe\head\Link;
use \oboe\Element;
use \zeptech\dynamic\Configurator;
use \zeptech\dynamic\InjectionConfigurator;
use \zeptech\dynamic\ServerConfigurator;
use \zeptech\orm\runtime\Clarinet;
use \zeptech\orm\runtime\Criteria;
use \zeptech\orm\runtime\Persister;
use \zeptech\rest\RestServer;
use \zpt\cdt\compile\Compiler;
use \zpt\cdt\di\Injector;
use \zpt\cdt\exception\AuthException;
use \zpt\util\File;
use \Exception;
use \PDO;

/**
 * The main interface for Conductor setup.
 *
 * @author <NAME> <<EMAIL>>
 */
class Conductor {

  const JQUERY_VERSION = '1.7.1';

  /* Conductor configuration */
  private static $_config;

  /* Whether or not conductor has been initialized */
  private static $_initialized = false;

  /**
   * Retrieve the {@link Configuration} object for the site.
   *
   * @return Configuration
   */
  public static function getConfig() {
    return self::$_config;
  }

  /**
   * Retrieve the configuration value with the given name.  In order for this to
   * work the database must be setup to handle configuration values.
   *
   * @param {string} $name The name of the configuration value to retrieve.
   */
  public static function getConfigValue($name) {
    self::_ensureInitialized();

    $c = new Criteria();
    $c->addEquals('name', $name);

    $persister = Persister::get('zpt\cdt\model\ConfigValue');
    $rows = $persister->retrieve($c);
    if (count($rows) == 0) {
      return null;
    }

    $obj = $rows[0];
    return $obj->getValue();
  }

  /**
   * Retrieve a list of configuration values that belong to the given group(s).
   * Nested groups can be accessed by provided a dot separated path to the
   * nested group.  Configuration values can be retrieved for several groups at
   * once by providing an array of group names.
   *
   * @param mixed $groups Either a single string group name or an array of
   *   group names.
   * @return A list of ConfigValue instances.
   */
  public static function getConfigValues($groups) {
    if (!is_array($groups)) {
      return self::getConfigValues(array($groups));
    }

    $groupConditions = array();
    foreach ($groups AS $group) {
      $groupConditions[] = "$group.%";
    }

    $c = new Criteria();
    $c->addLike('name', $groupConditions);

  
    $persister = Persister::get('zpt\cdt\model\ConfigValue');
    $values = $persister->retrieve($c);
    $idxd = array();
    foreach ($values AS $value) {
      $valName = $value->getName();

      $valName = substr($valName, strpos($valName, '.') + 1);

      $idxd[$valName] = $value;
    }

    return $idxd;
  }

  /**
   * Getter for the path info associated with conductor config used to
   * initialize this session.
   *
   * @return array
   */
  public static function getPathInfo() {
    self::_ensureInitialized();
    return self::$_config['pathInfo'];
  }

  /**
   * Initialize the framework.  This consists of registering the autoloaders for
   * the libraries, connecting to the database and initializing clarinet.
   *
   * @param string $root The path to the root of the website.
   * @param boolean $authenticate Whether or not to authenticate a session,
   *   default true.  The only time this is false is during a deploy compile.
   */
  public static function init($root, $authenticate = true) {
    if (self::$_initialized) {
      return;
    }
    self::$_initialized = true;

    // Register class loaders for conductor's dependencies
    Loader::registerDependencies($root);

    // Set dev mode configuration and do a compile if the target directory is
    // writeable.
    if (is_writable("$root/target")) {
      ini_set('display_errors', 'on');
      ini_set('html_errors', 'on');
      ini_set('error_log', "$root/target/php.error");

      assert_options(ASSERT_ACTIVE, 1);
      assert_options(ASSERT_WARNING, 1);
      assert_options(ASSERT_BAIL, 0);
      assert_options(ASSERT_QUIET_EVAL, 0);

      try {
        if (File::dirlock("$root/target")) {
          $compiler = new Compiler();
          $compiler->compile($root);
          File::dirunlock("$root/target");
        } else {
          // This currently shouldn't happen since the only reason File::dirlock
          // would return false is if the target isn't writeable but if that
          // changes this may start to show up in the logs.
          error_log('Unable to compile');
        }
      } catch (Exception $e) {
        File::dirunlock("$root/target");
        throw new Exception($e);
      }
    }

    // Load the site's configuration
    self::$_config = Configurator::getConfig();

    $pathInfo = self::$_config['pathInfo'];
    $namespace = self::$_config['namespace'];

    // Register a class loader for the site's base namespace
    Loader::registerNamespace($namespace, "$root/src");

    // Initiate a database connection<commit_msg>Updated dev mode initialization to output HTML allowing user to forcefully remove a directory lock leftover from a previous error when a DirectoryLockTimeoutException is encountered.  This eliminates the need for the user to have root access or be able to switch to the web server user to be able to remove the lock<commit_after><?php
/**
 * =============================================================================
 * Copyright (c) 2010, <NAME>
 * All rights reserved.
 *
 * This file is part of Conductor and is licensed by the Copyright holder under
 * the 3-clause BSD License.  The full text of the license can be found in the
 * LICENSE.txt file included in the root directory of this distribution or at
 * the link below.
 * =============================================================================
 *
 * @license http://www.opensource.org/licenses/bsd-license.php
 */
namespace zpt\cdt;

use \oboe\head\Javascript;
use \oboe\head\Link;
use \oboe\Element;
use \zeptech\dynamic\Configurator;
use \zeptech\dynamic\InjectionConfigurator;
use \zeptech\dynamic\ServerConfigurator;
use \zeptech\orm\runtime\Clarinet;
use \zeptech\orm\runtime\Criteria;
use \zeptech\orm\runtime\Persister;
use \zeptech\rest\RestServer;
use \zpt\cdt\compile\Compiler;
use \zpt\cdt\di\Injector;
use \zpt\cdt\exception\AuthException;
use \zpt\util\File;
use \zpt\util\DirectoryLockTimeoutException;
use \Exception;
use \PDO;

/**
 * The main interface for Conductor setup.
 *
 * @author <NAME> <<EMAIL>>
 */
class Conductor {

  const JQUERY_VERSION = '1.7.1';

  /* Conductor configuration */
  private static $_config;

  /* Whether or not conductor has been initialized */
  private static $_initialized = false;

  /**
   * Retrieve the {@link Configuration} object for the site.
   *
   * @return Configuration
   */
  public static function getConfig() {
    return self::$_config;
  }

  /**
   * Retrieve the configuration value with the given name.  In order for this to
   * work the database must be setup to handle configuration values.
   *
   * @param {string} $name The name of the configuration value to retrieve.
   */
  public static function getConfigValue($name) {
    self::_ensureInitialized();

    $c = new Criteria();
    $c->addEquals('name', $name);

    $persister = Persister::get('zpt\cdt\model\ConfigValue');
    $rows = $persister->retrieve($c);
    if (count($rows) == 0) {
      return null;
    }

    $obj = $rows[0];
    return $obj->getValue();
  }

  /**
   * Retrieve a list of configuration values that belong to the given group(s).
   * Nested groups can be accessed by provided a dot separated path to the
   * nested group.  Configuration values can be retrieved for several groups at
   * once by providing an array of group names.
   *
   * @param mixed $groups Either a single string group name or an array of
   *   group names.
   * @return A list of ConfigValue instances.
   */
  public static function getConfigValues($groups) {
    if (!is_array($groups)) {
      return self::getConfigValues(array($groups));
    }

    $groupConditions = array();
    foreach ($groups AS $group) {
      $groupConditions[] = "$group.%";
    }

    $c = new Criteria();
    $c->addLike('name', $groupConditions);

  
    $persister = Persister::get('zpt\cdt\model\ConfigValue');
    $values = $persister->retrieve($c);
    $idxd = array();
    foreach ($values AS $value) {
      $valName = $value->getName();

      $valName = substr($valName, strpos($valName, '.') + 1);

      $idxd[$valName] = $value;
    }

    return $idxd;
  }

  /**
   * Getter for the path info associated with conductor config used to
   * initialize this session.
   *
   * @return array
   */
  public static function getPathInfo() {
    self::_ensureInitialized();
    return self::$_config['pathInfo'];
  }

  /**
   * Initialize the framework.  This consists of registering the autoloaders for
   * the libraries, connecting to the database and initializing clarinet.
   *
   * @param string $root The path to the root of the website.
   * @param boolean $authenticate Whether or not to authenticate a session,
   *   default true.  The only time this is false is during a deploy compile.
   */
  public static function init($root, $authenticate = true) {
    if (self::$_initialized) {
      return;
    }
    self::$_initialized = true;

    // Register class loaders for conductor's dependencies
    Loader::registerDependencies($root);

    // Set dev mode configuration and do a compile if the target directory is
    // writeable.
    if (is_writable("$root/target")) {
      ini_set('display_errors', 'on');
      ini_set('html_errors', 'on');
      ini_set('error_log', "$root/target/php.error");

      assert_options(ASSERT_ACTIVE, 1);
      assert_options(ASSERT_WARNING, 1);
      assert_options(ASSERT_BAIL, 0);
      assert_options(ASSERT_QUIET_EVAL, 0);

      try {
        if (File::dirlock("$root/target", isset($_GET['forceunlock']))) {
          $compiler = new Compiler();
          $compiler->compile($root);
          File::dirunlock("$root/target");
        } else {
          // This currently shouldn't happen since the only reason File::dirlock
          // would return false is if the target isn't writeable but if that
          // changes this may start to show up in the logs.
          error_log('Unable to compile');
        }
      } catch (DirectoryLockTimeoutException $e) {

        // Since this is dev mode send a response which will allow the user to
        // forcefully unlock the directory
        // TODO
        $url = $_SERVER['REQUEST_URI'];
        if (strpos($url, '?') !== false) {
          $url .= '&forceunlock';
        } else {
          $url .= '?forceunlock';
        }

        echo "<!DOCTYPE html>\n" .
             "<html lang=en><head><meta charset=utf-8 /><title>Force unlock</title></head>\n" .
             "<body><h1>Unable to aquire target lock</h1>\n".
             "<a href=\"$url\">Click to forcefully remove the lock and continue</a></body></html>\n";
        exit;
        
      } catch (Exception $e) {
        File::dirunlock("$root/target");
        throw new Exception($e);
      }
    }

    // Load the site's configuration
    self::$_config = Configurator::getConfig();

    $pathInfo = self::$_config['pathInfo'];
    $namespace = self::$_config['namespace'];

    // Register a class loader for the site's base namespace
    Loader::registerNamespace($namespace, "$root/src");

    // Initiate a database connection
{'fraction_non_alphanumeric': 0.14490086170835773, 'fraction_numerical': 0.002760813184974483, 'mean_word_length': 3.064603876232574, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '77809', 'n_tokens_mistral': 3518, 'n_tokens_neox': 3398, 'n_words': 1101}