Cake\ElasticSearch\TypeRegistry::get PHP Method

get() public static method

When getting an instance, if it does not already exist, a new instance will be created using the provide alias, and options.
public static get ( string $alias, array $options = [] ) : Type
$alias string The name of the alias to get.
$options array Configuration options for the type constructor.
return Type
    public static function get($alias, array $options = [])
    {
        if (isset(static::$instances[$alias])) {
            if (!empty($options) && static::$options[$alias] !== $options) {
                throw new RuntimeException(sprintf('You cannot configure "%s", it already exists in the registry.', $alias));
            }
            return static::$instances[$alias];
        }
        static::$options[$alias] = $options;
        list(, $classAlias) = pluginSplit($alias);
        $options = $options + ['name' => Inflector::underscore($classAlias)];
        if (empty($options['className'])) {
            $options['className'] = Inflector::camelize($alias);
        }
        $className = App::className($options['className'], 'Model/Type', 'Type');
        if ($className) {
            $options['className'] = $className;
        } else {
            if (!isset($options['name']) && strpos($options['className'], '\\') === false) {
                list(, $name) = pluginSplit($options['className']);
                $options['name'] = Inflector::underscore($name);
            }
            $options['className'] = 'Cake\\ElasticSearch\\Type';
        }
        if (empty($options['connection'])) {
            $connectionName = $options['className']::defaultConnectionName();
            $options['connection'] = ConnectionManager::get($connectionName);
        }
        static::$instances[$alias] = new $options['className']($options);
        return static::$instances[$alias];
    }

Usage Example

 /**
  * Prepare some additional data from the context.
  *
  * If the table option was provided to the constructor and it
  * was a string, TypeRegistry will be used to get the correct table instance.
  *
  * If an object is provided as the type option, it will be used as is.
  *
  * If no type option is provided, the type name will be derived based on
  * naming conventions. This inference will work with a number of common objects
  * like arrays, Collection objects and ResultSets.
  *
  * @return void
  * @throws \RuntimeException When a table object cannot be located/inferred.
  */
 protected function _prepare()
 {
     $type = $this->_context['type'];
     $entity = $this->_context['entity'];
     if (empty($type)) {
         if (is_array($entity) || $entity instanceof Traversable) {
             $entity = (new Collection($entity))->first();
         }
         $isDocument = $entity instanceof Document;
         if ($isDocument) {
             $type = $entity->source();
         }
         if (!$type && $isDocument && get_class($entity) !== 'Cake\\ElasticSearch\\Document') {
             list(, $entityClass) = namespaceSplit(get_class($entity));
             $type = Inflector::pluralize($entityClass);
         }
     }
     if (is_string($type)) {
         $type = TypeRegistry::get($type);
     }
     if (!is_object($type)) {
         throw new RuntimeException('Unable to find type class for current entity');
     }
     $this->_isCollection = is_array($entity) || $entity instanceof Traversable;
     $alias = $this->_rootName = $type->name();
     $this->_context['type'] = $type;
 }
All Usage Examples Of Cake\ElasticSearch\TypeRegistry::get