Return an instance of the given class name. Cache the created instance so
the same one is returned on multiple calls.

@param string $class
@return mixed
@throws \Ellipse\Container\Exceptions\ReflectionContainerException
11661
private function make(string $class)
    {
        if (! array_key_exists($class, $this->instances)) {

            try {

                return $this->instances[$class] = ($this->factory)($class)->value($this);

            }

            catch (ResolvingExceptionInterface $e) {

                throw new ReflectionContainerException($class, $e);

            }

        }

        return $this->instances[$class];
    }
949825
public function resolveClass(string $class)
    {
        $object = $this->getClass($class);

        $definitions = $this->container->isDefined($object->getName())
            ? $this->container->getDefiniition($object->getName()) : null;

        $constructor_parameters = $object->getConstructor() !== null ? $object->getConstructor()->getParameters() : null;
        $parameters = $this->resolveParameters($constructor_parameters ?? [], $definitions);

        return $object->newInstanceArgs($parameters);
    }
1250884
protected static function getSingleton($class = null)
    {
        //super magic sauce getting the callers class
        if (is_null($class) && function_exists('get_called_class')) {
            $class = get_called_class();
        }
        
        //get routed class, if any
        $class = Route::i()->get($class);
        
        //if it's not set
        if (!isset(self::$instances[$class])) {
            //set it
            self::$instances[$class] = self::getInstance($class);
        }
        
        //return the cached version
        return self::$instances[$class];
    }
1344218
private function createServiceFactoryFromClass(string $class): Closure
    {
        $reflection = new ReflectionClass($class);
        if (!$reflection->isInstantiable()) {
            throw new UninstantiableServiceException($class, $this->resolving);
        }
        $constructor = $reflection->getConstructor();

        if ($constructor && $constructor->getNumberOfParameters() > 0) {
            if ($constructor->getDeclaringClass() !== $class) {
                $invokable = new Invokable([$class, '__construct']);
            } else {
                $invokable = new Invokable($constructor);
            }

            return function (array $arguments = []) use ($invokable) {
                return $this->invoker->invoke($invokable, $arguments);
            };
        }

        return function () use ($class) {
            return new $class();
        };
    }
1054279
public function load(ReflectionClass $class)
    {
        $this->reset();

        $this->reflectionClass = $class;
        $this->name            = $this->reflectionClass->getName();

        if (!$this->reflectionClass->isInstantiable()) {
            return $this;
        }

        $this->interfaces            = $this->resolveInterfaces();
        $this->parent                = $this->resolveParent();
        $this->reflectionConstructor = $this->reflectionClass->getConstructor();

        if (is_null($this->reflectionConstructor)) {
            return $this;
        }

        $params = $this->reflectionConstructor->getParameters();

        if (empty($params)) {
            return $this;
        }

        /** @var $param \ReflectionParameter */
        foreach ($params as $param) {
            $paramName                = $param->getName();
            $this->params[$paramName] = $this->resolveArgumentValue($param);
            $this->paramNames[]       = $paramName;
        }

        $this->constructorParams = $params;

        return $this;
    }
790922
private function getService(string $class): ServiceInterface
    {
        if (!array_key_exists($class, $this->services)) {
            $this->services[$class] = new $class($this->client, $this->modelFactory);
        }

        return $this->services[$class];
    }
777376
public function getConstructor($class)
    {
        $cacheKey = self::CACHE_KEY_CTORS . strtolower($class);

        $reflectedConstructor = $this->cache->fetch($cacheKey);

        if ($reflectedConstructor === false) {
            $reflectionClass      = $this->getClass($class);
            $reflectedConstructor = $reflectionClass->getConstructor();
            $this->cache->store($cacheKey, $reflectedConstructor);
        }

        return $reflectedConstructor;
    }
175730
public function resolveClassFromCache(string $class)
    {
        if($this->cache->has($class)) {
            return $this->cache->get($class);
        }

        $resolved = $this->resolveClass($class);

        //cache class
        $this->store($class, $resolved);

        return $resolved;
    }
970106
private static function getInstance($class)
    {
        //get the backtrace
        $trace = debug_backtrace();
        $args = array();
        
        //the 2nd line is the caller method
        if (isset($trace[1]['args']) && count($trace[1]['args']) > 1) {
            //get the args
            $args = $trace[1]['args'];
            //shift out the class name
            array_shift($args);
            //then maybe it's the 3rd line?
        } else if (isset($trace[2]['args']) && count($trace[2]['args']) > 0) {
            //get the args
            $args = $trace[2]['args'];
        }
        
        //if there's no args or there's no construct to accept the args
        if (count($args) === 0 || !method_exists($class, '__construct')) {
            //just return the instantiation
            return new $class;
        }
        
        //at this point, we need to vitually call the class
        $reflect = new \ReflectionClass($class);
        
        try { //to return the instantiation
            return $reflect->newInstanceArgs($args);
        } catch (\ReflectionException $e) {
            //trigger error
            Exception::i()
                ->setMessage(self::ERROR_REFLECTION)
                ->addVariable($class)
                ->addVariable('new')
                ->trigger();
        }
    }
1113262
protected function getInstance(ReflectionClass $parameterClass)
    {
        if (!class_exists($parameterClass->name, true)) {
            return null;
        }
        
        if (strpos($parameterClass->name, 'Zepi\\Turbo\\') === 0) {
            return $this->getFrameworkInstance($parameterClass);
        }
        
        if ($parameterClass->isInstantiable()) {
            return $this->framework->getInstance($parameterClass->name);
        }
    }
549543
public function getObjectInstance($class)
  {
    // Eventbasierte Objekt-Erzeugung
    $event = new ClassBuilderEvent($class);

    // $class enthält bereits ein Objekt
    if ($event->isObject() === true)
    {
      // Vor der Rückgabe an die EventListener/EventSubscriber geben...
      $this->dispatcher->dispatch(TransformerEvents::OBJECT_RETURN_INSTANCE, $event);

      return $event->getObject();
    }

    // $class enthält noch kein Objekt
    if (class_exists($event->getObject()))
    {
      $this->dispatcher->dispatch(TransformerEvents::OBJECT_CREATE_INSTANCE, $event);

      // Wenn in keinem EventListener/EventSubscriber die Instanz erzeugt wurde...
      if ($event->isObject() === false)
      {
        $reflection = new \ReflectionClass($class);

        return $reflection->newInstanceWithoutConstructor();
      }

      // Wenn in einem EventListener/EventSubscriber die Instanz erzeugt wurde...
      return $event->getObject();
    }
    throw new TransformerException(sprintf('Class %s does not exist.', $class));
  }
497103
private function getModel($class)
    {
        $reflectionClass = new \ReflectionClass($class);
        $instance = $reflectionClass->newInstance();
        if (!($instance instanceof ModelInterface)) {
            throw new \Exception('This class is not supported');
        }

        return $instance;
    }
614441
private function getCacheFile(\ReflectionClass $reflectionClass)
    {
        return sprintf('%s/%s/%s.php.cache', $this->kernel->getCacheDir(), Constants::CACHE_DIR, str_replace('\\', '_', $reflectionClass->getName()));
    }
1153892
public function createInstance(string $class)
    {
        $refClass = new \ReflectionClass($class);
        $args = [];
        if ($refClass->getConstructor()) {
            foreach ($refClass->getConstructor()->getParameters() as $parameter) {
                $name = Inflector::tableize(str_replace('_', '.', $parameter->getName()));
                $dependency = null;
                if ($parameter->getClass()) {
                    if ($this->container->has($parameter->getClass()->getName())) {
                        $dependency = $this->container->get($parameter->getClass()->getName());
                    } elseif ($this->container->has($name)) {
                        $dependency = $this->container->get($name);
                    }
                } elseif ($this->container->hasParameter($name)) {
                    $dependency = $this->container->getParameter($name);
                }
                $args[] = $dependency;
            }
        }

        return $refClass->newInstanceArgs($args);
    }
592662
final protected static function getInstance($class, ...$params) {
        if (isset(self::$instances[$class]))
            return self::$instances[$class];

        // set a marker to detect recursive method invocations
        static $currentCreations;
        if (isset($currentCreations[$class])) throw new RuntimeException('Detected recursive call of '.__METHOD__.'('.$class.')');
        $currentCreations[$class] = true;

        // check validity of the passed class (omitting this check can cause an uncatchable fatal error)
        if (!is_a($class, __CLASS__, $allowString=true)) {
            if (!is_class($class)) throw new ClassNotFoundException('Class not found: '.$class );
            throw new InvalidArgumentException('Not a '.__CLASS__.' subclass: '.$class);
        }

        // instantiate the class with the passed parameters
        self::$instances[$class] = new $class(...$params);

        // reset the marker for detecting recursive method invocations
        unset($currentCreations[$class]);

        return self::$instances[$class];
    }
141023
public function getNewInstance($class) {
		if (isset($this->aliases[$class])) {
			$class = $this->aliases[$class];
		}

		$args = $this->resolveArgs($class, '__construct', " when constructing $class");
		if ($args) {
			$instance = new $class(...$args);
		} else {
			$instance = new $class;
		}

		return $instance;
	}
1051293
public function getMigration($class): MigrationInterface
    {
        try {
            $migration = $this->container->get($class);
            if (!$migration || !$migration instanceof MigrationInterface) {
                throw new \RuntimeException('Migration must implement ' . MigrationInterface::class);
            }
        } catch (\Exception $e) {
            throw new RuntimeException('Migration instantiate error: ' . $class, 0, $e);
        }
        return $migration;
    }