DI\Container::make PHP Method

make() public method

This method behave like get() except it forces the scope to "prototype", which means the definition of the entry will be re-evaluated each time. For example, if the entry is a class, then a new instance will be created each time. This method makes the container behave like a factory.
public make ( string $name, array $parameters = [] ) : mixed
$name string Entry name or a class name.
$parameters array Optional parameters to use to build the entry. Use this to force specific parameters to specific values. Parameters not defined in this array will be resolved using the container.
return mixed
    public function make($name, array $parameters = [])
    {
        if (!is_string($name)) {
            throw new InvalidArgumentException(sprintf('The name parameter must be of type string, %s given', is_object($name) ? get_class($name) : gettype($name)));
        }
        $definition = $this->definitionSource->getDefinition($name);
        if (!$definition) {
            // Try to find the entry in the singleton map
            if (array_key_exists($name, $this->singletonEntries)) {
                return $this->singletonEntries[$name];
            }
            throw new NotFoundException("No entry or class found for '{$name}'");
        }
        return $this->resolveDefinition($definition, $parameters);
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Widget constructor.
  *
  * @param string $widgetClassNameOrAlias
  * @param Container $container
  */
 public function __construct($widgetClassNameOrAlias, Container $container)
 {
     $instance = $container->make($widgetClassNameOrAlias, ['wpWidget' => $this]);
     if (!in_array(WidgetInterface::class, class_implements($instance))) {
         throw new RuntimeException("Incorrect widget class name or widget does not implement WidgetInterface");
     }
     /**
      * Check if instance have listeners for wordpress
      * events which should be registered while
      * widget registering.
      */
     if ($instance instanceof ActionInterface || $instance instanceof DataFilterInterface) {
         $container->get('EventManager')->attachListeners($instance);
     }
     $this->getInstance = function () use($instance) {
         return $instance;
     };
     $params = $instance->getParams();
     $controlOption = [];
     if ($params instanceof ControlableInterface) {
         $controlOption = $params->getControlOptions();
     }
     parent::__construct($params->getId(), $params->getName(), $params->getOptions(), $controlOption);
 }
All Usage Examples Of DI\Container::make