Beispiel #1
0
 /**
  * Reads mapping definition of selected data set into runtime and prepares
  * it for improved mapping operation.
  *
  * @param string $setName name of data set to be mapped
  * @param boolean $forward true to get forward mapping, false to get reverse mapping
  * @return array mapping of names for selected data set in desired direction
  * @throws \InvalidArgumentException on using invalid name data set
  */
 protected static function getDefinition($setName, $forward)
 {
     if (!array_key_exists($setName, static::$_mappings)) {
         if (!preg_match('/^([a-z][a-z0-9_]+)(\\.([a-z][a-z0-9_]+))*$/i', $setName)) {
             throw new \InvalidArgumentException('invalid set name');
         }
         static::$_mappings[$setName] = array('forward' => array(), 'backward' => array());
         foreach (config::getList('mappings.' . $setName . '.map') as $def) {
             if (is_array($def)) {
                 $from = trim(@$def['from']);
                 $to = trim(@$def['to']);
                 $drop = !!@$def['drop'] || $to === '';
                 static::$_mappings[$setName]['forward'][$from] = $drop ? null : $to;
                 if ($drop) {
                     static::$_mappings[$setName]['backward'][$to] = $from;
                 }
             }
         }
         unset($definition);
     }
     return $forward ? static::$_mappings[$setName]['forward'] : static::$_mappings[$setName]['backward'];
 }
Beispiel #2
0
 /**
  * Iterates over given or configured list of user database providers invoking
  * provided callback on every provider until first callback is returning
  * properly without throwing exception.
  *
  * @param callable $callback callback to invoke per iterated user database provider
  * @param array|string $explicitSource set of provider names to iterate or name of first provider in a sequence to test
  * @return mixed|null result return from callback, null if all callbacks threw exception
  * @throws \RuntimeException on missing valid set of providers to iterate
  * @throws unauthorized_exception if callback is throwing in other case than "user isn't found"
  * @throws \InvalidArgumentException on missing callback
  */
 public static final function findProvider($callback, $explicitSource = null)
 {
     if (!is_callable($callback)) {
         throw new \InvalidArgumentException('invalid callback on finding user provider');
     }
     // get list of sources to look up for requested user
     if (is_array($explicitSource)) {
         $sources = $explicitSource;
     } else {
         $sources = func_get_args();
         array_shift($sources);
     }
     if (!count($sources)) {
         $sources = config::getList('user.sources.enabled');
     }
     if (!count($sources)) {
         throw new \RuntimeException('missing/invalid user sources configuration');
     }
     // traverse list of sources ...
     foreach ($sources as $source) {
         if (trim($source) !== '' && ctype_alnum($source)) {
             // read its configuration
             $definition = config::get('user.sources.setup.' . $source);
             if (is_array($definition)) {
                 // read name of class for managing user source from configuration
                 $class = array_key_exists('class', $definition) ? data::isKeyword($definition['class']) : null;
                 if (!$class) {
                     $class = data::isKeyword($definition['type'] . '_user');
                 }
                 if (txf::import($class)) {
                     try {
                         // create instance of managing class
                         $factory = new \ReflectionClass($class);
                         $user = $factory->newInstance();
                         if ($user instanceof self) {
                             $user->configure($definition);
                             return call_user_func($callback, $user);
                         }
                     } catch (unauthorized_exception $e) {
                         if (!$e->isUserNotFound()) {
                             throw $e;
                         }
                     } catch (\Exception $e) {
                         log::warning('failed to search user source: ' . $e);
                     }
                 }
             }
         }
     }
     return null;
 }
Beispiel #3
0
 /**
  * Renders pager widget.
  *
  * @return string code describing pager widget
  */
 public function getCode()
 {
     if (!$this->isEnabled()) {
         return '';
     }
     $size = $this->size();
     $offset = $this->offset();
     // compile data to use on rendering pager
     $setup = array('sizeName' => $this->sizeName, 'offsetName' => $this->offsetName, 'itemCount' => $this->itemCount, 'pageOffsets' => array(), 'sizes' => config::getList('pager.size.option', array(10, 20, 50, 100)), 'size' => $size, 'offset' => $offset, 'useButtons' => $this->useButtons);
     for ($i = $offset; $i > -$size; $i -= $size) {
         array_unshift($setup['pageOffsets'], max(0, $i));
     }
     $setup['currentPage'] = count($setup['pageOffsets']) - 1;
     for ($i = $offset + $size; $i < $this->itemCount; $i += $size) {
         array_push($setup['pageOffsets'], min($this->itemCount - ($this->showFullFinalPage() ? $size : 1), $i));
     }
     // render pager using template
     return view::render('widgets/pager', $setup);
 }
Beispiel #4
0
 /**
  * Initializes support for class redirections by reading initial map from
  * runtime configuration.
  */
 protected function initializeClassRedirections()
 {
     $initialRedirections = config::getList('txf.autoloader.redirect');
     if (is_array($initialRedirections)) {
         foreach ($initialRedirections as $redirection) {
             try {
                 $this->redirectClass($redirection['source'], $redirection['target']);
             } catch (\InvalidArgumentException $e) {
                 trigger_error(sprintf('invalid class redirection %s -> %s ignored', $redirection['source'], $redirection['target']), E_USER_WARNING);
             }
         }
     }
 }
Beispiel #5
0
 /**
  * Ensures to read set of connection definitions from configuration.
  *
  * This method is prefetching all named datasource definitions from current
  * configuration. A named datasource link (here called "customlink") is
  * defined like this:
  *
  * <datasource>
  *  <link>
  *   <id>customlink</id>
  *   <dsn>DSN of my datasource</dsn>
  *   <user>myloginname</user>
  *   <password>mysecretpassword</password>
  *  </link>
  * </datasource>
  */
 protected static function getLinks()
 {
     if (!is_array(self::$definitions)) {
         self::$definitions = array();
         // load all properly named datasource configurations
         foreach (config::getList('datasource.link') as $link) {
             if (is_array($link) && trim(@$link['id']) !== '') {
                 self::$definitions[trim($link['id'])] = $link;
             }
         }
     }
     return self::$definitions;
 }