Skip to content

Allow for strategies to be objects or callables. #74

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

Closed
wants to merge 1 commit into from

Conversation

Nyholm
Copy link
Member

@Nyholm Nyholm commented Jul 14, 2016

This is just an idea. Im not sure if we want to do this or not but I would like to hear your opinions.

Do we want to allow that strategies are objects or callables? This might be overkill when we allow candidates to be callables.

Also, the DiscoveryStrategy interface says that getCandidates should be static. But calling it in a non static way is okey. https://3v4l.org/IbRNo

$candidates = $strategy($type);
} elseif ($strategy instanceof DiscoveryStrategy) {
$candidates = $strategy->getCandidates($type);
} elseif (is_string($strategy)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we check an instance of DiscoveryStrategy too?

@dbu
Copy link
Contributor

dbu commented Jul 14, 2016 via email

@Nyholm
Copy link
Member Author

Nyholm commented Jul 14, 2016

The reason why I've been back and forth with this is because Im not 100% happy with this: php-http/HttplugBundle#85 (comment)

But Im not sure this PR will create a possibility to make php-http/HttplugBundle#85 better. I know this is very vague..

@Nyholm
Copy link
Member Author

Nyholm commented Jul 14, 2016

No, changed my mind with this for good.

Since we allow closures as candidates once could easy create something like this:

class Strategy implements DiscoveryStrategy {

  private static $object; 
  public function __construct($object) { 
      self::$object = $object;
  }
  static function getCandidates($type) {
     $obj = self::$object;
     return [[
          'class'=>function() use ($obj) {
              return $obj->getClass($type);
          }, 
          'condition'=>function() use ($obj) {
              return $obj->evaluateCondition($type);
          }, 
       ]];
  }
}

@Nyholm Nyholm closed this Jul 14, 2016
@Nyholm Nyholm deleted the object-callable branch July 14, 2016 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants