Telegram\Bot\Api::getContainer PHP Method

getContainer() public method

Get the IoC Container.
public getContainer ( ) : Illuminate\Contracts\Container\Container
return Illuminate\Contracts\Container\Container
    public function getContainer()
    {
        return self::$container;
    }

Usage Example

Example #1
0
 /**
  * Use PHP Reflection and Laravel Container to instantiate the answer with type hinted dependencies.
  *
  * @param $answerClass
  *
  * @return object
  */
 protected function buildDependencyInjectedAnswer($answerClass)
 {
     // check if the command has a constructor
     if (!method_exists($answerClass, '__construct')) {
         return new $answerClass();
     }
     // get constructor params
     $constructorReflector = new \ReflectionMethod($answerClass, '__construct');
     $params = $constructorReflector->getParameters();
     // if no params are needed proceed with normal instantiation
     if (empty($params)) {
         return new $answerClass();
     }
     // otherwise fetch each dependency out of the container
     $container = $this->telegram->getContainer();
     $dependencies = [];
     foreach ($params as $param) {
         $dependencies[] = $container->make($param->getClass()->name);
     }
     // and instantiate the object with dependencies through ReflectionClass
     $classReflector = new \ReflectionClass($answerClass);
     return $classReflector->newInstanceArgs($dependencies);
 }