src/Container/Container.php
public function register(string $name, $define = null, array $map = []): void
    {
        if (isset($this->active[$name])) {
            throw new InvalidArgumentException("Attempted override of existing component: {$name}");
        }

        if ($define instanceof \Closure) {
            $func = $define;
        } elseif (\is_callable($define)) {
            // second argument is a creation function
            $func = \Closure::fromCallable($define);
        } elseif (\is_string($define)) {
            // second argument is a class-name
            $func = $define;
        } elseif (\is_array($define)) {
            $func = $name;
            $map = $define;
        } elseif (null === $define) {
            // first argument is both the component and class-name
            $func = $name;
            $map = [];
        } else {
            throw new InvalidArgumentException('Unexpected argument type for $define: ' . \gettype($define));
        }

        $this->factory[$name] = $func;
        $this->factoryMap[$name] = $map;

        unset($this->values[$name]);
    }
public function register(string $name, $define = null, array $map = []): void
    {
        if (isset($this->active[$name])) {
            throw new InvalidArgumentException("Attempted override of existing component: {$name}");
        }

        if ($define instanceof \Closure) {
            $func = $define;
        } elseif (\is_callable($define)) {
            // second argument is a creation function
            $func = \Closure::fromCallable($define);
        } elseif (\is_string($define)) {
            // second argument is a class-name
            $func = $define;
        } elseif (\is_array($define)) {
            $func = $name;
            $map = $define;
        } elseif (null === $define) {
            // first argument is both the component and class-name
            $func = $name;
            $map = [];
        } else {
            throw new InvalidArgumentException('Unexpected argument type for $define: ' . \gettype($define));
        }

        $this->factory[$name] = $func;
        $this->factoryMap[$name] = $map;

        unset($this->values[$name]);
    }
public function register ( string $ name , $ define = null , array $ map = [ ] ) : void { if ( isset ( $ this -> active [ $ name ] ) ) { throw new InvalidArgumentException ( "Attempted override of existing component: {$name}" ) ; } if ( $ define instanceof \ Closure ) { $ func = $ define ; } elseif ( \ is_callable ( $ define ) ) { // second argument is a creation function $ func = \ Closure :: fromCallable ( $ define ) ; } elseif ( \ is_string ( $ define ) ) { // second argument is a class-name $ func = $ define ; } elseif ( \ is_array ( $ define ) ) { $ func = $ name ; $ map = $ define ; } elseif ( null === $ define ) { // first argument is both the component and class-name $ func = $ name ; $ map = [ ] ; } else { throw new InvalidArgumentException ( 'Unexpected argument type for $define: ' . \ gettype ( $ define ) ) ; } $ this -> factory [ $ name ] = $ func ; $ this -> factoryMap [ $ name ] = $ map ; unset ( $ this -> values [ $ name ] ) ; }
Register a component for dependency injection.

There are numerous valid ways to register components.

* `register(Foo::class)` registers a component by it's class-name, and will try to
automatically resolve all of it's constructor arguments.

* `register(Foo::class, ['bar'])` registers a component by it's class-name, and will
use `'bar'` as the first constructor argument, and try to resolve the rest.

* `register(Foo::class, [$container->ref(Bar::class)])` creates a boxed reference to
a registered component `Bar` and provides that as the first argument.

* `register(Foo::class, ['bat' => 'zap'])` registers a component by it's class-name
and will use `'zap'` for the constructor argument named `$bat`, and try to resolve
any other arguments.

* `register(Bar::class, Foo::class)` registers a component `Foo` under another name
`Bar`, which might be an interface or an abstract class.

* `register(Bar::class, Foo::class, ['bar'])` same as above, but uses `'bar'` as the
first argument.

* `register(Bar::class, Foo::class, ['bat' => 'zap'])` same as above, but, well, guess.

* `register(Bar::class, function (Foo $foo) { return new Bar(...); })` registers a
component with a custom creation function.

* `register(Bar::class, function ($name) { ... }, [$container->ref('db.name')]);`
registers a component creation function with a reference to a component "db.name"
as the first argument.

In effect, you can think of `$func` as being an optional argument.

The provided parameter values may include any `\Closure`, such as the boxed
component referenced created by {@see Container::ref()} - these will be unboxed as late
as possible.

@param string                      $name                component name
@param callable|mixed|mixed[]|null $define              creation function or class-name, or, if the first
argument is a class-name, a map of constructor arguments
@param array                       $map                 mixed list/map of parameter values (and/or boxed values)

@return void

@throws InvalidArgumentException
Register a component for dependency injection .