From 59c44d2d4028a588e2730e262a9d5f83eca823dd Mon Sep 17 00:00:00 2001 From: pavlokomarov Date: Sun, 2 Oct 2022 13:14:40 +0300 Subject: [PATCH] add possibility to configure Run with entire Downloader, ItemPipeline and Processor objects add possibility to configure Run with entire Downloader, ItemPipeline and Processor objects --- src/Core/Engine.php | 21 ++++++++++++++++++--- src/Core/Run.php | 9 +++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Core/Engine.php b/src/Core/Engine.php index a660349..49d3116 100644 --- a/src/Core/Engine.php +++ b/src/Core/Engine.php @@ -144,9 +144,24 @@ private function scheduleRequest(Request $request): void private function configure(Run $run): void { $this->scheduler->setDelay($run->requestDelay); - $this->itemPipeline->setProcessors(...$run->itemProcessors); - $this->downloader->withMiddleware(...$run->downloaderMiddleware); - $this->responseProcessor->withMiddleware(...$run->responseMiddleware); + + if (null !== $run->itemPipeline) { + $this->itemPipeline = $run->itemPipeline; + } else { + $this->itemPipeline->setProcessors(...$run->itemProcessors); + } + + if (null !== $run->downloader) { + $this->downloader = $run->downloader; + } else { + $this->downloader->withMiddleware(...$run->downloaderMiddleware); + } + + if (null !== $run->responseProcessor) { + $this->responseProcessor = $run->responseProcessor; + } else { + $this->responseProcessor->withMiddleware(...$run->responseMiddleware); + } foreach ($run->extensions as $extension) { $this->eventDispatcher->addSubscriber($extension); diff --git a/src/Core/Run.php b/src/Core/Run.php index 48fcd48..51e882b 100644 --- a/src/Core/Run.php +++ b/src/Core/Run.php @@ -13,10 +13,13 @@ namespace RoachPHP\Core; +use RoachPHP\Downloader\Downloader; use RoachPHP\Downloader\DownloaderMiddlewareInterface; use RoachPHP\Extensions\ExtensionInterface; use RoachPHP\Http\Request; +use RoachPHP\ItemPipeline\ItemPipelineInterface; use RoachPHP\ItemPipeline\Processors\ItemProcessorInterface; +use RoachPHP\Spider\Processor; use RoachPHP\Spider\SpiderMiddlewareInterface; /** @@ -30,6 +33,9 @@ final class Run * @param ItemProcessorInterface[] $itemProcessors * @param SpiderMiddlewareInterface[] $responseMiddleware * @param ExtensionInterface[] $extensions + * @param null|Downloader $downloader + * @param null|ItemPipelineInterface $itemPipeline + * @param null|Processor $responseProcessor */ public function __construct( public array $startRequests, @@ -39,6 +45,9 @@ public function __construct( public array $extensions = [], public int $concurrency = 25, public int $requestDelay = 0, + public ?Downloader $downloader = null, + public ?ItemPipelineInterface $itemPipeline = null, + public ?Processor $responseProcessor = null, ) { } }