Kahlan\Box\Wrapper::get PHP Method

get() public method

Resolve the dependency.
public get ( ) : mixed
return mixed The shared variable or an new instance.
    public function get()
    {
        if ($this->__resolved) {
            return $this->__dependency;
        }
        $this->__resolved = true;
        $params = func_num_args() === 0 ? $this->__params : func_get_args();
        array_unshift($params, $this->__name);
        return $this->__dependency = call_user_func_array([$this->__box, 'get'], $params);
    }

Usage Example

Beispiel #1
0
            });
            $wrapper = new Wrapper(['box' => $this->box, 'name' => 'spec.stdClass']);
            $dependency = $wrapper->get();
            expect($dependency)->toBeAnInstanceOf("stdClass");
            expect($wrapper->get())->toBe($dependency);
        });
        it("throws an exception if the dependency doesn't exists", function () {
            $wrapper = new Wrapper(['box' => $this->box, 'name' => 'spec.stdUnexistingClass']);
            expect(function () use($wrapper) {
                $wrapper->get();
            })->toThrow(new BoxException());
        });
        it("passes parameters to the Closure", function () {
            $this->box->factory('spec.arguments', function () {
                return func_get_args();
            });
            $params = ['param1', 'param2'];
            $wrapper = new Wrapper(['box' => $this->box, 'name' => 'spec.arguments', 'params' => $params]);
            expect($wrapper->get())->toBe($params);
        });
        it("override passed parameters to the Closure", function () {
            $this->box->factory('spec.arguments', function () {
                return func_get_args();
            });
            $params = ['param1', 'param2'];
            $wrapper = new Wrapper(['box' => $this->box, 'name' => 'spec.arguments', 'params' => $params]);
            $overrided = ['param3', 'param4'];
            expect($wrapper->get('param3', 'param4'))->toBe($overrided);
        });
    });
});