示例#1
0
 /**
  * Creates a new ViewSandbox and calculates the necessary URLs.
  *
  * @param ContextStack|boolean context The context in which to load the
  * 		view.  If false or omitted, an empty context will be used.
  */
 public function __construct($context = false)
 {
     $this->context = $context ?: new ContextStack();
     $this->cleanAppURL = Config::getRequiredVal("general", "app_url");
     if ($this->cleanAppURL[strlen($this->cleanAppURL) - 1] == '/') {
         $this->cleanAppURL = substr($this->cleanAppURL, 0, -1);
     }
     $this->cleanViewURL = Config::getVal("view", "root_url");
     if ($this->cleanViewURL === null) {
         $this->cleanViewURL = $this->appURL(Config::getRequiredVal("view", "url_path"));
     }
     if ($this->cleanViewURL[strlen($this->cleanViewURL) - 1] == '/') {
         $this->cleanViewURL = substr($this->cleanViewURL, 0, -1);
     }
 }
示例#2
0
 /**
  * Construct a new RedisEngine.
  */
 public function __construct()
 {
     $pooling = Config::getVal("cache", "use_pooling");
     $server = Config::getRequiredVal("cache", "server");
     $port = Config::getVal("cache", "port") ?: 6379;
     $timeout = Config::getVal("cache", "timeout") ?: null;
     $password = Config::getVal("cache", "password");
     $this->engine = new Redis();
     if ($pooling) {
         $this->engine->pconnect($server, $port, $timeout);
     } else {
         $this->engine->connect($server, $port, $timeout);
     }
     if ($password) {
         $this->engine->auth($password);
     }
     $this->engine->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
 }
示例#3
0
 /**
  * Loads the contents of the specified template.
  *
  * @param string $templateName The name of the template to be loaded.
  * @return string The unaltered, unparsed contents of the specified
  * 		template.
  * @throws hydrogen\view\exceptions\NoSuchViewException if the specified
  * 		template is not found or cannot be loaded.
  */
 public function load($viewName)
 {
     $table = Config::getRequiredVal("view", "table_name");
     $nameCol = Config::getRequiredVal("view", "name_field");
     $contentCol = Config::getRequiredVal("view", "content_field");
     $query = new Query("SELECT");
     $query->field($contentCol);
     $query->from($table);
     $query->where($nameCol . " = ?", $viewName);
     $query->limit(1);
     $stmt = $query->prepare();
     $stmt->execute();
     $result = $stmt->fetchObject();
     if (!$result->{$contentCol}) {
         throw new NoSuchViewException("View " . $viewName . " does not exist in database.");
     }
     return $result->{$contentCol};
 }
示例#4
0
 public function render($phpFile)
 {
     $appURL = $this->relative ? Config::getRequiredVal('general', 'app_url') : '';
     while ($appURL && $appURL[strlen($appURL) - 1] === '/') {
         $appURL = substr($appURL, 0, -1);
     }
     $first = true;
     foreach ($this->folders as $folder) {
         if ($first && !$this->relative) {
             $appURL .= $folder;
             $first = false;
         } else {
             if (is_object($folder)) {
                 $appURL .= '/' . PHPFile::PHP_OPENTAG . 'echo urlencode(' . $folder->getVariablePHP($phpFile) . ');' . PHPFile::PHP_CLOSETAG;
             } else {
                 $appURL .= '/' . urlencode($folder);
             }
         }
     }
     if ($this->kvPairs) {
         if (strtolower(substr($appURL, -4)) !== '.php') {
             $appURL .= '/';
         }
         $appURL .= '?';
         foreach ($this->kvPairs as $key => $val) {
             $appURL .= $key . '=';
             if (is_object($val)) {
                 $appURL .= PHPFile::PHP_OPENTAG . 'echo urlencode(' . $val->getVariablePHP($phpFile) . ');' . PHPFile::PHP_CLOSETAG;
             } else {
                 $appURL .= urlencode($val);
             }
             $appURL .= '&';
         }
         $appURL = substr($appURL, 0, -1);
     }
     $phpFile->addPageContent($appURL);
 }
 /**
  * Creates a new instance of DatabaseEngine if the specified connection is new, or returns
  * the stored engine for a connection that an engine's already been instantiated for.  The
  * DatabaseEngine is connected as soon as it is created.
  * 
  * If no dbConfigName is specified, the database configuration from the
  * {@link \hydrogen\config\Config} object is used.  If there are multiple database configurations
  * in the Config object, the first one defined will be used here.
  *
  * If multiple database configurations have been specified in the Config object, the sub-key of
  * the appropriate configuration can be passed in for dbConfigName.  For example:
  * <pre>
  * [database]
  * host[primary] = localhost
  * port[primary] = 3306
  * socket[primary] = 
  * database[primary] = myDB
  * username[primary] = myDBUser
  * password[primary] = myDBPass
  * table_prefix[primary] = myApp_
  *
  * host[backup] = backup.mydomain.com
  * port[backup] = 3306
  * socket[backup] = 
  * database[backup] = backupDB
  * username[backup] = myDBUser
  * password[backup] = myDBPass
  * table_prefix[backup] = myApp_
  * </pre>
  * Using this configuration, this function could be called with either "primary" or "backup"
  * as the dbConfigName argument to get the appropriate DatabaseEngine.  If no dbConfigName
  * is specified, the "primary" sub-key will be used since it appears first in the file.
  *
  * @param string|boolean dbConfigName OPTIONAL: The sub-key of the engine configuration to
  * 		pull from the {@link \hydrogen\config\Config} object.  If false or unspecified, the
  * 		first (or only) database configuration is pulled from Config.
  * @throws hydrogen\database\exceptions\DatabaseConnectionException if a connection could not
  * 		be made.
  * @return DatabaseEngine The requested DatabaseEngine with a connection to the specified
  * 		database server.
  */
 public static function getEngine($dbConfigName = false)
 {
     if ($dbConfigName === false) {
         $engines = Config::getRequiredVal('database', 'engine');
         if (is_array($engines)) {
             $engines = array_keys($engines);
             $dbConfigName = $engines[0];
         }
     }
     return static::getCustomEngine(Config::getRequiredVal('database', 'engine', $dbConfigName), Config::getVal('database', 'host', $dbConfigName), Config::getVal('database', 'port', $dbConfigName), Config::getVal('database', 'socket', $dbConfigName), Config::getVal('database', 'database', $dbConfigName), Config::getVal('database', 'username', $dbConfigName), Config::getVal('database', 'password', $dbConfigName), Config::getVal('database', 'table_prefix', $dbConfigName));
 }
 /**
  * Creates a new instance of DatabaseEngine if the specified connection is new, or returns
  * the stored engine for a connection that an engine's already been instantiated for.  The
  * DatabaseEngine is connected as soon as it is created.
  * 
  * If no dbConfigName is specified, the database configuration from the
  * {@link \hydrogen\config\Config} object is used.  If there are multiple database configurations
  * in the Config object, the first one defined will be used here.
  *
  * If multiple database configurations have been specified in the Config object, the sub-key of
  * the appropriate configuration can be passed in for dbConfigName.  For example:
  * <pre>
  * [database]
  * host[primary] = localhost
  * port[primary] = 3306
  * socket[primary] = 
  * database[primary] = myDB
  * username[primary] = myDBUser
  * password[primary] = myDBPass
  * table_prefix[primary] = myApp_
  *
  * host[backup] = backup.mydomain.com
  * port[backup] = 3306
  * socket[backup] = 
  * database[backup] = backupDB
  * username[backup] = myDBUser
  * password[backup] = myDBPass
  * table_prefix[backup] = myApp_
  * </pre>
  * Using this configuration, this function could be called with either "primary" or "backup"
  * as the dbConfigName argument to get the appropriate DatabaseEngine.  If no dbConfigName
  * is specified, the "primary" sub-key will be used since it appears first in the file.
  *
  * @param string|boolean dbConfigName OPTIONAL: The sub-key of the engine configuration to
  * 		pull from the {@link \hydrogen\config\Config} object.  If false or unspecified, the
  * 		first (or only) database configuration is pulled from Config.
  * @throws hydrogen\database\exceptions\DatabaseConnectionException if a connection could not
  * 		be made.
  * @return DatabaseEngine The requested DatabaseEngine with a connection to the specified
  * 		database server.
  */
 public static function getEngine($dbConfigName = false)
 {
     if ($dbConfigName === false) {
         $engines = Config::getRequiredVal('database', 'engine');
         if (is_array($engines)) {
             $engines = array_keys($engines);
             $dbConfigName = $engines[0];
         }
     }
     try {
         $db = static::getCustomEngine(Config::getRequiredVal('database', 'engine', $dbConfigName), Config::getVal('database', 'host', $dbConfigName), Config::getVal('database', 'port', $dbConfigName), Config::getVal('database', 'socket', $dbConfigName), Config::getVal('database', 'database', $dbConfigName), Config::getVal('database', 'username', $dbConfigName), Config::getVal('database', 'password', $dbConfigName), Config::getVal('database', 'table_prefix', $dbConfigName));
     } catch (DatabaseConnectionException $e) {
         // Re-throw exception to remove DB credentials from the stack trace
         throw new DatabaseConnectionException($e->getMessage());
     }
     return $db;
 }
示例#7
0
 protected function uniqueKey($type, $key)
 {
     return Config::getRequiredVal('recache', 'unique_name') . ':' . $type . ':' . $key;
 }
示例#8
0
 /**
  * Loads and displays the specified view inside of an existing
  * ViewSandbox, respecting the [view]->use_cache setting in the autoconfig
  * to load the specified view from the cache (or write it into the cache
  * if it does not exist there) if appropriate.
  *
  * @param string $viewName The name of the view to load.
  * @param ViewSandbox $sandbox The sandbox into which the view should be
  * 		loaded.
  */
 public static function loadIntoSandbox($viewName, $sandbox)
 {
     if (Config::getRequiredVal('view', 'use_cache')) {
         static::loadCachedIntoSandbox($viewName, $sandbox);
     } else {
         $sandbox->loadRawPHP(static::getViewPHP($viewName));
     }
 }
示例#9
0
 /**
  * Translates a view name into an absolute path at which the view can
  * be found.
  *
  * @param string viewName The name of the view to be found.
  * @return string The absolute path to the requested view.
  */
 public function getViewPath($viewName)
 {
     $path = Config::getRequiredVal("view", "folder") . '/' . $viewName . Config::getRequiredVal("view", "file_extension");
     return Config::getAbsolutePath($path);
 }
示例#10
0
 /**
  * Generates a URL relative to the root view URL of this web application.
  * Calling this function with no arguments returns the root view URL for
  * the currently used view, with no trailing slash.  Calling this function
  * with a path returns the root view URL with the given path appended to it.
  *
  * If the Config value [view]->root_url is set, this will be used as the root
  * view URL.  Otherwise, the Config value [view]->url_path will be appended to
  * the URL stored in [general]->app_url.
  *
  * @param path string|boolean The path to append to the root view URL, or
  * 		false to return the view URL with no additional path.
  * @return string The root URL for this view with the given path appended,
  * 		if provided.
  */
 public function viewURL($path = false)
 {
     if (static::$viewURL === false) {
         static::$viewURL = Config::getVal("view", "root_url");
         if (static::$viewURL === false) {
             static::$viewURL = $this->appURL(Config::getRequiredVal("view", "url_path"));
         }
         if (static::$viewURL[strlen(static::$viewURL) - 1] == '/') {
             static::$viewURL = substr(static::$viewURL, 0, -1);
         }
     }
     if ($path !== false) {
         if ($path[0] == '/') {
             return static::$viewURL . $path;
         }
         return static::$viewURL . '/' . $path;
     }
     return static::$viewURL;
 }
示例#11
0
 /**
  * Gets the table name in which the templates are
  * stored.
  *
  * @return string table name.
  */
 public function getTableName()
 {
     return Config::getRequiredVal("view", "table_name");
 }