Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
3 / 6
CRAP
82.61% covered (warning)
82.61%
19 / 23
RecipeLocator
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
3 / 6
15.03
82.61% covered (warning)
82.61%
19 / 23
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 onRecipeClass
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 2
 getClassLocator
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getAnnotationReader
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 locateRecipes
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 locateClassRecipes
0.00% covered (danger)
0.00%
0 / 1
7.02
92.31% covered (success)
92.31%
12 / 13
<?php declare(strict_types = 1);
/*
 * Copyright (c) 2019, Josef Kufner  <josef@kufner.cz>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
namespace Smalldb\CodeCooker;
use Closure;
use ReflectionClass;
use Smalldb\CodeCooker\Annotation\GeneratedClass;
use Smalldb\StateMachine\Utils\AnnotationReader\AnnotationReaderInterface;
use Smalldb\ClassLocator\ClassLocator;
use Smalldb\StateMachine\Utils\AnnotationReader\AnnotationReader;
class RecipeLocator
{
    private ClassLocator $classLocator;
    private AnnotationReaderInterface $annotationReader;
    private ?Closure $onRecipeClassCallback = null;
    public function __construct(ClassLocator $classLocator, AnnotationReaderInterface $annotationReader = null)
    {
        $this->classLocator = $classLocator;
        $this->annotationReader = $annotationReader ?? (new AnnotationReader());
    }
    public function onRecipeClass(?Closure $callback)
    {
        $this->onRecipeClassCallback = $callback;
    }
    public function getClassLocator(): ClassLocator
    {
        return $this->classLocator;
    }
    public function getAnnotationReader(): AnnotationReaderInterface
    {
        return $this->annotationReader;
    }
    public function locateRecipes(): \Generator
    {
        foreach ($this->classLocator->getClasses() as $filename => $className) {
            yield from $this->locateClassRecipes(new ReflectionClass($className));
        }
    }
    /**
     * @return string[]  List of generated classes (FQCNs)
     */
    public function locateClassRecipes(ReflectionClass $sourceClass): \Generator
    {
        $annotations = $this->annotationReader->getClassAnnotations($sourceClass);
        // Do not process generated classes
        foreach ($annotations as $annotation) {
            if ($annotation instanceof GeneratedClass) {
                return;
            }
        }
        // Convert annotations to recipes
        $hasRecipe = false;
        foreach ($annotations as $annotation) {
            if ($annotation instanceof AnnotationRecipeBuilder) {
                if (!$hasRecipe) {
                    $hasRecipe = true;
                    if ($this->onRecipeClassCallback) {
                        ($this->onRecipeClassCallback)($sourceClass);
                    }
                }
                yield $annotation->buildRecipe($sourceClass);
            }
        }
    }
}