/** * * Creates and returns a new instance of a class using reflection and * the configuration parameters, optionally with overriding params. * * Parameters that are Lazy are invoked before instantiation. * * @param string $class The class to instantiate. * * @param array $params An associative array of override parameters where * the key is the name of the constructor parameter and the value is the * parameter value to use. * * @param array $setters An associative array of override setters where * the key is the name of the setter method to call and the value is the * value to be passed to the setter method. * * @return object * */ public function newInstance($class, array $params = [], array $setters = []) { list($config, $setter) = $this->config->fetch($class); $params = array_merge($config, (array) $params); // lazy-load params as needed foreach ($params as $key => $val) { if ($params[$key] instanceof Lazy) { $params[$key] = $params[$key](); } } // merge the setters $setters = array_merge($setter, $setters); // create the new instance $call = [$this->config->getReflect($class), 'newInstance']; $object = call_user_func_array($call, $params); // call setters after creation foreach ($setters as $method => $value) { // does the specified setter method exist? if (method_exists($object, $method)) { // lazy-load values as needed if ($value instanceof Lazy) { $value = $value(); } // call the setter $object->{$method}($value); } } // done! return $object; }
/** * * Creates and returns a new instance of a class using reflection and * the configuration parameters, optionally with overrides, invoking Lazy * values along the way. * * @param string $class The class to instantiate. * * @param array $merge_params An array of override parameters; the key may * be the name *or* the numeric position of the constructor parameter, and * the value is the parameter value to use. * * @param array $merge_setter An array of override setters; the key is the * name of the setter method to call and the value is the value to be * passed to the setter method. * * @return object * */ public function newInstance($class, array $merge_params = [], array $merge_setter = []) { // base configs list($params, $setter) = $this->config->fetch($class); // merge configs $params = $this->mergeParams($params, $merge_params); $setter = array_merge($setter, $merge_setter); // create the new instance $rclass = $this->config->getReflect($class); $object = $rclass->newInstanceArgs($params); // call setters after creation foreach ($setter as $method => $value) { // does the specified setter method exist? if (method_exists($object, $method)) { // lazy-load setter values as needed if ($value instanceof Lazy) { $value = $value(); } // call the setter $object->{$method}($value); } else { throw new Exception\SetterMethodNotFound("{$class}::{$method}"); } } // done! return $object; }
static function _getSettings() { $settings = array(); $config = new Config(); $config->find(); while ($config->fetch()) { $settings[] = array($config->section, $config->setting, $config->value); } $config->free(); return $settings; }
/** * Get a singleton MangoDB instance. If configuration is not specified, * it will be loaded from the MangoDB configuration file using the same * group as the name. * * // Load the default database * $db = MangoDB::instance(); * * // Create a custom configured instance * $db = MangoDB::instance('custom', $config); * * @param string instance name * @param array configuration parameters * @return Database */ public static function instance($name = NULL, array $config = NULL) { if ($name === NULL) { // Use the default instance name $name = MangoDB::$default; } if (!isset(MangoDB::$instances[$name])) { if ($config === NULL) { // Load the configuration for this database $config = Config::fetch('mangoDB.' . $name); } new MangoDB($name, $config); } return self::$instances[$name]; }
static function _getSettings() { $c = self::memcache(); if (!empty($c)) { $settings = $c->get(common_cache_key(self::settingsKey)); if ($settings !== false) { return $settings; } } $settings = array(); $config = new Config(); $config->find(); while ($config->fetch()) { $settings[] = array($config->section, $config->setting, $config->value); } $config->free(); if (!empty($c)) { $c->set(common_cache_key(self::settingsKey), $settings); } return $settings; }
require_once INSTALLDIR . '/scripts/commandline.inc'; if (empty($args)) { if (have_option('a', 'all')) { foreach ($config as $section => $section_value) { foreach ($section_value as $setting => $value) { if (have_option('v', 'verbose') || !is_array($value)) { // Don't print array's without the verbose flag printf("%-20s %-20s %s\n", $section, $setting, var_export($value, true)); } } } } else { $count = 0; $config = new Config(); $config->find(); while ($config->fetch()) { $count++; printf("%-20s %-20s %s\n", $config->section, $config->setting, var_export($config->value, true)); } if ($count == 0) { print "No configuration set in database for this site.\n"; } } exit(0); } if (count($args) < 2 || count($args) > 3) { show_help(); exit(1); } $section = $args[0]; $setting = $args[1];