Browse Source

Added Core\Config class to load local configuration when building page

- loaded from Core\Page::__construct()
Frederic G. MARAND 12 years ago
parent
commit
19bdac29d1
4 changed files with 117 additions and 1 deletions
  1. 65 0
      Memcache_UI/Core/Config.inc
  2. 32 1
      Memcache_UI/Core/Context.inc
  3. 2 0
      Memcache_UI/Core/Page.inc
  4. 18 0
      memcache_ui.local.php

+ 65 - 0
Memcache_UI/Core/Config.inc

@@ -0,0 +1,65 @@
+<?php
+/**
+ * @file
+ * Load configuration from defaults or a configuration file
+ */
+
+namespace Memcache_UI\Core {
+
+  class Config {
+    /**
+     * The single account allowed access to the UI.
+     *
+     * @var string
+     */
+    public $account = NULL;
+
+    /**
+     * The cleartext password for the authorized account.
+     *
+     * @var string
+     */
+    public $password = NULL;
+
+    /**
+     * The list of servers to monitor.
+     *
+     * @var array
+     *   Entries in "server:port" format
+     */
+    public $servers = array(
+      'localhost:11211',
+    );
+
+    public function __construct(Context $context) {
+      $path = $context->getDirectory() . '/memcache_ui.local.php';
+      if (file_exists($path)) {
+        if (is_readable($path)) {
+          $context->setMessage(Context::t('Loading config from @path', array('@path' => $path)), LOG_DEBUG);
+          require_once($path);
+          $context->setMessage($this, LOG_DEBUG);
+
+          if (empty($this->account)) {
+            $context->setMessage(Context::t('Empty account: anonymous access only.'), LOG_ERR);
+          }
+          elseif (empty($this->password)) {
+            $context->setMessage(Context::t('Empty password: anonymous access only.'), LOG_ERR);
+          }
+          elseif (empty($this->servers)) {
+            $context->setMessage(Context::t('Empty servers array: no server info.'), LOG_ERR);
+          }
+        }
+        else {
+          $context->setMessage(Context::t('Local config @path exists but is not readable', array(
+            '@path' => $path,
+          )), LOG_ERR);
+        }
+      }
+      else {
+        $context->setMessage(Context::t('Local config @path does not exist.', array(
+          '@path' => $path,
+        )), LOG_ERR);
+      }
+    }
+  }
+}

+ 32 - 1
Memcache_UI/Core/Context.inc

@@ -4,12 +4,17 @@ namespace Memcache_UI\Core {
     protected $logLevelClasses = NULL;
 
     /**
-     * Base URL for the script.
+     * Base URL for the script, for HTTP paths
      *
      * @var string
      */
     protected $base;
 
+    /**
+     * Base directory for the script, for file operations
+     */
+    protected $directory;
+
     /**
      * Graphics context
      *
@@ -95,6 +100,14 @@ namespace Memcache_UI\Core {
       return $this->base;
     }
 
+    public function getDirectory() {
+      if (!isset($this->directory)) {
+        $this->directory = dirname($_SERVER['SCRIPT_FILENAME']);
+      }
+
+      return $this->directory;
+    }
+
     public function getLogLevel() {
       if (!isset($this->logLevel)) {
         $usLogLevel = NULL;
@@ -272,5 +285,23 @@ namespace Memcache_UI\Core {
     static function t($message, $args = array()) {
       return strtr(gettext($message), $args);
     }
+
+    /**
+     * Embryonic version of Drupal url().
+     *
+     * @param string $path
+     * @param array $options
+     *   - No option currently recognized.
+     *   - 'absolute' likely at some point
+     *
+     * @return string
+     */
+    static function url($path = NULL, $options = array()) {
+      $options += array(
+        // 'absolute' => FALSE,
+      );
+
+    return $this->getBase() . '?q=' . urlencode($path);
+    }
   }
 }

+ 2 - 0
Memcache_UI/Core/Page.inc

@@ -1,5 +1,6 @@
 <?php
 namespace Memcache_UI\Core {
+
   abstract class Page extends Element {
 
     /**
@@ -58,6 +59,7 @@ namespace Memcache_UI\Core {
       parent::__construct('html');
       $context->setMessage($item, LOG_DEBUG);
       $this->context = $context;
+      $this->config = new Config($context);
       $this->initializeHead();
       $this->initializeBody();
       // Will fail if not reimplemented as a concrete method in a child class

+ 18 - 0
memcache_ui.local.php

@@ -0,0 +1,18 @@
+<?php
+/**
+ * @file
+ * Local configuration values
+ *
+ * This script is loaded from an instance of the Core\Config class.
+ *
+ * Useful properties:
+ * - $this->account
+ * - $this->password
+ * - $this->servers
+ */
+
+$this->account = '';
+$this->password = '';
+$this->servers = array(
+  'localhost:11211',
+);