Ejemplo n.º 1
0
 function &_singleton(&$caller)
 {
     global $CFG;
     static $instances;
     static $self;
     $plugin = NULL;
     $prefix = NULL;
     $name = NULL;
     $params = NULL;
     $filefilter = NULL;
     $public = false;
     $shared = false;
     $synchronize = false;
     $id = NULL;
     if ($caller != NULL) {
         $plugin = $caller->plugin;
         $prefix = $caller->prefix;
         $name = $caller->name;
         $params = $caller->params;
         $filefilter = $caller->filefilter;
         $public = $caller->public;
         $shared = $caller->shared;
         $synchronize = $caller->synchronize;
         $id = $caller->id;
     } else {
         $caller = new object();
     }
     if ($plugin == NULL && $prefix == NULL) {
         if (!is_object($self)) {
             $self = new podcaster_repositoryimpl('NULL', 'NULL');
         }
         return $self;
     }
     if (!is_array($instances)) {
         $instances = array();
     }
     if (!array_key_exists($prefix, $instances)) {
         include_once $CFG->dirroot . '/mod/podcaster/repository/' . $plugin . '/lib.php';
         $classname = $plugin . '_repository';
         if (!class_exists($classname)) {
             error('Unable to load plugin "' . $plugin . '" (required class: ' . $classname . ', expected in repository/' . $plugin . '/lib.php)');
         }
         $instances[$prefix] = NULL;
         $instances[$prefix] = new $classname($plugin, $prefix);
         if (is_object($instances[$prefix])) {
             foreach (explode("\r", $params) as $p) {
                 if (($lp = strpos($p, '=')) !== false) {
                     $lvalue = trim(substr($p, 0, $lp));
                     $rvalue = trim(substr($p, $lp + 1));
                     $instances[$prefix]->params[$lvalue] = $rvalue;
                 }
             }
             $instances[$prefix]->id = $id;
             $instances[$prefix]->name = $name;
             $instances[$prefix]->filefilter = $filefilter;
             $instances[$prefix]->prefix = $prefix;
             $instances[$prefix]->public = $public;
             $instances[$prefix]->shared = $shared;
             $instances[$prefix]->synchronize = $synchronize;
             $allowedTypes = explode(',', $filefilter);
             $instances[$prefix]->allowed_types = array();
             foreach ($allowedTypes as $t) {
                 $instances[$prefix]->allowed_types[strtolower(trim($t))] = true;
             }
             $class = get_class($instances[$prefix]);
             stream_wrapper_register($prefix, $class);
             // we need the reverse mapping too
             podcaster_repositoryimpl::add_plugin($plugin, $prefix);
         }
     }
     return $instances[$prefix];
 }