Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,15 @@ Discover::in(__DIR__)

### Parallel

Getting all structures in a bigger application can be slow due to many files being scanned. This process can be sped up by parallelized scanning. You can enable this as such:
Getting all structures in a bigger application can be slow due to many files being scanned.

Before running in parallel, make sure to install `amphp/parallel`

```shell
composer require amphp/parallel
```

The process can be sped up by parallelized scanning. You can enable this as such:

```php
Discover::in(__DIR__)->parallel()->get();
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
],
"require": {
"php": "^8.1",
"amphp/amp": "^v3.0",
"amphp/parallel": "^2.2",
"illuminate/collections": "^10.0|^11.0|^12.0",
"spatie/laravel-package-tools": "^1.4.3",
"symfony/finder": "^6.0|^7.0"
},
"require-dev": {
"amphp/parallel": "^2.2",
"illuminate/console": "^10.0|^11.0|^12.0",
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.0|^8.0",
Expand All @@ -37,6 +36,9 @@
"phpunit/phpunit": "^9.5|^10.0|^11.5.3",
"spatie/laravel-ray": "^1.26"
},
"suggest" : {
"amphp/parallel": "When you want to use the Parallel discover worker"
},
"autoload": {
"psr-4": {
"Spatie\\StructureDiscoverer\\": "src"
Expand Down
5 changes: 5 additions & 0 deletions src/Discover.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Spatie\StructureDiscoverer\DiscoverWorkers\ParallelDiscoverWorker;
use Spatie\StructureDiscoverer\DiscoverWorkers\SynchronousDiscoverWorker;
use Spatie\StructureDiscoverer\Enums\Sort;
use Spatie\StructureDiscoverer\Exceptions\AmpNotInstalled;
use Spatie\StructureDiscoverer\Exceptions\NoCacheConfigured;
use Spatie\StructureDiscoverer\StructureParsers\PhpTokenStructureParser;
use Spatie\StructureDiscoverer\StructureParsers\ReflectionStructureParser;
Expand Down Expand Up @@ -111,6 +112,10 @@ public function usingWorker(DiscoverWorker $worker): self

public function parallel(int $filesPerJob = 50): self
{
if (! function_exists('\Amp\Future\await')) {
throw AmpNotInstalled::create();
}

return $this->usingWorker(new ParallelDiscoverWorker($filesPerJob));
}

Expand Down
13 changes: 13 additions & 0 deletions src/Exceptions/AmpNotInstalled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\StructureDiscoverer\Exceptions;

use Exception;

class AmpNotInstalled extends Exception
{
public static function create(): self
{
return new self('Parallel structure discovery requires amphp/parallel to be installed.');
}
}
3 changes: 2 additions & 1 deletion src/Support/StructuresResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ protected function discover(DiscoverProfileConfig $config): array

$filenames = collect($files)
->reject(fn (SplFileInfo $file) => in_array($file->getPathname(), $config->ignoredFiles) || $file->getExtension() !== 'php')
->map(fn (SplFileInfo $file) => $file->getPathname());
->map(fn (SplFileInfo $file) => $file->getPathname())
->values();

return $this->discoverWorker->run($filenames, $config);
}
Expand Down
Loading