-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to remove class loader, add filepathLoader
- Loading branch information
Aaron Carlino
committed
Jan 12, 2022
1 parent
d8e0311
commit 9d53fe4
Showing
11 changed files
with
152 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
|
||
namespace SilverStripe\GraphQL\Schema\BulkLoader; | ||
|
||
use InvalidArgumentException; | ||
use SilverStripe\Core\ClassInfo; | ||
|
||
class BulkLoaderSet | ||
{ | ||
/** | ||
* @var AbstractBulkLoader[] | ||
*/ | ||
private $loaders; | ||
|
||
/** | ||
* @var Collection | ||
*/ | ||
private $collection; | ||
|
||
/** | ||
* BulkLoaderSet constructor. | ||
* @param AbstractBulkLoader[] ...$loaders | ||
* @param Collection|null $collection | ||
*/ | ||
public function __construct(...$loaders, ?Collection $collection = null) | ||
{ | ||
$this->setLoaders($loaders); | ||
if ($collection) { | ||
$this->setCollection($collection); | ||
} else { | ||
$this->collection = Collection::create(ClassInfo::allClasses()); | ||
} | ||
} | ||
|
||
public function addLoader(AbstractBulkLoader $loader) | ||
{ | ||
$this->loaders[] = $loader; | ||
|
||
return $this; | ||
} | ||
|
||
public function process(): Collection | ||
{ | ||
foreach ($this->loaders as $loader) { | ||
$this->collection = $loader->collect($this->collection); | ||
} | ||
} | ||
public function setLoaders($loaders): self | ||
{ | ||
foreach ($loaders as $loader) { | ||
if (!$loader instanceof AbstractBulkLoader) { | ||
throw new InvalidArgumentException(sprintf( | ||
'%s only accepts instances of %s', | ||
static::class, | ||
AbstractBulkLoader::class | ||
)); | ||
} | ||
} | ||
$this->loaders = $loaders; | ||
} | ||
|
||
public function setCollection(Collection $collection): self | ||
{ | ||
$this->collection = $collection; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
|
||
namespace SilverStripe\GraphQL\Schema\BulkLoader; | ||
|
||
use SilverStripe\Core\Path; | ||
|
||
/** | ||
* Loads classes by fuzzy match (glob), relative to the root e.g. `src/*.model.php` | ||
*/ | ||
class FilepathLoader extends AbstractBulkLoader | ||
{ | ||
const IDENTIFIER = 'filepathLoader'; | ||
|
||
public static function getIdentifier(): string | ||
{ | ||
return self::IDENTIFIER; | ||
} | ||
|
||
/** | ||
* @param Collection $collection | ||
* @return Collection | ||
*/ | ||
public function collect(Collection $collection): Collection | ||
{ | ||
$includedFiles = []; | ||
$excludedFiles = []; | ||
foreach ($this->includeList as $include) { | ||
foreach (glob(Path::join(BASE_PATH, $include)) as $path) { | ||
$includedFiles[$path] = true; | ||
} | ||
} | ||
foreach ($this->excludeList as $exclude) { | ||
foreach (glob(Path::join(BASE_PATH, $exclude)) as $path) { | ||
$excludedFiles[$path] = true; | ||
} | ||
|
||
} | ||
|
||
foreach ($collection->getFiles() as $file) { | ||
if (!isset($includedFiles[$file])) { | ||
$collection->removeFile($file); | ||
} | ||
if (isset($excludedFiles[$file])) { | ||
$collection->removeFile($file); | ||
} | ||
} | ||
|
||
return $collection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters