spatie / crawler

An easy to use, powerful crawler implemented in PHP. Can execute Javascript.
https://freek.dev/308-building-a-crawler-in-php
MIT License
2.51k stars 357 forks source link

`CrawlObserver` and `CrawlProfile` interfaces #428

Closed rudiedirkx closed 1 year ago

rudiedirkx commented 1 year ago

If CrawlObserver and CrawlProfile were interfaces, I could have the observer and profile be 1 object. Currently, since my observer and profile HAVE to extend your class, they need a separate object (my custom object) injected into the observer and profile for tracking/progress/state/counting etc. There's nothing wrong with that, but interfaces would be nicer. Generally you want to hint interfaces, not classes, because they can be anything.

What I do now:

class MyProgress {
  // my state & some shared crawling logic here
}

class MyObserver extends CrawlObserver {
  function __construct(public MyProgress $progress) {}
  // observer logic here, that uses $this->progress
}

class MyProfiler extends CrawlProfile {
  function __construct(public MyProgress $progress) {}
  // profile logic here, that uses $this->progress
}

What I'd like to do:

class MyProgress implements CrawlObserver, CrawlProfile {
  // my state here
  // and observer logic
  // and profile logic
}

You can't break extends CrawlObserver or extends CrawlProfile, but you can make a CrawlObserverInterface and CrawlProfileInterface and use those internally. Maybe?