Example #1
1
 public function loadConfig()
 {
     $cfg = PathResolver::getInstance()->getPath("{APP}/config/application.sdl");
     if (file_exists($cfg)) {
         $this->debug("WebApplication: Reading %s", $cfg);
         $config = new SdlTag("root");
         $config->loadString(file_get_contents($cfg));
         $this->config = $config;
     } else {
         $this->debug("WebApplication: Could not find %s", $cfg);
     }
 }
Example #2
0
 public function route($uri)
 {
     $fspath = PathResolver::getInstance()->getPath("{PUBLIC}" . $uri);
     if (_IS_CLI_SERVER && is_file($fspath)) {
         $r = new \Cherry\Web\Response();
         return $r->sendFile($fspath);
     }
     // Loop through every route and compare it with the URI
     foreach ($this->routes as $route) {
         // Create a route with all identifiers replaced with ([^/]+) regex syntax
         // E.g. $route_regex = shop-please/([^/]+)/moo (originally shop-please/:some_identifier/moo)
         $route_regex = preg_replace('@:[^/]+@', '([^/]+)', $route[0]);
         // Check if URI path matches regex pattern, if so create an array of values from the URI
         if (!preg_match('@^' . $route_regex . '$@', $uri, $matches)) {
             continue;
         }
         // Create an array of identifiers from the route
         preg_match('@^' . $route_regex . '$@', $route[0], $identifiers);
         // Decode the matches
         $matches = array_map(function ($str) {
             return urldecode($str);
         }, $matches);
         $identifiers = array_map(function ($str) {
             return substr($str, 1);
         }, $identifiers);
         // Combine the identifiers with the values
         $params = array_combine($identifiers, $matches);
         array_shift($params);
         $action = $route->action;
         $this->dispatch($action, $params);
         return 200;
     }
     return 404;
 }
Example #3
0
 public static function bind($identifier, $config, $writeable = false)
 {
     // If the file name contains a path, resolve it via pathresolver,
     // otherwise try to detect the location of the configuration file.
     if (strpos($config, "/") !== false) {
         $cfgpath = PathResolver::path($config);
     } else {
         $test = PathResolver::path("{APP}/{$config}");
         if (file_exists($test)) {
             $cfgpath = $test;
         } else {
             $test = PathResolver::path("{APP}/config/{$config}");
             if (file_exists($test)) {
                 $cfgpath = $test;
             }
         }
     }
     $cfghash = sha1($cfgpath);
     if (array_key_exists($identifier, self::$pools)) {
         // Key exists, make sure it's the same config file
         if (self::$pools[$identifier]->cfgpath == $cfgpath) {
             // Already set up, so update flags and return.
             self::$pools[$identifier]->writeable = $writeable;
             self::debug("Pool already bound. Updating flags for '{$identifier}");
             return true;
         }
     }
     self::debug("Binding pool '{$identifier}' to '{$cfgpath}'");
     $poolinfo = (object) ['id' => $identifier, 'cfgpath' => $cfgpath, 'cfghash' => $cfghash, 'writeable' => $writeable];
     self::$pools[$identifier] = $poolinfo;
 }
Example #4
0
 function run()
 {
     $ks = KeyStore::getInstance();
     $pr = PathResolver::getInstance();
     $ks_user = $pr->getPath("{USER}/user.cks");
     $ks_system = $pr->getPath("{SYSTEM}/system.cks");
     if (file_exists($ks_user)) {
         $key = getenv("KEYSTORE_USERKEY");
         if ($key) {
             $ks->attachFile($ks_user, $key);
             putenv("KEYSTORE_USERKEY");
         } else {
             $this->debug("Info: To auto-mount the user KeyStore, define the KEYSTORE_USERKEY envvar.");
         }
     } else {
         $this->debug("Keystore {$ks_user} not found.");
     }
     if (file_exists($ks_system)) {
         $key = getenv("KEYSTORE_SYSTEMKEY");
         if ($key) {
             $ks->attachFile($ks_system, $key);
             putenv("KEYSTORE_SYSTEMKEY");
         } else {
             $this->debug("Info: To auto-mount the system KeyStore, define the KEYSTORE_SYSTEMKEY envvar.");
         }
     } else {
         $this->debug("Keystore {$ks_system} not found.");
     }
     return $this->main();
 }
Example #5
0
 public function __construct($store = null, $key = null)
 {
     if (!$store) {
         $store = PathResolver::path("{DATA}/default.cks");
     }
     if (file_exists($store)) {
         $this->attachFile($store, $key);
     }
 }
Example #6
0
 public static function bindPool($config, $identifier, $writeable = false)
 {
     \utils::deprecated(__CLASS__ . "::bindPool", "\\Cherry\\Core\\ConfigManager::bind");
     $cfgpath = PathResolver::path($config);
     $cfghash = sha1($cfgpath);
     if (array_key_exists($identifier, self::$pools)) {
         // Key exists, make sure it's the same config file
         if (self::$pools[$identifier]->cfgpath == $cfgpath) {
             // Already set up, so update flags and return.
             self::$pools[$identifier]->writeable = $writeable;
             self::debug("Pool already bound. Updating flags for '{$identifier}");
             return true;
         }
     }
     self::debug("Binding pool '{$identifier}' to '{$cfgpath}'");
     $poolinfo = (object) ['id' => $identifier, 'cfgpath' => $cfgpath, 'cfghash' => $cfghash, 'writeable' => $writeable];
     self::$pools[$identifier] = $poolinfo;
 }
Example #7
0
function p($path)
{
    return \Cherry\Core\PathResolver::path($path);
}
Example #8
0
 static function getInstance($pool = null)
 {
     if (!$pool) {
         $pool = self::POOL_DEFAULT;
     }
     if (!self::$connections) {
         $cp = PathResolver::path("{APP}/config/database.sdl");
         if (file_exists($cp)) {
             $cfg = SdlTag::createFromFile($cp);
             foreach ($cfg->query("database/connection") as $connection) {
                 $cpool = $connection[0];
                 $curi = $connection[1];
                 if (empty(self::$connections[$cpool])) {
                     self::$connections[$cpool] = [];
                 }
                 self::sdebug("Database: Registered connection '{$curi}' to pool {$cpool}");
                 self::$connections[$cpool][] = $connection;
             }
         }
     }
     if (!self::$connections) {
         self::$connections = [];
     }
     if (!array_key_exists($pool, self::$dbpool)) {
         if (array_key_exists($pool, self::$connections)) {
             $pc = self::$connections[$pool];
             if (is_array($pc)) {
                 $dp = $pc[0][1];
             } else {
                 $dp = $pc[1];
             }
             //var_dump($pool);
             //die();
             //self::$dbpool[$pool] = new self(self::$connections[$pool]);
             $opts = ["pool" => $pool];
             self::$dbpool[$pool] = new self($dp, $opts);
         } else {
             if (strpos($pool, "://") !== false) {
                 self::$dbpool[$pool] = new self($pool);
             } else {
                 throw new \UnexpectedValueException("Unable to connect to pool {$pool}");
             }
         }
     }
     return self::$dbpool[$pool];
 }