Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional params to make() method #12

Merged
merged 6 commits into from
Jun 20, 2020
Merged

Add optional params to make() method #12

merged 6 commits into from
Jun 20, 2020

Conversation

feryardiant
Copy link
Member

@feryardiant feryardiant commented Jun 20, 2020

Add ability to optionally pass additional params to its callback and throws InvalidArgumentException if it was invalid, obviously.

Specs:

  • 2nd param must be an array or closure
    expect(function () {
    $this->c->make(Stubs\SomeClass::class, 'string');
    })->toThrow(new InvalidArgumentException(
    'Expect parameter 2 to be an array or Closure, string given'
    ));
    • If it was an array, treat it as arguments for the callback handler
    • If it was a closure, treat it as condition of method should handled by the callback handler
  • 3rd param must be a closure, condition of method should handled by the callback handler
    expect(
    $this->c->make(Stubs\SomeClass::class, ['new value'], function ($instance) {
    return [$instance, 'shouldCalled'];
    })
    )->toEqual('new value');
  • do nothing if no additional params present
  • no more than 3 params for make()
    expect(function () {
    $this->c->make(Stubs\SomeClass::class, ['string'], 'condition', 'more');
    })->toThrow(new InvalidArgumentException(
    'Could not accept more than 3 arguments, 4 given'
    ));

Signed-off-by: Fery Wardiyanto <ferywardiyanto@gmail.com>
@feryardiant feryardiant added the enhancement New feature or request label Jun 20, 2020
@feryardiant feryardiant self-assigned this Jun 20, 2020
Signed-off-by: Fery Wardiyanto <ferywardiyanto@gmail.com>
Signed-off-by: Fery Wardiyanto <ferywardiyanto@gmail.com>
@feryardiant
Copy link
Member Author

feryardiant commented Jun 20, 2020

Note

The array of second arguments will only be passed to callback method not the class constructor.

$container->make(SomeClass::class, ['value']); // returns 'value'

class SomeClass
{
    // The $param will call $container->get('param')
    public function __construct($param) {}

    // The $param will get 'value'
    public function __invoke($param) {
        return $param
    }
}

$container->make('AnotherClass::theMethod', ['value']); // returns 'value', OR
$container->make(['AnotherClass', 'theMethod'], ['value']); // returns 'value'

class AnotherClass
{
    // The $param will call $container->get('param')
    public function __construct($param) {}

    // The $param will get 'value'
    public function theMethod($param) {
        return $param
    }
}

If the 1st param is not callable or conditionally call a method from 3rd param, the array parameter will be ignored and make method will return the instance of the class.

// The 'value' will be ignored since this class doesn't have `__invoke` method.
$container->make(SomeClass::class, ['value']); // returns instance of SomeClass

class SomeClass
{
    // The $param will remain call $container->get('param')
    public function __construct($param) {}

    public function someMethod($param) {
        return $param
    }
}

Signed-off-by: Fery Wardiyanto <ferywardiyanto@gmail.com>
Signed-off-by: Fery Wardiyanto <ferywardiyanto@gmail.com>
Signed-off-by: Fery Wardiyanto <ferywardiyanto@gmail.com>
@feryardiant
Copy link
Member Author

If the 1st param is a closure, the 2nd param will passed to the callback

expect($this->c->make(function ($param) {
return $param;
}, ['value']))->toEqual('value');

Unless it overrided by the 3rd param

expect($this->c->make(function () {
return new Stubs\SomeClass;
}, ['value'], function ($closure) {
$instance = $closure();
return $instance instanceof Stubs\CertainInterface
? [$instance, 'shouldCalled'] // `shouldCalled` method will get the 'value'
: $closure;
}))->toEqual('value');

@feryardiant feryardiant merged commit b007a9c into master Jun 20, 2020
@feryardiant feryardiant deleted the optional-args branch June 20, 2020 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant