Skip to content

Commit

Permalink
add find-and-register functionality to the processor factory
Browse files Browse the repository at this point in the history
  • Loading branch information
alekitto committed Sep 23, 2019
1 parent 866e105 commit 10f09c0
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"require-dev": {
"doctrine/annotations": "~1.2",
"doctrine/cache": "^1.4",
"kcs/class-finder": "^0.1",
"mikey179/vfsstream": "^1.6",
"phpunit/phpunit": "^8.0",
"psr/event-dispatcher": "^1.0",
Expand Down
19 changes: 19 additions & 0 deletions lib/Loader/Processor/Annotation/Processor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace Kcs\Metadata\Loader\Processor\Annotation;

use Doctrine\Common\Annotations\Annotation\Required;

/**
* @Annotation()
* @Target({"CLASS"})
*/
class Processor
{
/**
* @var string
*
* @Required()
*/
public $annotation;
}
30 changes: 29 additions & 1 deletion lib/Loader/Processor/ProcessorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace Kcs\Metadata\Loader\Processor;

use Doctrine\Common\Annotations\AnnotationReader;
use Kcs\ClassFinder\Finder\RecursiveFinder;
use Kcs\Metadata\Exception\InvalidArgumentException;
use Kcs\Metadata\Loader\Processor\Annotation\Processor;

class ProcessorFactory implements ProcessorFactoryInterface
{
/**
* @var string[]
* @var string[][]
*/
private $processors = [];

Expand Down Expand Up @@ -37,6 +40,31 @@ public function registerProcessor(string $class, string $processorClass): void
}
}

/**
* Finds and register annotation processors recursively.
*
* @param string $dir
*/
public function registerProcessors(string $dir): void
{
if (! \class_exists(RecursiveFinder::class)) {
throw new \RuntimeException('Cannot find processors as the kcs/class-finder package is not installed.');
}

$reader = new AnnotationReader();
$finder = new RecursiveFinder($dir);
$finder
->annotatedBy(Processor::class)
->implementationOf(ProcessorInterface::class);

/** @var \ReflectionClass $reflClass */
foreach ($finder as $reflClass) {
/** @var Processor $annot */
$annot = $reader->getClassAnnotation($reflClass, Processor::class);
$this->registerProcessor($annot->annotation, $reflClass->getName());
}
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace Kcs\Metadata\Tests\Fixtures\AnnotationProcessorLoader\Processor;

use Kcs\Metadata\Loader\Processor\Annotation\Processor;
use Kcs\Metadata\Loader\Processor\ProcessorInterface;
use Kcs\Metadata\MetadataInterface;
use Kcs\Metadata\Tests\Fixtures\AnnotationProcessorLoader\Annotation\ClassAnnot;

/**
* @Processor(annotation=ClassAnnot::class)
*/
class ClassAnnotProcessor implements ProcessorInterface
{
public function process(MetadataInterface $metadata, $subject): void
{
}
}
16 changes: 16 additions & 0 deletions tests/Loader/Processor/ProcessorFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

namespace Kcs\Metadata\Tests\Loader\Processor;

use Doctrine\Common\Annotations\AnnotationRegistry;
use Kcs\Metadata\Exception\InvalidArgumentException;
use Kcs\Metadata\Loader\Processor\CompositeProcessor;
use Kcs\Metadata\Loader\Processor\ProcessorFactory;
use Kcs\Metadata\Loader\Processor\ProcessorInterface;
use Kcs\Metadata\MetadataInterface;
use Kcs\Metadata\Tests\Fixtures\Annotation;
use Kcs\Metadata\Tests\Fixtures\AnnotationProcessorLoader\Annotation\ClassAnnot;
use Kcs\Metadata\Tests\Fixtures\AnnotationProcessorLoader\Processor\ClassAnnotProcessor;
use PHPUnit\Framework\TestCase;

AnnotationRegistry::registerLoader('class_exists');

class FakeProcessor implements ProcessorInterface
{
public function process(MetadataInterface $metadata, $subject): void
Expand Down Expand Up @@ -91,4 +96,15 @@ public function get_processor_returns_composite_processor_if_more_than_one_proce

self::assertInstanceOf(CompositeProcessor::class, $factory->getProcessor(Annotation::class));
}

/**
* @test
*/
public function register_processors_should_find_and_register_annotated_processors()
{
$factory = new ProcessorFactory();
$factory->registerProcessors(__DIR__.'/../../Fixtures/AnnotationProcessorLoader/Processor');

self::assertInstanceOf(ClassAnnotProcessor::class, $factory->getProcessor(ClassAnnot::class));
}
}

0 comments on commit 10f09c0

Please sign in to comment.