Skip to content

Commit

Permalink
Refactor to remove class loader, add filepathLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Carlino committed Jan 12, 2022
1 parent d8e0311 commit 9d53fe4
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 38 deletions.
3 changes: 0 additions & 3 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ SilverStripe\Core\Injector\Injector:
SilverStripe\GraphQL\Schema\Storage\NameObfuscator:
class: SilverStripe\GraphQL\Schema\Storage\HybridObfuscator

SilverStripe\GraphQL\Schema\BulkLoader\Collection:
factory: SilverStripe\GraphQL\Schema\BulkLoader\CollectionFactory

SilverStripe\GraphQL\Schema\Schema:
schemas: []
---
Expand Down
11 changes: 7 additions & 4 deletions src/Schema/BulkLoader/AbstractBulkLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use SilverStripe\GraphQL\Schema\Interfaces\Identifiable;
use SilverStripe\GraphQL\Schema\Schema;

/**
* Provides base functionality to all bulk loaders. Must define a collect() method.
*/
abstract class AbstractBulkLoader implements Identifiable, ConfigurationApplier
{
use Injectable;
Expand Down Expand Up @@ -40,7 +43,7 @@ public function __construct(array $include = [], array $exclude = [])
*/
public function include(array $include): self
{
$this->includeList = array_merge($this->includeList, $include);
$this->includeList = $include;

return $this;
}
Expand All @@ -51,17 +54,17 @@ public function include(array $include): self
*/
public function exclude(array $exclude): self
{
$this->excludeList = array_merge($this->excludeList, $exclude);
$this->excludeList = $exclude;

return $this;
}

/**
* @param array $config
* @return mixed|AbstractBulkLoader
* @return AbstractBulkLoader
* @throws SchemaBuilderException
*/
public function applyConfig(array $config)
public function applyConfig(array $config): self
{
Schema::assertValidConfig($config, ['include', 'exclude'], ['include']);
$include = $config['include'];
Expand Down
69 changes: 69 additions & 0 deletions src/Schema/BulkLoader/BulkLoaderSet.php
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;
}
}
16 changes: 7 additions & 9 deletions src/Schema/BulkLoader/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,27 @@
use Composer\Autoload\ClassLoader;
use SilverStripe\Core\Injector\Injectable;
use Exception;
use ReflectionClass;

/**
* Defines a collection of class names paired with
* Defines a collection of class names paired with file paths
*/
class Collection
{
use Injectable;

private $manifest;

/**
* @var ClassLoader
* @var array
*/
private $loader;
private $manifest;

/**
* Collection constructor.
* @param ClassLoader $loader
* @param array $classList
* @throws Exception
*/
public function __construct(ClassLoader $loader, array $classList)
public function __construct(array $classList)
{
$this->loader = $loader;
$this->setClassList($classList);
}

Expand All @@ -46,7 +43,8 @@ public function setClassList(array $classList): self
if (!class_exists($class)) {
continue;
}
$filePath = $this->loader->findFile($class);
$reflection = new ReflectionClass($class);
$filePath = $reflection->getFileName();
if (!$filePath) {
continue;
}
Expand Down
22 changes: 0 additions & 22 deletions src/Schema/BulkLoader/CollectionFactory.php

This file was deleted.

6 changes: 6 additions & 0 deletions src/Schema/BulkLoader/ExtensionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
use InvalidArgumentException;
use SilverStripe\Core\Extension;

/**
* Loads classes that have a given extension assigned to them.
*/
class ExtensionLoader extends AbstractBulkLoader
{
const IDENTIFIER = 'extensionLoader';

/**
* @return string
*/
public static function getIdentifier(): string
{
return self::IDENTIFIER;
Expand Down
51 changes: 51 additions & 0 deletions src/Schema/BulkLoader/FilepathLoader.php
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;
}
}
3 changes: 3 additions & 0 deletions src/Schema/BulkLoader/InheritanceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

use InvalidArgumentException;

/**
* Loads classes that are in a given inheritance tree, e.g. MyApp\Models\Page
*/
class InheritanceLoader extends AbstractBulkLoader
{
const IDENTIFIER = 'inheritanceLoader';
Expand Down
3 changes: 3 additions & 0 deletions src/Schema/BulkLoader/NamespaceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace SilverStripe\GraphQL\Schema\BulkLoader;

/**
* Loads classes based on fuzzy match of FQCN, e.g. App\Models\*
*/
class NamespaceLoader extends AbstractBulkLoader
{
const IDENTIFIER = 'namespaceLoader';
Expand Down
3 changes: 3 additions & 0 deletions src/Schema/BulkLoader/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Injector\Injector;

/**
* Frontend for creating a cached registry instance based on all the qualifying subclasses.
*/
class Registry
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Schema/BulkLoader/RegistryBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use SilverStripe\GraphQL\Schema\Interfaces\Identifiable;
use InvalidArgumentException;

/**
* The instance of the registry, as composed by the Registry frontend
*/
class RegistryBackend
{
use Injectable;
Expand Down

0 comments on commit 9d53fe4

Please sign in to comment.