-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register query builders from a namespace. (#8)
* Add a Schema class to register query builders Test structure has been slightly refactored to move the builders out of the feature directory. * Fix syntax error for PHP <= 7.2 * Remove Schema class in favour of config Namespace lookup of query builders remains, but now a sensible default is given so people won't have to create a schema class. * Update documentation to reflect new changes * Fix don't load the query builders when loader is resolved
- Loading branch information
Showing
40 changed files
with
418 additions
and
116 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,16 @@ | ||
<?php | ||
|
||
namespace Apitizer\Exceptions; | ||
|
||
class ClassFinderException extends ApitizerException | ||
{ | ||
public static function composerFileNotFound(string $path) | ||
{ | ||
return new static("Could not find composer file on path [$path]"); | ||
} | ||
|
||
public static function psr4NotFound() | ||
{ | ||
return new static("Could not find PSR-4 definition in the composer.json"); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Apitizer; | ||
|
||
use Apitizer\Exceptions\ClassFinderException; | ||
use Apitizer\QueryBuilder; | ||
use Apitizer\Support\ComposerNamespaceClassFinder; | ||
|
||
class QueryBuilderLoader | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
protected $queryBuilders; | ||
|
||
public function loadFromConfig() | ||
{ | ||
$this->queryBuilders = array_unique(array_merge( | ||
$this->loadIndividualClasses(), | ||
$this->loadNamespaces() | ||
)); | ||
} | ||
|
||
public function loadIndividualClasses() | ||
{ | ||
return config('apitizer.query_builders.classes'); | ||
} | ||
|
||
public function loadNamespaces() | ||
{ | ||
$classes = []; | ||
|
||
foreach (config('apitizer.query_builders.namespaces', []) as $namespace) { | ||
$classes = array_merge($classes, $this->loadFromNamespace($namespace)); | ||
} | ||
|
||
return $classes; | ||
} | ||
|
||
public function loadFromNamespace(string $namespace) | ||
{ | ||
return ComposerNamespaceClassFinder::make($namespace, QueryBuilder::class)->all(); | ||
} | ||
|
||
public function getQueryBuilders() | ||
{ | ||
if (is_null($this->queryBuilders)) { | ||
$this->loadFromConfig(); | ||
} | ||
|
||
return $this->queryBuilders; | ||
} | ||
} |
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,63 @@ | ||
<?php | ||
|
||
namespace Apitizer\Support; | ||
|
||
use FilterIterator; | ||
use ReflectionClass; | ||
use Iterator; | ||
|
||
/** | ||
* An iterator that filters php files that follow PSR-4 to only those that | ||
* extend some class and returns the fully-qualified namespace for those | ||
* classes. | ||
*/ | ||
class ClassFilter extends FilterIterator | ||
{ | ||
/** | ||
* @var string the base namespace to use for all classes. | ||
*/ | ||
protected $namespace; | ||
|
||
/** | ||
* @var string the class that must be extended from. | ||
*/ | ||
protected $class; | ||
|
||
/** | ||
* @var string the namespace to the current file we're handling. This will | ||
* be the return value of it passes the accept function. | ||
*/ | ||
protected $current; | ||
|
||
public function __construct(string $namespace, string $class, Iterator $iterator) | ||
{ | ||
parent::__construct($iterator); | ||
$this->namespace = $namespace; | ||
$this->class = $class; | ||
} | ||
|
||
public function current() | ||
{ | ||
return $this->current; | ||
} | ||
|
||
public function accept() | ||
{ | ||
$fileInfo = $this->getInnerIterator()->current(); | ||
|
||
// TODO: What if we're using a recursive iterator? | ||
$this->current = $this->namespace . '\\' . $fileInfo->getBasename('.php'); | ||
|
||
try { | ||
$reflection = new ReflectionClass($this->current); | ||
|
||
if (! $reflection->isInstantiable()) { | ||
return false; | ||
} | ||
|
||
return $reflection->isSubclassOf($this->class); | ||
} catch (\Exception $e) { | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.